Search in sources :

Example 1 with CellPathResolver

use of com.facebook.buck.rules.CellPathResolver in project buck by facebook.

the class CxxGenruleDescription method translateConstructorArg.

@Override
public Optional<Arg> translateConstructorArg(BuildTarget target, CellPathResolver cellNames, TargetNodeTranslator translator, Arg constructorArg) {
    Arg newConstructorArg = createUnpopulatedConstructorArg();
    translator.translateConstructorArg(cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), constructorArg, newConstructorArg);
    newConstructorArg.cmd = newConstructorArg.cmd.map(c -> translateCmd(target, cellNames, translator, "cmd", c));
    newConstructorArg.bash = newConstructorArg.bash.map(c -> translateCmd(target, cellNames, translator, "bash", c));
    newConstructorArg.cmdExe = newConstructorArg.cmdExe.map(c -> translateCmd(target, cellNames, translator, "cmd_exe", c));
    return Optional.of(newConstructorArg);
}
Also used : Genrule(com.facebook.buck.shell.Genrule) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) AbstractGenruleDescription(com.facebook.buck.shell.AbstractGenruleDescription) TargetNodeTranslator(com.facebook.buck.versions.TargetNodeTranslator) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) SymlinkTree(com.facebook.buck.rules.SymlinkTree) FlavorDomain(com.facebook.buck.model.FlavorDomain) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) MacroFinder(com.facebook.buck.model.MacroFinder) StringArg(com.facebook.buck.rules.args.StringArg) Map(java.util.Map) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) Collection(java.util.Collection) MacroException(com.facebook.buck.model.MacroException) BuildTarget(com.facebook.buck.model.BuildTarget) ExecutableMacroExpander(com.facebook.buck.rules.macros.ExecutableMacroExpander) BuildTargetParseException(com.facebook.buck.parser.BuildTargetParseException) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Pattern(java.util.regex.Pattern) Joiner(com.google.common.base.Joiner) Iterables(com.google.common.collect.Iterables) CellPathResolver(com.facebook.buck.rules.CellPathResolver) SourcePath(com.facebook.buck.rules.SourcePath) AbstractMacroExpander(com.facebook.buck.rules.macros.AbstractMacroExpander) VersionPropagator(com.facebook.buck.versions.VersionPropagator) Flavored(com.facebook.buck.model.Flavored) BuildRule(com.facebook.buck.rules.BuildRule) TargetTranslatorOverridingDescription(com.facebook.buck.versions.TargetTranslatorOverridingDescription) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Escaper(com.facebook.buck.util.Escaper) StringExpander(com.facebook.buck.rules.macros.StringExpander) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) LocationMacro(com.facebook.buck.rules.macros.LocationMacro) MacroReplacer(com.facebook.buck.model.MacroReplacer) Iterator(java.util.Iterator) CaseFormat(com.google.common.base.CaseFormat) HumanReadableException(com.facebook.buck.util.HumanReadableException) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) MacroExpander(com.facebook.buck.rules.macros.MacroExpander) BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) Ordering(com.google.common.collect.Ordering) LocationMacroExpander(com.facebook.buck.rules.macros.LocationMacroExpander) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) MacroHandler(com.facebook.buck.rules.macros.MacroHandler) BuildTargets(com.facebook.buck.model.BuildTargets) StringArg(com.facebook.buck.rules.args.StringArg)

Example 2 with CellPathResolver

use of com.facebook.buck.rules.CellPathResolver in project buck by facebook.

the class QueryMacroExpander method extractTargets.

