Search in sources :

Example 11 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class WorkerProcessPoolFactory method getWorkerProcessPool.

/**
   * Returns an existing WorkerProcessPool for the given job params if one exists, otherwise
   * creates a new one.
   */
public WorkerProcessPool getWorkerProcessPool(final ExecutionContext context, WorkerJobParams paramsToUse) {
    ConcurrentMap<String, WorkerProcessPool> processPoolMap;
    final String key;
    final HashCode workerHash;
    if (paramsToUse.getPersistentWorkerKey().isPresent() && context.getPersistentWorkerPools().isPresent()) {
        processPoolMap = context.getPersistentWorkerPools().get();
        key = paramsToUse.getPersistentWorkerKey().get();
        workerHash = paramsToUse.getWorkerHash().get();
    } else {
        processPoolMap = context.getWorkerProcessPools();
        key = Joiner.on(' ').join(getCommand(context.getPlatform(), paramsToUse));
        workerHash = Hashing.sha1().hashString(key, StandardCharsets.UTF_8);
    }
    // If the worker pool has a different hash, recreate the pool.
    WorkerProcessPool pool = processPoolMap.get(key);
    if (pool != null && !pool.getPoolHash().equals(workerHash)) {
        if (processPoolMap.remove(key, pool)) {
            pool.close();
        }
        pool = processPoolMap.get(key);
    }
    if (pool == null) {
        pool = createWorkerProcessPool(context, paramsToUse, processPoolMap, key, workerHash);
    }
    int poolCapacity = pool.getCapacity();
    if (poolCapacity != paramsToUse.getMaxWorkers()) {
        context.postEvent(ConsoleEvent.warning("There are two 'worker_tool' targets declared with the same command (%s), but " + "different 'max_worker' settings (%d and %d). Only the first capacity is applied. " + "Consolidate these workers to avoid this warning.", key, poolCapacity, paramsToUse.getMaxWorkers()));
    }
    return pool;
}
Also used : HashCode(com.google.common.hash.HashCode)

