@@ -87,7 +87,9 @@ public CcLinkingOutputs getCcLinkingOutputs() {
87
87
private final BuildConfiguration configuration ;
88
88
private final CppConfiguration cppConfiguration ;
89
89
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 <>();
91
93
private final List <String > linkopts = new ArrayList <>();
92
94
private final List <CcLinkingContext > ccLinkingContexts = new ArrayList <>();
93
95
private final NestedSetBuilder <Artifact > linkstamps = NestedSetBuilder .stableOrder ();
@@ -96,6 +98,7 @@ public CcLinkingOutputs getCcLinkingOutputs() {
96
98
@ Nullable private Artifact linkerOutputArtifact ;
97
99
private LinkTargetType staticLinkType = LinkTargetType .STATIC_LIBRARY ;
98
100
private LinkTargetType dynamicLinkType = LinkTargetType .NODEPS_DYNAMIC_LIBRARY ;
101
+ private NestedSet <Artifact > additionalLinkerInputs ;
99
102
private boolean neverlink ;
100
103
101
104
private boolean emitInterfaceSharedLibraries ;
@@ -194,19 +197,27 @@ public CcLinkingHelper addAdditionalLinkstampDefines(List<String> additionalLink
194
197
return this ;
195
198
}
196
199
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
+ */
198
207
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 );
210
221
return this ;
211
222
}
212
223
@@ -361,6 +372,9 @@ public CcLinkingOutputs link(CcCompilationOutputs ccOutputs)
361
372
throws RuleErrorException , InterruptedException {
362
373
Preconditions .checkNotNull (ccOutputs );
363
374
375
+ Preconditions .checkState (additionalLinkerInputs == null );
376
+ additionalLinkerInputs = additionalLinkerInputsBuilder .build ();
377
+
364
378
// Create link actions (only if there are object files or if explicitly requested).
365
379
//
366
380
// On some systems, the linker gives an error message if there are no input files. Even with
@@ -401,7 +415,8 @@ public CcLinkingContext buildCcLinkingContextFromLibrariesToLink(
401
415
CcLinkingContext .LinkOptions .of (
402
416
ImmutableList .copyOf (linkopts ), symbolGenerator )))
403
417
.addLibraries (librariesToLink )
404
- .addNonCodeInputs (nonCodeLinkerInputs )
418
+ // additionalLinkerInputsBuilder not expected to be a big list for now.
419
+ .addNonCodeInputs (additionalLinkerInputsBuilder .build ().toList ())
405
420
.addLinkstamps (linkstampBuilder .build ())
406
421
.build ();
407
422
}
@@ -629,7 +644,6 @@ private CppLinkAction registerActionForStaticLibrary(
629
644
CppLinkAction action =
630
645
newLinkActionBuilder (linkedArtifact , linkTargetTypeUsedForNaming )
631
646
.addObjectFiles (ccOutputs .getObjectFiles (usePic ))
632
- .addNonCodeInputs (nonCodeLinkerInputs )
633
647
.addLtoCompilationContext (ccOutputs .getLtoCompilationContext ())
634
648
.setUsePicForLtoBackendActions (usePic )
635
649
.setLinkingMode (LinkingMode .STATIC )
@@ -694,7 +708,6 @@ private boolean createDynamicLinkAction(
694
708
.addActionInputs (linkActionInputs )
695
709
.addLinkopts (linkopts )
696
710
.addLinkopts (sonameLinkopts )
697
- .addNonCodeInputs (nonCodeLinkerInputs )
698
711
.addVariablesExtensions (variablesExtensions );
699
712
700
713
dynamicLinkActionBuilder .addObjectFiles (ccOutputs .getObjectFiles (usePic ));
@@ -829,28 +842,43 @@ private boolean createDynamicLinkAction(
829
842
830
843
private CppLinkActionBuilder newLinkActionBuilder (
831
844
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 ;
854
882
}
855
883
856
884
/**
@@ -876,6 +904,15 @@ private Artifact getLinkedArtifact(LinkTargetType linkTargetType) throws RuleErr
876
904
linkedName =
877
905
CppHelper .getArtifactNameForCategory (
878
906
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
+ }
879
916
PathFragment artifactFragment =
880
917
PathFragment .create (label .getName ()).getParentDirectory ().getRelative (linkedName );
881
918
0 commit comments