Search in sources :

Example 26 with TransitiveInfoCollection

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

the class ProtoCompileActionBuilderTest method outReplacementAreLazilyEvaluated.

@Test
public void outReplacementAreLazilyEvaluated() throws Exception {
    final boolean[] hasBeenCalled = new boolean[1];
    hasBeenCalled[0] = false;
    CharSequence outReplacement = new LazyString() {

        @Override
        public String toString() {
            hasBeenCalled[0] = true;
            return "mu";
        }
    };
    ProtoLangToolchainProvider toolchain = ProtoLangToolchainProvider.create("--java_out=param1,param2:$(OUT)", null, /* pluginExecutable */
    mock(TransitiveInfoCollection.class), /* runtime */
    NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
    SupportData supportData = SupportData.create(Predicates.<TransitiveInfoCollection>alwaysFalse(), ImmutableList.<Artifact>of(), NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER), /* protosInDirectDeps */
    NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER), true);
    CustomCommandLine cmdLine = createCommandLineFromToolchains(ImmutableList.of(new ToolchainInvocation("pluginName", toolchain, outReplacement)), supportData.getDirectProtoSources(), supportData.getTransitiveImports(), supportData.getProtosInDirectDeps(), "//foo:bar", true, /* allowServices */
    ImmutableList.<String>of());
    assertThat(hasBeenCalled[0]).isFalse();
    cmdLine.arguments();
    assertThat(hasBeenCalled[0]).isTrue();
}
Also used : ToolchainInvocation(com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.ToolchainInvocation) CustomCommandLine(com.google.devtools.build.lib.analysis.actions.CustomCommandLine) LazyString(com.google.devtools.build.lib.util.LazyString) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Test(org.junit.Test)

Example 27 with TransitiveInfoCollection

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

the class ProtoLangToolchainTest method protoToolchain.

@Test
public void protoToolchain() throws Exception {
    scratch.file("x/BUILD", "cc_binary(name = 'plugin', srcs = ['plugin.cc'])", "cc_library(name = 'runtime', srcs = ['runtime.cc'])", "filegroup(name = 'descriptors', srcs = ['metadata.proto', 'descriptor.proto'])", "filegroup(name = 'any', srcs = ['any.proto'])");
    scratch.file("foo/BUILD", "proto_lang_toolchain(", "    name = 'toolchain',", "    command_line = 'cmd-line',", "    plugin = '//x:plugin',", "    runtime = '//x:runtime',", "    blacklisted_protos = ['//x:descriptors', '//x:any']", ")");
    update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());
    ProtoLangToolchainProvider toolchain = getConfiguredTarget("//foo:toolchain").getProvider(ProtoLangToolchainProvider.class);
    assertThat(toolchain.commandLine()).isEqualTo("cmd-line");
    assertThat(toolchain.pluginExecutable().getExecutable().getRootRelativePathString()).isEqualTo("x/plugin" + OsUtils.executableExtension());
    TransitiveInfoCollection runtimes = toolchain.runtime();
    assertThat(runtimes.getLabel()).isEqualTo(Label.parseAbsolute("//x:runtime"));
    assertThat(prettyArtifactNames(toolchain.blacklistedProtos())).containsExactly("x/metadata.proto", "x/descriptor.proto", "x/any.proto");
}
Also used : TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 28 with TransitiveInfoCollection

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

the class InstrumentedFilesCollector method collect.

/**
   * Collects transitive instrumentation data from dependencies, collects local source files from
   * dependencies, collects local metadata files by traversing the action graph of the current
   * configured target, collect rule-specific instrumentation support file sand creates baseline
   * coverage actions for the transitive closure of source files (if
   * <code>withBaselineCoverage</code> is true).
   */
