use of sbt.internal.inc.ScalaInstance in project gradle by gradle.
the class ZincScalaCompilerFactory method getCompiler.
static ZincScalaCompiler getCompiler(CacheRepository cacheRepository, HashedClasspath hashedScalaClasspath) {
ScalaInstance scalaInstance;
try {
scalaInstance = getScalaInstance(hashedScalaClasspath);
} catch (Exception e) {
throw new RuntimeException("Failed create instance of the scala compiler", e);
}
String zincVersion = ZincCompilerUtil.class.getPackage().getImplementationVersion();
String scalaVersion = scalaInstance.actualVersion();
String javaVersion = Jvm.current().getJavaVersion().getMajorVersion();
String zincCacheKey = String.format("zinc-%s_%s_%s", zincVersion, scalaVersion, javaVersion);
String zincCacheName = String.format("%s compiler cache", zincCacheKey);
final PersistentCache zincCache = cacheRepository.cache(zincCacheKey).withDisplayName(zincCacheName).withLockOptions(mode(FileLockManager.LockMode.OnDemand)).open();
File compilerBridgeJar;
if (isScala3(scalaVersion)) {
compilerBridgeJar = findFile("scala3-sbt-bridge", hashedScalaClasspath.getClasspath());
} else {
File compilerBridgeSourceJar = findFile("compiler-bridge", hashedScalaClasspath.getClasspath());
compilerBridgeJar = getBridgeJar(zincCache, scalaInstance, compilerBridgeSourceJar, sbt.util.Logger.xlog2Log(new SbtLoggerAdapter()));
}
ScalaCompiler scalaCompiler = new AnalyzingCompiler(scalaInstance, ZincUtil.constantBridgeProvider(scalaInstance, compilerBridgeJar), ClasspathOptionsUtil.manual(), k -> scala.runtime.BoxedUnit.UNIT, Option.apply(COMPILER_CLASSLOADER_CACHE));
return new ZincScalaCompiler(scalaInstance, scalaCompiler, new AnalysisStoreProvider());
}
use of sbt.internal.inc.ScalaInstance in project gradle by gradle.
the class ZincScalaCompiler method execute.
public WorkResult execute(final ScalaJavaJointCompileSpec spec) {
LOGGER.info("Compiling with Zinc Scala compiler.");
Timer timer = Time.startTimer();
IncrementalCompilerImpl incremental = new IncrementalCompilerImpl();
Compilers compilers = incremental.compilers(scalaInstance, ClasspathOptionsUtil.boot(), Option.apply(Jvm.current().getJavaHome()), scalaCompiler);
List<String> scalacOptions = new ZincScalaCompilerArgumentsGenerator().generate(spec);
List<String> javacOptions = new JavaCompilerArgumentsBuilder(spec).includeClasspath(false).noEmptySourcePath().build();
File[] classpath = Iterables.toArray(spec.getCompileClasspath(), File.class);
CompileOptions compileOptions = CompileOptions.create().withSources(Iterables.toArray(spec.getSourceFiles(), File.class)).withClasspath(classpath).withScalacOptions(scalacOptions.toArray(new String[0])).withClassesDirectory(spec.getDestinationDir()).withJavacOptions(javacOptions.toArray(new String[0]));
File analysisFile = spec.getAnalysisFile();
Optional<AnalysisStore> analysisStore;
Optional<ClassFileManagerType> classFileManagerType;
if (spec.getScalaCompileOptions().isForce()) {
analysisStore = Optional.empty();
classFileManagerType = IncOptions.defaultClassFileManagerType();
} else {
analysisStore = Optional.of(analysisStoreProvider.get(analysisFile));
classFileManagerType = Optional.of(TransactionalManagerType.of(spec.getClassfileBackupDir(), new SbtLoggerAdapter()));
}
PreviousResult previousResult = analysisStore.flatMap(store -> store.get().map(a -> PreviousResult.of(Optional.of(a.getAnalysis()), Optional.of(a.getMiniSetup())))).orElse(PreviousResult.of(Optional.empty(), Optional.empty()));
IncOptions incOptions = IncOptions.of().withExternalHooks(new LookupOnlyExternalHooks(new ExternalBinariesLookup())).withRecompileOnMacroDef(Optional.of(false)).withClassfileManagerType(classFileManagerType).withTransitiveStep(5);
Setup setup = incremental.setup(new EntryLookup(spec), false, analysisFile, CompilerCache.fresh(), incOptions, // MappedPosition is used to make sure toString returns proper error messages
new LoggedReporter(100, new SbtLoggerAdapter(), MappedPosition::new), Option.empty(), getExtra());
Inputs inputs = incremental.inputs(compileOptions, compilers, setup, previousResult);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(inputs.toString());
}
if (spec.getScalaCompileOptions().isForce()) {
// TODO This should use Deleter
GFileUtils.deleteDirectory(spec.getDestinationDir());
GFileUtils.deleteQuietly(spec.getAnalysisFile());
}
LOGGER.info("Prepared Zinc Scala inputs: {}", timer.getElapsed());
try {
CompileResult compile = incremental.compile(inputs, new SbtLoggerAdapter());
if (analysisStore.isPresent()) {
AnalysisContents contentNext = AnalysisContents.create(compile.analysis(), compile.setup());
analysisStore.get().set(contentNext);
}
} catch (xsbti.CompileFailed e) {
throw new CompilationFailedException(e);
}
LOGGER.info("Completed Scala compilation: {}", timer.getElapsed());
return WorkResults.didWork(true);
}
use of sbt.internal.inc.ScalaInstance in project gradle by gradle.
the class ZincScalaCompilerFactory method getScalaInstance.
private static ScalaInstance getScalaInstance(HashedClasspath hashedScalaClasspath) throws MalformedURLException {
ClassPath scalaClasspath = hashedScalaClasspath.getClasspath();
File libraryJar = findFile(ArtifactInfo.ScalaLibraryID, scalaClasspath);
URL[] libraryUrls;
boolean isScala3 = false;
try {
File library3Jar = findFile(SCALA_3_LIBRARY_ID, scalaClasspath);
isScala3 = true;
libraryUrls = new URL[] { library3Jar.toURI().toURL(), libraryJar.toURI().toURL() };
} catch (IllegalStateException e) {
libraryUrls = new URL[] { libraryJar.toURI().toURL() };
}
ClassLoader scalaLibraryClassLoader;
ClassLoader scalaClassLoader;
if (isScala3) {
scalaLibraryClassLoader = new ScalaCompilerLoader(libraryUrls, xsbti.Reporter.class.getClassLoader());
scalaClassLoader = getCachedClassLoader(hashedScalaClasspath, scalaLibraryClassLoader);
} else {
scalaLibraryClassLoader = getClassLoader(DefaultClassPath.of(libraryJar), null);
scalaClassLoader = getCachedClassLoader(hashedScalaClasspath, null);
}
String scalaVersion = getScalaVersion(scalaClassLoader);
File compilerJar;
if (isScala3) {
compilerJar = findFile(SCALA_3_COMPILER_ID, scalaClasspath);
} else {
compilerJar = findFile(ArtifactInfo.ScalaCompilerID, scalaClasspath);
}
return new ScalaInstance(scalaVersion, scalaClassLoader, scalaLibraryClassLoader, libraryJar, compilerJar, Iterables.toArray(scalaClasspath.getAsFiles(), File.class), Option.empty());
}
Aggregations