Search in sources :

Example 1 with ScalaInstance

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());
}
Also used : PersistentCache(org.gradle.cache.PersistentCache) AnalyzingCompiler(sbt.internal.inc.AnalyzingCompiler) ZincCompilerUtil(xsbti.compile.ZincCompilerUtil) ScalaCompiler(xsbti.compile.ScalaCompiler) ScalaInstance(sbt.internal.inc.ScalaInstance) File(java.io.File) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 2 with ScalaInstance

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);
}
Also used : Compiler(org.gradle.language.base.internal.compile.Compiler) FileHash(xsbti.compile.FileHash) WorkResult(org.gradle.api.tasks.WorkResult) IncOptions(xsbti.compile.IncOptions) Inputs(xsbti.compile.Inputs) TransactionalManagerType(xsbti.compile.TransactionalManagerType) Map(java.util.Map) ScalaCompiler(xsbti.compile.ScalaCompiler) PreviousResult(xsbti.compile.PreviousResult) JavaCompilerArgumentsBuilder(org.gradle.api.internal.tasks.compile.JavaCompilerArgumentsBuilder) Some(scala.Some) Jvm(org.gradle.internal.jvm.Jvm) ClassFileManager(xsbti.compile.ClassFileManager) ExternalLookup(sbt.internal.inc.ExternalLookup) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DefinesClass(xsbti.compile.DefinesClass) AnalysisContents(xsbti.compile.AnalysisContents) WorkResults(org.gradle.api.tasks.WorkResults) List(java.util.List) CompilerCache(xsbti.compile.CompilerCache) IncrementalCompilerImpl(sbt.internal.inc.IncrementalCompilerImpl) JavaConverters(scala.collection.JavaConverters) Optional(java.util.Optional) ExternalHooks(xsbti.compile.ExternalHooks) Timer(org.gradle.internal.time.Timer) CompileAnalysis(xsbti.compile.CompileAnalysis) Set(scala.collection.immutable.Set) Iterables(com.google.common.collect.Iterables) LoggedReporter(sbt.internal.inc.LoggedReporter) Stamper(sbt.internal.inc.Stamper) CompileOptions(xsbti.compile.CompileOptions) MapBackedCache(org.gradle.cache.internal.MapBackedCache) Setup(xsbti.compile.Setup) HashMap(java.util.HashMap) CompileResult(xsbti.compile.CompileResult) Logger(org.gradle.api.logging.Logger) ClassFileManagerType(xsbti.compile.ClassFileManagerType) PerClasspathEntryLookup(xsbti.compile.PerClasspathEntryLookup) Inject(javax.inject.Inject) Changes(xsbti.compile.Changes) T2(xsbti.T2) GFileUtils(org.gradle.util.internal.GFileUtils) Compilers(xsbti.compile.Compilers) ScalaInstance(sbt.internal.inc.ScalaInstance) Option(scala.Option) AnalysisStore(xsbti.compile.AnalysisStore) File(java.io.File) Analysis(sbt.internal.inc.Analysis) CompilationFailedException(org.gradle.api.internal.tasks.compile.CompilationFailedException) Locate(sbt.internal.inc.Locate) ClasspathOptionsUtil(xsbti.compile.ClasspathOptionsUtil) Logging(org.gradle.api.logging.Logging) Time(org.gradle.internal.time.Time) Stamp(xsbti.compile.analysis.Stamp) JavaCompilerArgumentsBuilder(org.gradle.api.internal.tasks.compile.JavaCompilerArgumentsBuilder) CompilationFailedException(org.gradle.api.internal.tasks.compile.CompilationFailedException) IncrementalCompilerImpl(sbt.internal.inc.IncrementalCompilerImpl) AnalysisContents(xsbti.compile.AnalysisContents) Setup(xsbti.compile.Setup) Inputs(xsbti.compile.Inputs) ClassFileManagerType(xsbti.compile.ClassFileManagerType) PerClasspathEntryLookup(xsbti.compile.PerClasspathEntryLookup) LoggedReporter(sbt.internal.inc.LoggedReporter) PreviousResult(xsbti.compile.PreviousResult) CompileOptions(xsbti.compile.CompileOptions) IncOptions(xsbti.compile.IncOptions) Timer(org.gradle.internal.time.Timer) AnalysisStore(xsbti.compile.AnalysisStore) CompileResult(xsbti.compile.CompileResult) File(java.io.File) Compilers(xsbti.compile.Compilers)

Example 3 with ScalaInstance

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());
}
Also used : DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath) ClassPath(org.gradle.internal.classpath.ClassPath) URLClassLoader(java.net.URLClassLoader) ScalaInstance(sbt.internal.inc.ScalaInstance) File(java.io.File) URL(java.net.URL)

Aggregations

File (java.io.File)3 ScalaInstance (sbt.internal.inc.ScalaInstance)3 Iterables (com.google.common.collect.Iterables)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Inject (javax.inject.Inject)1 CompilationFailedException (org.gradle.api.internal.tasks.compile.CompilationFailedException)1 JavaCompilerArgumentsBuilder (org.gradle.api.internal.tasks.compile.JavaCompilerArgumentsBuilder)1 Logger (org.gradle.api.logging.Logger)1 Logging (org.gradle.api.logging.Logging)1 WorkResult (org.gradle.api.tasks.WorkResult)1 WorkResults (org.gradle.api.tasks.WorkResults)1 PersistentCache (org.gradle.cache.PersistentCache)1