/
CcLinkingHelper.java
1032 lines (946 loc) · 44.7 KB
/
CcLinkingHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.rules.cpp;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.ActionRegistry;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ArtifactRoot;
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RuleErrorConsumer;
import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
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;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.SymbolGenerator;
import com.google.devtools.build.lib.rules.cpp.CcLinkingContext.Linkstamp;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.VariablesExtension;
import com.google.devtools.build.lib.rules.cpp.CppLinkAction.LinkArtifactFactory;
import com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType;
import com.google.devtools.build.lib.rules.cpp.Link.LinkerOrArchiver;
import com.google.devtools.build.lib.rules.cpp.Link.LinkingMode;
import com.google.devtools.build.lib.rules.cpp.Link.Picness;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.LinkingInfoApi;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
/**
* A class to create C/C++ link actions in a way that is consistent with cc_library. Rules that
* generate source files and emulate cc_library on top of that should use this class instead of the
* lower-level APIs in CppHelper and CppLinkActionBuilder.
*
* <p>Rules that want to use this class are required to have implicit dependencies on the toolchain,
* the STL, and so on. Optionally, they can also have copts, and malloc attributes, but note that
* these require explicit calls to the corresponding setter methods.
*/
public final class CcLinkingHelper {
/** Contains the providers as well as the linking outputs. */
// TODO(plf): Only used by Starlark API. Remove after migrating.
@Deprecated
public static final class LinkingInfo implements LinkingInfoApi {
private final CcLinkingContext ccLinkingContext;
private final CcLinkingOutputs linkingOutputs;
public LinkingInfo(CcLinkingContext ccLinkingContext, CcLinkingOutputs linkingOutputs) {
this.ccLinkingContext = ccLinkingContext;
this.linkingOutputs = linkingOutputs;
}
@Override
public CcLinkingContext getCcLinkingContext() {
return ccLinkingContext;
}
@Override
public CcLinkingOutputs getCcLinkingOutputs() {
return linkingOutputs;
}
}
private final CppSemantics semantics;
private final BuildConfigurationValue configuration;
private final CppConfiguration cppConfiguration;
private final NestedSetBuilder<Artifact> additionalLinkerInputsBuilder =
NestedSetBuilder.stableOrder();
private final List<Artifact> linkerOutputs = new ArrayList<>();
private final List<String> linkopts = new ArrayList<>();
private final List<CcLinkingContext> ccLinkingContexts = new ArrayList<>();
private final NestedSetBuilder<Artifact> linkstamps = NestedSetBuilder.stableOrder();
private final List<Artifact> linkActionInputs = new ArrayList<>();
@Nullable private Artifact linkerOutputArtifact;
private LinkTargetType staticLinkType = LinkTargetType.STATIC_LIBRARY;
private LinkTargetType dynamicLinkType = LinkTargetType.NODEPS_DYNAMIC_LIBRARY;
private NestedSet<Artifact> additionalLinkerInputs;
private boolean neverlink;
private boolean emitInterfaceSharedLibraries;
private boolean shouldCreateDynamicLibrary = true;
private boolean shouldCreateStaticLibraries = true;
private boolean willOnlyBeLinkedIntoDynamicLibraries;
private final List<VariablesExtension> variablesExtensions = new ArrayList<>();
private boolean useTestOnlyFlags;
private Artifact pdbFile;
private Artifact defFile;
private LinkingMode linkingMode = LinkingMode.DYNAMIC;
private boolean nativeDeps;
private boolean wholeArchive;
private LinkArtifactFactory linkArtifactFactory = CppLinkAction.DEFAULT_ARTIFACT_FACTORY;
private final ImmutableList.Builder<String> additionalLinkstampDefines = ImmutableList.builder();
private final FeatureConfiguration featureConfiguration;
private final CcToolchainProvider ccToolchain;
private final FdoContext fdoContext;
private String linkedArtifactNameSuffix = "";
private String linkedDLLNameSuffix = "";
private final ActionConstructionContext actionConstructionContext;
private final Label label;
private final ActionRegistry actionRegistry;
private final RuleErrorConsumer ruleErrorConsumer;
private final SymbolGenerator<?> symbolGenerator;
private final ImmutableMap<String, String> executionInfo;
private boolean isStampingEnabled;
private boolean isTestOrTestOnlyTarget;
/**
* Creates a CcLinkingHelper that outputs artifacts in a given configuration.
*
* @param ruleErrorConsumer the RuleErrorConsumer
* @param label the Label of the rule being built
* @param actionRegistry the ActionRegistry of the rule being built
* @param actionConstructionContext the ActionConstructionContext of the rule being built
* @param semantics CppSemantics for the build
* @param featureConfiguration activated features and action configs for the build
* @param ccToolchain the C++ toolchain provider for the build
* @param fdoContext the C++ FDO optimization support provider for the build
* @param configuration the configuration that gives the directory of output artifacts
* @param executionInfo the execution info data associated with a rule
*/
public CcLinkingHelper(
RuleErrorConsumer ruleErrorConsumer,
Label label,
ActionRegistry actionRegistry,
ActionConstructionContext actionConstructionContext,
CppSemantics semantics,
FeatureConfiguration featureConfiguration,
CcToolchainProvider ccToolchain,
FdoContext fdoContext,
BuildConfigurationValue configuration,
CppConfiguration cppConfiguration,
SymbolGenerator<?> symbolGenerator,
ImmutableMap<String, String> executionInfo) {
this.semantics = Preconditions.checkNotNull(semantics);
this.featureConfiguration = Preconditions.checkNotNull(featureConfiguration);
this.ccToolchain = Preconditions.checkNotNull(ccToolchain);
this.fdoContext = Preconditions.checkNotNull(fdoContext);
this.configuration = Preconditions.checkNotNull(configuration);
this.cppConfiguration = cppConfiguration;
this.ruleErrorConsumer = ruleErrorConsumer;
this.label = label;
this.actionRegistry = actionRegistry;
this.actionConstructionContext = actionConstructionContext;
this.symbolGenerator = symbolGenerator;
this.executionInfo = executionInfo;
}
@CanIgnoreReturnValue
public CcLinkingHelper setNativeDeps(boolean nativeDeps) {
this.nativeDeps = nativeDeps;
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper setWholeArchive(boolean wholeArchive) {
this.wholeArchive = wholeArchive;
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper addAdditionalLinkstampDefines(List<String> additionalLinkstampDefines) {
this.additionalLinkstampDefines.addAll(additionalLinkstampDefines);
return this;
}
/**
* Adds the corresponding non-code files as linker inputs.
*
* <p>TODO(bazel-team): There is no practical difference in non-code inputs and additional linker
* inputs in CppLinkActionBuilder. So these should be merged. Even before that happens, it's
* totally fine for nonCodeLinkerInputs to contains precompiled libraries.
*/
@CanIgnoreReturnValue
public CcLinkingHelper addNonCodeLinkerInputs(List<Artifact> nonCodeLinkerInputs) {
this.additionalLinkerInputsBuilder.addAll(nonCodeLinkerInputs);
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper addTransitiveAdditionalLinkerInputs(
NestedSet<Artifact> additionalLinkerInputs) {
this.additionalLinkerInputsBuilder.addTransitive(additionalLinkerInputs);
return this;
}
/** TODO(bazel-team): Add to Starlark API */
@CanIgnoreReturnValue
public CcLinkingHelper addLinkerOutputs(List<Artifact> linkerOutputs) {
this.linkerOutputs.addAll(linkerOutputs);
return this;
}
/** Adds the given options as linker options to the link command. */
@CanIgnoreReturnValue
public CcLinkingHelper addLinkopts(Iterable<String> linkopts) {
Iterables.addAll(this.linkopts, linkopts);
return this;
}
/**
* Adds additional {@link CcLinkingContext} that will be used everywhere where CcLinkingInfos were
* obtained from deps.
*/
@CanIgnoreReturnValue
public CcLinkingHelper addCcLinkingContexts(Iterable<CcLinkingContext> ccLinkingContexts) {
Iterables.addAll(this.ccLinkingContexts, ccLinkingContexts);
return this;
}
/** Adds a variableExtension to template the crosstool. */
@CanIgnoreReturnValue
public CcLinkingHelper addVariableExtension(VariablesExtension variableExtension) {
Preconditions.checkNotNull(variableExtension);
this.variablesExtensions.add(variableExtension);
return this;
}
/**
* Overrides the path for the generated dynamic library - this should only be called if the
* dynamic library is an implicit or explicit output of the rule, i.e., if it is accessible by
* name from other rules in the same package. Set to {@code null} to use the default computation.
*/
@CanIgnoreReturnValue
public CcLinkingHelper setLinkerOutputArtifact(@Nullable Artifact linkerOutputArtifact) {
this.linkerOutputArtifact = linkerOutputArtifact;
return this;
}
/**
* Marks the output of this rule as alwayslink, i.e., the corresponding symbols will be retained
* by the linker even if they are not otherwise used. This is useful for libraries that register
* themselves somewhere during initialization.
*
* <p>This only sets the link type (see {@link #setStaticLinkType}), either to a static library or
* to an alwayslink static library (blaze uses a different file extension to signal alwayslink to
* downstream code).
*
* @deprecated This is only set here for naming always to link static libraries with the *.lo
* extension. This is no longer needed because {@link
* com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink}s already carry
* information about whether a library should always be linked or not. This method will be
* removed when we no longer use *.lo for always to link static libraries in native
* cc_library.
*/
@CanIgnoreReturnValue
@Deprecated
public CcLinkingHelper setAlwayslink(boolean alwayslink) {
staticLinkType =
alwayslink ? LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY : LinkTargetType.STATIC_LIBRARY;
return this;
}
/**
* Directly set the link type. This can be used instead of {@link #setAlwayslink}. Setting
* anything other than a static link causes this class to skip the link action creation. This
* exists only for Objective-C.
*/
@CanIgnoreReturnValue
public CcLinkingHelper setStaticLinkType(LinkTargetType linkType) {
Preconditions.checkNotNull(linkType);
Preconditions.checkState(linkType.linkerOrArchiver() == LinkerOrArchiver.ARCHIVER);
this.staticLinkType = linkType;
return this;
}
/**
* Marks the resulting code as neverlink, i.e., the code will not be linked into dependent
* libraries or binaries - the header files are still available.
*/
@CanIgnoreReturnValue
public CcLinkingHelper setNeverLink(boolean neverlink) {
this.neverlink = neverlink;
return this;
}
/*
* Adds a suffix for paths of linked artifacts. Normally their paths are derived solely from rule
* labels. In the case of multiple callers (e.g., aspects) acting on a single rule, they may
* generate the same linked artifact and therefore lead to artifact conflicts. This method
* provides a way to avoid this artifact conflict by allowing different callers acting on the same
* rule to provide a suffix that will be used to scope their own linked artifacts.
*/
@CanIgnoreReturnValue
public CcLinkingHelper setLinkedArtifactNameSuffix(String suffix) {
this.linkedArtifactNameSuffix = Preconditions.checkNotNull(suffix);
return this;
}
/*
* Adds a suffix (_{hash}) for path of linked DLL. This will be enabled only when rename_dll is
* enabled and the linked artifact's LinkTargetType is NODEPS_DYNAMIC_LIBRARY
* (i.e DLL generated by cc_library). We have to add the suffix to make sure the CppLinkAction
* link against the renamed DLL. If not, CppLinkAction will link against the DLL whose name is the
* same as the name of cc_library.
*/
@CanIgnoreReturnValue
public CcLinkingHelper setLinkedDLLNameSuffix(String suffix) {
this.linkedDLLNameSuffix = Preconditions.checkNotNull(suffix);
return this;
}
/**
* Enables the optional generation of interface dynamic libraries - this is only used when the
* linker generates a dynamic library, and only if the crosstool supports it. The default is not
* to generate interface dynamic libraries.
*/
@CanIgnoreReturnValue
public CcLinkingHelper emitInterfaceSharedLibraries(boolean emitInterfaceSharedLibraries) {
this.emitInterfaceSharedLibraries = emitInterfaceSharedLibraries;
return this;
}
/**
* This enables or disables the generation of a dynamic library link action. The default is to
* generate a dynamic library. Note that the selection between dynamic or static linking is
* performed at the binary rule level.
*/
@CanIgnoreReturnValue
public CcLinkingHelper setShouldCreateDynamicLibrary(boolean emitDynamicLibrary) {
this.shouldCreateDynamicLibrary = emitDynamicLibrary;
return this;
}
/**
* When shouldCreateStaticLibraries is true, there are no actions created for static libraries.
*/
@CanIgnoreReturnValue
public CcLinkingHelper setShouldCreateStaticLibraries(boolean emitStaticLibraries) {
this.shouldCreateStaticLibraries = emitStaticLibraries;
return this;
}
/** Create the C++ link actions, and the corresponding linking related providers. */
public CcLinkingOutputs link(CcCompilationOutputs ccOutputs)
throws RuleErrorException, InterruptedException {
Preconditions.checkNotNull(ccOutputs);
Preconditions.checkState(additionalLinkerInputs == null);
additionalLinkerInputs = additionalLinkerInputsBuilder.build();
// Create link actions (only if there are object files or if explicitly requested).
//
// On some systems, the linker gives an error message if there are no input files. Even with
// the check above, this can still happen if there is a .nopic.o or .o files in srcs, but no
// other files. To fix that, we'd have to check for each link action individually.
//
// An additional pre-existing issue is that the header check tokens are dropped if we don't
// generate any link actions, effectively disabling header checking in some cases.
return createCcLinkActions(ccOutputs);
}
public CcLinkingContext buildCcLinkingContextFromLibrariesToLink(
List<LibraryToLink> librariesToLink, CcCompilationContext ccCompilationContext)
throws InterruptedException {
ImmutableList.Builder<Linkstamp> linkstampBuilder = ImmutableList.builder();
for (Artifact linkstamp : linkstamps.build().toList()) {
try {
linkstampBuilder.add(
new Linkstamp( // throws InterruptedException
linkstamp,
ccCompilationContext.getDeclaredIncludeSrcs(),
actionConstructionContext.getActionKeyContext()));
} catch (CommandLineExpansionException ex) {
throw new AssertionError("unexpected failure of command line expansion", ex);
}
}
CcLinkingContext ccLinkingContext = CcLinkingContext.EMPTY;
if (!neverlink) {
// We want an empty set if there are no link options. We have to make sure we don't
// create a LinkOptions instance that contains an empty list.
ccLinkingContext =
CcLinkingContext.builder()
.setOwner(label)
.addUserLinkFlags(
linkopts.isEmpty()
? ImmutableList.of()
: ImmutableList.of(
CcLinkingContext.LinkOptions.of(
ImmutableList.copyOf(linkopts), symbolGenerator)))
.addLibraries(librariesToLink)
// additionalLinkerInputsBuilder not expected to be a big list for now.
.addNonCodeInputs(additionalLinkerInputsBuilder.build().toList())
.addLinkstamps(linkstampBuilder.build())
.build();
}
ImmutableList.Builder<CcLinkingContext> mergedCcLinkingContexts = ImmutableList.builder();
mergedCcLinkingContexts.add(ccLinkingContext);
mergedCcLinkingContexts.addAll(ccLinkingContexts);
return CcLinkingContext.merge(mergedCcLinkingContexts.build());
}
/**
* Constructs the C++ linker actions. It generally generates two actions, one for a static library
* and one for a dynamic library. If PIC is required for shared libraries, but not for binaries,
* it additionally creates a third action to generate a PIC static library. If PIC is required for
* shared libraries and binaries, then only PIC actions are registered.
*
* <p>For dynamic libraries, this method can additionally create an interface shared library that
* can be used for linking, but doesn't contain any executable code. This increases the number of
* cache hits for link actions. Call {@link #emitInterfaceSharedLibraries(boolean)} to enable this
* behavior.
*/
private CcLinkingOutputs createCcLinkActions(CcCompilationOutputs ccOutputs)
throws RuleErrorException, InterruptedException {
// For now only handle static links. Note that the dynamic library link below ignores
// staticLinkType.
// TODO(bazel-team): Either support non-static links or move this check to setStaticLinkType().
Preconditions.checkState(
staticLinkType.linkerOrArchiver() == LinkerOrArchiver.ARCHIVER,
"can only handle static links");
LibraryToLink.Builder libraryToLinkBuilder = LibraryToLink.builder();
boolean usePicForBinaries =
CppHelper.usePicForBinaries(ccToolchain, cppConfiguration, featureConfiguration);
boolean usePicForDynamicLibs =
ccToolchain.usePicForDynamicLibraries(cppConfiguration, featureConfiguration);
PathFragment labelName = PathFragment.create(label.getName());
String libraryIdentifier =
actionConstructionContext
.getPackageDirectory()
.getRelative(labelName.replaceName("lib" + labelName.getBaseName()))
.getPathString();
if (shouldCreateStaticLibraries) {
createNoPicAndPicStaticLibraries(
libraryToLinkBuilder,
usePicForBinaries,
usePicForDynamicLibs,
libraryIdentifier,
ccOutputs);
}
boolean hasBuiltDynamicLibrary = false;
CcLinkingOutputs.Builder ccLinkingOutputsBuilder = CcLinkingOutputs.builder();
if (shouldCreateDynamicLibrary) {
boolean usePic =
(!dynamicLinkType.isExecutable() && usePicForDynamicLibs)
|| (dynamicLinkType.isExecutable() && usePicForBinaries);
hasBuiltDynamicLibrary =
createDynamicLinkAction(
ccLinkingOutputsBuilder, libraryToLinkBuilder, usePic, libraryIdentifier, ccOutputs);
}
if (shouldCreateStaticLibraries
&& featureConfiguration.isEnabled(CppRuleClasses.DISABLE_WHOLE_ARCHIVE_FOR_STATIC_LIB)
&& (staticLinkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY)) {
ruleErrorConsumer.throwWithAttributeError(
"alwayslink",
"alwayslink should not be True for a target with the"
+ " disable_whole_archive_for_static_lib feature enabled.");
}
libraryToLinkBuilder.setDisableWholeArchive(
featureConfiguration.isEnabled(CppRuleClasses.DISABLE_WHOLE_ARCHIVE_FOR_STATIC_LIB));
if (hasBuiltDynamicLibrary || shouldCreateStaticLibraries) {
ccLinkingOutputsBuilder.setLibraryToLink(libraryToLinkBuilder.build());
}
return ccLinkingOutputsBuilder.build();
}
@CanIgnoreReturnValue
public CcLinkingHelper setWillOnlyBeLinkedIntoDynamicLibraries(
boolean willOnlyBeLinkedIntoDynamicLibraries) {
this.willOnlyBeLinkedIntoDynamicLibraries = willOnlyBeLinkedIntoDynamicLibraries;
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper setUseTestOnlyFlags(boolean useTestOnlyFlags) {
this.useTestOnlyFlags = useTestOnlyFlags;
return this;
}
/** Used to set the runfiles path for test rules' dynamic libraries. */
@CanIgnoreReturnValue
public CcLinkingHelper setTestOrTestOnlyTarget(boolean isTestOrTestOnlyTarget) {
this.isTestOrTestOnlyTarget = isTestOrTestOnlyTarget;
return this;
}
/** Whether linkstamping is enabled. */
@CanIgnoreReturnValue
public CcLinkingHelper setIsStampingEnabled(boolean isStampingEnabled) {
this.isStampingEnabled = isStampingEnabled;
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper setLinkingMode(LinkingMode linkingMode) {
this.linkingMode = linkingMode;
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper setDynamicLinkType(LinkTargetType dynamicLinkType) {
this.dynamicLinkType = dynamicLinkType;
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper setPdbFile(Artifact pdbFile) {
this.pdbFile = pdbFile;
return this;
}
@CanIgnoreReturnValue
public CcLinkingHelper setDefFile(Artifact defFile) {
this.defFile = defFile;
return this;
}
/** Needed for NativeDepsHelper. It is unclear whether native deps will be in the Starlark API. */
@CanIgnoreReturnValue
@Deprecated
public CcLinkingHelper setLinkArtifactFactory(LinkArtifactFactory linkArtifactFactory) {
this.linkArtifactFactory = linkArtifactFactory;
return this;
}
private void createNoPicAndPicStaticLibraries(
LibraryToLink.Builder libraryToLinkBuilder,
boolean usePicForBinaries,
boolean usePicForDynamicLibs,
String libraryIdentifier,
CcCompilationOutputs ccOutputs)
throws RuleErrorException, InterruptedException {
// Create static library (.a). The staticLinkType only reflects whether the library is
// alwayslink or not. The PIC-ness is determined by whether we need to use PIC or not. There
// are four cases:
// for (usePicForDynamicLibs usePicForBinaries):
//
// (1) (false false) -> no pic code is when toolchain and cppOptions don't need pic code for
// dynamic libraries or binaries
// (2) (true false) -> shared libraries as pic, but not binaries
// (3) (true true) -> both shared libraries and binaries as pic
// (4) (false true) -> only pic files generated when toolchain needs pic for shared libraries
// and {@link #willOnlyBeLinkedIntoDynamicLibraries} is set to true.
// In case (3), we always need PIC, so only create one static library containing the PIC
// object files. The name therefore does not match the content.
//
// Presumably, it is done this way because the .a file is an implicit output of every
// cc_library rule, so we can't use ".pic.a" that in the always-PIC case.
// If the crosstool is configured to select an output artifact, we use that selection.
// Otherwise, we use linux defaults.
boolean createNoPicAction;
boolean createPicAction;
if (willOnlyBeLinkedIntoDynamicLibraries) {
createNoPicAction = !usePicForDynamicLibs;
createPicAction = usePicForDynamicLibs;
} else {
createNoPicAction = !usePicForBinaries;
createPicAction = usePicForBinaries || usePicForDynamicLibs;
}
if (createNoPicAction) {
LinkerInputs.LibraryToLink staticLibrary =
registerActionForStaticLibrary(
staticLinkType, ccOutputs, /* usePic= */ false, libraryIdentifier)
.getOutputLibrary();
libraryToLinkBuilder
.setLibraryIdentifier(staticLibrary.getLibraryIdentifier())
.setStaticLibrary(staticLibrary.getArtifact())
.setObjectFiles(ImmutableList.copyOf(staticLibrary.getObjectFiles()))
.setLtoCompilationContext(staticLibrary.getLtoCompilationContext())
.setSharedNonLtoBackends(staticLibrary.getSharedNonLtoBackends())
.setAlwayslink(
staticLibrary.getArtifactCategory() == ArtifactCategory.ALWAYSLINK_STATIC_LIBRARY);
}
if (createPicAction) {
LinkTargetType linkTargetTypeUsedForNaming;
if (!createNoPicAction) {
// Only PIC library created, name does not match content.
linkTargetTypeUsedForNaming = staticLinkType;
} else {
linkTargetTypeUsedForNaming =
(staticLinkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY)
? LinkTargetType.ALWAYS_LINK_PIC_STATIC_LIBRARY
: LinkTargetType.PIC_STATIC_LIBRARY;
}
LinkerInputs.LibraryToLink picStaticLibrary =
registerActionForStaticLibrary(
linkTargetTypeUsedForNaming, ccOutputs, /* usePic= */ true, libraryIdentifier)
.getOutputLibrary();
libraryToLinkBuilder
.setLibraryIdentifier(picStaticLibrary.getLibraryIdentifier())
.setPicStaticLibrary(picStaticLibrary.getArtifact())
.setPicObjectFiles(ImmutableList.copyOf(picStaticLibrary.getObjectFiles()))
.setPicLtoCompilationContext(picStaticLibrary.getLtoCompilationContext())
.setPicSharedNonLtoBackends(picStaticLibrary.getSharedNonLtoBackends())
.setAlwayslink(
picStaticLibrary.getArtifactCategory() == ArtifactCategory.ALWAYSLINK_STATIC_LIBRARY);
}
}
/** Returns whether Propeller profiles should be passed to the linking step. */
private boolean shouldPassPropellerProfiles() {
return !ccToolchain.isToolConfiguration()
&& fdoContext.getPropellerOptimizeInputFile() != null
&& fdoContext.getPropellerOptimizeInputFile().getLdArtifact() != null;
}
private CppLinkAction registerActionForStaticLibrary(
LinkTargetType linkTargetTypeUsedForNaming,
CcCompilationOutputs ccOutputs,
boolean usePic,
String libraryIdentifier)
throws RuleErrorException, InterruptedException {
Artifact linkedArtifact = getLinkedArtifact(linkTargetTypeUsedForNaming);
CppLinkActionBuilder builder =
newLinkActionBuilder(linkedArtifact, linkTargetTypeUsedForNaming)
.setMnemonic("CppArchive")
.addObjectFiles(ccOutputs.getObjectFiles(usePic))
.addLtoCompilationContext(ccOutputs.getLtoCompilationContext())
.setUsePicForLtoBackendActions(usePic)
.setLinkingMode(LinkingMode.STATIC)
.addActionInputs(linkActionInputs)
.setLibraryIdentifier(libraryIdentifier)
.addVariablesExtensions(variablesExtensions);
if (shouldPassPropellerProfiles()) {
builder.addNonCodeInput(fdoContext.getPropellerOptimizeInputFile().getLdArtifact());
}
CppLinkAction action = builder.build();
actionConstructionContext.registerAction(action);
return action;
}
private boolean createDynamicLinkAction(
CcLinkingOutputs.Builder ccLinkingOutputs,
LibraryToLink.Builder libraryToLinkBuilder,
boolean usePic,
String libraryIdentifier,
CcCompilationOutputs ccOutputs)
throws RuleErrorException, InterruptedException {
boolean hasBuiltDynamicLibrary = false;
// Create dynamic library.
Artifact linkerOutput;
String mainLibraryIdentifier;
if (linkerOutputArtifact == null) {
// If the crosstool is configured to select an output artifact, we use that selection.
// Otherwise, we use linux defaults.
linkerOutput = getLinkedArtifact(dynamicLinkType);
mainLibraryIdentifier = libraryIdentifier;
} else {
// This branch is only used for vestigial Google-internal rules where the name of the output
// file is explicitly specified in the BUILD file and as such, is platform-dependent. Thus,
// we just hardcode some reasonable logic to compute the library identifier and hope that this
// will eventually go away.
linkerOutput = linkerOutputArtifact;
mainLibraryIdentifier =
FileSystemUtils.removeExtension(linkerOutput.getRootRelativePath().getPathString());
}
List<String> sonameLinkopts = ImmutableList.of();
Artifact soInterface = null;
if (CppHelper.useInterfaceSharedLibraries(cppConfiguration, ccToolchain, featureConfiguration)
&& emitInterfaceSharedLibraries) {
soInterface = getLinkedArtifact(LinkTargetType.INTERFACE_DYNAMIC_LIBRARY);
// TODO(b/28946988): Remove this hard-coded flag.
if (!featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
sonameLinkopts =
ImmutableList.of(
"-Wl,-soname="
+ SolibSymlinkAction.getDynamicLibrarySoname(
linkerOutput.getRootRelativePath(),
/* preserveName= */ dynamicLinkType
!= LinkTargetType.NODEPS_DYNAMIC_LIBRARY,
actionConstructionContext.getConfiguration().getMnemonic()));
}
}
CppLinkActionBuilder dynamicLinkActionBuilder =
newLinkActionBuilder(linkerOutput, dynamicLinkType)
.setWholeArchive(wholeArchive)
.setNativeDeps(nativeDeps)
.setAdditionalLinkstampDefines(additionalLinkstampDefines.build())
.setInterfaceOutput(soInterface)
.addNonCodeInputs(ccOutputs.getHeaderTokenFiles())
.addLtoCompilationContext(ccOutputs.getLtoCompilationContext())
.setLinkingMode(linkingMode)
.addActionInputs(linkActionInputs)
.addLinkopts(linkopts)
.addLinkopts(sonameLinkopts)
.addVariablesExtensions(variablesExtensions);
dynamicLinkActionBuilder.addObjectFiles(ccOutputs.getObjectFiles(usePic));
if (!dynamicLinkType.isExecutable()) {
dynamicLinkActionBuilder.setLibraryIdentifier(mainLibraryIdentifier);
}
if (linkingMode == LinkingMode.DYNAMIC) {
try {
dynamicLinkActionBuilder.setRuntimeInputs(
ArtifactCategory.DYNAMIC_LIBRARY,
ccToolchain.getDynamicRuntimeLinkInputs(featureConfiguration));
} catch (EvalException e) {
throw ruleErrorConsumer.throwWithRuleError(e);
}
} else {
try {
dynamicLinkActionBuilder.setRuntimeInputs(
ArtifactCategory.STATIC_LIBRARY,
ccToolchain.getStaticRuntimeLinkInputs(featureConfiguration));
} catch (EvalException e) {
throw ruleErrorConsumer.throwWithRuleError(e);
}
}
// On Windows, we cannot build a shared library with symbols unresolved, so here we
// dynamically link to all its dependencies, even for LinkTargetType.NODEPS_DYNAMIC_LIBRARY.
boolean shouldLinkTransitively =
featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)
|| dynamicLinkType != LinkTargetType.NODEPS_DYNAMIC_LIBRARY;
if (shouldLinkTransitively) {
CcLinkingContext ccLinkingContext = CcLinkingContext.merge(ccLinkingContexts);
List<LinkerInputs.LibraryToLink> libraries =
convertLibraryToLinkListToLinkerInputList(
ccLinkingContext.getLibraries(),
linkingMode != LinkingMode.DYNAMIC,
dynamicLinkType.isDynamicLibrary(),
featureConfiguration);
ImmutableList<CcLinkingContext.Linkstamp> linkstamps =
ccLinkingContext.getLinkstamps().toList();
if (dynamicLinkType == LinkTargetType.NODEPS_DYNAMIC_LIBRARY) {
linkstamps = ImmutableList.of();
}
dynamicLinkActionBuilder.addLinkParams(
libraries,
ccLinkingContext.getFlattenedUserLinkFlags(),
linkstamps,
ccLinkingContext.getNonCodeInputs().toList(),
ruleErrorConsumer);
}
if (pdbFile != null) {
dynamicLinkActionBuilder.addActionOutput(pdbFile);
}
if (defFile != null) {
dynamicLinkActionBuilder.setDefFile(defFile);
}
if (dynamicLinkActionBuilder.hasLtoBitcodeInputs()
&& featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
if (featureConfiguration.isEnabled(CppRuleClasses.SUPPORTS_START_END_LIB)) {
dynamicLinkActionBuilder.setLtoIndexing(true);
dynamicLinkActionBuilder.setUsePicForLtoBackendActions(usePic);
CppLinkAction indexAction = dynamicLinkActionBuilder.build();
if (indexAction != null) {
actionConstructionContext.registerAction(indexAction);
}
} else {
ruleErrorConsumer.ruleError(
"When using LTO. The feature "
+ CppRuleClasses.SUPPORTS_START_END_LIB
+ " must be enabled.");
}
dynamicLinkActionBuilder.setLtoIndexing(false);
}
if (shouldPassPropellerProfiles()) {
dynamicLinkActionBuilder.addNonCodeInput(
fdoContext.getPropellerOptimizeInputFile().getLdArtifact());
}
if (dynamicLinkActionBuilder.getAllLtoBackendArtifacts() != null) {
ccLinkingOutputs.addAllLtoArtifacts(dynamicLinkActionBuilder.getAllLtoBackendArtifacts());
}
Artifact implLibraryLinkArtifact = getDynamicLibrarySolibSymlinkOutput(linkerOutput);
if (implLibraryLinkArtifact != null) {
dynamicLinkActionBuilder.setDynamicLibrarySolibSymlinkOutput(implLibraryLinkArtifact);
}
CppLinkAction dynamicLinkAction = dynamicLinkActionBuilder.build();
if (dynamicLinkType.isExecutable()) {
ccLinkingOutputs.setExecutable(linkerOutput);
}
actionConstructionContext.registerAction(dynamicLinkAction);
LinkerInputs.LibraryToLink dynamicLibrary = dynamicLinkAction.getOutputLibrary();
LinkerInputs.LibraryToLink interfaceLibrary = dynamicLinkAction.getInterfaceOutputLibrary();
// If shared library has neverlink=1, then leave it untouched. Otherwise,
// create a mangled symlink for it and from now on reference it through
// mangled name only.
//
// When COPY_DYNAMIC_LIBRARIES_TO_BINARY is enabled, we don't need to create the special
// solibDir, instead we use the original interface library and dynamic library.
if (dynamicLibrary != null) {
hasBuiltDynamicLibrary = true;
libraryToLinkBuilder.setLibraryIdentifier(dynamicLibrary.getLibraryIdentifier());
if (neverlink
|| featureConfiguration.isEnabled(CppRuleClasses.COPY_DYNAMIC_LIBRARIES_TO_BINARY)) {
if (interfaceLibrary != null) {
libraryToLinkBuilder.setInterfaceLibrary(interfaceLibrary.getArtifact());
}
libraryToLinkBuilder.setDynamicLibrary(dynamicLibrary.getArtifact());
} else {
if (dynamicLinkType == LinkTargetType.NODEPS_DYNAMIC_LIBRARY) {
implLibraryLinkArtifact =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ actionRegistry,
/* actionConstructionContext= */ actionConstructionContext,
ccToolchain.getSolibDirectory(),
dynamicLibrary.getArtifact(),
/* preserveName= */ false,
/* prefixConsumer= */ false);
}
libraryToLinkBuilder.setDynamicLibrary(implLibraryLinkArtifact);
libraryToLinkBuilder.setResolvedSymlinkDynamicLibrary(dynamicLibrary.getArtifact());
if (interfaceLibrary != null) {
Artifact libraryLinkArtifact =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ actionRegistry,
/* actionConstructionContext= */ actionConstructionContext,
ccToolchain.getSolibDirectory(),
interfaceLibrary.getArtifact(),
// Need to preserve name for transitive shared libraries that may be distributed.
/* preserveName= */ dynamicLinkType != LinkTargetType.NODEPS_DYNAMIC_LIBRARY,
/* prefixConsumer= */ false);
libraryToLinkBuilder.setInterfaceLibrary(libraryLinkArtifact);
libraryToLinkBuilder.setResolvedSymlinkInterfaceLibrary(interfaceLibrary.getArtifact());
}
}
}
return hasBuiltDynamicLibrary;
}
private CppLinkActionBuilder newLinkActionBuilder(
Artifact outputArtifact, LinkTargetType linkType) {
String mnemonic = linkType.equals(LinkTargetType.OBJC_EXECUTABLE) ? "ObjcLink" : null;
CppLinkActionBuilder builder =
new CppLinkActionBuilder(
ruleErrorConsumer,
actionConstructionContext,
label,
outputArtifact,
configuration,
ccToolchain,
fdoContext,
featureConfiguration,
semantics)
.setMnemonic(mnemonic)
.setIsStampingEnabled(isStampingEnabled)
.setTestOrTestOnlyTarget(isTestOrTestOnlyTarget)
.setLinkType(linkType)
.setLinkerFiles(
(cppConfiguration.useSpecificToolFiles()
&& linkType.linkerOrArchiver() == LinkerOrArchiver.ARCHIVER)
? ccToolchain.getArFiles()
: ccToolchain.getLinkerFiles())
.setLinkArtifactFactory(linkArtifactFactory)
.setUseTestOnlyFlags(useTestOnlyFlags)
.addTransitiveActionInputs(additionalLinkerInputs)
.addExecutionInfo(executionInfo);
for (Artifact output : linkerOutputs) {
builder.addActionOutput(output);
}
return builder;
}
/**
* Returns the linked artifact resulting from a linking of the given type. Consults the feature
* configuration to obtain an action_config that provides the artifact. If the feature
* configuration provides no artifact, uses a default.
*
* <p>We cannot assume that the feature configuration contains an action_config for the link
* action, because the linux link action depends on hardcoded values in
* LinkCommandLine.getRawLinkArgv(), which are applied on the condition that an action_config is
* not present. TODO(b/30393154): Assert that the given link action has an action_config.
*/
private Artifact getLinkedArtifact(LinkTargetType linkTargetType) throws RuleErrorException {
String maybePicName = label.getName() + linkedArtifactNameSuffix;
if (linkTargetType.picness() == Picness.PIC) {
maybePicName =
CppHelper.getArtifactNameForCategory(
ccToolchain, ArtifactCategory.PIC_FILE, maybePicName);
}
String linkedName = maybePicName;
if (linkTargetType.equals(LinkTargetType.NODEPS_DYNAMIC_LIBRARY)) {
linkedName += linkedDLLNameSuffix;
}
linkedName =
CppHelper.getArtifactNameForCategory(
ccToolchain, linkTargetType.getLinkerOutput(), linkedName);
PathFragment artifactFragment = PathFragment.create(linkedName);
ArtifactRoot artifactRoot = configuration.getBinDirectory(label.getRepository());
if (linkTargetType.equals(LinkTargetType.OBJC_FULLY_LINKED_ARCHIVE)) {
// TODO(blaze-team): This unfortunate editing of the name is here bedcause Objective-C rules
// were creating this type of archive without the lib prefix, unlike what the objective-c
// toolchain says with getArtifactNameForCategory.
// This can be fixed either when implicit outputs are removed from objc_library by keeping the
// lib prefix, or by editing the toolchain not to add it.
Preconditions.checkState(artifactFragment.getBaseName().startsWith("lib"));
artifactFragment = artifactFragment.replaceName(artifactFragment.getBaseName().substring(3));
artifactRoot =
((RuleContext) actionConstructionContext).getRule().outputsToBindir()
? configuration.getBinDirectory(label.getRepository())
: configuration.getGenfilesDirectory(label.getRepository());
}
return CppHelper.getLinkedArtifact(
label,
actionConstructionContext,
artifactRoot,
configuration,
linkTargetType,
linkedArtifactNameSuffix,
artifactFragment);
}
private static List<LinkerInputs.LibraryToLink> convertLibraryToLinkListToLinkerInputList(
NestedSet<LibraryToLink> librariesToLink,
boolean staticMode,
boolean forDynamicLibrary,
FeatureConfiguration featureConfiguration) {
ImmutableList.Builder<LinkerInputs.LibraryToLink> librariesToLinkBuilder =
ImmutableList.builder();
for (LibraryToLink libraryToLink : librariesToLink.toList()) {
LinkerInputs.LibraryToLink staticLibraryToLink =
libraryToLink.getStaticLibrary() == null ? null : libraryToLink.getStaticLibraryToLink();
LinkerInputs.LibraryToLink picStaticLibraryToLink =
libraryToLink.getPicStaticLibrary() == null
? null
: libraryToLink.getPicStaticLibraryToLink();
LinkerInputs.LibraryToLink libraryToLinkToUse = null;
if (staticMode) {
if (forDynamicLibrary) {
if (picStaticLibraryToLink != null) {
libraryToLinkToUse = picStaticLibraryToLink;
} else if (staticLibraryToLink != null) {
libraryToLinkToUse = staticLibraryToLink;
}
} else {
if (staticLibraryToLink != null) {
libraryToLinkToUse = staticLibraryToLink;
} else if (picStaticLibraryToLink != null) {
libraryToLinkToUse = picStaticLibraryToLink;
}
}
if (libraryToLinkToUse == null) {
if (libraryToLink.getInterfaceLibrary() != null) {
libraryToLinkToUse = libraryToLink.getInterfaceLibraryToLink();
} else if (libraryToLink.getDynamicLibrary() != null) {
libraryToLinkToUse = libraryToLink.getDynamicLibraryToLink();
}
}
} else {
if (libraryToLink.getInterfaceLibrary() != null) {
libraryToLinkToUse = libraryToLink.getInterfaceLibraryToLink();
} else if (libraryToLink.getDynamicLibrary() != null) {
libraryToLinkToUse = libraryToLink.getDynamicLibraryToLink();
}
if (libraryToLinkToUse == null
|| !featureConfiguration.isEnabled(CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)) {
if (forDynamicLibrary) {
if (picStaticLibraryToLink != null) {
libraryToLinkToUse = picStaticLibraryToLink;
} else if (staticLibraryToLink != null) {
libraryToLinkToUse = staticLibraryToLink;
}
} else {
if (staticLibraryToLink != null) {
libraryToLinkToUse = staticLibraryToLink;