Example 12 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class AppleBundle method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> stepsBuilder = ImmutableList.builder();
    stepsBuilder.add(new MakeCleanDirectoryStep(getProjectFilesystem(), bundleRoot));
    Path resourcesDestinationPath = bundleRoot.resolve(this.destinations.getResourcesPath());
    if (assetCatalog.isPresent()) {
        stepsBuilder.add(new MkdirStep(getProjectFilesystem(), resourcesDestinationPath));
        Path bundleDir = assetCatalog.get().getOutputDir();
        stepsBuilder.add(CopyStep.forDirectory(getProjectFilesystem(), bundleDir, resourcesDestinationPath, CopyStep.DirectoryMode.CONTENTS_ONLY));
    }
    if (coreDataModel.isPresent()) {
        stepsBuilder.add(new MkdirStep(getProjectFilesystem(), resourcesDestinationPath));
        stepsBuilder.add(CopyStep.forDirectory(getProjectFilesystem(), context.getSourcePathResolver().getRelativePath(coreDataModel.get().getSourcePathToOutput()), resourcesDestinationPath, CopyStep.DirectoryMode.CONTENTS_ONLY));
    }
    if (sceneKitAssets.isPresent()) {
        stepsBuilder.add(new MkdirStep(getProjectFilesystem(), resourcesDestinationPath));
        stepsBuilder.add(CopyStep.forDirectory(getProjectFilesystem(), context.getSourcePathResolver().getRelativePath(sceneKitAssets.get().getSourcePathToOutput()), resourcesDestinationPath, CopyStep.DirectoryMode.CONTENTS_ONLY));
    }
    Path metadataPath = getMetadataPath();
    Path infoPlistInputPath = context.getSourcePathResolver().getAbsolutePath(infoPlist);
    Path infoPlistSubstitutionTempPath = BuildTargets.getScratchPath(getProjectFilesystem(), getBuildTarget(), "%s.plist");
    Path infoPlistOutputPath = metadataPath.resolve("Info.plist");
    stepsBuilder.add(new MkdirStep(getProjectFilesystem(), metadataPath), // TODO(bhamiltoncx): This is only appropriate for .app bundles.
    new WriteFileStep(getProjectFilesystem(), "APPLWRUN", metadataPath.resolve("PkgInfo"), /* executable */
    false), new MkdirStep(getProjectFilesystem(), infoPlistSubstitutionTempPath.getParent()), new FindAndReplaceStep(getProjectFilesystem(), infoPlistInputPath, infoPlistSubstitutionTempPath, InfoPlistSubstitution.createVariableExpansionFunction(withDefaults(infoPlistSubstitutions, ImmutableMap.of("EXECUTABLE_NAME", binaryName, "PRODUCT_NAME", binaryName)))), new PlistProcessStep(getProjectFilesystem(), infoPlistSubstitutionTempPath, assetCatalog.isPresent() ? Optional.of(assetCatalog.get().getOutputPlist()) : Optional.empty(), infoPlistOutputPath, getInfoPlistAdditionalKeys(), getInfoPlistOverrideKeys(), PlistProcessStep.OutputFormat.BINARY));
    if (hasBinary) {
        appendCopyBinarySteps(stepsBuilder, context.getSourcePathResolver());
        appendCopyDsymStep(stepsBuilder, buildableContext, context.getSourcePathResolver());
    }
    if (!Iterables.isEmpty(Iterables.concat(resources.getResourceDirs(), resources.getDirsContainingResourceDirs(), resources.getResourceFiles()))) {
        stepsBuilder.add(new MkdirStep(getProjectFilesystem(), resourcesDestinationPath));
        for (SourcePath dir : resources.getResourceDirs()) {
            stepsBuilder.add(CopyStep.forDirectory(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(dir), resourcesDestinationPath, CopyStep.DirectoryMode.DIRECTORY_AND_CONTENTS));
        }
        for (SourcePath dir : resources.getDirsContainingResourceDirs()) {
            stepsBuilder.add(CopyStep.forDirectory(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(dir), resourcesDestinationPath, CopyStep.DirectoryMode.CONTENTS_ONLY));
        }
        for (SourcePath file : resources.getResourceFiles()) {
            // TODO(shs96c): Check that this work cross-cell
            Path resolvedFilePath = context.getSourcePathResolver().getRelativePath(file);
            Path destinationPath = resourcesDestinationPath.resolve(resolvedFilePath.getFileName());
            addResourceProcessingSteps(context.getSourcePathResolver(), resolvedFilePath, destinationPath, stepsBuilder);
        }
    }
    ImmutableList.Builder<Path> codeSignOnCopyPathsBuilder = ImmutableList.builder();
    addStepsToCopyExtensionBundlesDependencies(context.getSourcePathResolver(), stepsBuilder, codeSignOnCopyPathsBuilder);
    for (SourcePath variantSourcePath : resources.getResourceVariantFiles()) {
        // TODO(shs96c): Ensure this works cross-cell, as relative path begins with "buck-out"
        Path variantFilePath = context.getSourcePathResolver().getRelativePath(variantSourcePath);
        Path variantDirectory = variantFilePath.getParent();
        if (variantDirectory == null || !variantDirectory.toString().endsWith(".lproj")) {
            throw new HumanReadableException("Variant files have to be in a directory with name ending in '.lproj', " + "but '%s' is not.", variantFilePath);
        }
        Path bundleVariantDestinationPath = resourcesDestinationPath.resolve(variantDirectory.getFileName());
        stepsBuilder.add(new MkdirStep(getProjectFilesystem(), bundleVariantDestinationPath));
        Path destinationPath = bundleVariantDestinationPath.resolve(variantFilePath.getFileName());
        addResourceProcessingSteps(context.getSourcePathResolver(), variantFilePath, destinationPath, stepsBuilder);
    }
    if (!frameworks.isEmpty()) {
        Path frameworksDestinationPath = bundleRoot.resolve(this.destinations.getFrameworksPath());
        stepsBuilder.add(new MkdirStep(getProjectFilesystem(), frameworksDestinationPath));
        for (SourcePath framework : frameworks) {
            Path srcPath = context.getSourcePathResolver().getAbsolutePath(framework);
            stepsBuilder.add(CopyStep.forDirectory(getProjectFilesystem(), srcPath, frameworksDestinationPath, CopyStep.DirectoryMode.DIRECTORY_AND_CONTENTS));
            codeSignOnCopyPathsBuilder.add(frameworksDestinationPath.resolve(srcPath.getFileName()));
        }
    }
    if (needCodeSign()) {
        Optional<Path> signingEntitlementsTempPath;
        Supplier<CodeSignIdentity> codeSignIdentitySupplier;
        if (adHocCodeSignIsSufficient()) {
            signingEntitlementsTempPath = Optional.empty();
            codeSignIdentitySupplier = () -> CodeSignIdentity.AD_HOC;
        } else {
            // Copy the .mobileprovision file if the platform requires it, and sign the executable.
            Optional<Path> entitlementsPlist = Optional.empty();
            final Path srcRoot = getProjectFilesystem().getRootPath().resolve(getBuildTarget().getBasePath());
            Optional<String> entitlementsPlistString = InfoPlistSubstitution.getVariableExpansionForPlatform(CODE_SIGN_ENTITLEMENTS, platform.getName(), withDefaults(infoPlistSubstitutions, ImmutableMap.of("SOURCE_ROOT", srcRoot.toString(), "SRCROOT", srcRoot.toString())));
            if (entitlementsPlistString.isPresent()) {
                entitlementsPlist = Optional.of(srcRoot.resolve(Paths.get(entitlementsPlistString.get())));
            }
            signingEntitlementsTempPath = Optional.of(BuildTargets.getScratchPath(getProjectFilesystem(), getBuildTarget(), "%s.xcent"));
            final Path dryRunResultPath = bundleRoot.resolve(PP_DRY_RUN_RESULT_FILE);
            final ProvisioningProfileCopyStep provisioningProfileCopyStep = new ProvisioningProfileCopyStep(getProjectFilesystem(), infoPlistOutputPath, platform, // Provisioning profile UUID -- find automatically.
            Optional.empty(), entitlementsPlist, provisioningProfileStore, resourcesDestinationPath.resolve("embedded.mobileprovision"), dryRunCodeSigning ? bundleRoot.resolve(CODE_SIGN_DRY_RUN_ENTITLEMENTS_FILE) : signingEntitlementsTempPath.get(), codeSignIdentityStore, dryRunCodeSigning ? Optional.of(dryRunResultPath) : Optional.empty());
            stepsBuilder.add(provisioningProfileCopyStep);
            codeSignIdentitySupplier = () -> {
                // Using getUnchecked here because the previous step should already throw if exception
                // occurred, and this supplier would never be evaluated.
                Optional<ProvisioningProfileMetadata> selectedProfile = Futures.getUnchecked(provisioningProfileCopyStep.getSelectedProvisioningProfileFuture());
                if (!selectedProfile.isPresent()) {
                    // This should only happen in dry-run codesign mode (since otherwise an exception
                    // would have been thrown already.)  Still, we need to return *something*.
                    Preconditions.checkState(dryRunCodeSigning);
                    return CodeSignIdentity.AD_HOC;
                }
                ImmutableSet<HashCode> fingerprints = selectedProfile.get().getDeveloperCertificateFingerprints();
                if (fingerprints.isEmpty()) {
                    // If no identities are available, use an ad-hoc identity.
                    return Iterables.getFirst(codeSignIdentityStore.getIdentities(), CodeSignIdentity.AD_HOC);
                }
                for (CodeSignIdentity identity : codeSignIdentityStore.getIdentities()) {
                    if (identity.getFingerprint().isPresent() && fingerprints.contains(identity.getFingerprint().get())) {
                        return identity;
                    }
                }
                throw new HumanReadableException("No code sign identity available for provisioning profile: %s\n" + "Profile requires an identity with one of the following SHA1 fingerprints " + "available in your keychain: \n  %s", selectedProfile.get().getProfilePath(), Joiner.on("\n  ").join(fingerprints));
            };
        }
        addSwiftStdlibStepIfNeeded(context.getSourcePathResolver(), bundleRoot.resolve(Paths.get("Frameworks")), dryRunCodeSigning ? Optional.<Supplier<CodeSignIdentity>>empty() : Optional.of(codeSignIdentitySupplier), stepsBuilder, false);
        for (Path codeSignOnCopyPath : codeSignOnCopyPathsBuilder.build()) {
            stepsBuilder.add(new CodeSignStep(getProjectFilesystem(), context.getSourcePathResolver(), codeSignOnCopyPath, Optional.empty(), codeSignIdentitySupplier, codesign, codesignAllocatePath, dryRunCodeSigning ? Optional.of(codeSignOnCopyPath.resolve(CODE_SIGN_DRY_RUN_ARGS_FILE)) : Optional.empty()));
        }
        stepsBuilder.add(new CodeSignStep(getProjectFilesystem(), context.getSourcePathResolver(), bundleRoot, signingEntitlementsTempPath, codeSignIdentitySupplier, codesign, codesignAllocatePath, dryRunCodeSigning ? Optional.of(bundleRoot.resolve(CODE_SIGN_DRY_RUN_ARGS_FILE)) : Optional.empty()));
    } else {
        addSwiftStdlibStepIfNeeded(context.getSourcePathResolver(), bundleRoot.resolve(Paths.get("Frameworks")), Optional.<Supplier<CodeSignIdentity>>empty(), stepsBuilder, false);
    }
    // Ensure the bundle directory is archived so we can fetch it later.
    buildableContext.recordArtifact(context.getSourcePathResolver().getRelativePath(getSourcePathToOutput()));
    return stepsBuilder.build();
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) FindAndReplaceStep(com.facebook.buck.step.fs.FindAndReplaceStep) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) RmStep(com.facebook.buck.step.fs.RmStep) CopyStep(com.facebook.buck.step.fs.CopyStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep) Step(com.facebook.buck.step.Step) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) MoveStep(com.facebook.buck.step.fs.MoveStep) FindAndReplaceStep(com.facebook.buck.step.fs.FindAndReplaceStep) NSString(com.dd.plist.NSString) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) HashCode(com.google.common.hash.HashCode) HumanReadableException(com.facebook.buck.util.HumanReadableException) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Supplier(com.google.common.base.Supplier) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep)

