use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.
the class JavaLibrary method init.
public RuleConfiguredTargetBuilder init(RuleContext ruleContext, final JavaCommon common) throws InterruptedException {
JavaTargetAttributes.Builder attributesBuilder = common.initCommon();
// Collect the transitive dependencies.
JavaCompilationHelper helper = new JavaCompilationHelper(ruleContext, semantics, common.getJavacOpts(), attributesBuilder);
helper.addLibrariesToAttributes(common.targetsTreatedAsDeps(ClasspathType.COMPILE_ONLY));
if (ruleContext.hasErrors()) {
return null;
}
semantics.checkRule(ruleContext, common);
JavaCompilationArtifacts.Builder javaArtifactsBuilder = new JavaCompilationArtifacts.Builder();
if (ruleContext.hasErrors()) {
common.setJavaCompilationArtifacts(JavaCompilationArtifacts.EMPTY);
return null;
}
JavaConfiguration javaConfig = ruleContext.getFragment(JavaConfiguration.class);
NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder();
JavaTargetAttributes attributes = helper.getAttributes();
if (attributes.hasMessages()) {
helper.setTranslations(semantics.translate(ruleContext, javaConfig, attributes.getMessages()));
}
ruleContext.checkSrcsSamePackage(true);
Artifact jar = null;
Artifact srcJar = ruleContext.getImplicitOutputArtifact(JavaSemantics.JAVA_LIBRARY_SOURCE_JAR);
Artifact classJar = ruleContext.getImplicitOutputArtifact(JavaSemantics.JAVA_LIBRARY_CLASS_JAR);
if (attributes.hasSourceFiles() || attributes.hasSourceJars() || attributes.hasResources() || attributes.hasMessages()) {
// We only want to add a jar to the classpath of a dependent rule if it has content.
javaArtifactsBuilder.addRuntimeJar(classJar);
jar = classJar;
}
filesBuilder.add(classJar);
Artifact manifestProtoOutput = helper.createManifestProtoOutput(classJar);
// The gensrc jar is created only if the target uses annotation processing.
// Otherwise, it is null, and the source jar action will not depend on the compile action.
Artifact genSourceJar = null;
Artifact genClassJar = null;
if (helper.usesAnnotationProcessing()) {
genClassJar = helper.createGenJar(classJar);
genSourceJar = helper.createGensrcJar(classJar);
helper.createGenJarAction(classJar, manifestProtoOutput, genClassJar);
}
Artifact outputDepsProto = helper.createOutputDepsProtoArtifact(classJar, javaArtifactsBuilder);
helper.createCompileActionWithInstrumentation(classJar, manifestProtoOutput, genSourceJar, outputDepsProto, javaArtifactsBuilder);
helper.createSourceJarAction(srcJar, genSourceJar);
Artifact iJar = null;
if ((attributes.hasSourceFiles() || attributes.hasSourceJars()) && jar != null) {
iJar = helper.createCompileTimeJarAction(jar, javaArtifactsBuilder);
}
boolean neverLink = JavaCommon.isNeverLink(ruleContext);
JavaCompilationArtifacts javaArtifacts = javaArtifactsBuilder.build();
common.setJavaCompilationArtifacts(javaArtifacts);
common.setClassPathFragment(new ClasspathConfiguredFragment(javaArtifacts, attributes, neverLink, helper.getBootclasspathOrDefault()));
CppCompilationContext transitiveCppDeps = common.collectTransitiveCppDeps();
NestedSet<Artifact> transitiveSourceJars = common.collectTransitiveSourceJars(srcJar);
// If sources are empty, treat this library as a forwarding node for dependencies.
JavaCompilationArgs javaCompilationArgs = common.collectJavaCompilationArgs(false, neverLink, false);
JavaCompilationArgs recursiveJavaCompilationArgs = common.collectJavaCompilationArgs(true, neverLink, false);
NestedSet<Artifact> compileTimeJavaDepArtifacts = common.collectCompileTimeDependencyArtifacts(javaArtifacts.getCompileTimeDependencyArtifact());
NestedSet<Artifact> runTimeJavaDepArtifacts = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
NestedSet<LinkerInput> transitiveJavaNativeLibraries = common.collectTransitiveJavaNativeLibraries();
CcLinkParamsStore ccLinkParamsStore = new CcLinkParamsStore() {
@Override
protected void collect(CcLinkParams.Builder builder, boolean linkingStatically, boolean linkShared) {
builder.addTransitiveTargets(common.targetsTreatedAsDeps(ClasspathType.BOTH), JavaCcLinkParamsProvider.TO_LINK_PARAMS, CcLinkParamsProvider.TO_LINK_PARAMS);
}
};
ProtoJavaApiInfoAspectProvider.Builder protoAspectBuilder = ProtoJavaApiInfoAspectProvider.builder();
for (TransitiveInfoCollection dep : common.getDependencies()) {
ProtoJavaApiInfoAspectProvider protoProvider = JavaProvider.getProvider(ProtoJavaApiInfoAspectProvider.class, dep);
if (protoProvider != null) {
protoAspectBuilder.addTransitive(protoProvider);
}
}
RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext);
semantics.addProviders(ruleContext, common, ImmutableList.<String>of(), classJar, srcJar, genClassJar, genSourceJar, ImmutableMap.<Artifact, Artifact>of(), filesBuilder, builder);
JavaRuleOutputJarsProvider ruleOutputJarsProvider = JavaRuleOutputJarsProvider.builder().addOutputJar(classJar, iJar, ImmutableList.of(srcJar)).setJdeps(outputDepsProto).build();
JavaCompilationArgsProvider compilationArgsProvider = JavaCompilationArgsProvider.create(javaCompilationArgs, recursiveJavaCompilationArgs, compileTimeJavaDepArtifacts, runTimeJavaDepArtifacts);
JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.create(transitiveSourceJars, ImmutableList.of(srcJar));
JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(ruleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(compilationArgsProvider);
NestedSet<Artifact> filesToBuild = filesBuilder.build();
common.addTransitiveInfoProviders(builder, skylarkApiProvider, filesToBuild, classJar);
common.addGenJarsProvider(builder, skylarkApiProvider, genClassJar, genSourceJar);
NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs();
CcLinkParamsProvider ccLinkParamsProvider = new CcLinkParamsProvider(ccLinkParamsStore);
JavaProvider javaProvider = JavaProvider.Builder.create().addProvider(JavaCompilationArgsProvider.class, compilationArgsProvider).addProvider(JavaSourceJarsProvider.class, sourceJarsProvider).addProvider(ProtoJavaApiInfoAspectProvider.class, protoAspectBuilder.build()).build();
builder.addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider).add(JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaArtifacts.getRuntimeJars())).add(RunfilesProvider.class, RunfilesProvider.simple(JavaCommon.getRunfiles(ruleContext, semantics, javaArtifacts, neverLink))).setFilesToBuild(filesToBuild).add(JavaNeverlinkInfoProvider.class, new JavaNeverlinkInfoProvider(neverLink)).add(CppCompilationContext.class, transitiveCppDeps).add(JavaCompilationArgsProvider.class, compilationArgsProvider).add(JavaProvider.class, javaProvider).add(CcLinkParamsProvider.class, ccLinkParamsProvider).addNativeDeclaredProvider(ccLinkParamsProvider).addNativeDeclaredProvider(javaProvider).add(JavaNativeLibraryProvider.class, new JavaNativeLibraryProvider(transitiveJavaNativeLibraries)).add(JavaSourceInfoProvider.class, JavaSourceInfoProvider.fromJavaTargetAttributes(attributes, semantics)).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(JavaPluginInfoProvider.class, JavaCommon.getTransitivePlugins(ruleContext)).add(ProguardSpecProvider.class, new ProguardSpecProvider(proguardSpecs)).addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, transitiveSourceJars).addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, proguardSpecs);
if (ruleContext.hasErrors()) {
return null;
}
return builder;
}
use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.
the class AppleBinary method create.
@Override
public final ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
PlatformType platformType = MultiArchSplitTransitionProvider.getPlatformType(ruleContext);
AppleConfiguration appleConfiguration = ruleContext.getFragment(AppleConfiguration.class);
Platform platform = appleConfiguration.getMultiArchPlatform(platformType);
ImmutableListMultimap<BuildConfiguration, ObjcProvider> configurationToNonPropagatedObjcMap = ruleContext.getPrerequisitesByConfiguration("non_propagated_deps", Mode.SPLIT, ObjcProvider.class);
ImmutableListMultimap<BuildConfiguration, TransitiveInfoCollection> configToDepsCollectionMap = ruleContext.getPrerequisitesByConfiguration("deps", Mode.SPLIT);
Set<BuildConfiguration> childConfigurations = getChildConfigurations(ruleContext);
Artifact outputArtifact = ObjcRuleClasses.intermediateArtifacts(ruleContext).combinedArchitectureBinary();
MultiArchBinarySupport multiArchBinarySupport = new MultiArchBinarySupport(ruleContext);
Map<BuildConfiguration, ObjcProvider> objcProviderByDepConfiguration = multiArchBinarySupport.objcProviderByDepConfiguration(childConfigurations, configToDepsCollectionMap, configurationToNonPropagatedObjcMap, getDylibProviders(ruleContext), getDylibProtoProviders(ruleContext));
multiArchBinarySupport.registerActions(platform, getExtraLinkArgs(ruleContext), objcProviderByDepConfiguration, getExtraLinkInputs(ruleContext), configToDepsCollectionMap, outputArtifact);
NestedSetBuilder<Artifact> filesToBuild = NestedSetBuilder.<Artifact>stableOrder().add(outputArtifact);
RuleConfiguredTargetBuilder targetBuilder = ObjcRuleClasses.ruleConfiguredTarget(ruleContext, filesToBuild.build());
ObjcProvider.Builder objcProviderBuilder = new ObjcProvider.Builder();
for (ObjcProvider objcProvider : objcProviderByDepConfiguration.values()) {
objcProviderBuilder.addTransitiveAndPropagate(objcProvider);
}
objcProviderBuilder.add(MULTI_ARCH_LINKED_BINARIES, outputArtifact);
ObjcProvider objcProvider = objcProviderBuilder.build();
// TODO(cparsons): Stop propagating ObjcProvider directly from this rule.
targetBuilder.addProvider(ObjcProvider.class, objcProvider);
switch(getBinaryType(ruleContext)) {
case EXECUTABLE:
targetBuilder.addNativeDeclaredProvider(new AppleExecutableBinaryProvider(outputArtifact, objcProvider));
break;
case DYLIB:
targetBuilder.addNativeDeclaredProvider(new AppleDylibBinaryProvider(outputArtifact, objcProvider));
break;
case LOADABLE_BUNDLE:
targetBuilder.addNativeDeclaredProvider(new AppleLoadableBundleBinaryProvider(outputArtifact));
break;
}
AppleDebugOutputsProvider.Builder builder = AppleDebugOutputsProvider.Builder.create();
for (BuildConfiguration childConfig : childConfigurations) {
AppleConfiguration childAppleConfig = childConfig.getFragment(AppleConfiguration.class);
ObjcConfiguration childObjcConfig = childConfig.getFragment(ObjcConfiguration.class);
IntermediateArtifacts intermediateArtifacts = new IntermediateArtifacts(ruleContext, /*archiveFileNameSuffix*/
"", /*outputPrefix*/
"", childConfig);
String arch = childAppleConfig.getSingleArchitecture();
if (childAppleConfig.getBitcodeMode() == AppleBitcodeMode.EMBEDDED) {
Artifact bitcodeSymbol = intermediateArtifacts.bitcodeSymbolMap();
builder.addOutput(arch, OutputType.BITCODE_SYMBOLS, bitcodeSymbol);
}
if (childObjcConfig.generateDsym()) {
Artifact dsymBinary = intermediateArtifacts.dsymSymbol(DsymOutputType.APP);
builder.addOutput(arch, OutputType.DSYM_BINARY, dsymBinary);
}
if (childObjcConfig.generateLinkmap()) {
Artifact linkmap = intermediateArtifacts.linkmap();
builder.addOutput(arch, OutputType.LINKMAP, linkmap);
}
}
targetBuilder.addNativeDeclaredProvider(builder.build());
return targetBuilder.build();
}
use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.
the class JavaLiteProtoLibrary method getJavaLiteRuntimeSpec.
private ProguardSpecProvider getJavaLiteRuntimeSpec(RuleContext ruleContext) {
NestedSet<Artifact> specs = new ProguardLibrary(ruleContext).collectProguardSpecs(ImmutableMultimap.<Mode, String>of());
TransitiveInfoCollection runtime = getProtoToolchainProvider(ruleContext).runtime();
if (runtime == null) {
return new ProguardSpecProvider(specs);
}
ProguardSpecProvider specProvider = runtime.getProvider(ProguardSpecProvider.class);
if (specProvider == null) {
return new ProguardSpecProvider(specs);
}
return new ProguardSpecProvider(NestedSetBuilder.fromNestedSet(specs).addTransitive(specProvider.getTransitiveProguardSpecs()).build());
}
use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.
the class JavaToolchain method getArtifact.
private static Artifact getArtifact(String attributeName, RuleContext ruleContext) {
TransitiveInfoCollection prerequisite = ruleContext.getPrerequisite(attributeName, Mode.HOST);
if (prerequisite == null) {
return null;
}
Iterable<Artifact> artifacts = prerequisite.getProvider(FileProvider.class).getFilesToBuild();
if (Iterables.size(artifacts) != 1) {
ruleContext.attributeError(attributeName, prerequisite.getLabel() + " expected a single artifact");
return null;
}
return Iterables.getOnlyElement(artifacts);
}
use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.
the class JavaCommon method collectTransitiveExports.
/**
* Collects labels of targets and artifacts reached transitively via the "exports" attribute.
*/
protected JavaExportsProvider collectTransitiveExports() {
NestedSetBuilder<Label> builder = NestedSetBuilder.stableOrder();
List<TransitiveInfoCollection> currentRuleExports = getExports(ruleContext);
builder.addAll(Iterables.transform(currentRuleExports, GET_COLLECTION_LABEL));
for (TransitiveInfoCollection dep : currentRuleExports) {
JavaExportsProvider exportsProvider = dep.getProvider(JavaExportsProvider.class);
if (exportsProvider != null) {
builder.addTransitive(exportsProvider.getTransitiveExports());
}
}
return new JavaExportsProvider(builder.build());
}
Aggregations