public static InstrumentedFilesProvider collect(RuleContext ruleContext, InstrumentationSpec spec, LocalMetadataCollector localMetadataCollector, Iterable<Artifact> rootFiles, NestedSet<Artifact> coverageSupportFiles, NestedSet<Pair<String, String>> coverageEnvironment, boolean withBaselineCoverage) {
    Preconditions.checkNotNull(ruleContext);
    Preconditions.checkNotNull(spec);
    if (!ruleContext.getConfiguration().isCodeCoverageEnabled()) {
        return InstrumentedFilesProviderImpl.EMPTY;
    }
    NestedSetBuilder<Artifact> instrumentedFilesBuilder = NestedSetBuilder.stableOrder();
    NestedSetBuilder<Artifact> metadataFilesBuilder = NestedSetBuilder.stableOrder();
    NestedSetBuilder<Artifact> baselineCoverageInstrumentedFilesBuilder = NestedSetBuilder.stableOrder();
    NestedSetBuilder<Artifact> coverageSupportFilesBuilder = NestedSetBuilder.<Artifact>stableOrder().addTransitive(coverageSupportFiles);
    NestedSetBuilder<Pair<String, String>> coverageEnvironmentBuilder = NestedSetBuilder.<Pair<String, String>>compileOrder().addTransitive(coverageEnvironment);
    // Transitive instrumentation data.
    for (TransitiveInfoCollection dep : getAllPrerequisites(ruleContext, spec.dependencyAttributes)) {
        InstrumentedFilesProvider provider = dep.getProvider(InstrumentedFilesProvider.class);
        if (provider != null) {
            instrumentedFilesBuilder.addTransitive(provider.getInstrumentedFiles());
            metadataFilesBuilder.addTransitive(provider.getInstrumentationMetadataFiles());
            baselineCoverageInstrumentedFilesBuilder.addTransitive(provider.getBaselineCoverageInstrumentedFiles());
            coverageSupportFilesBuilder.addTransitive(provider.getCoverageSupportFiles());
            coverageEnvironmentBuilder.addTransitive(provider.getCoverageEnvironment());
        }
    }
    // Local sources.
    NestedSet<Artifact> localSources = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
    if (shouldIncludeLocalSources(ruleContext)) {
        NestedSetBuilder<Artifact> localSourcesBuilder = NestedSetBuilder.stableOrder();
        for (TransitiveInfoCollection dep : getAllPrerequisites(ruleContext, spec.sourceAttributes)) {
            if (!spec.splitLists && dep.getProvider(InstrumentedFilesProvider.class) != null) {
                continue;
            }
            for (Artifact artifact : dep.getProvider(FileProvider.class).getFilesToBuild()) {
                if (artifact.isSourceArtifact() && spec.instrumentedFileTypes.matches(artifact.getFilename())) {
                    localSourcesBuilder.add(artifact);
                }
            }
        }
        localSources = localSourcesBuilder.build();
    }
    instrumentedFilesBuilder.addTransitive(localSources);
    if (withBaselineCoverage) {
        // Also add the local sources to the baseline coverage instrumented sources, if the current
        // rule supports baseline coverage.
        // TODO(ulfjack): Generate a local baseline coverage action, and then merge at the leaves.
        baselineCoverageInstrumentedFilesBuilder.addTransitive(localSources);
    }
    // Local metadata files.
    if (localMetadataCollector != null) {
        localMetadataCollector.collectMetadataArtifacts(rootFiles, ruleContext.getAnalysisEnvironment(), metadataFilesBuilder);
    }
    // Baseline coverage actions.
    NestedSet<Artifact> baselineCoverageFiles = baselineCoverageInstrumentedFilesBuilder.build();
    // Create one baseline coverage action per target, but for the transitive closure of files.
    NestedSet<Artifact> baselineCoverageArtifacts = BaselineCoverageAction.create(ruleContext, baselineCoverageFiles);
    return new InstrumentedFilesProviderImpl(instrumentedFilesBuilder.build(), metadataFilesBuilder.build(), baselineCoverageFiles, baselineCoverageArtifacts, coverageSupportFilesBuilder.build(), coverageEnvironmentBuilder.build());
}
Also used : FileProvider(com.google.devtools.build.lib.analysis.FileProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact) Pair(com.google.devtools.build.lib.util.Pair)

Example 29 with TransitiveInfoCollection

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

the class PyCommon method validateSrcs.

/**
   * Returns a mutable List of the source Artifacts.
   */
public List<Artifact> validateSrcs() {
    List<Artifact> sourceFiles = new ArrayList<>();
    //                 sources of the rule.
    for (TransitiveInfoCollection src : ruleContext.getPrerequisitesIf("srcs", Mode.TARGET, FileProvider.class)) {
        // Make sure that none of the sources contain hyphens.
        if (Util.containsHyphen(src.getLabel().getPackageFragment())) {
            ruleContext.attributeError("srcs", src.getLabel() + ": paths to Python packages may not contain '-'");
        }
        Iterable<Artifact> pySrcs = FileType.filter(src.getProvider(FileProvider.class).getFilesToBuild(), PyRuleClasses.PYTHON_SOURCE);
        Iterables.addAll(sourceFiles, pySrcs);
        if (Iterables.isEmpty(pySrcs)) {
            ruleContext.attributeWarning("srcs", "rule '" + src.getLabel() + "' does not produce any Python source files");
        }
    }
    LanguageDependentFragment.Checker.depsSupportsLanguage(ruleContext, PyRuleClasses.LANGUAGE);
    return convertedFiles != null ? ImmutableList.copyOf(convertedFiles.values()) : sourceFiles;
}
Also used : ArrayList(java.util.ArrayList) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 30 with TransitiveInfoCollection

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

the class PyCommon method collectTransitivePythonSourcesFrom.

private void collectTransitivePythonSourcesFrom(Iterable<? extends TransitiveInfoCollection> deps, NestedSetBuilder<Artifact> builder) {
    for (TransitiveInfoCollection dep : deps) {
        NestedSet<Artifact> pythonSourceFiles = getTransitivePythonSourcesFromSkylarkProvider(dep);
        if (pythonSourceFiles != null) {
            builder.addTransitive(pythonSourceFiles);
        } else {
            // TODO(bazel-team): We also collect .py source files from deps (e.g. for proto_library
            // rules). Rules should implement PythonSourcesProvider instead.
            FileProvider provider = dep.getProvider(FileProvider.class);
            builder.addAll(FileType.filter(provider.getFilesToBuild(), PyRuleClasses.PYTHON_SOURCE));
        }
    }
}
Also used : FileProvider(com.google.devtools.build.lib.analysis.FileProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Aggregations

TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)54 Artifact (com.google.devtools.build.lib.actions.Artifact)27 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)14 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)11 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)11 Label (com.google.devtools.build.lib.cmdline.Label)10 ImmutableList (com.google.common.collect.ImmutableList)8 ArrayList (java.util.ArrayList)8 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)7 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)5 Test (org.junit.Test)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)4 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)4 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)4 Pair (com.google.devtools.build.lib.util.Pair)4 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 Map (java.util.Map)4 CustomCommandLine (com.google.devtools.build.lib.analysis.actions.CustomCommandLine)3 CcLinkParamsProvider (com.google.devtools.build.lib.rules.cpp.CcLinkParamsProvider)3