Search in sources :

Example 1 with TransitiveInfoCollection

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

the class BazelJavaSemantics method addCoverageSupport.

// TODO(yueg): refactor this (only mainClass different for now)
@Override
public String addCoverageSupport(JavaCompilationHelper helper, JavaTargetAttributes.Builder attributes, Artifact executable, Artifact instrumentationMetadata, JavaCompilationArtifacts.Builder javaArtifactsBuilder, String mainClass) throws InterruptedException {
    // This method can be called only for *_binary/*_test targets.
    Preconditions.checkNotNull(executable);
    // Add our own metadata artifact (if any).
    if (instrumentationMetadata != null) {
        attributes.addInstrumentationMetadataEntries(ImmutableList.of(instrumentationMetadata));
    }
    if (!hasInstrumentationMetadata(attributes)) {
        return mainClass;
    }
    Artifact instrumentedJar = helper.getRuleContext().getBinArtifact(helper.getRuleContext().getLabel().getName() + "_instrumented.jar");
    // Create an instrumented Jar. This will be referenced on the runtime classpath prior
    // to all other Jars.
    JavaCommon.createInstrumentedJarAction(helper.getRuleContext(), this, attributes.getInstrumentationMetadata(), instrumentedJar, mainClass);
    javaArtifactsBuilder.setInstrumentedJar(instrumentedJar);
    // Add the coverage runner to the list of dependencies when compiling in coverage mode.
    TransitiveInfoCollection runnerTarget = helper.getRuleContext().getPrerequisite("$jacocorunner", Mode.TARGET);
    if (runnerTarget.getProvider(JavaCompilationArgsProvider.class) != null) {
        helper.addLibrariesToAttributes(ImmutableList.of(runnerTarget));
    } else {
        helper.getRuleContext().ruleError("this rule depends on " + helper.getRuleContext().attributes().get("$jacocorunner", BuildType.LABEL) + " which is not a java_library rule, or contains errors");
    }
    // script via an environment variable.
    return JACOCO_COVERAGE_RUNNER_MAIN_CLASS;
}
Also used : JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 2 with TransitiveInfoCollection

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

the class SkylarkRuleContextTest method shouldGetPrerequisites.

