use of com.google.devtools.build.lib.rules.cpp.CppCompilationContext in project bazel by bazelbuild.
the class JavaImport method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
ImmutableList<Artifact> srcJars = ImmutableList.of();
ImmutableList<Artifact> jars = collectJars(ruleContext);
Artifact srcJar = ruleContext.getPrerequisiteArtifact("srcjar", Mode.TARGET);
if (ruleContext.hasErrors()) {
return null;
}
ImmutableList<TransitiveInfoCollection> targets = ImmutableList.<TransitiveInfoCollection>builder().addAll(ruleContext.getPrerequisites("deps", Mode.TARGET)).addAll(ruleContext.getPrerequisites("exports", Mode.TARGET)).build();
final JavaCommon common = new JavaCommon(ruleContext, semantics, /*srcs=*/
ImmutableList.<Artifact>of(), targets, targets, targets);
semantics.checkRule(ruleContext, common);
// No need for javac options - no compilation happening here.
ImmutableBiMap.Builder<Artifact, Artifact> compilationToRuntimeJarMapBuilder = ImmutableBiMap.builder();
ImmutableList<Artifact> interfaceJars = processWithIjar(jars, ruleContext, compilationToRuntimeJarMapBuilder);
JavaCompilationArtifacts javaArtifacts = collectJavaArtifacts(jars, interfaceJars);
common.setJavaCompilationArtifacts(javaArtifacts);
CppCompilationContext transitiveCppDeps = common.collectTransitiveCppDeps();
NestedSet<LinkerInput> transitiveJavaNativeLibraries = common.collectTransitiveJavaNativeLibraries();
boolean neverLink = JavaCommon.isNeverLink(ruleContext);
JavaCompilationArgs javaCompilationArgs = common.collectJavaCompilationArgs(false, neverLink, false);
JavaCompilationArgs recursiveJavaCompilationArgs = common.collectJavaCompilationArgs(true, neverLink, false);
NestedSet<Artifact> transitiveJavaSourceJars = collectTransitiveJavaSourceJars(ruleContext, srcJar);
if (srcJar != null) {
srcJars = ImmutableList.of(srcJar);
}
// The "neverlink" attribute is transitive, so if it is enabled, we don't add any
// runfiles from this target or its dependencies.
Runfiles runfiles = neverLink ? Runfiles.EMPTY : new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addArtifacts(javaArtifacts.getRuntimeJars()).addTargets(targets, RunfilesProvider.DEFAULT_RUNFILES).addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES).addTargets(targets, JavaRunfilesProvider.TO_RUNFILES).add(ruleContext, JavaRunfilesProvider.TO_RUNFILES).build();
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);
}
};
RuleConfiguredTargetBuilder ruleBuilder = new RuleConfiguredTargetBuilder(ruleContext);
NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder();
filesBuilder.addAll(jars);
ImmutableBiMap<Artifact, Artifact> compilationToRuntimeJarMap = compilationToRuntimeJarMapBuilder.build();
semantics.addProviders(ruleContext, common, ImmutableList.<String>of(), null, /* classJar */
srcJar, /* srcJar */
null, /* genJar */
null, /* gensrcJar */
compilationToRuntimeJarMap, filesBuilder, ruleBuilder);
NestedSet<Artifact> filesToBuild = filesBuilder.build();
JavaSourceInfoProvider javaSourceInfoProvider = new JavaSourceInfoProvider.Builder().setJarFiles(jars).setSourceJarsForJarFiles(srcJars).build();
JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder = JavaRuleOutputJarsProvider.builder();
for (Artifact jar : jars) {
ruleOutputJarsProviderBuilder.addOutputJar(jar, compilationToRuntimeJarMap.inverse().get(jar), srcJars);
}
NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs();
JavaRuleOutputJarsProvider ruleOutputJarsProvider = ruleOutputJarsProviderBuilder.build();
JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.create(transitiveJavaSourceJars, srcJars);
JavaCompilationArgsProvider compilationArgsProvider = JavaCompilationArgsProvider.create(javaCompilationArgs, recursiveJavaCompilationArgs);
JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(ruleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(compilationArgsProvider);
common.addTransitiveInfoProviders(ruleBuilder, skylarkApiProvider, filesToBuild, null);
return ruleBuilder.setFilesToBuild(filesToBuild).addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider).add(JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaArtifacts.getRuntimeJars())).add(JavaNeverlinkInfoProvider.class, new JavaNeverlinkInfoProvider(neverLink)).add(RunfilesProvider.class, RunfilesProvider.simple(runfiles)).add(CcLinkParamsProvider.class, new CcLinkParamsProvider(ccLinkParamsStore)).add(JavaCompilationArgsProvider.class, compilationArgsProvider).add(JavaNativeLibraryProvider.class, new JavaNativeLibraryProvider(transitiveJavaNativeLibraries)).add(CppCompilationContext.class, transitiveCppDeps).add(JavaSourceInfoProvider.class, javaSourceInfoProvider).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(ProguardSpecProvider.class, new ProguardSpecProvider(proguardSpecs)).addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, transitiveJavaSourceJars).addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, proguardSpecs).build();
}
use of com.google.devtools.build.lib.rules.cpp.CppCompilationContext 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.rules.cpp.CppCompilationContext in project bazel by bazelbuild.
the class CcProtoLibraryTest method aliasProtos.
@Test
public void aliasProtos() throws Exception {
scratch.file("x/BUILD", "cc_proto_library(name = 'foo_cc_proto', deps = ['alias_proto'])", "proto_library(name = 'alias_proto', deps = [':foo_proto'])", "proto_library(name = 'foo_proto', srcs = ['foo.proto'])");
CppCompilationContext context = getConfiguredTarget("//x:foo_cc_proto").getProvider(CppCompilationContext.class);
assertThat(prettyArtifactNames(context.getDeclaredIncludeSrcs())).containsExactly("x/foo.pb.h");
}
use of com.google.devtools.build.lib.rules.cpp.CppCompilationContext in project bazel by bazelbuild.
the class CcProtoLibraryTest method cppCompilationContext.
@Test
public void cppCompilationContext() throws Exception {
scratch.file("x/BUILD", "cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])", "proto_library(name = 'foo_proto', srcs = ['foo.proto'], deps = [':bar_proto'])", "proto_library(name = 'bar_proto', srcs = ['bar.proto'])");
CppCompilationContext context = getConfiguredTarget("//x:foo_cc_proto").getProvider(CppCompilationContext.class);
assertThat(prettyArtifactNames(context.getDeclaredIncludeSrcs())).containsExactly("x/foo.pb.h", "x/bar.pb.h");
}
use of com.google.devtools.build.lib.rules.cpp.CppCompilationContext in project bazel by bazelbuild.
the class AndroidStudioInfoAspect method createIdeBuildArtifact.
private AndroidStudioInfoFilesProvider createIdeBuildArtifact(ConfiguredTarget base, RuleContext ruleContext, DependenciesResult dependenciesResult, AndroidStudioInfoFilesProvider.Builder providerBuilder) {
Artifact ideInfoFile = derivedArtifact(base, ruleContext, ASWB_BUILD_SUFFIX);
Artifact ideInfoTextFile = derivedArtifact(base, ruleContext, ASWB_BUILD_TEXT_SUFFIX);
providerBuilder.ideInfoFilesBuilder().add(ideInfoFile);
providerBuilder.ideInfoTextFilesBuilder().add(ideInfoTextFile);
NestedSetBuilder<Artifact> ideResolveArtifacts = providerBuilder.ideResolveFilesBuilder();
TargetIdeInfo.Builder outputBuilder = TargetIdeInfo.newBuilder();
outputBuilder.setLabel(base.getLabel().toString());
outputBuilder.setBuildFileArtifactLocation(makeArtifactLocation(ruleContext.getRule().getPackage(), ruleContext.getLabel()));
outputBuilder.setKindString(ruleContext.getRule().getRuleClass());
// Java rules
JavaRuleOutputJarsProvider outputJarsProvider = JavaProvider.getProvider(JavaRuleOutputJarsProvider.class, base);
if (outputJarsProvider != null && !androidStudioInfoSemantics.suppressJavaRuleInfo(base)) {
JavaIdeInfo javaIdeInfo = makeJavaIdeInfo(base, ruleContext, outputJarsProvider, providerBuilder);
outputBuilder.setJavaIdeInfo(javaIdeInfo);
}
// C rules
if (isCppRule(base)) {
CppCompilationContext cppCompilationContext = base.getProvider(CppCompilationContext.class);
if (cppCompilationContext != null) {
CIdeInfo cIdeInfo = makeCIdeInfo(base, ruleContext, cppCompilationContext, ideResolveArtifacts);
outputBuilder.setCIdeInfo(cIdeInfo);
}
}
// CCToolchain rule
CcToolchainProvider ccToolchainProvider = base.getProvider(CcToolchainProvider.class);
if (ccToolchainProvider != null) {
CppConfiguration cppConfiguration = ccToolchainProvider.getCppConfiguration();
if (cppConfiguration != null) {
CToolchainIdeInfo cToolchainIdeInfo = makeCToolchainIdeInfo(ruleContext, cppConfiguration);
if (cToolchainIdeInfo != null) {
outputBuilder.setCToolchainIdeInfo(cToolchainIdeInfo);
}
}
}
// Android rules
AndroidIdeInfoProvider androidIdeInfoProvider = base.getProvider(AndroidIdeInfoProvider.class);
if (androidIdeInfoProvider != null) {
outputBuilder.setAndroidIdeInfo(makeAndroidIdeInfo(androidIdeInfoProvider, dependenciesResult, ideResolveArtifacts));
}
// Python rules
if (isPythonRule(base)) {
outputBuilder.setPyIdeInfo(makePyIdeInfo(base, ruleContext, ideResolveArtifacts));
}
// Test rules
if (TargetUtils.isTestRule(base.getTarget())) {
TestInfo.Builder builder = TestInfo.newBuilder();
String attr = NonconfigurableAttributeMapper.of(base.getTarget().getAssociatedRule()).get("size", Type.STRING);
if (attr != null) {
builder.setSize(attr);
}
outputBuilder.setTestInfo(builder);
}
// Java toolchain rule
JavaToolchainProvider javaToolchainProvider = base.getProvider(JavaToolchainProvider.class);
if (javaToolchainProvider != null) {
outputBuilder.setJavaToolchainIdeInfo(JavaToolchainIdeInfo.newBuilder().setSourceVersion(javaToolchainProvider.getSourceVersion()).setTargetVersion(javaToolchainProvider.getTargetVersion()));
}
androidStudioInfoSemantics.augmentRuleInfo(outputBuilder, base, ruleContext, ideResolveArtifacts);
AndroidStudioInfoFilesProvider provider = providerBuilder.build();
outputBuilder.addAllDependencies(transform(dependenciesResult.deps, LABEL_TO_STRING));
outputBuilder.addAllRuntimeDeps(transform(dependenciesResult.runtimeDeps, LABEL_TO_STRING));
outputBuilder.addAllTags(base.getTarget().getAssociatedRule().getRuleTags());
final TargetIdeInfo targetIdeInfo = outputBuilder.build();
ruleContext.registerAction(makeProtoWriteAction(ruleContext.getActionOwner(), targetIdeInfo, ideInfoFile));
ruleContext.registerAction(makeProtoTextWriteAction(ruleContext.getActionOwner(), targetIdeInfo, ideInfoTextFile));
return provider;
}
Aggregations