Search in sources :

Example 31 with TargetGraph

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

the class AuditClasspathCommandTest method testJsonClassPathWithVersions.

@Test
public void testJsonClassPathWithVersions() throws Exception {
    // Build the test target graph.
    TargetNode<?, ?> javaLibrary = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:test-java-library")).addSrc(Paths.get("src/com/facebook/TestJavaLibrary.java")).build();
    TargetNode<?, ?> androidLibrary = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:test-android-library")).addSrc(Paths.get("src/com/facebook/TestAndroidLibrary.java")).addDep(javaLibrary.getBuildTarget()).build();
    TargetNode<?, ?> version = new VersionedAliasBuilder(BuildTargetFactory.newInstance("//:version")).setVersions("1.0", "//:test-android-library").build();
    TargetNode<?, ?> binary = new JavaBinaryRuleBuilder(BuildTargetFactory.newInstance("//:rule")).setDeps(ImmutableSortedSet.of(version.getBuildTarget())).build();
    TargetGraph targetGraph = TargetGraphFactory.newInstance(javaLibrary, androidLibrary, version, binary);
    // Run the command.
    auditClasspathCommand.printJsonClasspath(params.withBuckConfig(FakeBuckConfig.builder().setSections(ImmutableMap.of("build", ImmutableMap.of("versions", "true"))).build()), targetGraph, ImmutableSet.of(androidLibrary.getBuildTarget(), javaLibrary.getBuildTarget()));
    // Verify output.
    Path root = javaLibrary.getBuildTarget().getCellPath();
    ObjectMapper mapper = ObjectMappers.newDefaultInstance();
    String expected = String.format(EXPECTED_JSON, mapper.valueToTree(root.resolve(BuildTargets.getGenPath(params.getCell().getFilesystem(), javaLibrary.getBuildTarget(), "lib__%s__output").resolve(javaLibrary.getBuildTarget().getShortName() + ".jar"))), mapper.valueToTree(root.resolve(BuildTargets.getGenPath(params.getCell().getFilesystem(), androidLibrary.getBuildTarget(), "lib__%s__output").resolve(androidLibrary.getBuildTarget().getShortName() + ".jar"))), mapper.valueToTree(root.resolve(BuildTargets.getGenPath(params.getCell().getFilesystem(), javaLibrary.getBuildTarget(), "lib__%s__output").resolve(javaLibrary.getBuildTarget().getShortName() + ".jar"))));
    assertEquals(expected, console.getTextWrittenToStdOut());
    assertEquals("", console.getTextWrittenToStdErr());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) JavaBinaryRuleBuilder(com.facebook.buck.jvm.java.JavaBinaryRuleBuilder) TargetGraph(com.facebook.buck.rules.TargetGraph) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VersionedAliasBuilder(com.facebook.buck.versions.VersionedAliasBuilder) Test(org.junit.Test)

Example 32 with TargetGraph

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

the class TargetsCommandTest method testDetectTestChanges.

