Search in sources :

Example 36 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class KotlinLibraryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    BuildTarget target = params.getBuildTarget();
    // creating the action graph from the target graph.
    if (CalculateAbi.isAbiTarget(target)) {
        BuildTarget libraryTarget = CalculateAbi.getLibraryTarget(params.getBuildTarget());
        BuildRule libraryRule = resolver.requireRule(libraryTarget);
        return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params, Preconditions.checkNotNull(libraryRule.getSourcePathToOutput()));
    }
    ImmutableSortedSet<Flavor> flavors = target.getFlavors();
    BuildRuleParams paramsWithMavenFlavor = null;
    if (flavors.contains(JavaLibrary.MAVEN_JAR)) {
        paramsWithMavenFlavor = params;
        // Maven rules will depend upon their vanilla versions, so the latter have to be constructed
        // without the maven flavor to prevent output-path conflict
        params = params.withoutFlavor(JavaLibrary.MAVEN_JAR);
    }
    if (flavors.contains(JavaLibrary.SRC_JAR)) {
        args.mavenCoords = args.mavenCoords.map(input -> AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_SOURCES));
        if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
            return new JavaSourceJar(params, args.srcs, args.mavenCoords);
        } else {
            return MavenUberJar.SourceJar.create(Preconditions.checkNotNull(paramsWithMavenFlavor), args.srcs, args.mavenCoords, args.mavenPomTemplate);
        }
    }
    JavacOptions javacOptions = JavacOptionsFactory.create(defaultOptions, params, resolver, ruleFinder, args);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps);
    BuildRuleParams javaLibraryParams = params.copyAppendingExtraDeps(Iterables.concat(BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), exportedDeps, resolver.getAllRules(args.providedDeps))), ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))));
    DefaultKotlinLibrary defaultKotlinLibrary = new DefaultKotlinLibrary(javaLibraryParams, pathResolver, ruleFinder, args.srcs, validateResources(pathResolver, params.getProjectFilesystem(), args.resources), Optional.empty(), Optional.empty(), ImmutableList.of(), exportedDeps, resolver.getAllRules(args.providedDeps), JavaLibraryRules.getAbiInputs(resolver, javaLibraryParams.getDeps()), false, ImmutableSet.of(), new KotlincToJarStepFactory(kotlinBuckConfig.getKotlinCompiler().get(), args.extraKotlincArguments), args.resourcesRoot, args.manifestFile, args.mavenCoords, args.tests, args.removeClasses);
    if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
        return defaultKotlinLibrary;
    } else {
        return MavenUberJar.create(defaultKotlinLibrary, Preconditions.checkNotNull(paramsWithMavenFlavor), args.mavenCoords, args.mavenPomTemplate);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) CalculateAbi(com.facebook.buck.jvm.java.CalculateAbi) JavaLibraryRules(com.facebook.buck.jvm.java.JavaLibraryRules) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) JavacOptionsFactory(com.facebook.buck.jvm.java.JavacOptionsFactory) Flavored(com.facebook.buck.model.Flavored) BuildRule(com.facebook.buck.rules.BuildRule) JavaSourceJar(com.facebook.buck.jvm.java.JavaSourceJar) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) ResourceValidator.validateResources(com.facebook.buck.jvm.common.ResourceValidator.validateResources) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildRules(com.facebook.buck.rules.BuildRules) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) AetherUtil(com.facebook.buck.maven.AetherUtil) TargetGraph(com.facebook.buck.rules.TargetGraph) MavenUberJar(com.facebook.buck.jvm.java.MavenUberJar) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Description(com.facebook.buck.rules.Description) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) Flavor(com.facebook.buck.model.Flavor) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) JavaSourceJar(com.facebook.buck.jvm.java.JavaSourceJar)

Example 37 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class JavaTestIntegrationTest method testClasspath.

@Test
public void testClasspath() throws IOException {
    final ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "test_rule_classpath", temp);
    workspace.setUp();
    ProjectWorkspace.ProcessResult result = workspace.runBuckCommand("audit", "classpath", "//:top");
    result.assertSuccess();
    ImmutableSortedSet<Path> actualPaths = FluentIterable.from(Arrays.asList(result.getStdout().split("\\s+"))).transform(input -> temp.getRoot().relativize(Paths.get(input))).toSortedSet(Ordering.natural());
    ImmutableSortedSet<Path> expectedPaths = ImmutableSortedSet.of(Paths.get("buck-out/gen/lib__top__output/top.jar"), Paths.get("buck-out/gen/lib__direct_dep__output/direct_dep.jar"), Paths.get("buck-out/gen/lib__mid_test#testsjar__output/mid_test#testsjar.jar"), Paths.get("buck-out/gen/lib__transitive_lib__output/transitive_lib.jar"));
    assertEquals(expectedPaths, actualPaths);
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Arrays(java.util.Arrays) Platform(com.facebook.buck.util.environment.Platform) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) TestDataHelper(com.facebook.buck.testutil.integration.TestDataHelper) Test(org.junit.Test) IOException(java.io.IOException) TemporaryPaths(com.facebook.buck.testutil.integration.TemporaryPaths) Assert.assertThat(org.junit.Assert.assertThat) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) Rule(org.junit.Rule) Ordering(com.google.common.collect.Ordering) FluentIterable(com.google.common.collect.FluentIterable) Paths(java.nio.file.Paths) Assume.assumeTrue(org.junit.Assume.assumeTrue) Path(java.nio.file.Path) Assert.assertEquals(org.junit.Assert.assertEquals) Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) Test(org.junit.Test)

