Search in sources :

Example 1 with PersistentCache

use of org.gradle.cache.PersistentCache in project gradle by gradle.

the class ZincScalaCompilerFactory method createParallelSafeCompiler.

static Compiler createParallelSafeCompiler(final Iterable<File> scalaClasspath, final Iterable<File> zincClasspath, final xsbti.Logger logger, File gradleUserHome) {
    File zincCacheHomeDir = new File(System.getProperty(ZincScalaCompilerUtil.ZINC_CACHE_HOME_DIR_SYSTEM_PROPERTY, gradleUserHome.getAbsolutePath()));
    CacheRepository cacheRepository = ZincCompilerServices.getInstance(zincCacheHomeDir).get(CacheRepository.class);
    String zincVersion = Setup.zincVersion().published();
    String zincCacheKey = String.format("zinc-%s", zincVersion);
    String zincCacheName = String.format("Zinc %s compiler cache", zincVersion);
    final PersistentCache zincCache = cacheRepository.cache(zincCacheKey).withDisplayName(zincCacheName).withLockOptions(mode(FileLockManager.LockMode.Exclusive)).open();
    Compiler compiler;
    try {
        final File cacheDir = zincCache.getBaseDir();
        final String userSuppliedZincDir = System.getProperty("zinc.dir");
        if (userSuppliedZincDir != null && !userSuppliedZincDir.equals(cacheDir.getAbsolutePath())) {
            LOGGER.warn(ZincScalaCompilerUtil.ZINC_DIR_IGNORED_MESSAGE);
        }
        compiler = SystemProperties.getInstance().withSystemProperty(ZincScalaCompilerUtil.ZINC_DIR_SYSTEM_PROPERTY, cacheDir.getAbsolutePath(), new Factory<Compiler>() {

            @Override
            public Compiler create() {
                Setup zincSetup = createZincSetup(scalaClasspath, zincClasspath, logger);
                return createCompiler(zincSetup, zincCache, logger);
            }
        });
    } finally {
        zincCache.close();
    }
    return compiler;
}
Also used : AnalyzingCompiler(sbt.compiler.AnalyzingCompiler) Compiler(com.typesafe.zinc.Compiler) JavaCompiler(xsbti.compile.JavaCompiler) PersistentCache(org.gradle.cache.PersistentCache) Factory(org.gradle.internal.Factory) CacheRepository(org.gradle.cache.CacheRepository) Setup(com.typesafe.zinc.Setup) File(java.io.File)

Example 2 with PersistentCache

use of org.gradle.cache.PersistentCache in project gradle by gradle.

the class DefaultBuildOutputCleanupCache method cleanIfStale.

@Override
public void cleanIfStale() {
    IoActions.withResource(createCache(), new Action<PersistentCache>() {

        @Override
        public void execute(PersistentCache cache) {
            final File markerFile = new File(cache.getBaseDir(), "built.bin");
            cache.useCache(new Runnable() {

                @Override
                public void run() {
                    boolean markerFileExists = markerFile.exists();
                    if (!markerFileExists) {
                        buildOutputDeleter.delete(buildOutputCleanupRegistry.getOutputs());
                        GFileUtils.touch(markerFile);
                    }
                }
            });
        }
    });
}
Also used : PersistentCache(org.gradle.cache.PersistentCache) File(java.io.File)

Example 3 with PersistentCache

use of org.gradle.cache.PersistentCache in project gradle by gradle.

the class FileCacheBackedScriptClassCompiler method compile.

@Override
public <T extends Script, M> CompiledScript<T, M> compile(final ScriptSource source, final ClassLoader classLoader, final ClassLoaderId classLoaderId, final CompileOperation<M> operation, final Class<T> scriptBaseClass, final Action<? super ClassNode> verifier) {
    assert source.getResource().isContentCached();
    if (source.getResource().getHasEmptyContent()) {
        return emptyCompiledScript(classLoaderId, operation);
    }
    HashCode sourceHashCode = hasher.hash(source.getResource());
    final String sourceHash = HashUtil.compactStringFor(sourceHashCode);
    final String dslId = operation.getId();
    HashCode classLoaderHash = classLoaderHierarchyHasher.getClassLoaderHash(classLoader);
    if (classLoaderHash == null) {
        throw new IllegalArgumentException("Unknown classloader: " + classLoader);
    }
    final String classpathHash = dslId + classLoaderHash;
    final RemappingScriptSource remapped = new RemappingScriptSource(source);
    // Caching involves 2 distinct caches, so that 2 scripts with the same (hash, classpath) do not get compiled twice
    // 1. First, we look for a cache script which (path, hash) matches. This cache is invalidated when the compile classpath of the script changes
    // 2. Then we look into the 2d cache for a "generic script" with the same hash, that will be remapped to the script class name
    // Both caches can be closed directly after use because:
    // For 1, if the script changes or its compile classpath changes, a different directory will be used
    // For 2, if the script changes, a different cache is used. If the classpath changes, the cache is invalidated, but classes are remapped to 1. anyway so never directly used
    PersistentCache remappedClassesCache = cacheRepository.cache("scripts-remapped/" + source.getClassName() + "/" + sourceHash + "/" + classpathHash).withDisplayName(dslId + " remapped class cache for " + sourceHash).withValidator(validator).withInitializer(new ProgressReportingInitializer(progressLoggerFactory, new RemapBuildScriptsAction<M, T>(remapped, classpathHash, sourceHash, dslId, classLoader, operation, verifier, scriptBaseClass), "Compiling script into cache", "Compiling " + source.getFileName() + " into local compilation cache")).open();
    remappedClassesCache.close();
    File remappedClassesDir = classesDir(remappedClassesCache);
    File remappedMetadataDir = metadataDir(remappedClassesCache);
    return scriptCompilationHandler.loadFromDir(source, sourceHashCode, classLoader, remappedClassesDir, remappedMetadataDir, operation, scriptBaseClass, classLoaderId);
}
Also used : PersistentCache(org.gradle.cache.PersistentCache) HashCode(com.google.common.hash.HashCode) File(java.io.File)

Aggregations

File (java.io.File)3 PersistentCache (org.gradle.cache.PersistentCache)3 HashCode (com.google.common.hash.HashCode)1 Compiler (com.typesafe.zinc.Compiler)1 Setup (com.typesafe.zinc.Setup)1 CacheRepository (org.gradle.cache.CacheRepository)1 Factory (org.gradle.internal.Factory)1 AnalyzingCompiler (sbt.compiler.AnalyzingCompiler)1 JavaCompiler (xsbti.compile.JavaCompiler)1