-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
CcLibrary.java
823 lines (759 loc) · 37.5 KB
/
CcLibrary.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
// 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.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.FailAction;
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
import com.google.devtools.build.lib.analysis.MakeVariableSupplier.MapBackedMakeVariableSupplier;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RuleErrorConsumer;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.analysis.test.InstrumentedFilesInfo;
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.collect.nestedset.Order;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.RawAttributeMapper;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.rules.cpp.CcCommon.CcFlagsSupplier;
import com.google.devtools.build.lib.rules.cpp.CcCommon.Language;
import com.google.devtools.build.lib.rules.cpp.CcCompilationHelper.CompilationInfo;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType;
import com.google.devtools.build.lib.server.FailureDetails.FailAction.Code;
import com.google.devtools.build.lib.util.FileTypeSet;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
/**
* A ConfiguredTarget for <code>cc_library</code> rules.
*/
public abstract class CcLibrary implements RuleConfiguredTargetFactory {
/** A string constant for the name of archive library(.a, .lo) output group. */
public static final String ARCHIVE_LIBRARY_OUTPUT_GROUP_NAME = "archive";
/** A string constant for the name of dynamic library output group. */
public static final String DYNAMIC_LIBRARY_OUTPUT_GROUP_NAME = "dynamic_library";
/** A string constant for the name of Windows def file output group. */
public static final String DEF_FILE_OUTPUT_GROUP_NAME = "def_file";
private final CppSemantics semantics;
protected CcLibrary(CppSemantics semantics) {
this.semantics = semantics;
}
// These file extensions don't generate object files.
private static final FileTypeSet NO_OBJECT_GENERATING_FILETYPES = FileTypeSet.of(
CppFileTypes.CPP_HEADER, CppFileTypes.ARCHIVE, CppFileTypes.PIC_ARCHIVE,
CppFileTypes.ALWAYS_LINK_LIBRARY, CppFileTypes.ALWAYS_LINK_PIC_LIBRARY,
CppFileTypes.SHARED_LIBRARY, CppFileTypes.VERSIONED_SHARED_LIBRARY);
@Override
@Nullable
public ConfiguredTarget create(RuleContext context)
throws InterruptedException, RuleErrorException, ActionConflictException {
RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(context);
boolean linkStatic = context.attributes().get("linkstatic", Type.BOOLEAN);
init(
semantics,
context,
builder,
/* additionalCopts= */ ImmutableList.of(),
/* soFilename= */ null,
context.attributes().get("alwayslink", Type.BOOLEAN),
/* neverLink= */ false,
linkStatic,
/* addDynamicRuntimeInputArtifactsToRunfiles= */ false);
return builder.build();
}
public static void init(
CppSemantics semantics,
RuleContext ruleContext,
RuleConfiguredTargetBuilder targetBuilder,
ImmutableList<String> additionalCopts,
PathFragment soFilename,
boolean alwaysLink,
boolean neverLink,
boolean linkStatic,
boolean addDynamicRuntimeInputArtifactsToRunfiles)
throws RuleErrorException, InterruptedException {
semantics.validateDeps(ruleContext);
if (ruleContext.hasErrors()) {
addEmptyRequiredProviders(targetBuilder);
return;
}
semantics.checkCanUseImplementationDeps(ruleContext);
final CcCommon common = new CcCommon(ruleContext);
common.reportInvalidOptions(ruleContext);
CcToolchainProvider ccToolchain = common.getToolchain();
ImmutableMap.Builder<String, String> toolchainMakeVariables = ImmutableMap.builder();
ccToolchain.addGlobalMakeVariables(toolchainMakeVariables);
ruleContext.initConfigurationMakeVariableContext(
new MapBackedMakeVariableSupplier(toolchainMakeVariables.buildOrThrow()),
new CcFlagsSupplier(ruleContext));
FdoContext fdoContext = common.getFdoContext();
FeatureConfiguration featureConfiguration =
CcCommon.configureFeaturesOrReportRuleError(
ruleContext, Language.CPP, ccToolchain, semantics);
PrecompiledFiles precompiledFiles = new PrecompiledFiles(ruleContext);
semantics.validateAttributes(ruleContext);
if (ruleContext.hasErrors()) {
addEmptyRequiredProviders(targetBuilder);
return;
}
ImmutableList<TransitiveInfoCollection> deps =
ImmutableList.copyOf(ruleContext.getPrerequisites("deps"));
if (ruleContext.hasErrors()) {
addEmptyRequiredProviders(targetBuilder);
return;
}
ImmutableList.Builder<CcCompilationContext> interfaceDeps = ImmutableList.builder();
ImmutableList.Builder<CcCompilationContext> implementationDeps = ImmutableList.builder();
interfaceDeps.addAll(
CppHelper.getCompilationContextsFromDeps(
ImmutableList.copyOf(ruleContext.getPrerequisites("deps"))));
implementationDeps.addAll(
CppHelper.getCompilationContextsFromDeps(
ImmutableList.copyOf(ruleContext.getPrerequisites("implementation_deps"))));
interfaceDeps.add(CcCompilationHelper.getStlCcCompilationContext(ruleContext));
CcCompilationHelper compilationHelper =
new CcCompilationHelper(
ruleContext,
ruleContext,
ruleContext.getLabel(),
CppHelper.getGrepIncludes(ruleContext),
semantics,
featureConfiguration,
ccToolchain,
fdoContext,
TargetUtils.getExecutionInfo(
ruleContext.getRule(), ruleContext.isAllowTagsPropagation()),
/* shouldProcessHeaders= */ true)
.fromCommon(common, additionalCopts)
.addSources(common.getSources())
.addPrivateHeaders(common.getPrivateHeaders())
.addPublicHeaders(common.getHeaders())
.setCodeCoverageEnabled(CcCompilationHelper.isCodeCoverageEnabled(ruleContext))
.addCcCompilationContexts(interfaceDeps.build())
.addImplementationDepsCcCompilationContexts(implementationDeps.build())
.setHeadersCheckingMode(semantics.determineHeadersCheckingMode(ruleContext));
CcLinkingHelper linkingHelper =
new CcLinkingHelper(
ruleContext,
ruleContext.getLabel(),
ruleContext,
ruleContext,
semantics,
featureConfiguration,
ccToolchain,
fdoContext,
ruleContext.getConfiguration(),
ruleContext.getFragment(CppConfiguration.class),
ruleContext.getSymbolGenerator(),
TargetUtils.getExecutionInfo(
ruleContext.getRule(), ruleContext.isAllowTagsPropagation()))
.fromCommon(ruleContext, common)
.addCcLinkingContexts(
CppHelper.getLinkingContextsFromDeps(
ImmutableList.copyOf(ruleContext.getPrerequisites("deps"))))
.addCcLinkingContexts(
CppHelper.getLinkingContextsFromDeps(
ImmutableList.copyOf(ruleContext.getPrerequisites("implementation_deps"))))
.setGrepIncludes(CppHelper.getGrepIncludes(ruleContext))
.setTestOrTestOnlyTarget(ruleContext.isTestOnlyTarget())
.addLinkopts(common.getLinkopts())
.emitInterfaceSharedLibraries(true)
.setAlwayslink(alwaysLink)
.setNeverLink(neverLink)
.addLinkstamps(ruleContext.getPrerequisites("linkstamp"));
Artifact soImplArtifact = null;
boolean supportsDynamicLinker = ccToolchain.supportsDynamicLinker(featureConfiguration);
// TODO(djasper): This is hacky. We should actually try to figure out whether we generate
// ccOutputs.
boolean createDynamicLibrary =
!linkStatic
&& supportsDynamicLinker
&& (appearsToHaveObjectFiles(ruleContext.attributes())
|| featureConfiguration.isEnabled(CppRuleClasses.HEADER_MODULE_CODEGEN));
if (soFilename != null) {
if (!soFilename.getPathString().endsWith(".so")) {
ruleContext.attributeError("outs", "file name must end in '.so'");
}
if (createDynamicLibrary) {
soImplArtifact = ruleContext.getBinArtifact(soFilename);
}
}
if (ruleContext.getRule().isAttrDefined("textual_hdrs", BuildType.LABEL_LIST)) {
compilationHelper.addPublicTextualHeaders(
ruleContext.getPrerequisiteArtifacts("textual_hdrs").list());
}
if (ruleContext.getRule().isAttrDefined("include_prefix", Type.STRING)
&& ruleContext.attributes().isAttributeValueExplicitlySpecified("include_prefix")) {
compilationHelper.setIncludePrefix(
ruleContext.attributes().get("include_prefix", Type.STRING));
}
if (ruleContext.getRule().isAttrDefined("strip_include_prefix", Type.STRING)
&& ruleContext.attributes().isAttributeValueExplicitlySpecified("strip_include_prefix")) {
compilationHelper.setStripIncludePrefix(
ruleContext.attributes().get("strip_include_prefix", Type.STRING));
}
if (common.getLinkopts().contains("-static")) {
ruleContext.attributeWarning(
"linkopts",
"Using '-static' here won't work. " + "Did you mean to use 'linkstatic=True' instead?");
}
linkingHelper.setShouldCreateDynamicLibrary(createDynamicLibrary);
linkingHelper.setLinkerOutputArtifact(soImplArtifact);
CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
// If the reason we're not creating a dynamic library is that the toolchain
// doesn't support it, then register an action which complains when triggered,
// which only happens when some rule explicitly depends on the dynamic library.
if (!createDynamicLibrary && !supportsDynamicLinker) {
ImmutableList.Builder<Artifact> dynamicLibraries = ImmutableList.builder();
dynamicLibraries.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
ruleContext.getConfiguration(),
LinkTargetType.NODEPS_DYNAMIC_LIBRARY,
CppHelper.getDLLHashSuffix(ruleContext, featureConfiguration)));
if (CppHelper.useInterfaceSharedLibraries(
cppConfiguration, ccToolchain, featureConfiguration)) {
dynamicLibraries.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
ruleContext.getConfiguration(),
LinkTargetType.INTERFACE_DYNAMIC_LIBRARY));
}
ruleContext.registerAction(
new FailAction(
ruleContext.getActionOwner(),
dynamicLibraries.build(),
"Toolchain does not support dynamic linking",
Code.DYNAMIC_LINKING_NOT_SUPPORTED));
} else if (!createDynamicLibrary
&& ruleContext.attributes().isConfigurable("srcs")) {
// If "srcs" is configurable, the .so output is always declared because the logic that
// determines implicit outs doesn't know which value of "srcs" will ultimately get chosen.
// Here, where we *do* have the correct value, it may not contain any source files to
// generate an .so with. If that's the case, register a fake generating action to prevent
// a "no generating action for this artifact" error.
ImmutableList.Builder<Artifact> dynamicLibraries = ImmutableList.builder();
dynamicLibraries.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
ruleContext.getConfiguration(),
LinkTargetType.NODEPS_DYNAMIC_LIBRARY,
CppHelper.getDLLHashSuffix(ruleContext, featureConfiguration)));
if (CppHelper.useInterfaceSharedLibraries(
cppConfiguration, ccToolchain, featureConfiguration)) {
dynamicLibraries.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
ruleContext.getConfiguration(),
LinkTargetType.INTERFACE_DYNAMIC_LIBRARY));
}
ruleContext.registerAction(
new FailAction(
ruleContext.getActionOwner(),
dynamicLibraries.build(),
"configurable \"srcs\" triggers an implicit .so output even though there are no"
+ " sources to compile in this configuration",
Code.SOURCE_FILES_MISSING));
}
CompilationInfo compilationInfo = compilationHelper.compile(ruleContext);
CcCompilationOutputs precompiledFilesObjects =
CcCompilationOutputs.builder()
.addObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ true))
.addPicObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ true))
.build();
CcCompilationOutputs ccCompilationOutputs =
CcCompilationOutputs.builder()
.merge(precompiledFilesObjects)
.merge(compilationInfo.getCcCompilationOutputs())
.build();
// Generate .a and .so outputs even without object files to fulfill the rule class
// contract wrt. implicit output files, if the contract says so. Behavior here differs
// between Bazel and Blaze.
CcLinkingOutputs ccLinkingOutputs = CcLinkingOutputs.EMPTY;
if (semantics.createEmptyArchive() || !ccCompilationOutputs.isEmpty()) {
if (featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
String dllNameSuffix = CppHelper.getDLLHashSuffix(ruleContext, featureConfiguration);
linkingHelper.setLinkedDLLNameSuffix(dllNameSuffix);
Artifact generatedDefFile = null;
Artifact defParser = common.getDefParser();
if (defParser != null) {
generatedDefFile =
CppHelper.createDefFileActions(
ruleContext,
defParser,
ccCompilationOutputs.getObjectFiles(false),
ccToolchain
.getFeatures()
.getArtifactNameForCategory(
ArtifactCategory.DYNAMIC_LIBRARY,
ruleContext.getLabel().getName() + dllNameSuffix));
targetBuilder.addOutputGroup(DEF_FILE_OUTPUT_GROUP_NAME, generatedDefFile);
}
linkingHelper.setDefFile(
CppHelper.getWindowsDefFileForLinking(
ruleContext, common.getWinDefFile(), generatedDefFile, featureConfiguration));
}
ccLinkingOutputs = linkingHelper.link(ccCompilationOutputs);
}
ImmutableSortedMap.Builder<String, NestedSet<Artifact>> outputGroups =
ImmutableSortedMap.naturalOrder();
if (!ccLinkingOutputs.isEmpty()) {
outputGroups.putAll(
addLinkerOutputArtifacts(
ruleContext,
ccToolchain,
cppConfiguration,
ruleContext.getConfiguration(),
ccCompilationOutputs,
featureConfiguration));
}
List<LibraryToLink> precompiledLibraries =
convertPrecompiledLibrariesToLibraryToLink(
ruleContext,
common,
ruleContext.getFragment(CppConfiguration.class).forcePic(),
precompiledFiles);
if (!ccCompilationOutputs.isEmpty()) {
checkIfLinkOutputsCollidingWithPrecompiledFiles(
ruleContext, ccLinkingOutputs, precompiledLibraries);
}
ImmutableList<LibraryToLink> libraryToLinks =
createLibrariesToLinkList(
ccLinkingOutputs.getLibraryToLink(),
precompiledLibraries,
ccCompilationOutputs.isEmpty());
CcLinkingContext ccLinkingContext =
linkingHelper.buildCcLinkingContextFromLibrariesToLink(
neverLink ? ImmutableList.of() : libraryToLinks,
compilationInfo.getCcCompilationContext());
CcNativeLibraryInfo ccNativeLibraryInfo =
CppHelper.collectNativeCcLibraries(ruleContext.getPrerequisites("deps"), libraryToLinks);
/*
* We always generate a static library, even if there aren't any source files.
* This keeps things simpler by avoiding special cases when making use of the library.
* For example, this is needed to ensure that building a library with "bazel build"
* will also build all of the library's "deps".
* However, we only generate a dynamic library if there are source files.
*/
// For now, we don't add the precompiled libraries to the files to build.
NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder();
if (!ccLinkingOutputs.isEmpty()) {
LibraryToLink artifactsToBuild = ccLinkingOutputs.getLibraryToLink();
if (artifactsToBuild.getStaticLibrary() != null) {
filesBuilder.add(artifactsToBuild.getStaticLibrary());
}
if (artifactsToBuild.getPicStaticLibrary() != null) {
filesBuilder.add(artifactsToBuild.getPicStaticLibrary());
}
if (!featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
if (artifactsToBuild.getResolvedSymlinkDynamicLibrary() != null) {
filesBuilder.add(artifactsToBuild.getResolvedSymlinkDynamicLibrary());
} else if (artifactsToBuild.getDynamicLibrary() != null) {
filesBuilder.add(artifactsToBuild.getDynamicLibrary());
}
if (artifactsToBuild.getResolvedSymlinkInterfaceLibrary() != null) {
filesBuilder.add(artifactsToBuild.getResolvedSymlinkInterfaceLibrary());
} else if (artifactsToBuild.getInterfaceLibrary() != null) {
filesBuilder.add(artifactsToBuild.getInterfaceLibrary());
}
}
}
if (!featureConfiguration.isEnabled(CppRuleClasses.HEADER_MODULE_CODEGEN)) {
warnAboutEmptyLibraries(ruleContext, ccCompilationOutputs, linkStatic);
}
NestedSet<Artifact> filesToBuild = filesBuilder.build();
List<Artifact> instrumentedObjectFiles = new ArrayList<>();
instrumentedObjectFiles.addAll(compilationInfo.getCcCompilationOutputs().getObjectFiles(false));
instrumentedObjectFiles.addAll(compilationInfo.getCcCompilationOutputs().getObjectFiles(true));
InstrumentedFilesInfo instrumentedFilesProvider =
common.getInstrumentedFilesProvider(
instrumentedObjectFiles,
/* withBaselineCoverage= */ true,
/* virtualToOriginalHeaders= */ NestedSetBuilder.create(Order.STABLE_ORDER),
/* additionalMetadata= */ null);
CppHelper.maybeAddStaticLinkMarkerProvider(targetBuilder, ruleContext);
Runfiles.Builder builder = new Runfiles.Builder(ruleContext.getWorkspaceName());
builder.addDataDeps(ruleContext);
builder.add(ruleContext, RunfilesProvider.DEFAULT_RUNFILES);
if (addDynamicRuntimeInputArtifactsToRunfiles) {
try {
builder.addTransitiveArtifacts(
ccToolchain.getDynamicRuntimeLinkInputs(featureConfiguration));
} catch (EvalException e) {
throw ruleContext.throwWithRuleError(e);
}
}
Runfiles runfiles = builder.build();
Runfiles.Builder defaultRunfiles =
new Runfiles.Builder(ruleContext.getWorkspaceName())
.merge(runfiles)
.addArtifacts(LibraryToLink.getDynamicLibrariesForRuntime(!neverLink, libraryToLinks));
Runfiles.Builder dataRunfiles =
new Runfiles.Builder(ruleContext.getWorkspaceName())
.merge(runfiles)
.addArtifacts(
LibraryToLink.getDynamicLibrariesForRuntime(
/* linkingStatically= */ false, libraryToLinks));
Map<String, NestedSet<Artifact>> currentOutputGroups =
CcCompilationHelper.buildOutputGroupsForEmittingCompileProviders(
compilationInfo.getCcCompilationOutputs(),
compilationInfo.getCcCompilationContext(),
ruleContext.getFragment(CppConfiguration.class),
ccToolchain,
featureConfiguration,
ruleContext,
/* generateHiddenTopLevelGroup= */ true);
CcStarlarkApiProvider.maybeAdd(ruleContext, targetBuilder);
targetBuilder
.setFilesToBuild(filesToBuild)
.addNativeDeclaredProvider(
CcInfo.builder()
.setCcCompilationContext(compilationInfo.getCcCompilationContext())
.setCcLinkingContext(ccLinkingContext)
.setCcDebugInfoContext(
CppHelper.mergeCcDebugInfoContexts(
compilationInfo.getCcCompilationOutputs(),
AnalysisUtils.getProviders(deps, CcInfo.PROVIDER)))
.setCcNativeLibraryInfo(ccNativeLibraryInfo)
.build())
.addOutputGroups(
CcCommon.mergeOutputGroups(
ImmutableList.of(currentOutputGroups, outputGroups.buildOrThrow())))
.addNativeDeclaredProvider(instrumentedFilesProvider)
.addProvider(RunfilesProvider.withData(ruleContext, defaultRunfiles.build(), dataRunfiles.build()));
}
private static void warnAboutEmptyLibraries(RuleContext ruleContext,
CcCompilationOutputs ccCompilationOutputs,
boolean linkstaticAttribute) {
if (ccCompilationOutputs.getObjectFiles(false).isEmpty()
&& ccCompilationOutputs.getObjectFiles(true).isEmpty()) {
if (!linkstaticAttribute && appearsToHaveObjectFiles(ruleContext.attributes())) {
ruleContext.attributeWarning(
"linkstatic", "setting 'linkstatic=True' is recommended if there are no object files");
}
} else {
if (!linkstaticAttribute && !appearsToHaveObjectFiles(ruleContext.attributes())) {
Artifact element = Iterables.getFirst(
ccCompilationOutputs.getObjectFiles(false),
ccCompilationOutputs.getObjectFiles(true).get(0));
ruleContext.attributeWarning(
"srcs",
"this library appears at first glance to have no object files, "
+ "but on closer inspection it does have something to link, e.g. "
+ element.prettyPrint()
+ ". "
+ "(You may have used some very confusing rule names in srcs? "
+ "Or the library consists entirely of a linker script?) "
+ "Bazel assumed linkstatic=True, but this may be inappropriate. "
+ "You may need to add an explicit '.cc' file to 'srcs'. "
+ "Alternatively, add 'linkstatic=True' to suppress this warning");
}
}
}
/**
* Returns true if the rule (which must be a cc_library rule) appears to have object files.
* This only looks at the rule itself, not at any other rules (from this package or other
* packages) that it might reference.
*
* <p>In some cases, this may return "true" even though the rule actually has no object files.
* For example, it will return true for a rule such as
* <code>cc_library(name = 'foo', srcs = [':bar'])</code> because we can't tell what ':bar' is;
* it might be a genrule that generates a source file, or it might be a genrule that generates a
* header file. Likewise,
* <code>cc_library(name = 'foo', srcs = select({':a': ['foo.cc'], ':b': []}))</code> returns
* "true" even though the sources *may* be empty. This reflects the fact that there's no way
* to tell which value "srcs" will take without knowing the rule's configuration.
*
* <p>In other cases, this may return "false" even though the rule actually does have object
* files. For example, it will return false for a rule such as
* <code>cc_library(name = 'foo', srcs = ['bar.h'])</code> but as in the other example above,
* we can't tell whether 'bar.h' is a file name or a rule name, and 'bar.h' could in fact be the
* name of a genrule that generates a source file.
*/
public static boolean appearsToHaveObjectFiles(AttributeMap rule) {
if ((rule instanceof RawAttributeMapper) && rule.isConfigurable("srcs")) {
// Since this method gets called by loading phase logic (e.g. the cc_library implicit outputs
// function), the attribute mapper may not be able to resolve configurable attributes. When
// that's the case, there's no way to know which value a configurable "srcs" will take, so
// we conservatively assume object files are possible.
return true;
}
List<Label> srcs = rule.get("srcs", BuildType.LABEL_LIST);
if (srcs != null) {
for (Label srcfile : srcs) {
/*
* We cheat a little bit here by looking at the file extension
* of the Label treated as file name. In general that might
* not necessarily work, because of the possibility that the
* user might give a rule a funky name ending in one of these
* extensions, e.g.
* genrule(name = 'foo.h', outs = ['foo.cc'], ...) // Funky rule name!
* cc_library(name = 'bar', srcs = ['foo.h']) // This DOES have object files.
*/
if (!NO_OBJECT_GENERATING_FILETYPES.matches(srcfile.getName())) {
return true;
}
}
}
return false;
}
/**
* Adds linker output artifacts to the given map, to be registered on the configured target as
* output groups.
*/
private static Map<String, NestedSet<Artifact>> addLinkerOutputArtifacts(
RuleContext ruleContext,
CcToolchainProvider ccToolchain,
CppConfiguration cppConfiguration,
BuildConfigurationValue configuration,
CcCompilationOutputs ccCompilationOutputs,
FeatureConfiguration featureConfiguration)
throws RuleErrorException {
NestedSetBuilder<Artifact> archiveFile = new NestedSetBuilder<>(Order.STABLE_ORDER);
NestedSetBuilder<Artifact> dynamicLibrary = new NestedSetBuilder<>(Order.STABLE_ORDER);
ImmutableSortedMap.Builder<String, NestedSet<Artifact>> outputGroups =
ImmutableSortedMap.naturalOrder();
if (!ruleContext.attributes().has("alwayslink", Type.BOOLEAN)
|| !ruleContext.attributes().has("linkstatic", Type.BOOLEAN)) {
return outputGroups.buildOrThrow();
}
if (ruleContext.attributes().get("alwayslink", Type.BOOLEAN)) {
archiveFile.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
configuration,
Link.LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY,
/* linkedArtifactNameSuffix= */ ""));
} else {
archiveFile.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
configuration,
Link.LinkTargetType.STATIC_LIBRARY,
/* linkedArtifactNameSuffix= */ ""));
}
if (!ruleContext.attributes().get("linkstatic", Type.BOOLEAN)
&& !ccCompilationOutputs.isEmpty()) {
dynamicLibrary.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
configuration,
Link.LinkTargetType.NODEPS_DYNAMIC_LIBRARY,
CppHelper.getDLLHashSuffix(ruleContext, featureConfiguration)));
if (CppHelper.useInterfaceSharedLibraries(
cppConfiguration, ccToolchain, featureConfiguration)) {
dynamicLibrary.add(
CppHelper.getLinkedArtifact(
ruleContext,
ccToolchain,
configuration,
LinkTargetType.INTERFACE_DYNAMIC_LIBRARY,
/* linkedArtifactNameSuffix= */ ""));
}
}
outputGroups.put(ARCHIVE_LIBRARY_OUTPUT_GROUP_NAME, archiveFile.build());
outputGroups.put(DYNAMIC_LIBRARY_OUTPUT_GROUP_NAME, dynamicLibrary.build());
return outputGroups.buildOrThrow();
}
private static ImmutableList<LibraryToLink> createLibrariesToLinkList(
@Nullable LibraryToLink outputLibrary,
List<LibraryToLink> precompiledLibraries,
boolean ccCompilationOutputsIsEmpty) {
ImmutableList.Builder<LibraryToLink> librariesToLink = ImmutableList.builder();
librariesToLink.addAll(precompiledLibraries);
// For cc_library if it contains precompiled libraries we link them. If it contains normal
// sources we link them as well, if it doesn't contain normal sources, then we don't do
// anything else if there were precompiled libraries. However, if there are no precompiled
// libraries and there are no normal sources, then we use the implicitly created link output
// files if they exist.
if (!ccCompilationOutputsIsEmpty
|| (precompiledLibraries.isEmpty()
&& isContentsOfCcLinkingOutputsImplicitlyCreated(
ccCompilationOutputsIsEmpty, outputLibrary == null))) {
if (outputLibrary != null) {
librariesToLink.add(outputLibrary);
}
}
return librariesToLink.build();
}
private static boolean isContentsOfCcLinkingOutputsImplicitlyCreated(
boolean ccCompilationOutputsIsEmpty, boolean ccLinkingOutputsIsEmpty) {
return ccCompilationOutputsIsEmpty && !ccLinkingOutputsIsEmpty;
}
private static ImmutableMap<String, Artifact> buildMapIdentifierToArtifact(
RuleErrorConsumer ruleErrorConsumer, Iterable<Artifact> artifacts) {
Map<String, Artifact> libraries = new LinkedHashMap<>();
for (Artifact artifact : artifacts) {
String identifier = CcLinkingOutputs.libraryIdentifierOf(artifact);
if (libraries.containsKey(identifier)) {
ruleErrorConsumer.attributeError(
"srcs",
String.format(
"Trying to link twice a library with the same identifier '%s', files: %s and %s",
identifier, artifact.toDetailString(), libraries.get(identifier).toDetailString()));
}
libraries.put(identifier, artifact);
}
return ImmutableMap.copyOf(libraries);
}
/*
* Add the libraries from srcs, if any. For static/mostly static
* linking we setup the dynamic libraries if there are no static libraries
* to choose from. Path to the libraries will be mangled to avoid using
* absolute path names on the -rpath, but library filenames will be
* preserved (since some libraries might have SONAME tag) - symlink will
* be created to the parent directory instead.
*
* For compatibility with existing BUILD files, any ".a" or ".lo" files listed in
* srcs are assumed to be position-independent code, or at least suitable for
* inclusion in shared libraries, unless they end with ".nopic.a" or ".nopic.lo".
*
* Note that some target platforms do not require shared library code to be PIC.
*/
private static List<LibraryToLink> convertPrecompiledLibrariesToLibraryToLink(
RuleErrorConsumer ruleErrorConsumer,
CcCommon common,
boolean forcePic,
PrecompiledFiles precompiledFiles) {
ImmutableList.Builder<LibraryToLink> librariesToLink = ImmutableList.builder();
Map<String, Artifact> staticLibraries =
buildMapIdentifierToArtifact(ruleErrorConsumer, precompiledFiles.getStaticLibraries());
Map<String, Artifact> picStaticLibraries =
buildMapIdentifierToArtifact(ruleErrorConsumer, precompiledFiles.getPicStaticLibraries());
Map<String, Artifact> alwayslinkStaticLibraries =
buildMapIdentifierToArtifact(
ruleErrorConsumer, precompiledFiles.getAlwayslinkStaticLibraries());
Map<String, Artifact> alwayslinkPicStaticLibraries =
buildMapIdentifierToArtifact(
ruleErrorConsumer, precompiledFiles.getPicAlwayslinkLibraries());
Map<String, Artifact> dynamicLibraries =
buildMapIdentifierToArtifact(ruleErrorConsumer, precompiledFiles.getSharedLibraries());
Set<String> identifiersUsed = new HashSet<>();
for (Map.Entry<String, Artifact> staticLibraryEntry :
Iterables.concat(staticLibraries.entrySet(), alwayslinkStaticLibraries.entrySet())) {
LibraryToLink.Builder libraryToLinkBuilder = LibraryToLink.builder();
String identifier = staticLibraryEntry.getKey();
libraryToLinkBuilder.setLibraryIdentifier(identifier);
boolean hasPic = picStaticLibraries.containsKey(identifier);
boolean hasAlwaysPic = alwayslinkPicStaticLibraries.containsKey(identifier);
if (hasPic || hasAlwaysPic) {
Artifact picStaticLibrary = null;
if (hasPic) {
picStaticLibrary = picStaticLibraries.get(identifier);
} else {
picStaticLibrary = alwayslinkPicStaticLibraries.get(identifier);
}
libraryToLinkBuilder.setPicStaticLibrary(picStaticLibrary);
}
if (!forcePic || !(hasPic || hasAlwaysPic)) {
libraryToLinkBuilder.setStaticLibrary(staticLibraryEntry.getValue());
}
if (dynamicLibraries.containsKey(identifier)) {
Artifact library = dynamicLibraries.get(identifier);
Artifact symlink = common.getDynamicLibrarySymlink(library, true);
libraryToLinkBuilder.setDynamicLibrary(symlink);
libraryToLinkBuilder.setResolvedSymlinkDynamicLibrary(library);
}
libraryToLinkBuilder.setAlwayslink(alwayslinkStaticLibraries.containsKey(identifier));
identifiersUsed.add(identifier);
librariesToLink.add(libraryToLinkBuilder.build());
}
for (Map.Entry<String, Artifact> picStaticLibraryEntry :
Iterables.concat(picStaticLibraries.entrySet(), alwayslinkPicStaticLibraries.entrySet())) {
String identifier = picStaticLibraryEntry.getKey();
if (identifiersUsed.contains(identifier)) {
continue;
}
LibraryToLink.Builder libraryToLinkBuilder = LibraryToLink.builder();
libraryToLinkBuilder.setLibraryIdentifier(identifier);
libraryToLinkBuilder.setPicStaticLibrary(picStaticLibraryEntry.getValue());
if (dynamicLibraries.containsKey(identifier)) {
Artifact library = dynamicLibraries.get(identifier);
Artifact symlink = common.getDynamicLibrarySymlink(library, true);
libraryToLinkBuilder.setDynamicLibrary(symlink);
libraryToLinkBuilder.setResolvedSymlinkDynamicLibrary(library);
}
libraryToLinkBuilder.setAlwayslink(alwayslinkPicStaticLibraries.containsKey(identifier));
identifiersUsed.add(identifier);
librariesToLink.add(libraryToLinkBuilder.build());
}
for (Map.Entry<String, Artifact> dynamicLibraryEntry : dynamicLibraries.entrySet()) {
String identifier = dynamicLibraryEntry.getKey();
if (identifiersUsed.contains(identifier)) {
continue;
}
LibraryToLink.Builder libraryToLinkBuilder = LibraryToLink.builder();
libraryToLinkBuilder.setLibraryIdentifier(identifier);
Artifact library = dynamicLibraryEntry.getValue();
Artifact symlink = common.getDynamicLibrarySymlink(library, true);
libraryToLinkBuilder.setDynamicLibrary(symlink);
libraryToLinkBuilder.setResolvedSymlinkDynamicLibrary(library);
librariesToLink.add(libraryToLinkBuilder.build());
}
return librariesToLink.build();
}
private static void checkIfLinkOutputsCollidingWithPrecompiledFiles(
RuleContext ruleContext,
CcLinkingOutputs ccLinkingOutputs,
List<LibraryToLink> precompiledLibraries) {
String identifier = ccLinkingOutputs.getLibraryToLink().getLibraryIdentifier();
for (LibraryToLink precompiledLibrary : precompiledLibraries) {
if (identifier.equals(precompiledLibrary.getLibraryIdentifier())) {
ruleContext.ruleError(
"Can't put library with identifier '"
+ precompiledLibrary.getLibraryIdentifier()
+ "' into the srcs of a "
+ ruleContext.getRuleClassNameForLogging()
+ " with the same name ("
+ ruleContext.getRule().getName()
+ ") which also contains other code or objects to link");
}
}
}
private static void addEmptyRequiredProviders(RuleConfiguredTargetBuilder builder) {
builder.addProvider(RunfilesProvider.EMPTY);
builder.addProvider(FileProvider.EMPTY);
builder.addProvider(FilesToRunProvider.EMPTY);
}
}