Example 13 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class Manifest method hashesMatch.

private boolean hashesMatch(FileHashCache fileHashCache, SourcePathResolver resolver, ImmutableListMultimap<String, SourcePath> universe, int[] hashIndices) throws IOException {
    for (int hashIndex : hashIndices) {
        Pair<Integer, HashCode> hashEntry = hashes.get(hashIndex);
        String header = headers.get(hashEntry.getFirst());
        ImmutableList<SourcePath> candidates = universe.get(header);
        if (candidates.isEmpty()) {
            return false;
        }
        HashCode onDiskHeaderHash;
        try {
            onDiskHeaderHash = hashSourcePathGroup(fileHashCache, resolver, candidates);
        } catch (NoSuchFileException e) {
            return false;
        }
        HashCode headerHash = hashEntry.getSecond();
        if (!headerHash.equals(onDiskHeaderHash)) {
            return false;
        }
    }
    return true;
}
Also used : HashCode(com.google.common.hash.HashCode) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 14 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class ActionGraphCache method getActionGraph.

/**
   * It returns an {@link ActionGraphAndResolver}. If the {@code targetGraph} exists in the cache
   * it returns a cached version of the {@link ActionGraphAndResolver}, else returns a new one and
   * updates the cache.
   * @param eventBus the {@link BuckEventBus} to post the events of the processing.
   * @param skipActionGraphCache if true, do not invalidate the {@link ActionGraph} cached in
   *     memory. Instead, create a new {@link ActionGraph} for this request, which should be
   *     garbage-collected at the end of the request.
   * @param targetGraph the target graph that the action graph will be based on.
   * @return a {@link ActionGraphAndResolver}
   */
