Search in sources :

Example 26 with DefaultRuleKeyFactory

use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.

the class GenruleTest method thatChangingOutChangesRuleKey.

@Test
public void thatChangingOutChangesRuleKey() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, new NullFileHashCache(), pathResolver, ruleFinder);
    // Get a rule key for two genrules using two different output names, but are otherwise the
    // same.
    RuleKey key1 = ruleKeyFactory.build(GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:genrule1")).setOut("foo").build(resolver));
    RuleKey key2 = ruleKeyFactory.build(GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:genrule2")).setOut("bar").build(resolver));
    // Verify that just the difference in output name is enough to make the rule key different.
    assertNotEquals(key1, key2);
}
Also used : DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 27 with DefaultRuleKeyFactory

use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.

the class GenruleTest method inputBasedRuleKeyExecutableMacro.

@Test
public void inputBasedRuleKeyExecutableMacro() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    GenruleBuilder ruleBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:rule")).setCmd("run $(exe //:dep)").setOut("output");
    // Create an initial input-based rule key
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildRule dep = new ShBinaryBuilder(BuildTargetFactory.newInstance("//:dep")).setMain(new PathSourcePath(filesystem, Paths.get("dep.exe"))).build(resolver, filesystem);
    filesystem.writeContentsToPath("something", Paths.get("dep.exe"));
    filesystem.writeContentsToPath("something", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
    BuildRule rule = ruleBuilder.build(resolver);
    DefaultRuleKeyFactory defaultRuleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    InputBasedRuleKeyFactory inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey originalRuleKey = defaultRuleKeyFactory.build(rule);
    RuleKey originalInputRuleKey = inputBasedRuleKeyFactory.build(rule);
    // Change the dep's resource list, which will change its normal rule key, but since we're
    // keeping its output the same, the input-based rule key for the consuming rule will stay the
    // same.  This is because the input-based rule key for the consuming rule only cares about the
    // contents of the output this rule produces.
    resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    Genrule extra = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:extra")).setOut("something").build(resolver);
    new ShBinaryBuilder(BuildTargetFactory.newInstance("//:dep")).setMain(new PathSourcePath(filesystem, Paths.get("dep.exe"))).setDeps(ImmutableSortedSet.of(extra.getBuildTarget())).build(resolver, filesystem);
    rule = ruleBuilder.build(resolver);
    ruleFinder = new SourcePathRuleFinder(resolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    defaultRuleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey unchangedRuleKey = defaultRuleKeyFactory.build(rule);
    RuleKey unchangedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
    assertThat(unchangedRuleKey, Matchers.not(Matchers.equalTo(originalRuleKey)));
    assertThat(unchangedInputBasedRuleKey, Matchers.equalTo(originalInputRuleKey));
    // Make a change to the dep's output, which *should* affect the input-based rule key.
    resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    dep = new ShBinaryBuilder(BuildTargetFactory.newInstance("//:dep")).setMain(new PathSourcePath(filesystem, Paths.get("dep.exe"))).build(resolver, filesystem);
    filesystem.writeContentsToPath("something else", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
    rule = ruleBuilder.build(resolver);
    ruleFinder = new SourcePathRuleFinder(resolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey changedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
    assertThat(changedInputBasedRuleKey, Matchers.not(Matchers.equalTo(originalInputRuleKey)));
}
Also used : DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) InputBasedRuleKeyFactory(com.facebook.buck.rules.keys.InputBasedRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) 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) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 28 with DefaultRuleKeyFactory

use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.

the class GenruleTest method inputBasedRuleKeyClasspathMacro.

@Test
public void inputBasedRuleKeyClasspathMacro() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    GenruleBuilder ruleBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:rule")).setCmd("run $(classpath //:dep)").setOut("output");
    // Create an initial input-based rule key
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    JavaLibrary dep = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep")).addSrc(Paths.get("source.java")).build(resolver, filesystem);
    filesystem.writeContentsToPath("something", Paths.get("source.java"));
    filesystem.writeContentsToPath("something", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
    BuildRule rule = ruleBuilder.build(resolver);
    DefaultRuleKeyFactory defaultRuleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    InputBasedRuleKeyFactory inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey originalRuleKey = defaultRuleKeyFactory.build(rule);
    RuleKey originalInputRuleKey = inputBasedRuleKeyFactory.build(rule);
    // Change the dep's resource root, which will change its normal rule key, but since we're
    // keeping its output JAR the same, the input-based rule key for the consuming rule will stay
    // the same.  This is because the input-based rule key for the consuming rule only cares about
    // the contents of the output this rule produces.
    resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep")).addSrc(Paths.get("source.java")).setResourcesRoot(Paths.get("resource_root")).build(resolver, filesystem);
    rule = ruleBuilder.build(resolver);
    ruleFinder = new SourcePathRuleFinder(resolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    defaultRuleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey unchangedRuleKey = defaultRuleKeyFactory.build(rule);
    RuleKey unchangedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
    assertThat(unchangedRuleKey, Matchers.not(Matchers.equalTo(originalRuleKey)));
    assertThat(unchangedInputBasedRuleKey, Matchers.equalTo(originalInputRuleKey));
    // Make a change to the dep's output, which *should* affect the input-based rule key.
    resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    dep = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep")).addSrc(Paths.get("source.java")).build(resolver, filesystem);
    filesystem.writeContentsToPath("something else", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
    rule = ruleBuilder.build(resolver);
    ruleFinder = new SourcePathRuleFinder(resolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey changedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
    assertThat(changedInputBasedRuleKey, Matchers.not(Matchers.equalTo(originalInputRuleKey)));
}
Also used : DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) InputBasedRuleKeyFactory(com.facebook.buck.rules.keys.InputBasedRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 29 with DefaultRuleKeyFactory

use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.

the class ShTestDescriptionTest method getRuleKey.

private RuleKey getRuleKey(BuildRule rule) {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(rule.getProjectFilesystem())));
    DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder);
    return factory.build(rule);
}
Also used : DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver)

Example 30 with DefaultRuleKeyFactory

use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.

the class TargetsCommand method computeShowRules.

/**
   * Assumes at least one target is specified.  Computes each of the
   * specified targets, followed by the rule key, output path, and/or
   * target hash, depending on what flags are passed in.
   * @return  An immutable map consisting of result of show options
   * for to each target rule
   */
private ImmutableMap<BuildTarget, ShowOptions> computeShowRules(CommandRunnerParams params, ListeningExecutorService executor, TargetGraphAndTargetNodes targetGraphAndTargetNodes) throws IOException, InterruptedException, BuildFileParseException, BuildTargetException, CycleException {
    Map<BuildTarget, ShowOptions.Builder> showOptionBuilderMap = new HashMap<>();
    if (isShowTargetHash()) {
        computeShowTargetHash(params, executor, targetGraphAndTargetNodes, showOptionBuilderMap);
    }
    // We only need the action graph if we're showing the output or the keys, and the
    // RuleKeyFactory if we're showing the keys.
    Optional<ActionGraph> actionGraph = Optional.empty();
    Optional<BuildRuleResolver> buildRuleResolver = Optional.empty();
    Optional<DefaultRuleKeyFactory> ruleKeyFactory = Optional.empty();
    if (isShowRuleKey() || isShowOutput() || isShowFullOutput()) {
        ActionGraphAndResolver result = Preconditions.checkNotNull(ActionGraphCache.getFreshActionGraph(params.getBuckEventBus(), targetGraphAndTargetNodes.getTargetGraph()));
        actionGraph = Optional.of(result.getActionGraph());
        buildRuleResolver = Optional.of(result.getResolver());
        if (isShowRuleKey()) {
            SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(result.getResolver());
            ruleKeyFactory = Optional.of(new DefaultRuleKeyFactory(new RuleKeyFieldLoader(params.getBuckConfig().getKeySeed()), params.getFileHashCache(), new SourcePathResolver(ruleFinder), ruleFinder));
        }
    }
    for (TargetNode<?, ?> targetNode : targetGraphAndTargetNodes.getTargetNodes()) {
        ShowOptions.Builder showOptionsBuilder = getShowOptionBuilder(showOptionBuilderMap, targetNode.getBuildTarget());
        Preconditions.checkNotNull(showOptionsBuilder);
        if (actionGraph.isPresent()) {
            BuildRule rule = buildRuleResolver.get().requireRule(targetNode.getBuildTarget());
            if (isShowRuleKey()) {
                showOptionsBuilder.setRuleKey(ruleKeyFactory.get().build(rule).toString());
            }
            if (isShowOutput() || isShowFullOutput()) {
                Optional<Path> outputPath = getUserFacingOutputPath(new SourcePathResolver(new SourcePathRuleFinder(buildRuleResolver.get())), rule, isShowFullOutput(), params.getBuckConfig().getBuckOutCompatLink());
                if (outputPath.isPresent()) {
                    showOptionsBuilder.setOutputPath(outputPath.get().toString());
                }
            }
        }
    }
    ImmutableMap.Builder<BuildTarget, ShowOptions> builder = new ImmutableMap.Builder<>();
    for (Entry<BuildTarget, ShowOptions.Builder> entry : showOptionBuilderMap.entrySet()) {
        builder.put(entry.getKey(), entry.getValue().build());
    }
    return builder.build();
}
Also used : RuleKeyFieldLoader(com.facebook.buck.rules.keys.RuleKeyFieldLoader) Path(java.nio.file.Path) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) ActionGraph(com.facebook.buck.rules.ActionGraph) HashMap(java.util.HashMap) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ImmutableMap(com.google.common.collect.ImmutableMap) BuildTarget(com.facebook.buck.model.BuildTarget) ActionGraphAndResolver(com.facebook.buck.rules.ActionGraphAndResolver) BuildRule(com.facebook.buck.rules.BuildRule)

Aggregations

DefaultRuleKeyFactory (com.facebook.buck.rules.keys.DefaultRuleKeyFactory)58 Test (org.junit.Test)43 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)42 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)42 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)35 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)34 RuleKey (com.facebook.buck.rules.RuleKey)34 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)27 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)27 Path (java.nio.file.Path)26 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)24 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)23 BuildTarget (com.facebook.buck.model.BuildTarget)22 DefaultFileHashCache (com.facebook.buck.util.cache.DefaultFileHashCache)21 PathSourcePath (com.facebook.buck.rules.PathSourcePath)20 FileHashCache (com.facebook.buck.util.cache.FileHashCache)18 BuildRule (com.facebook.buck.rules.BuildRule)17 SourcePath (com.facebook.buck.rules.SourcePath)16 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)11 ImmutableMap (com.google.common.collect.ImmutableMap)10