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()));
}
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());
}
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;
}
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);
}
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)));
}
Aggregations