Skip to content

Commit a8f6152

Browse files
oquenchilcopybara-github
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. PiperOrigin-RevId: 352585170
1 parent 2074946 commit a8f6152

File tree

4 files changed

+179
-107
lines changed

4 files changed

+179
-107
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public Builder addUserLinkFlags(List<LinkOptions> userLinkFlags) {
566566
return this;
567567
}
568568

569-
Builder addLinkstamps(List<Linkstamp> linkstamps) {
569+
public Builder addLinkstamps(List<Linkstamp> linkstamps) {
570570
hasDirectLinkerInput = true;
571571
linkerInputBuilder.addLinkstamps(linkstamps);
572572
return this;

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

+75-38
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ public CcLinkingOutputs getCcLinkingOutputs() {
8787
private final BuildConfiguration configuration;
8888
private final CppConfiguration cppConfiguration;
8989

90-
private final List<Artifact> nonCodeLinkerInputs = new ArrayList<>();
90+
private final NestedSetBuilder<Artifact> additionalLinkerInputsBuilder =
91+
NestedSetBuilder.stableOrder();
92+
private final List<Artifact> linkerOutputs = new ArrayList<>();
9193
private final List<String> linkopts = new ArrayList<>();
9294
private final List<CcLinkingContext> ccLinkingContexts = new ArrayList<>();
9395
private final NestedSetBuilder<Artifact> linkstamps = NestedSetBuilder.stableOrder();
@@ -96,6 +98,7 @@ public CcLinkingOutputs getCcLinkingOutputs() {
9698
@Nullable private Artifact linkerOutputArtifact;
9799
private LinkTargetType staticLinkType = LinkTargetType.STATIC_LIBRARY;
98100
private LinkTargetType dynamicLinkType = LinkTargetType.NODEPS_DYNAMIC_LIBRARY;
101+
private NestedSet<Artifact> additionalLinkerInputs;
99102
private boolean neverlink;
100103

101104
private boolean emitInterfaceSharedLibraries;
@@ -194,19 +197,27 @@ public CcLinkingHelper addAdditionalLinkstampDefines(List<String> additionalLink
194197
return this;
195198
}
196199

197-
/** Adds the corresponding non-code files as linker inputs. */
200+
/**
201+
* Adds the corresponding non-code files as linker inputs.
202+
*
203+
* <p>TODO(bazel-team): There is no practical difference in non-code inputs and additional linker
204+
* inputs in CppLinkActionBuilder. So these should be merged. Even before that happens, it's
205+
* totally fine for nonCodeLinkerInputs to contains precompiled libraries.
206+
*/
198207
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-
}
208+
this.additionalLinkerInputsBuilder.addAll(nonCodeLinkerInputs);
209+
return this;
210+
}
211+
212+
public CcLinkingHelper addTransitiveAdditionalLinkerInputs(
213+
NestedSet<Artifact> additionalLinkerInputs) {
214+
this.additionalLinkerInputsBuilder.addTransitive(additionalLinkerInputs);
215+
return this;
216+
}
217+
218+
/** TODO(bazel-team): Add to Starlark API */
219+
public CcLinkingHelper addLinkerOutputs(List<Artifact> linkerOutputs) {
220+
this.linkerOutputs.addAll(linkerOutputs);
210221
return this;
211222
}
212223

@@ -361,6 +372,9 @@ public CcLinkingOutputs link(CcCompilationOutputs ccOutputs)
361372
throws RuleErrorException, InterruptedException {
362373
Preconditions.checkNotNull(ccOutputs);
363374

375+
Preconditions.checkState(additionalLinkerInputs == null);
376+
additionalLinkerInputs = additionalLinkerInputsBuilder.build();
377+
364378
// Create link actions (only if there are object files or if explicitly requested).
365379
//
366380
// On some systems, the linker gives an error message if there are no input files. Even with
@@ -401,7 +415,8 @@ public CcLinkingContext buildCcLinkingContextFromLibrariesToLink(
401415
CcLinkingContext.LinkOptions.of(
402416
ImmutableList.copyOf(linkopts), symbolGenerator)))
403417
.addLibraries(librariesToLink)
404-
.addNonCodeInputs(nonCodeLinkerInputs)
418+
// additionalLinkerInputsBuilder not expected to be a big list for now.
419+
.addNonCodeInputs(additionalLinkerInputsBuilder.build().toList())
405420
.addLinkstamps(linkstampBuilder.build())
406421
.build();
407422
}
@@ -629,7 +644,6 @@ private CppLinkAction registerActionForStaticLibrary(
629644
CppLinkAction action =
630645
newLinkActionBuilder(linkedArtifact, linkTargetTypeUsedForNaming)
631646
.addObjectFiles(ccOutputs.getObjectFiles(usePic))
632-
.addNonCodeInputs(nonCodeLinkerInputs)
633647
.addLtoCompilationContext(ccOutputs.getLtoCompilationContext())
634648
.setUsePicForLtoBackendActions(usePic)
635649
.setLinkingMode(LinkingMode.STATIC)
@@ -694,7 +708,6 @@ private boolean createDynamicLinkAction(
694708
.addActionInputs(linkActionInputs)
695709
.addLinkopts(linkopts)
696710
.addLinkopts(sonameLinkopts)
697-
.addNonCodeInputs(nonCodeLinkerInputs)
698711
.addVariablesExtensions(variablesExtensions);
699712

700713
dynamicLinkActionBuilder.addObjectFiles(ccOutputs.getObjectFiles(usePic));
@@ -829,28 +842,43 @@ private boolean createDynamicLinkAction(
829842

830843
private CppLinkActionBuilder newLinkActionBuilder(
831844
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);
845+
if (!additionalLinkerInputsBuilder.isEmpty()) {
846+
if (fdoContext.getPropellerOptimizeInputFile() != null
847+
&& fdoContext.getPropellerOptimizeInputFile().getLdArtifact() != null) {
848+
this.additionalLinkerInputsBuilder.add(
849+
fdoContext.getPropellerOptimizeInputFile().getLdArtifact());
850+
}
851+
}
852+
CppLinkActionBuilder builder =
853+
new CppLinkActionBuilder(
854+
ruleErrorConsumer,
855+
actionConstructionContext,
856+
label,
857+
outputArtifact,
858+
configuration,
859+
ccToolchain,
860+
fdoContext,
861+
featureConfiguration,
862+
semantics)
863+
.setGrepIncludes(grepIncludes)
864+
.setMnemonic(
865+
featureConfiguration.isEnabled(CppRuleClasses.LANG_OBJC) ? "ObjcLink" : null)
866+
.setIsStampingEnabled(isStampingEnabled)
867+
.setTestOrTestOnlyTarget(isTestOrTestOnlyTarget)
868+
.setLinkType(linkType)
869+
.setLinkerFiles(
870+
(cppConfiguration.useSpecificToolFiles()
871+
&& linkType.linkerOrArchiver() == LinkerOrArchiver.ARCHIVER)
872+
? ccToolchain.getArFiles()
873+
: ccToolchain.getLinkerFiles())
874+
.setLinkArtifactFactory(linkArtifactFactory)
875+
.setUseTestOnlyFlags(useTestOnlyFlags)
876+
.addTransitiveActionInputs(additionalLinkerInputs)
877+
.addExecutionInfo(executionInfo);
878+
for (Artifact output : linkerOutputs) {
879+
builder.addActionOutput(output);
880+
}
881+
return builder;
854882
}
855883

856884
/**
@@ -876,6 +904,15 @@ private Artifact getLinkedArtifact(LinkTargetType linkTargetType) throws RuleErr
876904
linkedName =
877905
CppHelper.getArtifactNameForCategory(
878906
ruleErrorConsumer, ccToolchain, linkTargetType.getLinkerOutput(), linkedName);
907+
if (linkTargetType.equals(LinkTargetType.OBJC_FULLY_LINKED_ARCHIVE)) {
908+
// TODO(blaze-team): This unfortunate editing of the name is here bedcause Objective-C rules
909+
// were creating this type of archive without the lib prefix, unlike what the objective-c
910+
// toolchain says with getArtifactNameForCategory.
911+
// This can be fixed either when implicit outputs are removed from objc_library by keeping the
912+
// lib prefix, or by editing the toolchain not to add it.
913+
Preconditions.checkState(linkedName.startsWith("lib"));
914+
linkedName = linkedName.substring(3);
915+
}
879916
PathFragment artifactFragment =
880917
PathFragment.create(label.getName()).getParentDirectory().getRelative(linkedName);
881918

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)