Skip to content

Commit

Permalink
Migrate J2ObjcLibrary to generate CcInfo with compile info
Browse files Browse the repository at this point in the history
    Part of overall migration of compile info to CcInfo.  See
    bazelbuild/bazel#10674.

    RELNOTES: None
    PiperOrigin-RevId: 298466963
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent a3e922c commit 7128519
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.google.devtools.build.lib.rules.objc;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.devtools.build.lib.analysis.TransitionMode.TARGET;
import static com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode.TARGET;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
Expand All @@ -33,14 +33,14 @@
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.TransitionMode;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine.VectorArg;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.config.ConfigAwareAspectBuilder;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
Expand All @@ -51,7 +51,7 @@
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.NativeAspectClass;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkProviderIdentifier;
import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.apple.AppleToolchain;
import com.google.devtools.build.lib.rules.apple.XcodeConfigRule;
Expand Down Expand Up @@ -104,13 +104,13 @@ private static LabelLateBoundDefault<ProtoConfiguration> getProtoToolchainLabel(

private static final ImmutableList<Attribute> JAVA_DEPENDENT_ATTRIBUTES =
ImmutableList.of(
new Attribute("$jre_lib", TransitionMode.TARGET),
new Attribute("deps", TransitionMode.TARGET),
new Attribute("exports", TransitionMode.TARGET),
new Attribute("runtime_deps", TransitionMode.TARGET));
new Attribute("$jre_lib", Mode.TARGET),
new Attribute("deps", Mode.TARGET),
new Attribute("exports", Mode.TARGET),
new Attribute("runtime_deps", Mode.TARGET));

private static final ImmutableList<Attribute> PROTO_DEPENDENT_ATTRIBUTES =
ImmutableList.of(new Attribute("deps", TransitionMode.TARGET));
ImmutableList.of(new Attribute("deps", Mode.TARGET));

private static final String J2OBJC_PROTO_TOOLCHAIN_ATTR = ":j2objc_proto_toolchain";