@Test
public void shouldGetPrerequisites() throws Exception {
    SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
    Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.srcs");
    // Check for a known provider
    TransitiveInfoCollection tic1 = (TransitiveInfoCollection) ((SkylarkList) result).get(0);
    assertNotNull(tic1.getProvider(JavaSourceJarsProvider.class));
    // Check an unimplemented provider too
    assertNull(tic1.getProvider(SkylarkProviders.class).getValue(PyCommon.PYTHON_SKYLARK_PROVIDER_NAME));
}
Also used : JavaSourceJarsProvider(com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 3 with TransitiveInfoCollection

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

the class CcBinary method collectRunfiles.

private static Runfiles collectRunfiles(RuleContext context, CcToolchainProvider toolchain, CcLinkingOutputs linkingOutputs, CcLibraryHelper.Info info, LinkStaticness linkStaticness, NestedSet<Artifact> filesToBuild, Iterable<Artifact> fakeLinkerInputs, boolean fake, ImmutableSet<CppSource> cAndCppSources, boolean linkCompileOutputSeparately) {
    Runfiles.Builder builder = new Runfiles.Builder(context.getWorkspaceName(), context.getConfiguration().legacyExternalRunfiles());
    Function<TransitiveInfoCollection, Runfiles> runfilesMapping = CppRunfilesProvider.runfilesFunction(linkStaticness != LinkStaticness.DYNAMIC);
    builder.addTransitiveArtifacts(filesToBuild);
    // Add the shared libraries to the runfiles. This adds any shared libraries that are in the
    // srcs of this target.
    builder.addArtifacts(linkingOutputs.getLibrariesForRunfiles(true));
    builder.addRunfiles(context, RunfilesProvider.DEFAULT_RUNFILES);
    builder.add(context, runfilesMapping);
    // Add the C++ runtime libraries if linking them dynamically.
    if (linkStaticness == LinkStaticness.DYNAMIC) {
        builder.addTransitiveArtifacts(toolchain.getDynamicRuntimeLinkInputs());
    }
    if (linkCompileOutputSeparately) {
        builder.addArtifacts(LinkerInputs.toLibraryArtifacts(info.getCcLinkingOutputs().getExecutionDynamicLibraries()));
    }
    // For cc_binary and cc_test rules, there is an implicit dependency on
    // the malloc library package, which is specified by the "malloc" attribute.
    // As the BUILD encyclopedia says, the "malloc" attribute should be ignored
    // if linkshared=1.
    boolean linkshared = isLinkShared(context);
    if (!linkshared) {
        TransitiveInfoCollection malloc = CppHelper.mallocForTarget(context);
        builder.addTarget(malloc, RunfilesProvider.DEFAULT_RUNFILES);
        builder.addTarget(malloc, runfilesMapping);
    }
    if (fake) {
        // Add the object files, libraries, and linker scripts that are used to
        // link this executable.
        builder.addSymlinksToArtifacts(Iterables.filter(fakeLinkerInputs, Artifact.MIDDLEMAN_FILTER));
        // The crosstool inputs for the link action are not sufficient; we also need the crosstool
        // inputs for compilation. Node that these cannot be middlemen because Runfiles does not
        // know how to expand them.
        builder.addTransitiveArtifacts(toolchain.getCrosstool());
        builder.addTransitiveArtifacts(toolchain.getLibcLink());
        // Add the sources files that are used to compile the object files.
        // We add the headers in the transitive closure and our own sources in the srcs
        // attribute. We do not provide the auxiliary inputs, because they are only used when we
        // do FDO compilation, and cc_fake_binary does not support FDO.
        ImmutableSet.Builder<Artifact> sourcesBuilder = ImmutableSet.<Artifact>builder();
        for (CppSource cppSource : cAndCppSources) {
            sourcesBuilder.add(cppSource.getSource());
        }
        builder.addSymlinksToArtifacts(sourcesBuilder.build());
        CppCompilationContext cppCompilationContext = info.getCppCompilationContext();
        builder.addSymlinksToArtifacts(cppCompilationContext.getDeclaredIncludeSrcs());
        // Add additional files that are referenced from the compile command, like module maps
        // or header modules.
        builder.addSymlinksToArtifacts(cppCompilationContext.getAdditionalInputs());
        builder.addSymlinksToArtifacts(cppCompilationContext.getTransitiveModules(CppHelper.usePic(context, !isLinkShared(context))));
    }
    return builder.build();
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) ImmutableSet(com.google.common.collect.ImmutableSet) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 4 with TransitiveInfoCollection

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

the class CcCommon method getLooseIncludeDirs.

/**
   * Determines a list of loose include directories that are only allowed to be referenced when
   * headers checking is {@link HeadersCheckingMode#LOOSE} or {@link HeadersCheckingMode#WARN}.
   */
List<PathFragment> getLooseIncludeDirs() {
    List<PathFragment> result = new ArrayList<>();
    // The package directory of the rule contributes includes. Note that this also covers all
    // non-subpackage sub-directories.
    PathFragment rulePackage = ruleContext.getLabel().getPackageIdentifier().getPathUnderExecRoot();
    result.add(rulePackage);
    // Gather up all the dirs from the rule's srcs as well as any of the srcs outputs.
    if (hasAttribute("srcs", BuildType.LABEL_LIST)) {
        for (TransitiveInfoCollection src : ruleContext.getPrerequisitesIf("srcs", Mode.TARGET, FileProvider.class)) {
            PathFragment packageDir = src.getLabel().getPackageIdentifier().getPathUnderExecRoot();
            for (Artifact a : src.getProvider(FileProvider.class).getFilesToBuild()) {
                result.add(packageDir);
                // Attempt to gather subdirectories that might contain include files.
                result.add(a.getRootRelativePath().getParentDirectory());
            }
        }
    }
    // Add in any 'includes' attribute values as relative path fragments
    if (ruleContext.getRule().isAttributeValueExplicitlySpecified("includes")) {
        PathFragment packageFragment = ruleContext.getLabel().getPackageIdentifier().getPathUnderExecRoot();
        // For now, anything with an 'includes' needs a blanket declaration
        result.add(packageFragment.getRelative("**"));
    }
    return result;
}
Also used : FileProvider(com.google.devtools.build.lib.analysis.FileProvider) ArrayList(java.util.ArrayList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 5 with TransitiveInfoCollection

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

the class CcCommon method getSources.

/**
   * Returns a list of ({@link Artifact}, {@link Label}) pairs. Each pair represents an input
   * source file and the label of the rule that generates it (or the label of the source file
   * itself if it is an input file).
   */
List<Pair<Artifact, Label>> getSources() {
    Map<Artifact, Label> map = Maps.newLinkedHashMap();
    Iterable<? extends TransitiveInfoCollection> providers = ruleContext.getPrerequisitesIf("srcs", Mode.TARGET, FileProvider.class);
    for (TransitiveInfoCollection provider : providers) {
        for (Artifact artifact : provider.getProvider(FileProvider.class).getFilesToBuild()) {
            // TODO(bazel-team): We currently do not produce an error for duplicate headers and other
            // non-source artifacts with different labels, as that would require cleaning up the code
            // base without significant benefit; we should eventually make this consistent one way or
            // the other.
            Label oldLabel = map.put(artifact, provider.getLabel());
            boolean isHeader = CppFileTypes.CPP_HEADER.matches(artifact.getExecPath());
            if (!isHeader && SourceCategory.CC_AND_OBJC.getSourceTypes().matches(artifact.getExecPathString()) && oldLabel != null && !oldLabel.equals(provider.getLabel())) {
                ruleContext.attributeError("srcs", String.format("Artifact '%s' is duplicated (through '%s' and '%s')", artifact.getExecPathString(), oldLabel, provider.getLabel()));
            }
        }
    }
    ImmutableList.Builder<Pair<Artifact, Label>> result = ImmutableList.builder();
    for (Map.Entry<Artifact, Label> entry : map.entrySet()) {
        result.add(Pair.of(entry.getKey(), entry.getValue()));
    }
    return result.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) FileProvider(com.google.devtools.build.lib.analysis.FileProvider) Label(com.google.devtools.build.lib.cmdline.Label) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Map(java.util.Map) Artifact(com.google.devtools.build.lib.actions.Artifact) Pair(com.google.devtools.build.lib.util.Pair)

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