|
| 1 | +/* |
| 2 | + * Zinc - The incremental compiler for Scala. |
| 3 | + * Copyright Lightbend, Inc. and Mark Harrah |
| 4 | + */ |
| 5 | + |
| 6 | +package dotty.tools.xsbt; |
| 7 | + |
| 8 | +import dotty.tools.dotc.Compiler; |
| 9 | +import dotty.tools.dotc.Driver; |
| 10 | +import dotty.tools.dotc.config.CompilerCommand; |
| 11 | +import dotty.tools.dotc.config.Properties; |
| 12 | +import dotty.tools.dotc.core.Contexts; |
| 13 | +import dotty.tools.io.AbstractFile; |
| 14 | +import scala.collection.mutable.ListBuffer; |
| 15 | +import scala.io.Codec; |
| 16 | +import xsbti.Problem; |
| 17 | +import xsbti.*; |
| 18 | +import xsbti.compile.Output; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +public class CompilerBridgeDriver extends Driver { |
| 25 | + private final String[] scalacOptions; |
| 26 | + private final String[] args; |
| 27 | + |
| 28 | + public CompilerBridgeDriver(String[] scalacOptions, Output output) { |
| 29 | + super(); |
| 30 | + this.scalacOptions = scalacOptions; |
| 31 | + |
| 32 | + if (!output.getSingleOutput().isPresent()) |
| 33 | + throw new IllegalArgumentException("output should be a SingleOutput, was a " + output.getClass().getName()); |
| 34 | + |
| 35 | + this.args = new String[scalacOptions.length + 2]; |
| 36 | + System.arraycopy(scalacOptions, 0, args, 0, scalacOptions.length); |
| 37 | + args[scalacOptions.length] = "-d"; |
| 38 | + args[scalacOptions.length + 1] = output.getSingleOutput().get().getAbsolutePath(); |
| 39 | + } |
| 40 | + |
| 41 | + private static final String StopInfoError = |
| 42 | + "Compiler option supplied that disabled Zinc compilation."; |
| 43 | + |
| 44 | + /** |
| 45 | + * `sourcesRequired` is set to false because the context is set up with no sources |
| 46 | + * The sources are passed programmatically to the compiler in the form of AbstractFiles |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public boolean sourcesRequired() { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, Logger log, Reporter delegate) { |
| 54 | + DelegatingReporter reporter = new DelegatingReporter(delegate); |
| 55 | + try { |
| 56 | + log.debug(this::infoOnCachedCompiler); |
| 57 | + |
| 58 | + Contexts.Context initialCtx = initCtx() |
| 59 | + .fresh() |
| 60 | + .setReporter(reporter) |
| 61 | + .setSbtCallback(callback); |
| 62 | + |
| 63 | + Contexts.Context context = setup(args, initialCtx)._2; |
| 64 | + |
| 65 | + if(CompilerCommand.shouldStopWithInfo(context)) { |
| 66 | + throw new InterfaceCompileFailed(args, new Problem[0], StopInfoError); |
| 67 | + } |
| 68 | + |
| 69 | + if (!delegate.hasErrors()) { |
| 70 | + log.debug(this::prettyPrintCompilationArguments); |
| 71 | + Compiler compiler = newCompiler(context); |
| 72 | + |
| 73 | + VirtualFile[] sortedSources = new VirtualFile[sources.length]; |
| 74 | + System.arraycopy(sources, 0, sortedSources, 0, sources.length); |
| 75 | + Arrays.sort( |
| 76 | + sortedSources, |
| 77 | + new Comparator<VirtualFile>() { |
| 78 | + @Override |
| 79 | + public int compare(VirtualFile x0, VirtualFile x1) { |
| 80 | + return x0.id().compareTo(x1.id()); |
| 81 | + } |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + ListBuffer<AbstractFile> sourcesBuffer = new ListBuffer<>(); |
| 86 | + for (VirtualFile file: sortedSources) |
| 87 | + sourcesBuffer.append(asDottyFile(file)); |
| 88 | + doCompileFiles(compiler, sourcesBuffer.toList(), context); |
| 89 | + |
| 90 | + for (xsbti.Problem problem: delegate.problems()) { |
| 91 | + callback.problem(problem.category(), problem.position(), problem.message(), problem.severity(), |
| 92 | + true); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + delegate.printSummary(); |
| 97 | + |
| 98 | + if (delegate.hasErrors()) { |
| 99 | + log.debug(() -> "Compilation failed"); |
| 100 | + throw new InterfaceCompileFailed(args, delegate.problems(), "Compilation failed"); |
| 101 | + } |
| 102 | + } finally { |
| 103 | + reporter.dropDelegate(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static AbstractFile asDottyFile(VirtualFile virtualFile) { |
| 108 | + if (virtualFile instanceof PathBasedFile) |
| 109 | + return new ZincPlainFile((PathBasedFile) virtualFile); |
| 110 | + |
| 111 | + try { |
| 112 | + return new ZincVirtualFile(virtualFile); |
| 113 | + } catch (IOException e) { |
| 114 | + throw new IllegalArgumentException("invalid file " + virtualFile.name(), e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private String infoOnCachedCompiler() { |
| 119 | + String compilerId = Integer.toHexString(hashCode()); |
| 120 | + String compilerVersion = Properties.versionString(); |
| 121 | + return String.format("[zinc] Running cached compiler %s for Scala Compiler %s", compilerId, compilerVersion); |
| 122 | + } |
| 123 | + |
| 124 | + private String prettyPrintCompilationArguments() { |
| 125 | + StringBuilder builder = new StringBuilder(); |
| 126 | + builder.append("[zinc] The Scala compiler is invoked with:"); |
| 127 | + for (String opt: scalacOptions) { |
| 128 | + builder.append("\n\t"); |
| 129 | + builder.append(opt); |
| 130 | + } |
| 131 | + return builder.toString(); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments