Search in sources :

Example 6 with AnalysisEnvironment

use of com.google.devtools.build.lib.analysis.AnalysisEnvironment in project bazel by bazelbuild.

the class CppModel method createModuleAction.

private Collection<Artifact> createModuleAction(CcCompilationOutputs.Builder result, CppModuleMap cppModuleMap) {
    AnalysisEnvironment env = ruleContext.getAnalysisEnvironment();
    Label moduleMapLabel = Label.parseAbsoluteUnchecked(context.getCppModuleMap().getName());
    Artifact moduleMapArtifact = cppModuleMap.getArtifact();
    CppCompileActionBuilder builder = initializeCompileAction(moduleMapArtifact, moduleMapLabel);
    builder.setSemantics(semantics);
    // - it creates a header module (.pcm file).
    return createSourceAction(FileSystemUtils.removeExtension(semantics.getEffectiveSourcePath(moduleMapArtifact)).getPathString(), result, env, moduleMapArtifact, builder, ArtifactCategory.CPP_MODULE, cppModuleMap, /*addObject=*/
    false, /*enableCoverage=*/
    false, /*generateDwo=*/
    false, CppFileTypes.mustProduceDotdFile(moduleMapArtifact), ImmutableMap.<String, String>of());
}
Also used : AnalysisEnvironment(com.google.devtools.build.lib.analysis.AnalysisEnvironment) Label(com.google.devtools.build.lib.cmdline.Label) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 7 with AnalysisEnvironment

use of com.google.devtools.build.lib.analysis.AnalysisEnvironment in project bazel by bazelbuild.

the class TestActionBuilder method createTestAction.

/**
   * Creates a test action and artifacts for the given rule. The test action will
   * use the specified executable and runfiles.
   *
   * @return ordered list of test artifacts, one per action. These are used to drive
   *    execution in Skyframe, and by AggregatingTestListener and
   *    TestResultAnalyzer to keep track of completed and pending test runs.
   */
private TestParams createTestAction(int shards) {
    PathFragment targetName = new PathFragment(ruleContext.getLabel().getName());
    BuildConfiguration config = ruleContext.getConfiguration();
    AnalysisEnvironment env = ruleContext.getAnalysisEnvironment();
    Root root = config.getTestLogsDirectory(ruleContext.getRule().getRepository());
    NestedSetBuilder<Artifact> inputsBuilder = NestedSetBuilder.stableOrder();
    inputsBuilder.addTransitive(NestedSetBuilder.create(Order.STABLE_ORDER, runfilesSupport.getRunfilesMiddleman()));
    NestedSet<Artifact> testRuntime = PrerequisiteArtifacts.nestedSet(ruleContext, "$test_runtime", Mode.HOST);
    inputsBuilder.addTransitive(testRuntime);
    TestTargetProperties testProperties = new TestTargetProperties(ruleContext, executionRequirements);
    // If the test rule does not provide InstrumentedFilesProvider, there's not much that we can do.
    final boolean collectCodeCoverage = config.isCodeCoverageEnabled() && instrumentedFiles != null;
    TreeMap<String, String> testEnv = new TreeMap<>();
    TestTargetExecutionSettings executionSettings;
    if (collectCodeCoverage) {
        inputsBuilder.addTransitive(instrumentedFiles.getCoverageSupportFiles());
        // Add instrumented file manifest artifact to the list of inputs. This file will contain
        // exec paths of all source files that should be included into the code coverage output.
        NestedSet<Artifact> metadataFiles = instrumentedFiles.getInstrumentationMetadataFiles();
        inputsBuilder.addTransitive(metadataFiles);
        inputsBuilder.addTransitive(PrerequisiteArtifacts.nestedSet(ruleContext, "$coverage_support", Mode.DONT_CHECK));
        // We don't add this attribute to non-supported test target
        if (ruleContext.isAttrDefined("$lcov_merger", LABEL)) {
            Artifact lcovMerger = ruleContext.getPrerequisiteArtifact("$lcov_merger", Mode.TARGET);
            if (lcovMerger != null) {
                inputsBuilder.addTransitive(PrerequisiteArtifacts.nestedSet(ruleContext, "$lcov_merger", Mode.TARGET));
                // Pass this LcovMerger_deploy.jar path to collect_coverage.sh
                testEnv.put("LCOV_MERGER", lcovMerger.getExecPathString());
            }
        }
        Artifact instrumentedFileManifest = InstrumentedFileManifestAction.getInstrumentedFileManifest(ruleContext, instrumentedFiles.getInstrumentedFiles(), metadataFiles);
        executionSettings = new TestTargetExecutionSettings(ruleContext, runfilesSupport, executable, instrumentedFileManifest, shards);
        inputsBuilder.add(instrumentedFileManifest);
        for (Pair<String, String> coverageEnvEntry : instrumentedFiles.getCoverageEnvironment()) {
            testEnv.put(coverageEnvEntry.getFirst(), coverageEnvEntry.getSecond());
        }
    } else {
        executionSettings = new TestTargetExecutionSettings(ruleContext, runfilesSupport, executable, null, shards);
    }
    testEnv.putAll(extraEnv);
    if (config.getRunUnder() != null) {
        Artifact runUnderExecutable = executionSettings.getRunUnderExecutable();
        if (runUnderExecutable != null) {
            inputsBuilder.add(runUnderExecutable);
        }
    }
    int runsPerTest = config.getRunsPerTestForLabel(ruleContext.getLabel());
    Iterable<Artifact> inputs = inputsBuilder.build();
    int shardRuns = (shards > 0 ? shards : 1);
    List<Artifact> results = Lists.newArrayListWithCapacity(runsPerTest * shardRuns);
    ImmutableList.Builder<Artifact> coverageArtifacts = ImmutableList.builder();
    boolean useExperimentalTestRunner = false;
    if (ruleContext.attributes().has("use_testrunner", Type.BOOLEAN)) {
        useExperimentalTestRunner = ruleContext.attributes().get("use_testrunner", Type.BOOLEAN) && ruleContext.attributes().get("tags", Type.STRING_LIST).contains("experimental_testrunner");
    }
    for (int run = 0; run < runsPerTest; run++) {
        // Use a 1-based index for user friendliness.
        String testRunDir = runsPerTest > 1 ? String.format("run_%d_of_%d", run + 1, runsPerTest) : "";
        for (int shard = 0; shard < shardRuns; shard++) {
            String shardRunDir = (shardRuns > 1 ? String.format("shard_%d_of_%d", shard + 1, shards) : "");
            if (testRunDir.isEmpty()) {
                shardRunDir = shardRunDir.isEmpty() ? "" : shardRunDir + PathFragment.SEPARATOR_CHAR;
            } else {
                testRunDir += PathFragment.SEPARATOR_CHAR;
                shardRunDir = shardRunDir.isEmpty() ? testRunDir : shardRunDir + "_" + testRunDir;
            }
            Artifact testLog = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "test.log"), root);
            Artifact cacheStatus = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "test.cache_status"), root);
            Artifact coverageArtifact = null;
            if (collectCodeCoverage) {
                coverageArtifact = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "coverage.dat"), root);
                coverageArtifacts.add(coverageArtifact);
            }
            Artifact microCoverageArtifact = null;
            if (collectCodeCoverage && config.isMicroCoverageEnabled()) {
                microCoverageArtifact = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "coverage.micro.dat"), root);
            }
            env.registerAction(new TestRunnerAction(ruleContext.getActionOwner(), inputs, testRuntime, testLog, cacheStatus, coverageArtifact, microCoverageArtifact, testProperties, testEnv, executionSettings, shard, run, config, ruleContext.getWorkspaceName(), useExperimentalTestRunner));
            results.add(cacheStatus);
        }
    }
    // TODO(bazel-team): Passing the reportGenerator to every TestParams is a bit strange.
    Artifact reportGenerator = null;
    if (config.isCodeCoverageEnabled()) {
        // It's not enough to add this if the rule has coverage enabled because the command line may
        // contain rules with baseline coverage but no test rules that have coverage enabled, and in
        // that case, we still need the report generator.
        reportGenerator = ruleContext.getPrerequisiteArtifact("$coverage_report_generator", Mode.HOST);
    }
    return new TestParams(runsPerTest, shards, TestTimeout.getTestTimeout(ruleContext.getRule()), ruleContext.getRule().getRuleClass(), ImmutableList.copyOf(results), coverageArtifacts.build(), reportGenerator);
}
Also used : Root(com.google.devtools.build.lib.actions.Root) ImmutableList(com.google.common.collect.ImmutableList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) TreeMap(java.util.TreeMap) Artifact(com.google.devtools.build.lib.actions.Artifact) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) AnalysisEnvironment(com.google.devtools.build.lib.analysis.AnalysisEnvironment) TestParams(com.google.devtools.build.lib.rules.test.TestProvider.TestParams)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)7 AnalysisEnvironment (com.google.devtools.build.lib.analysis.AnalysisEnvironment)7 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)3 ImmutableList (com.google.common.collect.ImmutableList)2 Label (com.google.devtools.build.lib.cmdline.Label)2 Builder (com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder)2 StringSequenceBuilder (com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.StringSequenceBuilder)2 ActionAnalysisMetadata (com.google.devtools.build.lib.actions.ActionAnalysisMetadata)1 Root (com.google.devtools.build.lib.actions.Root)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 ThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)1 CppCompileAction (com.google.devtools.build.lib.rules.cpp.CppCompileAction)1 LinkTargetType (com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType)1 LibraryToLink (com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink)1 TestParams (com.google.devtools.build.lib.rules.test.TestProvider.TestParams)1 Collection (java.util.Collection)1 TreeMap (java.util.TreeMap)1