private Stream<BuildTarget> extractTargets(BuildTarget target, CellPathResolver cellNames, Optional<BuildRuleResolver> resolver, T input) {
    String queryExpression = CharMatcher.anyOf("\"'").trimFrom(input.getQuery().getQuery());
    final GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(resolver, targetGraph, cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), ImmutableSet.of());
    try {
        QueryExpression parsedExp = QueryExpression.parse(queryExpression, env);
        HashSet<String> targetLiterals = new HashSet<>();
        parsedExp.collectTargetPatterns(targetLiterals);
        return targetLiterals.stream().flatMap(pattern -> {
            try {
                return env.getTargetsMatchingPattern(pattern, executorService).stream();
            } catch (Exception e) {
                throw new HumanReadableException(e, "Error parsing target expression %s for target %s", pattern, target);
            }
        }).map(queryTarget -> {
            Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
            return ((QueryBuildTarget) queryTarget).getBuildTarget();
        });
    } catch (QueryException e) {
        throw new HumanReadableException("Error executing query in macro for target %s", target, e);
    }
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ImmutableSet(com.google.common.collect.ImmutableSet) CellPathResolver(com.facebook.buck.rules.CellPathResolver) QueryException(com.facebook.buck.query.QueryException) TargetGraph(com.facebook.buck.rules.TargetGraph) CharMatcher(com.google.common.base.CharMatcher) Set(java.util.Set) Query(com.facebook.buck.rules.query.Query) MacroException(com.facebook.buck.model.MacroException) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Stream(java.util.stream.Stream) QueryExpression(com.facebook.buck.query.QueryExpression) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) QueryTarget(com.facebook.buck.query.QueryTarget) GraphEnhancementQueryEnvironment(com.facebook.buck.rules.query.GraphEnhancementQueryEnvironment) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) GraphEnhancementQueryEnvironment(com.facebook.buck.rules.query.GraphEnhancementQueryEnvironment) QueryException(com.facebook.buck.query.QueryException) HumanReadableException(com.facebook.buck.util.HumanReadableException) QueryExpression(com.facebook.buck.query.QueryExpression) QueryException(com.facebook.buck.query.QueryException) MacroException(com.facebook.buck.model.MacroException) HumanReadableException(com.facebook.buck.util.HumanReadableException) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) HashSet(java.util.HashSet)

Example 3 with CellPathResolver

use of com.facebook.buck.rules.CellPathResolver in project buck by facebook.

the class TargetNodeTranslator method translateNode.

/**
   * @return a copy of the given {@link TargetNode} with all found {@link BuildTarget}s translated,
   *         or {@link Optional#empty()} if the node requires no translation.
   */
public <A> Optional<TargetNode<A, ?>> translateNode(TargetNode<A, ?> node) {
    CellPathResolver cellPathResolver = node.getCellNames();
    BuildTargetPatternParser<BuildTargetPattern> pattern = BuildTargetPatternParser.forBaseName(node.getBuildTarget().getBaseName());
    Optional<BuildTarget> target = translateBuildTarget(node.getBuildTarget());
    Optional<A> constructorArg = translateConstructorArg(cellPathResolver, pattern, node);
    Optional<ImmutableSet<BuildTarget>> declaredDeps = translateSet(cellPathResolver, pattern, node.getDeclaredDeps());
    Optional<ImmutableSet<BuildTarget>> extraDeps = translateSet(cellPathResolver, pattern, node.getExtraDeps());
    Optional<ImmutableMap<BuildTarget, Version>> newSelectedVersions = getSelectedVersions(node.getBuildTarget());
    Optional<ImmutableMap<BuildTarget, Version>> oldSelectedVersions = node.getSelectedVersions();
    Optional<Optional<ImmutableMap<BuildTarget, Version>>> selectedVersions = oldSelectedVersions.equals(newSelectedVersions) ? Optional.empty() : Optional.of(newSelectedVersions);
    // If nothing has changed, don't generate a new node.
    if (!target.isPresent() && !constructorArg.isPresent() && !declaredDeps.isPresent() && !extraDeps.isPresent() && !selectedVersions.isPresent()) {
        return Optional.empty();
    }
    return Optional.of(node.withTargetConstructorArgDepsAndSelectedVerisons(target.orElse(node.getBuildTarget()), constructorArg.orElse(node.getConstructorArg()), declaredDeps.orElse(node.getDeclaredDeps()), extraDeps.orElse(node.getExtraDeps()), selectedVersions.orElse(oldSelectedVersions)));
}
Also used : BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) Optional(java.util.Optional) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) CellPathResolver(com.facebook.buck.rules.CellPathResolver)

Example 4 with CellPathResolver

use of com.facebook.buck.rules.CellPathResolver in project buck by facebook.

the class PrebuiltCxxLibraryDescriptionTest method platformMacro.

