|
1 | 1 | package sbt_inc;
|
2 | 2 |
|
3 |
| -import com.typesafe.zinc.Compiler; |
4 |
| -import com.typesafe.zinc.*; |
| 3 | +import scala.compat.java8.functionConverterImpls.*; |
| 4 | + |
5 | 5 | import org.apache.maven.plugin.logging.Log;
|
6 |
| -import org.apache.maven.toolchain.Toolchain; |
| 6 | +import sbt.internal.inc.*; |
| 7 | +import sbt.internal.inc.FileAnalysisStore; |
| 8 | +import sbt.internal.inc.ScalaInstance; |
| 9 | +import sbt.internal.inc.classpath.ClasspathUtilities; |
7 | 10 | import scala.Option;
|
8 |
| -import scala_maven_executions.MainHelper; |
9 |
| -import util.JavaLocator; |
| 11 | +import scala_maven.VersionNumber; |
| 12 | +import xsbti.Logger; |
| 13 | +import xsbti.T2; |
| 14 | +import xsbti.compile.*; |
| 15 | +import xsbti.compile.AnalysisStore; |
| 16 | +import xsbti.compile.CompilerCache; |
10 | 17 |
|
11 | 18 | import java.io.File;
|
| 19 | +import java.net.URL; |
| 20 | +import java.net.URLClassLoader; |
12 | 21 | import java.util.ArrayList;
|
13 | 22 | import java.util.List;
|
14 |
| -import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.function.Function; |
15 | 25 |
|
16 | 26 | public class SbtIncrementalCompiler {
|
17 | 27 |
|
18 |
| - public static final String SBT_GROUP_ID = "com.typesafe.sbt"; |
19 |
| - public static final String COMPILER_INTEGRATION_ARTIFACT_ID = "incremental-compiler"; |
20 |
| - public static final String COMPILER_INTERFACE_ARTIFACT_ID = "compiler-interface"; |
21 |
| - public static final String COMPILER_INTERFACE_CLASSIFIER = "sources"; |
22 |
| - public static final String XSBTI_ARTIFACT_ID = "sbt-interface"; |
23 |
| - |
24 |
| - private static final String ANALYSIS_MAP_ARG_SEPARATOR = ","; |
25 |
| - private static final String ANALYSIS_MAP_PAIR_SEPARATOR = File.pathSeparator; |
26 |
| - |
27 |
| - private Log log; |
28 |
| - |
29 |
| - private ZincClient zinc; |
30 |
| - |
31 |
| - private boolean useServer = false; |
32 |
| - |
33 |
| - private File compilerJar; |
34 |
| - |
35 |
| - private File libraryJar; |
36 |
| - |
37 |
| - private List<File> extraJars; |
38 |
| - |
39 |
| - private List<String> extraArgs; |
40 |
| - |
41 |
| - private xsbti.Logger logger; |
42 |
| - |
43 |
| - private Compiler compiler; |
44 |
| - |
45 |
| - public SbtIncrementalCompiler(boolean useZincServer, String zincHost, int zincPort, File libraryJar, File compilerJar, List<File> extraJars, File xsbtiJar, File interfaceJar, Log l, List<String> args) throws Exception { |
46 |
| - this.log = l; |
47 |
| - if (useZincServer) { |
48 |
| - this.zinc = new ZincClient(zincHost, zincPort); |
49 |
| - if (zinc.serverAvailable()) { |
50 |
| - l.info("Using zinc server for incremental compilation"); |
51 |
| - this.useServer = true; |
52 |
| - this.compilerJar = compilerJar; |
53 |
| - this.libraryJar = libraryJar; |
54 |
| - this.extraJars = extraJars; |
55 |
| - this.extraArgs = args; |
56 |
| - } else { |
57 |
| - l.warn("Zinc server is not available at port " + zincPort + " - reverting to normal incremental compile"); |
58 |
| - this.useServer = false; |
| 28 | + public static final String SBT_GROUP_ID = "org.scala-sbt"; |
| 29 | + public static final String ZINC_ARTIFACT_ID = "zinc"; |
| 30 | + public static final String COMPILER_BRIDGE_ARTIFACT_ID = "compiler-bridge"; |
| 31 | + |
| 32 | + private final Logger logger; |
| 33 | + private final IncrementalCompilerImpl compiler; |
| 34 | + private final Compilers compilers; |
| 35 | + private final Setup setup; |
| 36 | + private final AnalysisStore analysisStore; |
| 37 | + |
| 38 | + public SbtIncrementalCompiler(File libraryJar, File reflectJar, File compilerJar, VersionNumber scalaVersion, List<File> extraJars, File compilerBridgeJar, Log l, List<String> args, File cacheFile) throws Exception { |
| 39 | + l.info("Using incremental compilation"); |
| 40 | + if (args.size() > 0) l.warn("extra args for zinc are ignored in non-server mode"); |
| 41 | + this.logger = new SbtLogger(l); |
| 42 | + |
| 43 | + List<File> allJars = new ArrayList<>(extraJars); |
| 44 | + allJars.add(libraryJar); |
| 45 | + allJars.add(reflectJar); |
| 46 | + allJars.add(compilerJar); |
| 47 | + |
| 48 | + ScalaInstance scalaInstance = new ScalaInstance( |
| 49 | + scalaVersion.toString(), // version |
| 50 | + new URLClassLoader(new URL[]{libraryJar.toURI().toURL(), reflectJar.toURI().toURL(), compilerJar.toURI().toURL()}), // loader |
| 51 | + ClasspathUtilities.rootLoader(), // loaderLibraryOnly |
| 52 | + libraryJar, // libraryJar |
| 53 | + compilerJar, // compilerJar |
| 54 | + allJars.toArray(new File[]{}), // allJars |
| 55 | + Option.apply(scalaVersion.toString()) // explicitActual |
| 56 | + ); |
| 57 | + |
| 58 | + compiler = new IncrementalCompilerImpl(); |
| 59 | + |
| 60 | + AnalyzingCompiler scalaCompiler = new AnalyzingCompiler( |
| 61 | + scalaInstance, // scalaInstance |
| 62 | + ZincCompilerUtil.constantBridgeProvider(scalaInstance, compilerBridgeJar), //provider |
| 63 | + ClasspathOptionsUtil.auto(), // classpathOptions |
| 64 | + new FromJavaConsumer<>(noop -> { |
| 65 | + }), //FIXME foo -> {}, // onArgsHandler |
| 66 | + Option.apply(null) // classLoaderCache |
| 67 | + ); |
| 68 | + |
| 69 | + compilers = compiler.compilers(scalaInstance, ClasspathOptionsUtil.boot(), Option.apply(null), scalaCompiler); |
| 70 | + |
| 71 | + PerClasspathEntryLookup lookup = new PerClasspathEntryLookup() { |
| 72 | + @Override |
| 73 | + public Optional<CompileAnalysis> analysis(File classpathEntry) { |
| 74 | + return Optional.empty(); |
59 | 75 | }
|
60 |
| - } |
61 |
| - if (!useServer) { |
62 |
| - l.info("Using incremental compilation"); |
63 |
| - if (args.size() > 0) l.warn("extra args for zinc are ignored in non-server mode"); |
64 |
| - this.logger = new SbtLogger(l); |
65 |
| - Setup setup = Setup.create(compilerJar, libraryJar, extraJars, xsbtiJar, interfaceJar, null, false); |
66 |
| - if (l.isDebugEnabled()) Setup.debug(setup, logger); |
67 |
| - this.compiler = Compiler.create(setup, logger); |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - private IncOptions defaultOptions() { |
72 |
| - sbt.inc.IncOptions defaultSbtOptions = sbt.inc.IncOptions.Default(); |
73 |
| - return new IncOptions( |
74 |
| - defaultSbtOptions.transitiveStep(), |
75 |
| - defaultSbtOptions.recompileAllFraction(), |
76 |
| - defaultSbtOptions.relationsDebug(), |
77 |
| - defaultSbtOptions.apiDebug(), |
78 |
| - defaultSbtOptions.apiDiffContextSize(), |
79 |
| - defaultSbtOptions.apiDumpDirectory(), |
80 |
| - false, |
81 |
| - Option.<File>empty(), |
82 |
| - defaultSbtOptions.recompileOnMacroDef(), |
83 |
| - defaultSbtOptions.nameHashing()); |
84 |
| - } |
85 | 76 |
|
86 |
| - public void compile(File baseDir, List<String> classpathElements, List<File> sources, File classesDirectory, List<String> scalacOptions, List<String> javacOptions, File cacheFile, Map<File, File> cacheMap, String compileOrder, Toolchain toolchain) throws Exception { |
87 |
| - if (useServer) { |
88 |
| - zincCompile(baseDir, classpathElements, sources, classesDirectory, scalacOptions, javacOptions, cacheFile, cacheMap, compileOrder, toolchain); |
89 |
| - } else { |
90 |
| - if (log.isDebugEnabled()) log.debug("Incremental compiler = " + compiler + " [" + Integer.toHexString(compiler.hashCode()) + "]"); |
91 |
| - List<File> classpath = pathsToFiles(classpathElements); |
92 |
| - Inputs inputs = Inputs.create(classpath, sources, classesDirectory, scalacOptions, javacOptions, cacheFile, cacheMap, compileOrder, defaultOptions(), true); |
93 |
| - if (log.isDebugEnabled()) Inputs.debug(inputs, logger); |
94 |
| - compiler.compile(inputs, logger); |
95 |
| - } |
96 |
| - } |
97 |
| - |
98 |
| - private void zincCompile(File baseDir, List<String> classpathElements, List<File> sources, File classesDirectory, List<String> scalacOptions, List<String> javacOptions, File cacheFile, Map<File, File> cacheMap, String compileOrder, Toolchain toolchain) throws Exception { |
99 |
| - List<String> arguments = new ArrayList<String>(extraArgs); |
100 |
| - arguments.add("-log-level"); |
101 |
| - arguments.add(logLevelToString(log)); |
102 |
| - arguments.add("-scala-compiler"); |
103 |
| - arguments.add(compilerJar.getAbsolutePath()); |
104 |
| - arguments.add("-scala-library"); |
105 |
| - arguments.add(libraryJar.getAbsolutePath()); |
106 |
| - arguments.add("-scala-extra"); |
107 |
| - List<String> extraPaths = new ArrayList<String>(); |
108 |
| - for (File extraJar : extraJars) { |
109 |
| - extraPaths.add(extraJar.getAbsolutePath()); |
110 |
| - } |
111 |
| - arguments.add(MainHelper.toMultiPath(extraPaths)); |
112 |
| - if (!classpathElements.isEmpty()) { |
113 |
| - arguments.add("-classpath"); |
114 |
| - arguments.add(MainHelper.toMultiPath(classpathElements)); |
115 |
| - } |
116 |
| - arguments.add("-d"); |
117 |
| - arguments.add(classesDirectory.getAbsolutePath()); |
118 |
| - for (String scalacOption : scalacOptions) { |
119 |
| - arguments.add("-S" + scalacOption); |
120 |
| - } |
121 |
| - |
122 |
| - String javaHome = JavaLocator.findHomeFromToolchain(toolchain); |
123 |
| - if (javaHome != null) { |
124 |
| - log.info("Toolchain in scala-maven-plugin: " + javaHome); |
125 |
| - arguments.add("-java-home"); |
126 |
| - arguments.add(javaHome); |
127 |
| - } |
128 |
| - |
129 |
| - for (String javacOption : javacOptions) { |
130 |
| - arguments.add("-C" + javacOption); |
131 |
| - } |
132 |
| - arguments.add("-compile-order"); |
133 |
| - arguments.add(compileOrder); |
134 |
| - arguments.add("-analysis-cache"); |
135 |
| - arguments.add(cacheFile.getAbsolutePath()); |
136 |
| - arguments.add("-analysis-map"); |
137 |
| - arguments.add(cacheMapToString(cacheMap)); |
138 |
| - for (File source : sources) { |
139 |
| - arguments.add(source.getAbsolutePath()); |
140 |
| - } |
141 |
| - |
142 |
| - int exitCode = zinc.run(arguments, baseDir, System.out, System.err); |
143 |
| - |
144 |
| - if (exitCode != 0) { |
145 |
| - xsbti.Problem[] problems = null; |
146 |
| - throw new sbt.compiler.CompileFailed(arguments.toArray(new String[arguments.size()]), "Compile failed via zinc server", problems); |
147 |
| - } |
| 77 | + @Override |
| 78 | + public DefinesClass definesClass(File classpathEntry) { |
| 79 | + return Locate.definesClass(classpathEntry); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + LoggedReporter reporter = new LoggedReporter(100, logger, pos -> pos); |
| 84 | + |
| 85 | + analysisStore = AnalysisStore.getCachedStore(FileAnalysisStore.binary(cacheFile)); |
| 86 | + |
| 87 | + setup = |
| 88 | + compiler.setup( |
| 89 | + lookup, // lookup |
| 90 | + false, // skip |
| 91 | + cacheFile, // cacheFile |
| 92 | + CompilerCache.fresh(), // cache |
| 93 | + IncOptions.of(), // incOptions |
| 94 | + reporter, // reporter |
| 95 | + Option.apply(null), // optionProgress |
| 96 | + new T2[]{} |
| 97 | + ); |
148 | 98 | }
|
149 | 99 |
|
150 |
| - private List<File> pathsToFiles(List<String> paths) { |
151 |
| - List<File> files = new ArrayList<File>(paths.size()); |
152 |
| - for (String path : paths) { |
153 |
| - files.add(new File(path)); |
| 100 | + public void compile(List<String> classpathElements, List<File> sources, File classesDirectory, List<String> scalacOptions, List<String> javacOptions, String compileOrder) { |
| 101 | + |
| 102 | + Inputs inputs = compiler.inputs( |
| 103 | + classpathElements.stream().map(File::new).toArray(size -> new File[size]), //classpath |
| 104 | + sources.toArray(new File[]{}), // sources |
| 105 | + classesDirectory, // classesDirectory |
| 106 | + scalacOptions.toArray(new String[]{}), // scalacOptions |
| 107 | + javacOptions.toArray(new String[]{}), // javacOptions |
| 108 | + 100, // maxErrors |
| 109 | + new Function[]{}, // sourcePositionMappers |
| 110 | + toCompileOrder(compileOrder), // order |
| 111 | + compilers, |
| 112 | + setup, |
| 113 | + compiler.emptyPreviousResult() |
| 114 | + ); |
| 115 | + |
| 116 | + Optional<AnalysisContents> analysisContents = analysisStore.get(); |
| 117 | + if (analysisContents.isPresent()) { |
| 118 | + AnalysisContents analysisContents0 = analysisContents.get(); |
| 119 | + CompileAnalysis previousAnalysis = analysisContents0.getAnalysis(); |
| 120 | + MiniSetup previousSetup = analysisContents0.getMiniSetup(); |
| 121 | + PreviousResult previousResult = PreviousResult.of(Optional.of(previousAnalysis), Optional.of(previousSetup)); |
| 122 | + inputs = inputs.withPreviousResult(previousResult); |
154 | 123 | }
|
155 |
| - return files; |
156 |
| - } |
157 | 124 |
|
158 |
| - private String logLevelToString(Log l) { |
159 |
| - if (l.isDebugEnabled()) return "debug"; |
160 |
| - else if (l.isInfoEnabled()) return "info"; |
161 |
| - else if (l.isWarnEnabled()) return "warn"; |
162 |
| - else if (l.isErrorEnabled()) return "error"; |
163 |
| - else return "info"; |
| 125 | + CompileResult newResult = compiler.compile(inputs, logger); |
| 126 | + analysisStore.set(AnalysisContents.create(newResult.analysis(), newResult.setup())); |
164 | 127 | }
|
165 | 128 |
|
166 |
| - private String cacheMapToString(Map<File, File> cacheMap) throws Exception { |
167 |
| - String analysisMap = ""; |
168 |
| - boolean addArgSeparator = false; |
169 |
| - for (Map.Entry<File, File> entry : cacheMap.entrySet()) { |
170 |
| - if (addArgSeparator) analysisMap += ANALYSIS_MAP_ARG_SEPARATOR; |
171 |
| - analysisMap += entry.getKey().getAbsolutePath(); |
172 |
| - analysisMap += ANALYSIS_MAP_PAIR_SEPARATOR; |
173 |
| - analysisMap += entry.getValue().getAbsolutePath(); |
174 |
| - addArgSeparator = true; |
| 129 | + private CompileOrder toCompileOrder(String name) { |
| 130 | + if (name.equalsIgnoreCase(CompileOrder.Mixed.name())) { |
| 131 | + return CompileOrder.Mixed; |
| 132 | + } else if (name.equalsIgnoreCase(CompileOrder.JavaThenScala.name())) { |
| 133 | + return CompileOrder.JavaThenScala; |
| 134 | + } else if (name.equalsIgnoreCase(CompileOrder.ScalaThenJava.name())) { |
| 135 | + return CompileOrder.ScalaThenJava; |
| 136 | + } else { |
| 137 | + throw new IllegalArgumentException("Unknown compileOrder: " + name); |
175 | 138 | }
|
176 |
| - return analysisMap; |
177 | 139 | }
|
178 | 140 | }
|
0 commit comments