use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.
the class PrebuiltCxxLibraryTest method testGetNativeLinkWithDep.
@Test
public void testGetNativeLinkWithDep() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
CxxPlatform platform = CxxLibraryBuilder.createDefaultPlatform();
GenruleBuilder genSrcBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:gen_libx")).setOut("gen_libx").setCmd("something");
BuildTarget target = BuildTargetFactory.newInstance("//:x");
PrebuiltCxxLibraryBuilder builder = new PrebuiltCxxLibraryBuilder(target).setLibName("x").setLibDir("$(location //:gen_libx)");
TargetGraph targetGraph = TargetGraphFactory.newInstance(genSrcBuilder.build(), builder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
BuildRule genSrc = genSrcBuilder.build(resolver, filesystem, targetGraph);
filesystem.writeContentsToPath("class Test {}", pathResolver.getAbsolutePath(genSrc.getSourcePathToOutput()).resolve("libx.a"));
PrebuiltCxxLibrary lib = (PrebuiltCxxLibrary) builder.build(resolver, filesystem, targetGraph);
lib.getNativeLinkableInput(platform, Linker.LinkableDepType.STATIC);
FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
RuleKey ruleKey = factory.build(lib);
assertNotNull(ruleKey);
}
use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.
the class DefaultJavaLibraryTest method testInputBasedRuleKeyWithRecursiveExportedDeps.
/**
* Tests that input-based rule keys work properly with a Java library dep exported through
* multiple Java library dependencies.
*/
@Test
public void testInputBasedRuleKeyWithRecursiveExportedDeps() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
// Setup a Java library which builds against another Java library dep exporting another Java
// library dep.
TargetNode<JavaLibraryDescription.Arg, ?> exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).build();
TargetNode<?, ?> dep2Node = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep2"), filesystem).addExportedDep(exportedDepNode.getBuildTarget()).build();
TargetNode<?, ?> dep1Node = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep1"), filesystem).addExportedDep(dep2Node.getBuildTarget()).build();
TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib"), filesystem).addDep(dep1Node.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(exportedDepNode, dep2Node, dep1Node, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
JavaLibrary exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
JavaLibrary library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "ABI JAR contents");
FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
InputBasedRuleKeyFactory factory = new InputBasedRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
RuleKey originalRuleKey = factory.build(library);
// Now change the exported Java library dependency such that its rule key changes, and change
// its JAR contents, but keep its ABI JAR the same. This should *not* affect the input-based
// rule key of the consuming java library, since it only cares about the contents of the ABI
// JAR.
exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).setResourcesRoot(Paths.get("some root that changes the rule key")).build();
targetGraph = TargetGraphFactory.newInstance(exportedDepNode, dep2Node, dep1Node, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("different JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
FileHashCache unaffectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, unaffectedHashCache, pathResolver, ruleFinder);
RuleKey unaffectedRuleKey = factory.build(library);
assertThat(originalRuleKey, equalTo(unaffectedRuleKey));
// Now actually change the exproted Java library dependency's ABI JAR. This *should* affect
// the input-based rule key of the consuming java library.
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "changed ABI JAR contents");
FileHashCache affectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, affectedHashCache, pathResolver, ruleFinder);
RuleKey affectedRuleKey = factory.build(library);
assertThat(originalRuleKey, Matchers.not(equalTo(affectedRuleKey)));
}
use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.
the class DefaultJavaLibraryTest method testInputBasedRuleKeyWithExportedDeps.
/**
* Tests that input-based rule keys work properly with a Java library dep exported by a
* first-order dep.
*/
@Test
public void testInputBasedRuleKeyWithExportedDeps() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
// Setup a Java library which builds against another Java library dep exporting another Java
// library dep.
TargetNode<JavaLibraryDescription.Arg, ?> exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).build();
TargetNode<?, ?> depNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep"), filesystem).addExportedDep(exportedDepNode.getBuildTarget()).build();
TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib"), filesystem).addDep(depNode.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(exportedDepNode, depNode, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
JavaLibrary exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
JavaLibrary library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "ABI JAR contents");
FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
InputBasedRuleKeyFactory factory = new InputBasedRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
RuleKey originalRuleKey = factory.build(library);
// Now change the exported Java library dependency such that its rule key changes, and change
// its JAR contents, but keep its ABI JAR the same. This should *not* affect the input-based
// rule key of the consuming java library, since it only cares about the contents of the ABI
// JAR.
exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).setResourcesRoot(Paths.get("some root that changes the rule key")).build();
targetGraph = TargetGraphFactory.newInstance(exportedDepNode, depNode, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("different JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
FileHashCache unaffectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, unaffectedHashCache, pathResolver, ruleFinder);
RuleKey unaffectedRuleKey = factory.build(library);
assertThat(originalRuleKey, equalTo(unaffectedRuleKey));
// Now actually change the exproted Java library dependency's ABI JAR. This *should* affect
// the input-based rule key of the consuming java library.
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "changed ABI JAR contents");
FileHashCache affectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, affectedHashCache, pathResolver, ruleFinder);
RuleKey affectedRuleKey = factory.build(library);
assertThat(originalRuleKey, Matchers.not(equalTo(affectedRuleKey)));
}
use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.
the class JavaLibrarySymbolsFinderTest method onlyNonGeneratedSrcsShouldAffectRuleKey.
@Test
@SuppressWarnings("PMD.PrematureDeclaration")
public void onlyNonGeneratedSrcsShouldAffectRuleKey() throws IOException {
TestDataHelper.createProjectWorkspaceForScenario(this, "java_library_symbols_finder", tmp).setUp();
final ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp.getRoot());
Function<String, SourcePath> convert = src -> new PathSourcePath(projectFilesystem, Paths.get(src));
SourcePath example1 = convert.apply("Example1.java");
SourcePath example2 = convert.apply("Example2.java");
final BuildTarget fakeBuildTarget = BuildTargetFactory.newInstance("//foo:GenEx.java");
SourcePath generated = new DefaultBuildTargetSourcePath(fakeBuildTarget);
final boolean shouldRecordRequiredSymbols = true;
JavaLibrarySymbolsFinder example1Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1), javaFileParser, shouldRecordRequiredSymbols);
JavaLibrarySymbolsFinder example2Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example2), javaFileParser, shouldRecordRequiredSymbols);
JavaLibrarySymbolsFinder example1AndGeneratedSrcFinder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1, generated), javaFileParser, shouldRecordRequiredSymbols);
// Mock out calls to a SourcePathResolver so we can create a legitimate
// DefaultRuleKeyFactory.
final SourcePathRuleFinder ruleFinder = createMock(SourcePathRuleFinder.class);
final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
expect(ruleFinder.getRule(anyObject(SourcePath.class))).andAnswer(() -> {
SourcePath input = (SourcePath) EasyMock.getCurrentArguments()[0];
if (input instanceof ExplicitBuildTargetSourcePath) {
return Optional.of(new FakeBuildRule(fakeBuildTarget, pathResolver));
} else {
return Optional.empty();
}
}).anyTimes();
// Calculates the RuleKey for a JavaSymbolsRule with the specified JavaLibrarySymbolsFinder.
final FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(projectFilesystem)));
final DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder);
Function<JavaLibrarySymbolsFinder, RuleKey> createRuleKey = finder -> {
JavaSymbolsRule javaSymbolsRule = new JavaSymbolsRule(BuildTargetFactory.newInstance("//foo:rule"), finder, ImmutableSortedSet.of(), ObjectMappers.newDefaultInstance(), projectFilesystem);
return ruleKeyFactory.build(javaSymbolsRule);
};
RuleKey key1 = createRuleKey.apply(example1Finder);
RuleKey key2 = createRuleKey.apply(example2Finder);
RuleKey key3 = createRuleKey.apply(example1AndGeneratedSrcFinder);
assertNotNull(key1);
assertNotNull(key2);
assertNotNull(key3);
assertNotEquals("Two instances of a JavaSymbolsRule with different srcs should change the RuleKey.", key1, key2);
assertEquals("Introducing an extra generated .java file to the srcs should not change the RuleKey.", key1, key3);
}
use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.
the class CommandToolTest method sourcePathsContributeToRuleKeys.
@Test
public void sourcePathsContributeToRuleKeys() {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
SourcePath path = new FakeSourcePath("input");
CommandTool tool = new CommandTool.Builder().addArg(SourcePathArg.of(path)).build();
FileHashCache hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.of("input", Strings.repeat("a", 40)));
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder);
RuleKey ruleKey = new UncachedRuleKeyBuilder(ruleFinder, pathResolver, hashCache, ruleKeyFactory).setReflectively("key", tool).build();
hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.of("input", Strings.repeat("b", 40)));
ruleKeyFactory = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder);
RuleKey changedRuleKey = new UncachedRuleKeyBuilder(ruleFinder, pathResolver, hashCache, ruleKeyFactory).setReflectively("key", tool).build();
assertThat(ruleKey, Matchers.not(Matchers.equalTo(changedRuleKey)));
}
Aggregations