public ActionGraphAndResolver getActionGraph(final BuckEventBus eventBus, final boolean checkActionGraphs, final boolean skipActionGraphCache, final TargetGraph targetGraph, int keySeed) {
    ActionGraphEvent.Started started = ActionGraphEvent.started();
    eventBus.post(started);
    ActionGraphAndResolver out;
    try {
        RuleKeyFieldLoader fieldLoader = new RuleKeyFieldLoader(keySeed);
        if (lastActionGraph != null && lastActionGraph.getFirst().equals(targetGraph)) {
            eventBus.post(ActionGraphEvent.Cache.hit());
            LOG.info("ActionGraph cache hit.");
            if (checkActionGraphs) {
                compareActionGraphs(eventBus, lastActionGraph.getSecond(), targetGraph, fieldLoader);
            }
            out = lastActionGraph.getSecond();
        } else {
            eventBus.post(ActionGraphEvent.Cache.miss(lastActionGraph == null));
            LOG.debug("Computing TargetGraph HashCode...");
            HashCode targetGraphHash = getTargetGraphHash(targetGraph);
            if (lastActionGraph == null) {
                LOG.info("ActionGraph cache miss. Cache was empty.");
            } else if (Objects.equals(lastTargetGraphHash, targetGraphHash)) {
                LOG.info("ActionGraph cache miss. TargetGraphs mismatched but hashes are the same.");
                eventBus.post(ActionGraphEvent.Cache.missWithTargetGraphHashMatch());
            } else {
                LOG.info("ActionGraph cache miss. TargetGraphs mismatched.");
            }
            lastTargetGraphHash = targetGraphHash;
            Pair<TargetGraph, ActionGraphAndResolver> freshActionGraph = new Pair<TargetGraph, ActionGraphAndResolver>(targetGraph, createActionGraph(eventBus, new DefaultTargetNodeToBuildRuleTransformer(), targetGraph));
            out = freshActionGraph.getSecond();
            if (!skipActionGraphCache) {
                LOG.info("ActionGraph cache assignment. skipActionGraphCache? %s", skipActionGraphCache);
                lastActionGraph = freshActionGraph;
            }
        }
    } finally {
        eventBus.post(ActionGraphEvent.finished(started));
    }
    return out;
}
Also used : RuleKeyFieldLoader(com.facebook.buck.rules.keys.RuleKeyFieldLoader) HashCode(com.google.common.hash.HashCode) ActionGraphEvent(com.facebook.buck.event.ActionGraphEvent) Pair(com.facebook.buck.model.Pair)

