Search in sources :

Example 76 with HashCode

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

the class AccumulateClassNamesStep method calculateClassHashes.

/**
   * @return an Optional that will be absent if there was an error.
   */
public static Optional<ImmutableSortedMap<String, HashCode>> calculateClassHashes(ExecutionContext context, ProjectFilesystem filesystem, Path path) {
    final Map<String, HashCode> classNames = new HashMap<>();
    ClasspathTraversal traversal = new ClasspathTraversal(Collections.singleton(path), filesystem) {

        @Override
        public void visit(final FileLike fileLike) throws IOException {
            // end in .class, which should be ignored.
            if (!FileLikes.isClassFile(fileLike)) {
                return;
            }
            String key = FileLikes.getFileNameWithoutClassSuffix(fileLike);
            ByteSource input = new ByteSource() {

                @Override
                public InputStream openStream() throws IOException {
                    return fileLike.getInput();
                }
            };
            HashCode value = input.hash(Hashing.sha1());
            HashCode existing = classNames.putIfAbsent(key, value);
            if (existing != null && !existing.equals(value)) {
                throw new IllegalArgumentException(String.format("Multiple entries with same key but differing values: %1$s=%2$s and %1$s=%3$s", key, value, existing));
            }
        }
    };
    try {
        new DefaultClasspathTraverser().traverse(traversal);
    } catch (IOException e) {
        context.logError(e, "Error accumulating class names for %s.", path);
        return Optional.empty();
    }
    return Optional.of(ImmutableSortedMap.copyOf(classNames, Ordering.natural()));
}
Also used : ClasspathTraversal(com.facebook.buck.jvm.java.classes.ClasspathTraversal) HashCode(com.google.common.hash.HashCode) HashMap(java.util.HashMap) DefaultClasspathTraverser(com.facebook.buck.jvm.java.classes.DefaultClasspathTraverser) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) FileLike(com.facebook.buck.jvm.java.classes.FileLike)

Example 77 with HashCode

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

the class AccumulateClassNamesStep method parseClassHashes.

/**
   * @param lines that were written in the same format output by {@link #execute(ExecutionContext)}.
   */
public static ImmutableSortedMap<String, HashCode> parseClassHashes(List<String> lines) {
    final Map<String, HashCode> classNames = new HashMap<>();
    for (String line : lines) {
        List<String> parts = CLASS_NAME_AND_HASH_SPLITTER.splitToList(line);
        Preconditions.checkState(parts.size() == 2);
        String key = parts.get(0);
        HashCode value = HashCode.fromString(parts.get(1));
        HashCode existing = classNames.putIfAbsent(key, value);
        if (existing != null && !existing.equals(value)) {
            throw new IllegalArgumentException(String.format("Multiple entries with same key but differing values: %1$s=%2$s and %1$s=%3$s", key, value, existing));
        }
    }
    return ImmutableSortedMap.copyOf(classNames, Ordering.natural());
}
Also used : HashCode(com.google.common.hash.HashCode) HashMap(java.util.HashMap)

Example 78 with HashCode

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

the class InterCellIntegrationTest method findObjectFiles.

private ImmutableMap<String, HashCode> findObjectFiles(final ProjectWorkspace workspace) throws IOException {
    ProjectFilesystem filesystem = new ProjectFilesystem(workspace.getDestPath());
    final Path buckOut = workspace.getPath(filesystem.getBuckPaths().getBuckOut());
    final ImmutableMap.Builder<String, HashCode> objectHashCodes = ImmutableMap.builder();
    Files.walkFileTree(buckOut, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (MorePaths.getFileExtension(file).equals("o")) {
                HashCode hash = MorePaths.asByteSource(file).hash(Hashing.sha1());
                objectHashCodes.put(buckOut.relativize(file).toString(), hash);
            }
            return FileVisitResult.CONTINUE;
        }
    });
    ImmutableMap<String, HashCode> toReturn = objectHashCodes.build();
    Preconditions.checkState(!toReturn.isEmpty());
    return toReturn;
}
Also used : Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) FileVisitResult(java.nio.file.FileVisitResult) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 79 with HashCode

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

the class InterCellIntegrationTest method xCellCxxLibraryBuildsShouldBeHermetic.