Expand Down Expand Up @@ -144,9 +144,9 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
.propagateAlongAttribute("deps")
.propagateAlongAttribute("exports")
.propagateAlongAttribute("runtime_deps")
.requireSkylarkProviders(StarlarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey()))
.requireSkylarkProviders(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey()))
.requireSkylarkProviders(ProtoInfo.PROVIDER.id())
.advertiseProvider(ImmutableList.of(ObjcProvider.STARLARK_CONSTRUCTOR.id()))
.advertiseProvider(ImmutableList.of(ObjcProvider.SKYLARK_CONSTRUCTOR.id()))
.requiresConfigurationFragments(
AppleConfiguration.class,
CppConfiguration.class,
Expand Down Expand Up @@ -244,21 +244,22 @@ public ConfiguredAspect create(
throws InterruptedException, ActionConflictException {
ConfiguredTarget base = ctadBase.getConfiguredTarget();
if (isProtoRule(base)) {
return proto(base, ruleContext);
return proto(base, ruleContext, parameters);
} else {
return java(base, ruleContext);
return java(base, ruleContext, parameters);
}
}

private ConfiguredAspect buildAspect(
ConfiguredTarget base,
RuleContext ruleContext,
AspectParameters parameters,
J2ObjcSource j2ObjcSource,
J2ObjcMappingFileProvider directJ2ObjcMappingFileProvider,
List<Attribute> depAttributes,
List<TransitiveInfoCollection> otherDeps)
throws InterruptedException, ActionConflictException {
ConfiguredAspect.Builder builder = new ConfiguredAspect.Builder(ruleContext);
ConfiguredAspect.Builder builder = new ConfiguredAspect.Builder(this, parameters, ruleContext);
ObjcCommon common;
ObjcProvider objcProvider = null;

Expand Down Expand Up @@ -319,7 +320,8 @@ private ConfiguredAspect buildAspect(
.build();
}

private ConfiguredAspect java(ConfiguredTarget base, RuleContext ruleContext)
private ConfiguredAspect java(
ConfiguredTarget base, RuleContext ruleContext, AspectParameters parameters)
throws InterruptedException, ActionConflictException {
JavaCompilationArgsProvider compilationArgsProvider =
JavaInfo.getProvider(JavaCompilationArgsProvider.class, base);
Expand Down Expand Up @@ -361,13 +363,15 @@ private ConfiguredAspect java(ConfiguredTarget base, RuleContext ruleContext)
return buildAspect(
base,
ruleContext,
parameters,
j2ObjcSource,
directJ2ObjcMappingFileProvider,
JAVA_DEPENDENT_ATTRIBUTES,
ImmutableList.of());
}

private ConfiguredAspect proto(ConfiguredTarget base, RuleContext ruleContext)
private ConfiguredAspect proto(
ConfiguredTarget base, RuleContext ruleContext, AspectParameters parameters)
throws InterruptedException, ActionConflictException {
ProtoInfo protoInfo = base.get(ProtoInfo.PROVIDER);
ImmutableList<Artifact> protoSources = protoInfo.getDirectProtoSources();
Expand Down Expand Up @@ -395,6 +399,7 @@ private ConfiguredAspect proto(ConfiguredTarget base, RuleContext ruleContext)
return buildAspect(
base,
ruleContext,
parameters,
j2ObjcSource,
directJ2ObjcMappingFileProvider,
PROTO_DEPENDENT_ATTRIBUTES,
Expand Down Expand Up @@ -485,7 +490,7 @@ private static J2ObjcMappingFileProvider createJ2ObjcTranspilationAction(
PathFragment javaExecutable = JavaCommon.getHostJavaExecutable(ruleContext);
argBuilder.add("--java", javaExecutable.getPathString());

Artifact j2ObjcDeployJar = ruleContext.getPrerequisiteArtifact("$j2objc", TransitionMode.HOST);
Artifact j2ObjcDeployJar = ruleContext.getPrerequisiteArtifact("$j2objc", Mode.HOST);
argBuilder.addExecPath("--j2objc", j2ObjcDeployJar);

argBuilder.add("--main_class").add("com.google.devtools.j2objc.J2ObjC");
Expand Down Expand Up @@ -528,22 +533,20 @@ private static J2ObjcMappingFileProvider createJ2ObjcTranspilationAction(
Artifact compiledLibrary = ObjcRuleClasses.j2objcIntermediateArtifacts(ruleContext).archive();
argBuilder.addExecPath("--compiled_archive_file_path", compiledLibrary);

Artifact bootclasspathJar =
ruleContext.getPrerequisiteArtifact("$jre_emul_jar", TransitionMode.HOST);
Artifact bootclasspathJar = ruleContext.getPrerequisiteArtifact("$jre_emul_jar", Mode.HOST);
argBuilder.addFormatted("-Xbootclasspath:%s", bootclasspathJar);

// A valid Java system module contains 3 files. The top directory contains a file "release".
ImmutableList<Artifact> moduleFiles =
ruleContext.getPrerequisiteArtifacts("$jre_emul_module", TransitionMode.HOST).list();
ruleContext.getPrerequisiteArtifacts("$jre_emul_module", Mode.HOST).list();
for (Artifact a : moduleFiles) {
if (a.getFilename().equals("release")) {
argBuilder.add("--system", a.getDirname());
break;
}
}

Artifact deadCodeReport =
ruleContext.getPrerequisiteArtifact(":dead_code_report", TransitionMode.HOST);
Artifact deadCodeReport = ruleContext.getPrerequisiteArtifact(":dead_code_report", Mode.HOST);
if (deadCodeReport != null) {
argBuilder.addExecPath("--dead-code-report", deadCodeReport);
}
Expand All @@ -560,9 +563,8 @@ private static J2ObjcMappingFileProvider createJ2ObjcTranspilationAction(
SpawnAction.Builder transpilationAction =
new SpawnAction.Builder()
.setMnemonic("TranspilingJ2objc")
.setExecutable(
ruleContext.getPrerequisiteArtifact("$j2objc_wrapper", TransitionMode.HOST))
.addInput(ruleContext.getPrerequisiteArtifact("$j2objc_wrapper", TransitionMode.HOST))
.setExecutable(ruleContext.getPrerequisiteArtifact("$j2objc_wrapper", Mode.HOST))
.addInput(ruleContext.getPrerequisiteArtifact("$j2objc_wrapper", Mode.HOST))
.addInput(j2ObjcDeployJar)
.addInput(bootclasspathJar)
.addInputs(moduleFiles)
Expand Down Expand Up @@ -604,10 +606,8 @@ private static J2ObjcMappingFileProvider createJ2ObjcTranspilationAction(
ruleContext.registerAction(
new SpawnAction.Builder()
.setMnemonic("GenerateJ2objcHeaderMap")
.setExecutable(
ruleContext.getPrerequisiteArtifact("$j2objc_header_map", TransitionMode.HOST))
.addInput(
ruleContext.getPrerequisiteArtifact("$j2objc_header_map", TransitionMode.HOST))
.setExecutable(ruleContext.getPrerequisiteArtifact("$j2objc_header_map", Mode.HOST))
.addInput(ruleContext.getPrerequisiteArtifact("$j2objc_header_map", Mode.HOST))
.addInputs(sources)
.addInputs(sourceJars)
.addCommandLine(
Expand Down Expand Up @@ -683,7 +683,7 @@ private static void addJ2ObjCMappingsForAttribute(
String attributeName) {
if (context.attributes().has(attributeName, BuildType.LABEL_LIST)) {
for (TransitiveInfoCollection dependencyInfoDatum :
context.getPrerequisites(attributeName, TransitionMode.TARGET)) {
context.getPrerequisites(attributeName, Mode.TARGET)) {
J2ObjcMappingFileProvider provider =
dependencyInfoDatum.getProvider(J2ObjcMappingFileProvider.class);
if (provider != null) {
Expand Down Expand Up @@ -795,7 +795,7 @@ private static PathFragment getProtoOutputRoot(RuleContext ruleContext)
.getExecPath(
ruleContext
.getAnalysisEnvironment()
.getStarlarkSemantics()
.getSkylarkSemantics()
.experimentalSiblingRepositoryLayout()));
}

Expand Down Expand Up @@ -869,7 +869,7 @@ private static ObjcCommon common(

for (Attribute dependentAttribute : dependentAttributes) {
String attrName = dependentAttribute.getName();
TransitionMode attrMode = dependentAttribute.getAccessMode();
Mode attrMode = dependentAttribute.getAccessMode();
if (ruleContext.attributes().has(attrName, BuildType.LABEL_LIST)
|| ruleContext.attributes().has(attrName, BuildType.LABEL)) {
ImmutableList.Builder<CcInfo> ccInfoList = new ImmutableList.Builder<>();
Expand All @@ -886,14 +886,14 @@ private static ObjcCommon common(
}
builder.addDepCcHeaderProviders(ccInfoList.build());
builder.addDepObjcProviders(
ruleContext.getPrerequisites(attrName, attrMode, ObjcProvider.STARLARK_CONSTRUCTOR));
ruleContext.getPrerequisites(attrName, attrMode, ObjcProvider.SKYLARK_CONSTRUCTOR));
}
}

// We can't just use addDeps since that now takes ConfiguredTargetAndData and we only have
// TransitiveInfoCollections
builder.addDepObjcProviders(
otherDeps.stream().map(d -> d.get(ObjcProvider.STARLARK_CONSTRUCTOR)).collect(toList()));
otherDeps.stream().map(d -> d.get(ObjcProvider.SKYLARK_CONSTRUCTOR)).collect(toList()));
builder.addDepCcHeaderProviders(
otherDeps.stream().map(d -> d.get(CcInfo.PROVIDER)).collect(toList()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.analysis.TransitionMode;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.Type;
Expand All @@ -49,12 +49,12 @@ public class J2ObjcLibrary implements RuleConfiguredTargetFactory {

private ObjcCommon common(RuleContext ruleContext) throws InterruptedException {
List<J2ObjcCcInfo> j2objcCcInfos =
ruleContext.getPrerequisites("deps", TransitionMode.TARGET, J2ObjcCcInfo.class);
ruleContext.getPrerequisites("deps", Mode.TARGET, J2ObjcCcInfo.class);
return new ObjcCommon.Builder(ObjcCommon.Purpose.LINK_ONLY, ruleContext)
.setCompilationAttributes(
CompilationAttributes.Builder.fromRuleContext(ruleContext).build())
.addDeps(ruleContext.getPrerequisiteConfiguredTargets("deps"))
.addDeps(ruleContext.getPrerequisiteConfiguredTargets("jre_deps"))
.addDeps(ruleContext.getPrerequisiteConfiguredTargetAndTargets("deps", Mode.TARGET))
.addDeps(ruleContext.getPrerequisiteConfiguredTargetAndTargets("jre_deps", Mode.TARGET))
.addDepCcHeaderProviders(
j2objcCcInfos.stream().map(J2ObjcCcInfo::getCcInfo).collect(toList()))
.setIntermediateArtifacts(ObjcRuleClasses.intermediateArtifacts(ruleContext))
Expand All @@ -79,10 +79,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
ObjcCommon common = common(ruleContext);
ObjcProvider objcProvider = common.getObjcProviderBuilder().build();

J2ObjcMappingFileProvider j2ObjcMappingFileProvider =
J2ObjcMappingFileProvider.union(
ruleContext.getPrerequisites(
"deps", TransitionMode.TARGET, J2ObjcMappingFileProvider.class));
J2ObjcMappingFileProvider j2ObjcMappingFileProvider = J2ObjcMappingFileProvider.union(
ruleContext.getPrerequisites("deps", Mode.TARGET, J2ObjcMappingFileProvider.class));

CompilationArtifacts moduleMapCompilationArtifacts =
new CompilationArtifacts.Builder()
Expand Down Expand Up @@ -110,7 +108,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
CcInfo.builder()
.setCcCompilationContext(objcProvider.getCcCompilationContext())
.build())
.addStarlarkTransitiveInfo(ObjcProvider.STARLARK_NAME, objcProvider)
.addSkylarkTransitiveInfo(ObjcProvider.SKYLARK_NAME, objcProvider)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.collect.nestedset.NestedSetExpander;
import com.google.devtools.build.lib.packages.NativeAspectClass;
import com.google.devtools.build.lib.rules.apple.ApplePlatform.PlatformType;
import com.google.devtools.build.lib.rules.apple.AppleToolchain;
Expand Down Expand Up @@ -803,8 +802,7 @@ public void testJ2ObjCCustomModuleMap() throws Exception {
/*topLevelFilesets=*/ ImmutableMap.of(),
DUMMY_ARTIFACT_EXPANDER,
/*actionFileSystem=*/ null,
/*skyframeDepsResult=*/ null,
NestedSetExpander.DEFAULT);
/*skyframeDepsResult=*/ null);
ByteArrayOutputStream moduleMapStream = new ByteArrayOutputStream();
ByteArrayOutputStream umbrellaHeaderStream = new ByteArrayOutputStream();
moduleMapAction.newDeterministicWriter(dummyActionExecutionContext)
Expand Down Expand Up @@ -857,8 +855,7 @@ public void testModuleMapFromGenJarTreeArtifact() throws Exception {
/*topLevelFilesets=*/ ImmutableMap.of(),
DUMMY_ARTIFACT_EXPANDER,
/*actionFileSystem=*/ null,
/*skyframeDepsResult=*/ null,
NestedSetExpander.DEFAULT);
/*skyframeDepsResult=*/ null);

ByteArrayOutputStream moduleMapStream = new ByteArrayOutputStream();
ByteArrayOutputStream umbrellaHeaderStream = new ByteArrayOutputStream();
Expand Down

0 comments on commit 7128519

Please sign in to comment.