Example 15 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class DefaultFileHashCache method get.

/**
   * @return The {@link com.google.common.hash.HashCode} of the contents of path.
   */
@Override
public HashCode get(Path relativePath) throws IOException {
    Preconditions.checkArgument(!relativePath.isAbsolute());
    checkNotIgnored(relativePath);
    HashCode sha1;
    try {
        sha1 = loadingCache.get(relativePath.normalize()).getHashCode();
    } catch (ExecutionException e) {
        Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
        throw new RuntimeException(e.getCause());
    }
    return Preconditions.checkNotNull(sha1, "Failed to find a HashCode for %s.", relativePath);
}
Also used : HashCode(com.google.common.hash.HashCode) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

HashCode (com.google.common.hash.HashCode)120 Path (java.nio.file.Path)40 Test (org.junit.Test)39 BuildTarget (com.facebook.buck.model.BuildTarget)19 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)13 IOException (java.io.IOException)13 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)12 PathSourcePath (com.facebook.buck.rules.PathSourcePath)11 FileHashCache (com.facebook.buck.util.cache.FileHashCache)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)10 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)10 Hasher (com.google.common.hash.Hasher)9 Map (java.util.Map)9 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)7 SourcePath (com.facebook.buck.rules.SourcePath)7 File (java.io.File)7 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5