Example 38 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class TestCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    LOG.debug("Running with arguments %s", getArguments());
    try (CommandThreadManager pool = new CommandThreadManager("Test", getConcurrencyLimit(params.getBuckConfig()))) {
        // Post the build started event, setting it to the Parser recorded start time if appropriate.
        BuildEvent.Started started = BuildEvent.started(getArguments());
        if (params.getParser().getParseStartTime().isPresent()) {
            params.getBuckEventBus().post(started, params.getParser().getParseStartTime().get());
        } else {
            params.getBuckEventBus().post(started);
        }
        // The first step is to parse all of the build files. This will populate the parser and find
        // all of the test rules.
        TargetGraphAndBuildTargets targetGraphAndBuildTargets;
        ParserConfig parserConfig = params.getBuckConfig().getView(ParserConfig.class);
        try {
            // If the user asked to run all of the tests, parse all of the build files looking for any
            // test rules.
            boolean ignoreBuckAutodepsFiles = false;
            if (isRunAllTests()) {
                targetGraphAndBuildTargets = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), ImmutableList.of(TargetNodePredicateSpec.of(input -> Description.getBuildRuleType(input.getDescription()).isTestRule(), BuildFileSpec.fromRecursivePath(Paths.get(""), params.getCell().getRoot()))), ignoreBuckAutodepsFiles, parserConfig.getDefaultFlavorsMode());
                targetGraphAndBuildTargets = targetGraphAndBuildTargets.withBuildTargets(ImmutableSet.of());
            // Otherwise, the user specified specific test targets to build and run, so build a graph
            // around these.
            } else {
                LOG.debug("Parsing graph for arguments %s", getArguments());
                targetGraphAndBuildTargets = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), parseArgumentsAsTargetNodeSpecs(params.getBuckConfig(), getArguments()), ignoreBuckAutodepsFiles, parserConfig.getDefaultFlavorsMode());
                LOG.debug("Got explicit build targets %s", targetGraphAndBuildTargets.getBuildTargets());
                ImmutableSet.Builder<BuildTarget> testTargetsBuilder = ImmutableSet.builder();
                for (TargetNode<?, ?> node : targetGraphAndBuildTargets.getTargetGraph().getAll(targetGraphAndBuildTargets.getBuildTargets())) {
                    ImmutableSortedSet<BuildTarget> nodeTests = TargetNodes.getTestTargetsForNode(node);
                    if (!nodeTests.isEmpty()) {
                        LOG.debug("Got tests for target %s: %s", node.getBuildTarget(), nodeTests);
                        testTargetsBuilder.addAll(nodeTests);
                    }
                }
                ImmutableSet<BuildTarget> testTargets = testTargetsBuilder.build();
                if (!testTargets.isEmpty()) {
                    LOG.debug("Got related test targets %s, building new target graph...", testTargets);
                    TargetGraph targetGraph = params.getParser().buildTargetGraph(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), Iterables.concat(targetGraphAndBuildTargets.getBuildTargets(), testTargets));
                    LOG.debug("Finished building new target graph with tests.");
                    targetGraphAndBuildTargets = targetGraphAndBuildTargets.withTargetGraph(targetGraph);
                }
            }
            if (params.getBuckConfig().getBuildVersions()) {
                targetGraphAndBuildTargets = toVersionedTargetGraph(params, targetGraphAndBuildTargets);
            }
        } catch (BuildTargetException | BuildFileParseException | VersionException e) {
            params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
            return 1;
        }
        ActionGraphAndResolver actionGraphAndResolver = Preconditions.checkNotNull(params.getActionGraphCache().getActionGraph(params.getBuckEventBus(), params.getBuckConfig().isActionGraphCheckingEnabled(), params.getBuckConfig().isSkipActionGraphCache(), targetGraphAndBuildTargets.getTargetGraph(), params.getBuckConfig().getKeySeed()));
        // Look up all of the test rules in the action graph.
        Iterable<TestRule> testRules = Iterables.filter(actionGraphAndResolver.getActionGraph().getNodes(), TestRule.class);
        // the build.
        if (!isBuildFiltered(params.getBuckConfig())) {
            testRules = filterTestRules(params.getBuckConfig(), targetGraphAndBuildTargets.getBuildTargets(), testRules);
        }
        CachingBuildEngineBuckConfig cachingBuildEngineBuckConfig = params.getBuckConfig().getView(CachingBuildEngineBuckConfig.class);
        try (CommandThreadManager artifactFetchService = getArtifactFetchService(params.getBuckConfig(), pool.getExecutor());
            RuleKeyCacheScope<RuleKey> ruleKeyCacheScope = getDefaultRuleKeyCacheScope(params, new RuleKeyCacheRecycler.SettingsAffectingCache(params.getBuckConfig().getKeySeed(), actionGraphAndResolver.getActionGraph()))) {
            LocalCachingBuildEngineDelegate localCachingBuildEngineDelegate = new LocalCachingBuildEngineDelegate(params.getFileHashCache());
            CachingBuildEngine cachingBuildEngine = new CachingBuildEngine(new LocalCachingBuildEngineDelegate(params.getFileHashCache()), pool.getExecutor(), artifactFetchService == null ? pool.getExecutor() : artifactFetchService.getExecutor(), new DefaultStepRunner(), getBuildEngineMode().orElse(cachingBuildEngineBuckConfig.getBuildEngineMode()), cachingBuildEngineBuckConfig.getBuildDepFiles(), cachingBuildEngineBuckConfig.getBuildMaxDepFileCacheEntries(), cachingBuildEngineBuckConfig.getBuildArtifactCacheSizeLimit(), params.getObjectMapper(), actionGraphAndResolver.getResolver(), cachingBuildEngineBuckConfig.getResourceAwareSchedulingInfo(), new RuleKeyFactoryManager(params.getBuckConfig().getKeySeed(), fs -> localCachingBuildEngineDelegate.getFileHashCache(), actionGraphAndResolver.getResolver(), cachingBuildEngineBuckConfig.getBuildInputRuleKeyFileSizeLimit(), ruleKeyCacheScope.getCache()));
            try (Build build = createBuild(params.getBuckConfig(), actionGraphAndResolver.getActionGraph(), actionGraphAndResolver.getResolver(), params.getCell(), params.getAndroidPlatformTargetSupplier(), cachingBuildEngine, params.getArtifactCacheFactory().newInstance(), params.getConsole(), params.getBuckEventBus(), getTargetDeviceOptional(), params.getPersistentWorkerPools(), params.getPlatform(), params.getEnvironment(), params.getObjectMapper(), params.getClock(), Optional.of(getAdbOptions(params.getBuckConfig())), Optional.of(getTargetDeviceOptions()), params.getExecutors())) {
                // Build all of the test rules.
                int exitCode = build.executeAndPrintFailuresToEventBus(RichStream.from(testRules).map(TestRule::getBuildTarget).collect(MoreCollectors.toImmutableList()), isKeepGoing(), params.getBuckEventBus(), params.getConsole(), getPathToBuildReport(params.getBuckConfig()));
                params.getBuckEventBus().post(BuildEvent.finished(started, exitCode));
                if (exitCode != 0) {
                    return exitCode;
                }
                // the filtering here, after we've done the build but before we run the tests.
                if (isBuildFiltered(params.getBuckConfig())) {
                    testRules = filterTestRules(params.getBuckConfig(), targetGraphAndBuildTargets.getBuildTargets(), testRules);
                }
                // Once all of the rules are built, then run the tests.
                Optional<ImmutableList<String>> externalTestRunner = params.getBuckConfig().getExternalTestRunner();
                if (externalTestRunner.isPresent()) {
                    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(actionGraphAndResolver.getResolver()));
                    return runTestsExternal(params, build, externalTestRunner.get(), testRules, pathResolver);
                }
                return runTestsInternal(params, cachingBuildEngine, build, testRules);
            }
        }
    }
}
Also used : TargetNodePredicateSpec(com.facebook.buck.parser.TargetNodePredicateSpec) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) RichStream(com.facebook.buck.util.RichStream) AdbOptions(com.facebook.buck.step.AdbOptions) TestRunningOptions(com.facebook.buck.test.TestRunningOptions) RuleKey(com.facebook.buck.rules.RuleKey) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultStepRunner(com.facebook.buck.step.DefaultStepRunner) Map(java.util.Map) ConcurrencyLimit(com.facebook.buck.util.concurrent.ConcurrencyLimit) TestRule(com.facebook.buck.rules.TestRule) Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) TargetGraph(com.facebook.buck.rules.TargetGraph) BuildTargetException(com.facebook.buck.model.BuildTargetException) Set(java.util.Set) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) Option(org.kohsuke.args4j.Option) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) Collectors(java.util.stream.Collectors) ActionGraphAndResolver(com.facebook.buck.rules.ActionGraphAndResolver) CachingBuildEngine(com.facebook.buck.rules.CachingBuildEngine) List(java.util.List) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec) Optional(java.util.Optional) Description(com.facebook.buck.rules.Description) RuleKeyFactoryManager(com.facebook.buck.rules.keys.RuleKeyFactoryManager) ExternalTestRunnerRule(com.facebook.buck.rules.ExternalTestRunnerRule) Iterables(com.google.common.collect.Iterables) TargetGraphAndBuildTargets(com.facebook.buck.rules.TargetGraphAndBuildTargets) Build(com.facebook.buck.command.Build) VersionException(com.facebook.buck.versions.VersionException) RuleKeyCacheRecycler(com.facebook.buck.rules.keys.RuleKeyCacheRecycler) MoreExceptions(com.facebook.buck.util.MoreExceptions) HashMap(java.util.HashMap) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Lists(com.google.common.collect.Lists) ParserConfig(com.facebook.buck.parser.ParserConfig) Label(com.facebook.buck.rules.Label) ImmutableList(com.google.common.collect.ImmutableList) LocalCachingBuildEngineDelegate(com.facebook.buck.rules.LocalCachingBuildEngineDelegate) CachingBuildEngineBuckConfig(com.facebook.buck.rules.CachingBuildEngineBuckConfig) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) PrintStream(java.io.PrintStream) Logger(com.facebook.buck.log.Logger) RuleKeyCacheScope(com.facebook.buck.rules.keys.RuleKeyCacheScope) BuildFileSpec(com.facebook.buck.parser.BuildFileSpec) Files(java.nio.file.Files) TargetNode(com.facebook.buck.rules.TargetNode) Channels(java.nio.channels.Channels) TargetDeviceOptions(com.facebook.buck.step.TargetDeviceOptions) IOException(java.io.IOException) TargetDevice(com.facebook.buck.step.TargetDevice) ExecutionException(java.util.concurrent.ExecutionException) BuildEvent(com.facebook.buck.rules.BuildEvent) CoverageReportFormat(com.facebook.buck.test.CoverageReportFormat) Paths(java.nio.file.Paths) BuildEngine(com.facebook.buck.rules.BuildEngine) Preconditions(com.google.common.base.Preconditions) TargetNodes(com.facebook.buck.rules.TargetNodes) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) Comparator(java.util.Comparator) ImmutableList(com.google.common.collect.ImmutableList) TargetGraph(com.facebook.buck.rules.TargetGraph) RuleKeyFactoryManager(com.facebook.buck.rules.keys.RuleKeyFactoryManager) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) Build(com.facebook.buck.command.Build) DefaultStepRunner(com.facebook.buck.step.DefaultStepRunner) ActionGraphAndResolver(com.facebook.buck.rules.ActionGraphAndResolver) BuildTargetException(com.facebook.buck.model.BuildTargetException) LocalCachingBuildEngineDelegate(com.facebook.buck.rules.LocalCachingBuildEngineDelegate) RuleKey(com.facebook.buck.rules.RuleKey) CachingBuildEngine(com.facebook.buck.rules.CachingBuildEngine) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) TestRule(com.facebook.buck.rules.TestRule) BuildEvent(com.facebook.buck.rules.BuildEvent) RuleKeyCacheRecycler(com.facebook.buck.rules.keys.RuleKeyCacheRecycler) TargetGraphAndBuildTargets(com.facebook.buck.rules.TargetGraphAndBuildTargets) ParserConfig(com.facebook.buck.parser.ParserConfig) VersionException(com.facebook.buck.versions.VersionException) CachingBuildEngineBuckConfig(com.facebook.buck.rules.CachingBuildEngineBuckConfig)