@Test
public void testDetectTestChanges() throws CmdLineException, IOException {
    BuildTarget libraryTarget = BuildTargetFactory.newInstance("//foo:lib");
    BuildTarget libraryTestTarget1 = BuildTargetFactory.newInstance("//foo:xctest1");
    BuildTarget libraryTestTarget2 = BuildTargetFactory.newInstance("//foo:xctest2");
    BuildTarget testLibraryTarget = BuildTargetFactory.newInstance("//testlib:testlib");
    BuildTarget testLibraryTestTarget = BuildTargetFactory.newInstance("//testlib:testlib-xctest");
    TargetNode<?, ?> libraryNode = AppleLibraryBuilder.createBuilder(libraryTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/foo.m")))).setTests(ImmutableSortedSet.of(libraryTestTarget1, libraryTestTarget2)).build();
    TargetNode<?, ?> libraryTestNode1 = AppleTestBuilder.createBuilder(libraryTestTarget1).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/testfoo1.m")))).setDeps(ImmutableSortedSet.of(libraryTarget)).build();
    TargetNode<?, ?> libraryTestNode2 = AppleTestBuilder.createBuilder(libraryTestTarget2).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/testfoo2.m")))).setDeps(ImmutableSortedSet.of(testLibraryTarget)).build();
    TargetNode<?, ?> testLibraryNode = AppleLibraryBuilder.createBuilder(testLibraryTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("testlib/testlib.m")))).setTests(ImmutableSortedSet.of(testLibraryTestTarget)).build();
    TargetNode<?, ?> testLibraryTestNode = AppleTestBuilder.createBuilder(testLibraryTestTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("testlib/testlib-test.m")))).setDeps(ImmutableSortedSet.of(testLibraryTarget)).build();
    ImmutableSet<TargetNode<?, ?>> nodes = ImmutableSet.of(libraryNode, libraryTestNode1, libraryTestNode2, testLibraryNode, testLibraryTestNode);
    TargetGraph targetGraph = TargetGraphFactory.newInstance(nodes);
    // No target depends on the referenced file.
    SortedMap<String, TargetNode<?, ?>> matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/bar.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
    assertTrue(matchingBuildRules.isEmpty());
    // Test1, test2 and the library depend on the referenced file.
    matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/testfoo1.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
    assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1"), matchingBuildRules.keySet());
    // Test1, test2 and the library depend on the referenced file.
    matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/testfoo2.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
    assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1", "//foo:xctest2"), matchingBuildRules.keySet());
    // Library, test1, test2, test library and its test depend on the referenced file.
    matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("testlib/testlib.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
    assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1", "//foo:xctest2", "//testlib:testlib", "//testlib:testlib-xctest"), matchingBuildRules.keySet());
    // Library, test1, test2, test library and its test depend on the referenced file.
    matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("testlib/testlib-test.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
    assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1", "//foo:xctest2", "//testlib:testlib", "//testlib:testlib-xctest"), matchingBuildRules.keySet());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph) Test(org.junit.Test)

Example 33 with TargetGraph

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

the class TargetsCommand method computeTargetsAndGraphToShowTargetHash.

private TargetGraphAndTargetNodes computeTargetsAndGraphToShowTargetHash(CommandRunnerParams params, ListeningExecutorService executor, TargetGraphAndTargetNodes targetGraphAndTargetNodes) throws InterruptedException, BuildFileParseException, BuildTargetException, IOException {
    if (isDetectTestChanges()) {
        ImmutableSet<BuildTarget> explicitTestTargets = TargetGraphAndTargets.getExplicitTestTargets(targetGraphAndTargetNodes.getTargetGraph().getSubgraph(targetGraphAndTargetNodes.getTargetNodes()).getNodes());
        LOG.debug("Got explicit test targets: %s", explicitTestTargets);
        Iterable<BuildTarget> matchingBuildTargetsWithTests = mergeBuildTargets(targetGraphAndTargetNodes.getTargetNodes(), explicitTestTargets);
        // Parse the BUCK files for the tests of the targets passed in from the command line.
        TargetGraph targetGraphWithTests = params.getParser().buildTargetGraph(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), executor, matchingBuildTargetsWithTests);
        return TargetGraphAndTargetNodes.builder().setTargetGraph(targetGraphWithTests).setTargetNodes(targetGraphWithTests.getAll(matchingBuildTargetsWithTests)).build();
    } else {
        return targetGraphAndTargetNodes;
    }
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph)

Example 34 with TargetGraph

use of com.facebook.buck.rules.TargetGraph 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 35 with TargetGraph

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

the class AppleDescriptions method createBuildRulesForCoreDataDependencies.

public static Optional<CoreDataModel> createBuildRulesForCoreDataDependencies(TargetGraph targetGraph, BuildRuleParams params, String moduleName, AppleCxxPlatform appleCxxPlatform) {
    TargetNode<?, ?> targetNode = targetGraph.get(params.getBuildTarget());
    ImmutableSet<AppleWrapperResourceArg> coreDataModelArgs = AppleBuildRules.collectTransitiveBuildRules(targetGraph, Optional.empty(), AppleBuildRules.CORE_DATA_MODEL_DESCRIPTION_CLASSES, ImmutableList.of(targetNode));
    BuildRuleParams coreDataModelParams = params.withAppendedFlavor(CoreDataModel.FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    if (coreDataModelArgs.isEmpty()) {
        return Optional.empty();
    } else {
        return Optional.of(new CoreDataModel(coreDataModelParams, appleCxxPlatform, moduleName, coreDataModelArgs.stream().map(input -> new PathSourcePath(params.getProjectFilesystem(), input.path)).collect(MoreCollectors.toImmutableSet())));
    }
}
Also used : OptionalCompat(com.facebook.buck.util.OptionalCompat) ProvidesLinkedBinaryDeps(com.facebook.buck.cxx.ProvidesLinkedBinaryDeps) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) AbstractGenruleDescription(com.facebook.buck.shell.AbstractGenruleDescription) RichStream(com.facebook.buck.util.RichStream) InternalFlavor(com.facebook.buck.model.InternalFlavor) FlavorDomain(com.facebook.buck.model.FlavorDomain) CxxCompilationDatabase(com.facebook.buck.cxx.CxxCompilationDatabase) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourceList(com.facebook.buck.rules.coercer.SourceList) StripStyle(com.facebook.buck.cxx.StripStyle) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) BuildRules(com.facebook.buck.rules.BuildRules) FrameworkDependencies(com.facebook.buck.cxx.FrameworkDependencies) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) Set(java.util.Set) BuildTarget(com.facebook.buck.model.BuildTarget) Sets(com.google.common.collect.Sets) LinkerMapMode(com.facebook.buck.cxx.LinkerMapMode) SourceWithFlags(com.facebook.buck.rules.SourceWithFlags) Predicate(com.google.common.base.Predicate) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) CxxBinaryDescription(com.facebook.buck.cxx.CxxBinaryDescription) Joiner(com.google.common.base.Joiner) CxxStrip(com.facebook.buck.cxx.CxxStrip) CxxLibraryDescription(com.facebook.buck.cxx.CxxLibraryDescription) SourcePath(com.facebook.buck.rules.SourcePath) Either(com.facebook.buck.model.Either) BuildRule(com.facebook.buck.rules.BuildRule) HashSet(java.util.HashSet) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Predicates(com.google.common.base.Predicates) Suppliers(com.google.common.base.Suppliers) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) CxxConstructorArg(com.facebook.buck.cxx.CxxConstructorArg) TargetNode(com.facebook.buck.rules.TargetNode) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) HumanReadableException(com.facebook.buck.util.HumanReadableException) MorePaths(com.facebook.buck.io.MorePaths) Ordering(com.google.common.collect.Ordering) BuildRuleWithBinary(com.facebook.buck.cxx.BuildRuleWithBinary) Paths(java.nio.file.Paths) SWIFT_EXTENSION(com.facebook.buck.swift.SwiftDescriptions.SWIFT_EXTENSION) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) PathSourcePath(com.facebook.buck.rules.PathSourcePath)

Aggregations

TargetGraph (com.facebook.buck.rules.TargetGraph)267 Test (org.junit.Test)230 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)195 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)179 BuildTarget (com.facebook.buck.model.BuildTarget)157 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)126 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)119 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)96 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)88 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)75 BuildRule (com.facebook.buck.rules.BuildRule)60 AllExistingProjectFilesystem (com.facebook.buck.testutil.AllExistingProjectFilesystem)58 Path (java.nio.file.Path)48 PathSourcePath (com.facebook.buck.rules.PathSourcePath)45 TargetNode (com.facebook.buck.rules.TargetNode)44 SourcePath (com.facebook.buck.rules.SourcePath)37 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)36 ImmutableSet (com.google.common.collect.ImmutableSet)29 Optional (java.util.Optional)25 ImmutableList (com.google.common.collect.ImmutableList)24