@Test
public void platformMacro() {
    Optional<String> libDir = Optional.of("libs/$(platform)");
    Optional<String> libName = Optional.of("test-$(platform)");
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    ProjectFilesystem filesystem = new AllExistingProjectFilesystem();
    CellPathResolver cellRoots = TestCellBuilder.createCellRoots(filesystem);
    CxxPlatform platform1 = CxxPlatformUtils.DEFAULT_PLATFORM.withFlavor(InternalFlavor.of("PLATFORM1"));
    CxxPlatform platform2 = CxxPlatformUtils.DEFAULT_PLATFORM.withFlavor(InternalFlavor.of("PLATFORM2"));
    assertEquals(filesystem.resolve(TARGET.getBasePath().resolve(String.format("libs/PLATFORM1/libtest-PLATFORM1.%s", platform1.getSharedLibraryExtension()))), pathResolver.getAbsolutePath(PrebuiltCxxLibraryDescription.getSharedLibraryPath(TARGET, cellRoots, filesystem, resolver, platform1, Optional.empty(), libDir, libName)));
    assertEquals(filesystem.resolve(TARGET.getBasePath().resolve("libs/PLATFORM1/libtest-PLATFORM1.a")), pathResolver.getAbsolutePath(PrebuiltCxxLibraryDescription.getStaticLibraryPath(TARGET, cellRoots, filesystem, resolver, platform1, Optional.empty(), libDir, libName)));
    assertEquals(filesystem.resolve(TARGET.getBasePath().resolve(String.format("libs/PLATFORM2/libtest-PLATFORM2.%s", platform2.getSharedLibraryExtension()))), pathResolver.getAbsolutePath(PrebuiltCxxLibraryDescription.getSharedLibraryPath(TARGET, cellRoots, filesystem, resolver, platform2, Optional.empty(), libDir, libName)));
    assertEquals(filesystem.resolve(TARGET.getBasePath().resolve("libs/PLATFORM2/libtest-PLATFORM2.a")), pathResolver.getAbsolutePath(PrebuiltCxxLibraryDescription.getStaticLibraryPath(TARGET, cellRoots, filesystem, resolver, platform2, Optional.empty(), libDir, libName)));
}
Also used : CellPathResolver(com.facebook.buck.rules.CellPathResolver) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) Test(org.junit.Test)

Example 5 with CellPathResolver

use of com.facebook.buck.rules.CellPathResolver in project buck by facebook.

the class BuildTargetParserTest method testParseWithRepoName.

@Test
public void testParseWithRepoName() {
    final Path localRepoRoot = Paths.get("/opt/local/repo");
    CellPathResolver cellRoots = new FakeCellPathResolver(ImmutableMap.of("localreponame", localRepoRoot));
    String targetStr = "localreponame//foo/bar:baz";
    BuildTarget buildTarget = parser.parse(targetStr, fullyQualifiedParser, cellRoots);
    assertEquals("localreponame//foo/bar:baz", buildTarget.getFullyQualifiedName());
    assertTrue(buildTarget.getCell().isPresent());
    assertEquals(localRepoRoot, buildTarget.getCellPath());
}
Also used : Path(java.nio.file.Path) BuildTarget(com.facebook.buck.model.BuildTarget) FakeCellPathResolver(com.facebook.buck.rules.FakeCellPathResolver) CellPathResolver(com.facebook.buck.rules.CellPathResolver) FakeCellPathResolver(com.facebook.buck.rules.FakeCellPathResolver) Test(org.junit.Test)

Aggregations

CellPathResolver (com.facebook.buck.rules.CellPathResolver)12 BuildTarget (com.facebook.buck.model.BuildTarget)8 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)6 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 Path (java.nio.file.Path)5 Optional (java.util.Optional)5 BuildTargetPattern (com.facebook.buck.model.BuildTargetPattern)4 TargetGraph (com.facebook.buck.rules.TargetGraph)4 BuildTargetPatternParser (com.facebook.buck.parser.BuildTargetPatternParser)3 BuildRule (com.facebook.buck.rules.BuildRule)3 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)3 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)3 Test (org.junit.Test)3 BuildTargets (com.facebook.buck.model.BuildTargets)2 Flavor (com.facebook.buck.model.Flavor)2 FlavorDomain (com.facebook.buck.model.FlavorDomain)2 Flavored (com.facebook.buck.model.Flavored)2 MacroException (com.facebook.buck.model.MacroException)2 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)2