@Test
public void xCellCxxLibraryBuildsShouldBeHermetic() throws IOException {
    assumeThat(Platform.detect(), is(not(WINDOWS)));
    Pair<ProjectWorkspace, ProjectWorkspace> cells = prepare("inter-cell/export-file/primary", "inter-cell/export-file/secondary");
    ProjectWorkspace primary = cells.getFirst();
    ProjectWorkspace secondary = cells.getSecond();
    Path firstBinary = primary.buildAndReturnOutput("//:cxxbinary");
    ImmutableMap<String, HashCode> firstPrimaryObjectFiles = findObjectFiles(primary);
    ImmutableMap<String, HashCode> firstObjectFiles = findObjectFiles(secondary);
    // Now recreate an identical checkout
    cells = prepare("inter-cell/export-file/primary", "inter-cell/export-file/secondary");
    primary = cells.getFirst();
    secondary = cells.getSecond();
    Path secondBinary = primary.buildAndReturnOutput("//:cxxbinary");
    ImmutableMap<String, HashCode> secondPrimaryObjectFiles = findObjectFiles(primary);
    ImmutableMap<String, HashCode> secondObjectFiles = findObjectFiles(secondary);
    assertEquals(firstPrimaryObjectFiles, secondPrimaryObjectFiles);
    assertEquals(firstObjectFiles, secondObjectFiles);
    MoreAsserts.assertContentsEqual(firstBinary, secondBinary);
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) HashCode(com.google.common.hash.HashCode) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 80 with HashCode

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

the class DependencyFileRuleKeyFactoryTest method testKeysGetHashed.

@Test
public void testKeysGetHashed() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    RuleKeyFieldLoader fieldLoader = new RuleKeyFieldLoader(0);
    BuildRuleResolver ruleResolver = newRuleResolver();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    SourcePath unusedSourcePath = new PathSourcePath(filesystem, Paths.get("input0"));
    SourcePath sourcePath = new PathSourcePath(filesystem, Paths.get("input"));
    DependencyFileEntry dependencyFileEntry = DependencyFileEntry.fromSourcePath(sourcePath, pathResolver);
    ImmutableMap<Path, HashCode> hashes = ImmutableMap.of(pathResolver.getAbsolutePath(sourcePath), HashCode.fromInt(42));
    Predicate<SourcePath> coveredPredicate = ImmutableSet.of(sourcePath, unusedSourcePath)::contains;
    FakeDepFileBuildRule rule1 = new FakeDepFileBuildRule("//:rule") {

        @AddToRuleKey
        final Object myField1 = sourcePath;

        @AddToRuleKey
        final Object myField2 = unusedSourcePath;
    };
    rule1.setCoveredByDepFilePredicate(coveredPredicate);
    FakeFileHashCache hashCache = new FakeFileHashCache(hashes, true, ImmutableMap.of());
    RuleKeyAndInputs res1 = new DefaultDependencyFileRuleKeyFactory(fieldLoader, hashCache, pathResolver, ruleFinder).build(rule1, ImmutableList.of(dependencyFileEntry));
    FakeDepFileBuildRule rule2 = new FakeDepFileBuildRule("//:rule") {

        @AddToRuleKey
        final Object myField1 = unusedSourcePath;

        @AddToRuleKey
        final Object myField2 = sourcePath;
    };
    rule2.setCoveredByDepFilePredicate(coveredPredicate);
    hashCache = new FakeFileHashCache(hashes, true, ImmutableMap.of());
    RuleKeyAndInputs res2 = new DefaultDependencyFileRuleKeyFactory(fieldLoader, hashCache, pathResolver, ruleFinder).build(rule2, ImmutableList.of(dependencyFileEntry));
    assertThat(res2.getRuleKey(), Matchers.not(Matchers.equalTo(res1.getRuleKey())));
    assertThat(res2.getInputs(), Matchers.equalTo(ImmutableSet.of(sourcePath)));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) Path(java.nio.file.Path) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FakeDepFileBuildRule(com.facebook.buck.rules.FakeDepFileBuildRule) SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) HashCode(com.google.common.hash.HashCode) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Aggregations

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