Example 39 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class CxxPrecompiledHeaderTemplate method getPreprocessDeps.

private ImmutableSortedSet<BuildRule> getPreprocessDeps(CxxPlatform cxxPlatform) {
    ImmutableSortedSet.Builder<BuildRule> builder = ImmutableSortedSet.naturalOrder();
    for (CxxPreprocessorInput input : getCxxPreprocessorInputs(cxxPlatform)) {
        builder.addAll(input.getDeps(ruleResolver, ruleFinder));
    }
    for (CxxHeaders cxxHeaders : getIncludes(cxxPlatform)) {
        builder.addAll(cxxHeaders.getDeps(ruleFinder));
    }
    for (FrameworkPath frameworkPath : getFrameworks(cxxPlatform)) {
        builder.addAll(frameworkPath.getDeps(ruleFinder));
    }
    builder.addAll(getDeps());
    builder.addAll(getExportedDeps());
    return builder.build();
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath)

Example 40 with ImmutableSortedSet

use of com.google.common.collect.ImmutableSortedSet in project buck by facebook.

the class CxxTestDescription method createBuildRule.

@SuppressWarnings("PMD.PrematureDeclaration")
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams inputParams, final BuildRuleResolver resolver, final A args) throws NoSuchBuildTargetException {
    Optional<StripStyle> flavoredStripStyle = StripStyle.FLAVOR_DOMAIN.getValue(inputParams.getBuildTarget());
    Optional<LinkerMapMode> flavoredLinkerMapMode = LinkerMapMode.FLAVOR_DOMAIN.getValue(inputParams.getBuildTarget());
    inputParams = CxxStrip.removeStripStyleFlavorInParams(inputParams, flavoredStripStyle);
    inputParams = LinkerMapMode.removeLinkerMapModeFlavorInParams(inputParams, flavoredLinkerMapMode);
    final BuildRuleParams params = inputParams;
    Optional<CxxPlatform> platform = cxxPlatforms.getValue(params.getBuildTarget());
    CxxPlatform cxxPlatform = platform.orElse(defaultCxxPlatform);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.COMPILATION_DATABASE)) {
        BuildRuleParams paramsWithoutFlavor = params.withoutFlavor(CxxCompilationDatabase.COMPILATION_DATABASE);
        CxxLinkAndCompileRules cxxLinkAndCompileRules = CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(targetGraph, paramsWithoutFlavor, resolver, cxxBuckConfig, cxxPlatform, args, flavoredStripStyle, flavoredLinkerMapMode);
        return CxxCompilationDatabase.createCompilationDatabase(params, cxxLinkAndCompileRules.compileRules);
    }
    if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.UBER_COMPILATION_DATABASE)) {
        return CxxDescriptionEnhancer.createUberCompilationDatabase(platform.isPresent() ? params : params.withAppendedFlavor(cxxPlatform.getFlavor()), resolver);
    }
    if (params.getBuildTarget().getFlavors().contains(CxxDescriptionEnhancer.SANDBOX_TREE_FLAVOR)) {
        return CxxDescriptionEnhancer.createSandboxTreeBuildRule(resolver, args, cxxPlatform, params);
    }
    // Generate the link rule that builds the test binary.
    final CxxLinkAndCompileRules cxxLinkAndCompileRules = CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(targetGraph, params, resolver, cxxBuckConfig, cxxPlatform, args, flavoredStripStyle, flavoredLinkerMapMode);
    // Construct the actual build params we'll use, notably with an added dependency on the
    // CxxLink rule above which builds the test binary.
    BuildRuleParams testParams = params.copyReplacingDeclaredAndExtraDeps(() -> cxxLinkAndCompileRules.deps, params.getExtraDeps()).copyAppendingExtraDeps(cxxLinkAndCompileRules.executable.getDeps(ruleFinder));
    testParams = CxxStrip.restoreStripStyleFlavorInParams(testParams, flavoredStripStyle);
    testParams = LinkerMapMode.restoreLinkerMapModeFlavorInParams(testParams, flavoredLinkerMapMode);
    // Supplier which expands macros in the passed in test environment.
    ImmutableMap<String, String> testEnv = ImmutableMap.copyOf(Maps.transformValues(args.env, CxxDescriptionEnhancer.MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)));
    // Supplier which expands macros in the passed in test arguments.
    Supplier<ImmutableList<String>> testArgs = () -> args.args.stream().map(CxxDescriptionEnhancer.MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)::apply).collect(MoreCollectors.toImmutableList());
    Supplier<ImmutableSortedSet<BuildRule>> additionalDeps = () -> {
        ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();
        // It's not uncommon for users to add dependencies onto other binaries that they run
        // during the test, so make sure to add them as runtime deps.
        deps.addAll(Sets.difference(params.getDeps(), cxxLinkAndCompileRules.getBinaryRule().getDeps()));
        // Add any build-time from any macros embedded in the `env` or `args` parameter.
        for (String part : Iterables.concat(args.args, args.env.values())) {
            try {
                deps.addAll(CxxDescriptionEnhancer.MACRO_HANDLER.extractBuildTimeDeps(params.getBuildTarget(), params.getCellRoots(), resolver, part));
            } catch (MacroException e) {
                throw new HumanReadableException(e, "%s: %s", params.getBuildTarget(), e.getMessage());
            }
        }
        return deps.build();
    };
    CxxTest test;
    CxxTestType type = args.framework.orElse(getDefaultTestType());
    switch(type) {
        case GTEST:
            {
                test = new CxxGtestTest(testParams, ruleFinder, cxxLinkAndCompileRules.getBinaryRule(), cxxLinkAndCompileRules.executable, testEnv, testArgs, FluentIterable.from(args.resources).transform(p -> new PathSourcePath(params.getProjectFilesystem(), p)).toSortedSet(Ordering.natural()), additionalDeps, args.labels, args.contacts, args.runTestSeparately.orElse(false), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), cxxBuckConfig.getMaximumTestOutputSize());
                break;
            }
        case BOOST:
            {
                test = new CxxBoostTest(testParams, ruleFinder, cxxLinkAndCompileRules.getBinaryRule(), cxxLinkAndCompileRules.executable, testEnv, testArgs, FluentIterable.from(args.resources).transform(p -> new PathSourcePath(params.getProjectFilesystem(), p)).toSortedSet(Ordering.natural()), additionalDeps, args.labels, args.contacts, args.runTestSeparately.orElse(false), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs));
                break;
            }
        default:
            {
                Preconditions.checkState(false, "Unhandled C++ test type: %s", type);
                throw new RuntimeException();
            }
    }
    return test;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Optional(java.util.Optional) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) HumanReadableException(com.facebook.buck.util.HumanReadableException) MacroException(com.facebook.buck.model.MacroException)

Aggregations

ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)53 BuildTarget (com.facebook.buck.model.BuildTarget)26 BuildRule (com.facebook.buck.rules.BuildRule)26 Path (java.nio.file.Path)25 SourcePath (com.facebook.buck.rules.SourcePath)22 ImmutableList (com.google.common.collect.ImmutableList)22 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)21 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)19 HumanReadableException (com.facebook.buck.util.HumanReadableException)17 Optional (java.util.Optional)17 ImmutableSet (com.google.common.collect.ImmutableSet)16 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)15 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)14 Map (java.util.Map)13 Flavor (com.facebook.buck.model.Flavor)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 Preconditions (com.google.common.base.Preconditions)11 Suppliers (com.google.common.base.Suppliers)10 TargetGraph (com.facebook.buck.rules.TargetGraph)9