Skip to content

Commit ba74df0

Browse files
oquenchilkatre
authored andcommitted
Refactors CompilationSupport for objc to use existing API
This CL makes CompilationSupport use CcLinkingHelper instead of CppLinkActionBuilder. The former is the Java class used by the existing Starlark linking API. This is in preparation for a future (unknown when) re-write of objc rules to Starlark. This is a rollforward after fixing an issue with implicit outputs of proto_libraries which had the j2objc_aspect applied on it. These artifacts were created with the genfiles dir while the C++ sandwich was always creating them in the bin dir. RELNOTES:none PiperOrigin-RevId: 353879792 (cherry picked from commit 31b689b)
1 parent f4e1036 commit ba74df0

File tree

5 files changed

+196
-110
lines changed

5 files changed

+196
-110
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public Builder addUserLinkFlags(List<LinkOptions> userLinkFlags) {
526526
return this;
527527
}
528528

529-
Builder addLinkstamps(List<Linkstamp> linkstamps) {
529+
public Builder addLinkstamps(List<Linkstamp> linkstamps) {
530530
hasDirectLinkerInput = true;
531531
linkerInputBuilder.addLinkstamps(linkstamps);
532532
return this;

src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingHelper.java

+83-38
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.collect.Iterables;
2222
import com.google.devtools.build.lib.actions.ActionRegistry;
2323
import com.google.devtools.build.lib.actions.Artifact;
24+
import com.google.devtools.build.lib.actions.ArtifactRoot;
2425
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
2526
import com.google.devtools.build.lib.analysis.FileProvider;
2627
import com.google.devtools.build.lib.analysis.RuleContext;
@@ -87,7 +88,9 @@ public CcLinkingOutputs getCcLinkingOutputs() {
8788
private final BuildConfiguration configuration;
8889
private final CppConfiguration cppConfiguration;
8990

90-
private final List<Artifact> nonCodeLinkerInputs = new ArrayList<>();
91+
private final NestedSetBuilder<Artifact> additionalLinkerInputsBuilder =
92+
NestedSetBuilder.stableOrder();
93+
private final List<Artifact> linkerOutputs = new ArrayList<>();
9194
private final List<String> linkopts = new ArrayList<>();
9295
private final List<CcLinkingContext> ccLinkingContexts = new ArrayList<>();
9396
private final NestedSetBuilder<Artifact> linkstamps = NestedSetBuilder.stableOrder();
@@ -96,6 +99,7 @@ public CcLinkingOutputs getCcLinkingOutputs() {
9699
@Nullable private Artifact linkerOutputArtifact;
97100
private LinkTargetType staticLinkType = LinkTargetType.STATIC_LIBRARY;
98101
private LinkTargetType dynamicLinkType = LinkTargetType.NODEPS_DYNAMIC_LIBRARY;
102+
private NestedSet<Artifact> additionalLinkerInputs;
99103
private boolean neverlink;
100104

101105
private boolean emitInterfaceSharedLibraries;
@@ -194,19 +198,27 @@ public CcLinkingHelper addAdditionalLinkstampDefines(List<String> additionalLink
194198
return this;
195199
}
196200

197-
/** Adds the corresponding non-code files as linker inputs. */
201+
/**
202+
* Adds the corresponding non-code files as linker inputs.
203+
*
204+
* <p>TODO(bazel-team): There is no practical difference in non-code inputs and additional linker
205+
* inputs in CppLinkActionBuilder. So these should be merged. Even before that happens, it's
206+
* totally fine for nonCodeLinkerInputs to contains precompiled libraries.
207+
*/
198208
public CcLinkingHelper addNonCodeLinkerInputs(List<Artifact> nonCodeLinkerInputs) {
199-
for (Artifact nonCodeLinkerInput : nonCodeLinkerInputs) {
200-
String basename = nonCodeLinkerInput.getFilename();
201-
Preconditions.checkArgument(!Link.OBJECT_FILETYPES.matches(basename));
202-
Preconditions.checkArgument(!Link.ARCHIVE_LIBRARY_FILETYPES.matches(basename));
203-
Preconditions.checkArgument(!Link.SHARED_LIBRARY_FILETYPES.matches(basename));
204-
this.nonCodeLinkerInputs.add(nonCodeLinkerInput);
205-
}
206-
if (fdoContext.getPropellerOptimizeInputFile() != null
207-
&& fdoContext.getPropellerOptimizeInputFile().getLdArtifact() != null) {
208-
this.nonCodeLinkerInputs.add(fdoContext.getPropellerOptimizeInputFile().getLdArtifact());
209-
}
209+
this.additionalLinkerInputsBuilder.addAll(nonCodeLinkerInputs);
210+
return this;
211+
}
212+
213+
public CcLinkingHelper addTransitiveAdditionalLinkerInputs(
214+
NestedSet<Artifact> additionalLinkerInputs) {
215+
this.additionalLinkerInputsBuilder.addTransitive(additionalLinkerInputs);
216+
return this;
217+
}
218+
219+
/** TODO(bazel-team): Add to Starlark API */
220+
public CcLinkingHelper addLinkerOutputs(List<Artifact> linkerOutputs) {
221+
this.linkerOutputs.addAll(linkerOutputs);
210222
return this;
211223
}
212224

@@ -361,6 +373,9 @@ public CcLinkingOutputs link(CcCompilationOutputs ccOutputs)
361373
throws RuleErrorException, InterruptedException {
362374
Preconditions.checkNotNull(ccOutputs);
363375

376+
Preconditions.checkState(additionalLinkerInputs == null);
377+
additionalLinkerInputs = additionalLinkerInputsBuilder.build();
378+
364379
// Create link actions (only if there are object files or if explicitly requested).
365380
//
366381
// On some systems, the linker gives an error message if there are no input files. Even with
@@ -401,7 +416,8 @@ public CcLinkingContext buildCcLinkingContextFromLibrariesToLink(
401416
CcLinkingContext.LinkOptions.of(
402417
ImmutableList.copyOf(linkopts), symbolGenerator)))
403418
.addLibraries(librariesToLink)
404-
.addNonCodeInputs(nonCodeLinkerInputs)
419+
// additionalLinkerInputsBuilder not expected to be a big list for now.
420+
.addNonCodeInputs(additionalLinkerInputsBuilder.build().toList())
405421
.addLinkstamps(linkstampBuilder.build())
406422
.build();
407423
}
@@ -629,7 +645,6 @@ private CppLinkAction registerActionForStaticLibrary(
629645
CppLinkAction action =
630646
newLinkActionBuilder(linkedArtifact, linkTargetTypeUsedForNaming)
631647
.addObjectFiles(ccOutputs.getObjectFiles(usePic))
632-
.addNonCodeInputs(nonCodeLinkerInputs)
633648
.addLtoCompilationContext(ccOutputs.getLtoCompilationContext())
634649
.setUsePicForLtoBackendActions(usePic)
635650
.setLinkingMode(LinkingMode.STATIC)
@@ -694,7 +709,6 @@ private boolean createDynamicLinkAction(
694709
.addActionInputs(linkActionInputs)
695710
.addLinkopts(linkopts)
696711
.addLinkopts(sonameLinkopts)
697-
.addNonCodeInputs(nonCodeLinkerInputs)
698712
.addVariablesExtensions(variablesExtensions);
699713

700714
dynamicLinkActionBuilder.addObjectFiles(ccOutputs.getObjectFiles(usePic));
@@ -829,28 +843,43 @@ private boolean createDynamicLinkAction(
829843

830844
private CppLinkActionBuilder newLinkActionBuilder(
831845
Artifact outputArtifact, LinkTargetType linkType) {
832-
return new CppLinkActionBuilder(
833-
ruleErrorConsumer,
834-
actionConstructionContext,
835-
label,
836-
outputArtifact,
837-
configuration,
838-
ccToolchain,
839-
fdoContext,
840-
featureConfiguration,
841-
semantics)
842-
.setGrepIncludes(grepIncludes)
843-
.setIsStampingEnabled(isStampingEnabled)
844-
.setTestOrTestOnlyTarget(isTestOrTestOnlyTarget)
845-
.setLinkType(linkType)
846-
.setLinkerFiles(
847-
(cppConfiguration.useSpecificToolFiles()
848-
&& linkType.linkerOrArchiver() == LinkerOrArchiver.ARCHIVER)
849-
? ccToolchain.getArFiles()
850-
: ccToolchain.getLinkerFiles())
851-
.setLinkArtifactFactory(linkArtifactFactory)
852-
.setUseTestOnlyFlags(useTestOnlyFlags)
853-
.addExecutionInfo(executionInfo);
846+
if (!additionalLinkerInputsBuilder.isEmpty()) {
847+
if (fdoContext.getPropellerOptimizeInputFile() != null
848+
&& fdoContext.getPropellerOptimizeInputFile().getLdArtifact() != null) {
849+
this.additionalLinkerInputsBuilder.add(
850+
fdoContext.getPropellerOptimizeInputFile().getLdArtifact());
851+
}
852+
}
853+
CppLinkActionBuilder builder =
854+
new CppLinkActionBuilder(
855+
ruleErrorConsumer,
856+
actionConstructionContext,
857+
label,
858+
outputArtifact,
859+
configuration,
860+
ccToolchain,
861+
fdoContext,
862+
featureConfiguration,
863+
semantics)
864+
.setGrepIncludes(grepIncludes)
865+
.setMnemonic(
866+
featureConfiguration.isEnabled(CppRuleClasses.LANG_OBJC) ? "ObjcLink" : null)
867+
.setIsStampingEnabled(isStampingEnabled)
868+
.setTestOrTestOnlyTarget(isTestOrTestOnlyTarget)
869+
.setLinkType(linkType)
870+
.setLinkerFiles(
871+
(cppConfiguration.useSpecificToolFiles()
872+
&& linkType.linkerOrArchiver() == LinkerOrArchiver.ARCHIVER)
873+
? ccToolchain.getArFiles()
874+
: ccToolchain.getLinkerFiles())
875+
.setLinkArtifactFactory(linkArtifactFactory)
876+
.setUseTestOnlyFlags(useTestOnlyFlags)
877+
.addTransitiveActionInputs(additionalLinkerInputs)
878+
.addExecutionInfo(executionInfo);
879+
for (Artifact output : linkerOutputs) {
880+
builder.addActionOutput(output);
881+
}
882+
return builder;
854883
}
855884

856885
/**
@@ -876,12 +905,28 @@ private Artifact getLinkedArtifact(LinkTargetType linkTargetType) throws RuleErr
876905
linkedName =
877906
CppHelper.getArtifactNameForCategory(
878907
ruleErrorConsumer, ccToolchain, linkTargetType.getLinkerOutput(), linkedName);
908+
909+
ArtifactRoot artifactRoot = configuration.getBinDirectory(label.getRepository());
910+
if (linkTargetType.equals(LinkTargetType.OBJC_FULLY_LINKED_ARCHIVE)) {
911+
// TODO(blaze-team): This unfortunate editing of the name is here bedcause Objective-C rules
912+
// were creating this type of archive without the lib prefix, unlike what the objective-c
913+
// toolchain says with getArtifactNameForCategory.
914+
// This can be fixed either when implicit outputs are removed from objc_library by keeping the
915+
// lib prefix, or by editing the toolchain not to add it.
916+
Preconditions.checkState(linkedName.startsWith("lib"));
917+
linkedName = linkedName.substring(3);
918+
artifactRoot =
919+
((RuleContext) actionConstructionContext).getRule().hasBinaryOutput()
920+
? configuration.getBinDir()
921+
: configuration.getGenfilesDir();
922+
}
879923
PathFragment artifactFragment =
880924
PathFragment.create(label.getName()).getParentDirectory().getRelative(linkedName);
881925

882926
return CppHelper.getLinkedArtifact(
883927
label,
884928
actionConstructionContext,
929+
artifactRoot,
885930
configuration,
886931
linkTargetType,
887932
linkedArtifactNameSuffix,

src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.devtools.build.lib.actions.ActionOwner;
3232
import com.google.devtools.build.lib.actions.Artifact;
3333
import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
34+
import com.google.devtools.build.lib.actions.ArtifactRoot;
3435
import com.google.devtools.build.lib.actions.FailAction;
3536
import com.google.devtools.build.lib.actions.MiddlemanFactory;
3637
import com.google.devtools.build.lib.actions.ParamFileInfo;
@@ -473,19 +474,24 @@ public static Artifact getLinkedArtifact(
473474
}
474475

475476
return getLinkedArtifact(
476-
ruleContext.getLabel(), ruleContext, config, linkType, linkedArtifactNameSuffix, name);
477+
ruleContext.getLabel(),
478+
ruleContext,
479+
ruleContext.getBinDirectory(),
480+
config,
481+
linkType,
482+
linkedArtifactNameSuffix,
483+
name);
477484
}
478485

479486
public static Artifact getLinkedArtifact(
480487
Label label,
481488
ActionConstructionContext actionConstructionContext,
489+
ArtifactRoot artifactRoot,
482490
BuildConfiguration config,
483491
LinkTargetType linkType,
484492
String linkedArtifactNameSuffix,
485493
PathFragment name) {
486-
Artifact result =
487-
actionConstructionContext.getPackageRelativeArtifact(
488-
name, config.getBinDirectory(label.getRepository()));
494+
Artifact result = actionConstructionContext.getPackageRelativeArtifact(name, artifactRoot);
489495

490496
// If the linked artifact is not the linux default, then a FailAction is generated for said
491497
// linux default to satisfy the requirements of any implicit outputs.
@@ -507,7 +513,7 @@ public static Artifact getLinkedArtifact(
507513
return result;
508514
}
509515

510-
public static Artifact getLinuxLinkedArtifact(
516+
private static Artifact getLinuxLinkedArtifact(
511517
Label label,
512518
ActionConstructionContext actionConstructionContext,
513519
BuildConfiguration config,

src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ public CppLinkActionBuilder addObjectFiles(Iterable<Artifact> inputs) {
13151315
* Adds non-code files to the set of inputs. They will not be passed to the linker command line
13161316
* unless that is explicitly modified, too.
13171317
*/
1318+
// TOOD: Remove and just use method for addLinkerInputs
13181319
public CppLinkActionBuilder addNonCodeInputs(Iterable<Artifact> inputs) {
13191320
for (Artifact input : inputs) {
13201321
addNonCodeInput(input);
@@ -1328,11 +1329,6 @@ public CppLinkActionBuilder addNonCodeInputs(Iterable<Artifact> inputs) {
13281329
* line unless that is explicitly modified, too.
13291330
*/
13301331
public CppLinkActionBuilder addNonCodeInput(Artifact input) {
1331-
String basename = input.getFilename();
1332-
Preconditions.checkArgument(!Link.ARCHIVE_LIBRARY_FILETYPES.matches(basename), basename);
1333-
Preconditions.checkArgument(!Link.SHARED_LIBRARY_FILETYPES.matches(basename), basename);
1334-
Preconditions.checkArgument(!Link.OBJECT_FILETYPES.matches(basename), basename);
1335-
13361332
this.nonCodeInputs.add(input);
13371333
return this;
13381334
}

0 commit comments

Comments
 (0)