-
Notifications
You must be signed in to change notification settings - Fork 197
/
cxx_library.bzl
1524 lines (1362 loc) · 65.3 KB
/
cxx_library.bzl
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 (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
load(
"@prelude//:artifact_tset.bzl",
"ArtifactTSet", # @unused Used as a type
"make_artifact_tset",
)
load("@prelude//:paths.bzl", "paths")
load(
"@prelude//:resources.bzl",
"ResourceInfo",
"gather_resources",
)
load(
"@prelude//android:android_providers.bzl",
"merge_android_packageable_info",
)
load(
"@prelude//apple:apple_frameworks.bzl",
"apple_build_link_args_with_deduped_flags",
"apple_create_frameworks_linkable",
"apple_get_link_info_by_deduping_link_infos",
)
load("@prelude//apple:resource_groups.bzl", "create_resource_graph")
load(
"@prelude//apple:xcode.bzl",
"get_project_root_file",
)
load(
"@prelude//apple/swift:swift_runtime.bzl",
"create_swift_runtime_linkable",
)
load(
"@prelude//ide_integrations:xcode.bzl",
"XCODE_DATA_SUB_TARGET",
"XcodeDataInfo",
"generate_xcode_data",
)
load(
"@prelude//java:java_providers.bzl",
"get_java_packaging_info",
)
load("@prelude//linking:execution_preference.bzl", "LinkExecutionPreference", "get_link_execution_preference")
load(
"@prelude//linking:link_groups.bzl",
"LinkGroupLib", # @unused Used as a type
"LinkGroupLibInfo",
"gather_link_group_libs",
"merge_link_group_lib_info",
)
load(
"@prelude//linking:link_info.bzl",
"ArchiveLinkable",
"FrameworksLinkable", # @unused Used as a type
"LibOutputStyle",
"LinkArgs",
"LinkCommandDebugOutputInfo",
"LinkInfo",
"LinkInfos",
"LinkOrdering",
"LinkStrategy",
"Linkage",
"LinkedObject", # @unused Used as a type
"ObjectsLinkable",
"SharedLibLinkable",
"SwiftRuntimeLinkable", # @unused Used as a type
"SwiftmoduleLinkable", # @unused Used as a type
"UnstrippedLinkOutputInfo",
"create_merged_link_info",
"get_lib_output_style",
"get_link_args_for_strategy",
"get_output_styles_for_linkage",
"make_link_command_debug_output",
"make_link_command_debug_output_json_info",
"process_link_strategy_for_pic_behavior",
"subtarget_for_output_style",
"to_link_strategy",
"unpack_link_args",
"wrap_link_info",
)
load(
"@prelude//linking:linkable_graph.bzl",
"DlopenableLibraryInfo",
"LinkableRootInfo",
"create_linkable_graph",
"create_linkable_graph_node",
"create_linkable_node",
"get_linkable_graph_node_map_func",
"linkable_deps",
)
load("@prelude//linking:shared_libraries.bzl", "SharedLibraryInfo", "create_shared_libraries", "merge_shared_libraries")
load("@prelude//linking:strip.bzl", "strip_debug_info")
load("@prelude//utils:arglike.bzl", "ArgLike")
load("@prelude//utils:expect.bzl", "expect")
load("@prelude//utils:lazy.bzl", "lazy")
load(
"@prelude//utils:utils.bzl",
"flatten",
"map_val",
"value_or",
)
load(
"@prelude//apple/apple_resource_types.bzl",
"CxxResourceSpec",
)
load(":archive.bzl", "make_archive")
load(
":argsfiles.bzl",
"ABS_ARGSFILES_SUBTARGET",
"ARGSFILES_SUBTARGET",
"get_argsfiles_output",
)
load(":bitcode.bzl", "BitcodeBundle", "BitcodeBundleInfo", "BitcodeTSet", "make_bitcode_bundle")
load(
":comp_db.bzl",
"CxxCompilationDbInfo",
"create_compilation_database",
"make_compilation_db_info",
)
load(
":compile.bzl",
"CxxCompileCommandOutput",
"CxxCompileOutput", # @unused Used as a type
"compile_cxx",
"create_compile_cmds",
)
load(":cxx_context.bzl", "get_cxx_platform_info", "get_cxx_toolchain_info")
load(
":cxx_library_utility.bzl",
"OBJECTS_SUBTARGET",
"cxx_attr_deps",
"cxx_attr_exported_deps",
"cxx_attr_link_strategy",
"cxx_attr_link_style",
"cxx_attr_linker_flags_all",
"cxx_attr_preferred_linkage",
"cxx_attr_resources",
"cxx_inherited_link_info",
"cxx_is_gnu",
"cxx_objects_sub_targets",
"cxx_platform_supported",
"cxx_use_shlib_intfs",
)
load(":cxx_toolchain_types.bzl", "is_bitcode_format")
load(
":cxx_types.bzl",
"CxxRuleConstructorParams", # @unused Used as a type
)
load(
":link.bzl",
"CxxLinkResult", # @unused Used as a type
"CxxLinkerMapData",
"cxx_link_shared_library",
)
load(
":link_groups.bzl",
"LINK_GROUP_MAP_DATABASE_SUB_TARGET",
"get_filtered_labels_to_links_map",
"get_filtered_links",
"get_filtered_targets",
"get_link_group",
"get_link_group_info",
"get_link_group_map_json",
"get_link_group_preferred_linkage",
)
load(
":link_types.bzl",
"link_options",
)
load(
":linker.bzl",
"get_default_shared_library_name",
"get_ignore_undefined_symbols_flags",
"get_shared_library_name",
"get_shared_library_name_for_param",
)
load(
":omnibus.bzl",
"create_linkable_root",
)
load(
":preprocessor.bzl",
"CPreprocessor", # @unused Used as a type
"CPreprocessorForTestsInfo",
"CPreprocessorInfo", # @unused Used as a type
"cxx_exported_preprocessor_info",
"cxx_inherited_preprocessor_infos",
"cxx_merge_cpreprocessors",
"cxx_private_preprocessor_info",
)
load(
":shared_library_interface.bzl",
"shared_library_interface",
)
# A possible output of a `cxx_library`. This could be an archive or a shared library. Generally for an archive
# it represents just the sources of the library target itself, while a shared library will bundle multiple libraries
# together.
CxxLibraryOutput = record(
# The output style of this output.
output_style = field(LibOutputStyle),
# The main output.
default = field(Artifact),
unstripped = field(Artifact),
# Additional outputs that are implicitly used along with the above output
# (e.g. external object files referenced by a thin archive).
#
# Note: It's possible that this can contain some of the artifacts which are
# also present in object_files.
other = field(list[Artifact], []),
# The bitcode bundle. This only available for archive outputs.
# TODO(cjhopman): always available? when is it/is it not available?
bitcode_bundle = field([BitcodeBundle, None], None),
# Additional debug info which referenced by but not included in the library output.
external_debug_info = field(ArtifactTSet, ArtifactTSet()),
# A shared shared library may have an associated dwp file with
# its corresponding DWARF debug info.
# May be None when Split DWARF is disabled, for static/static-pic libraries,
# for some types of synthetic link objects or for pre-built shared libraries.
dwp = field([Artifact, None], None),
# A shared shared library may have an associated PDB file with
# its corresponding Windows debug info.
pdb = field([Artifact, None], None),
# The import library is the linkable output of a Windows shared library build.
implib = field([Artifact, None], None),
# Data about the linker map, only available on shared libraries
# TODO(cjhopman): always available? when is it/is it not available?
linker_map = field([CxxLinkerMapData, None], None),
# Extra sub targets to be returned as outputs of this rule, by link style.
sub_targets = field(dict[str, list[DefaultInfo]]),
# The object files used to create the artifact in `default`. This only includes the object files
# for this library itself, so it's not particularly meaningful for a shared lib output.
object_files = field(list[Artifact], []),
)
# The outputs of either archiving or linking the outputs of the library
_CxxAllLibraryOutputs = record(
# The outputs for each lib output style.
# For 'static'/'static_pic', these will be archives containing just this library's object files.
# For 'shared', the output as a shared library. That output will be built using either this library's link_strategy link strategy
# or via the link group strategy if this library has link group mapping.
# A header-only lib won't produce any outputs (but it may still provide LinkInfos below).
# TODO(cjhopman): make library-level link_group shared lib not put its output here
outputs = field(dict[LibOutputStyle, CxxLibraryOutput]),
# The link infos for linking against this lib of each output style. It's possible for a library to
# add link_infos even when it doesn't produce an output itself.
link_infos = field(dict[LibOutputStyle, LinkInfos]),
# Extra providers to be returned consumers of this rule.
providers = field(list[Provider], default = []),
# Shared object name to shared library mapping if this target produces a shared library.
solib = field([(str, LinkedObject), None]),
sanitizer_runtime_files = field(list[Artifact], []),
)
_CxxLibraryCompileOutput = record(
# object files
objects = field(list[Artifact]),
# object files stripped of debug information
stripped_objects = field(list[Artifact]),
# Those outputs which are bitcode
bitcode_objects = field([list[Artifact], None]),
# yaml file with optimization remarks about clang compilation
clang_remarks = field([list[Artifact], None]),
# json file with trace information about clang compilation
clang_traces = field(list[Artifact]),
# Externally referenced debug info, which doesn't get linked with the
# object (e.g. the above `.o` when using `-gsplit-dwarf=single` or the
# the `.dwo` when using `-gsplit-dwarf=split`).
external_debug_info = field(list[Artifact]),
# Whether there is any debug info
objects_have_external_debug_info = field(bool),
# sub_target for each object
objects_sub_targets = field(dict[str, list[DefaultInfo]]),
)
# The output of compiling all the source files in the library, containing
# the commands use to compile them and all the object file variants.
_CxxCompiledSourcesOutput = record(
# Compile commands used to compile the source files or generate object files
compile_cmds = field(CxxCompileCommandOutput),
# PIC compile outputs
pic = field(_CxxLibraryCompileOutput),
# Non PIC compile outputs
non_pic = field([_CxxLibraryCompileOutput, None]),
)
# The outputs of a cxx_library_parameterized rule.
_CxxLibraryParameterizedOutput = record(
# The default output of a cxx library rule
default_output = field([CxxLibraryOutput, None], None),
# The other outputs available
all_outputs = field([_CxxAllLibraryOutputs, None], None),
# Any generated sub targets as requested by impl_params. Most of these will just be a
# DefaultInfo to expose outputs to be consumed or built individually, but some (like "headers")
# will have richer providers.
sub_targets = field(dict[str, list[Provider]]),
# A bundle of all bitcode files as a subtarget
bitcode_bundle = field([BitcodeBundle, None], None),
# Any generated providers as requested by impl_params.
providers = field(list[Provider]),
# XcodeDataInfo provider, returned separately as we cannot check
# provider type from providers above
xcode_data_info = field([XcodeDataInfo, None], None),
# CxxCompilationDbInfo provider, returned separately as we cannot check
# provider type from providers above
cxx_compilationdb_info = field([CxxCompilationDbInfo, None], None),
# LinkableRootInfo provider, same as above.
linkable_root = field([LinkableRootInfo, None], None),
# List of shared libraries for the sanitizer runtime linked into the library
sanitizer_runtime_files = field(list[Artifact], []),
)
def cxx_library_parameterized(ctx: AnalysisContext, impl_params: CxxRuleConstructorParams) -> _CxxLibraryParameterizedOutput:
"""
Defines the outputs for a cxx library, return the default output and any subtargets and providers based upon the requested params.
"""
if not cxx_platform_supported(ctx):
sub_targets = {}
# Needed to handle cases of the named output (e.g. [static-pic]) being called directly.
for output_style in get_output_styles_for_linkage(cxx_attr_preferred_linkage(ctx)):
sub_targets[subtarget_for_output_style(output_style)] = [DefaultInfo(default_output = None)]
return _CxxLibraryParameterizedOutput(
providers = [
DefaultInfo(default_output = None, sub_targets = sub_targets),
SharedLibraryInfo(set = None),
],
sub_targets = sub_targets,
)
non_exported_deps = cxx_attr_deps(ctx)
exported_deps = cxx_attr_exported_deps(ctx)
# TODO(T110378095) right now we implement reexport of exported_* flags manually, we should improve/automate that in the macro layer
project_root_file = get_project_root_file(ctx)
# Gather preprocessor inputs.
(own_non_exported_preprocessor_info, test_preprocessor_infos) = cxx_private_preprocessor_info(
ctx = ctx,
headers_layout = impl_params.headers_layout,
extra_preprocessors = impl_params.extra_preprocessors,
non_exported_deps = non_exported_deps,
is_test = impl_params.is_test,
project_root_file = project_root_file,
)
own_exported_preprocessor_info = cxx_exported_preprocessor_info(ctx, impl_params.headers_layout, project_root_file, impl_params.extra_exported_preprocessors)
own_preprocessors = [own_non_exported_preprocessor_info, own_exported_preprocessor_info] + test_preprocessor_infos
inherited_non_exported_preprocessor_infos = cxx_inherited_preprocessor_infos(
non_exported_deps + filter(None, [ctx.attrs.precompiled_header]),
)
inherited_exported_preprocessor_infos = cxx_inherited_preprocessor_infos(exported_deps)
preferred_linkage = cxx_attr_preferred_linkage(ctx)
compiled_srcs = cxx_compile_srcs(
ctx = ctx,
impl_params = impl_params,
own_preprocessors = own_preprocessors,
inherited_non_exported_preprocessor_infos = inherited_non_exported_preprocessor_infos,
inherited_exported_preprocessor_infos = inherited_exported_preprocessor_infos,
preferred_linkage = preferred_linkage,
)
sub_targets = {}
providers = []
if len(ctx.attrs.tests) > 0 and impl_params.generate_providers.preprocessor_for_tests:
providers.append(
CPreprocessorForTestsInfo(
test_names = [test_target.name for test_target in ctx.attrs.tests],
own_non_exported_preprocessor = own_non_exported_preprocessor_info,
),
)
if impl_params.generate_sub_targets.argsfiles:
sub_targets[ARGSFILES_SUBTARGET] = [get_argsfiles_output(ctx, compiled_srcs.compile_cmds.argsfiles.relative, "argsfiles")]
sub_targets[ABS_ARGSFILES_SUBTARGET] = [get_argsfiles_output(ctx, compiled_srcs.compile_cmds.argsfiles.absolute, "abs-argsfiles")]
if impl_params.generate_sub_targets.clang_remarks:
if compiled_srcs.non_pic and compiled_srcs.non_pic.clang_remarks:
sub_targets["clang-remarks"] = [DefaultInfo(
default_outputs = compiled_srcs.non_pic.clang_remarks,
)]
if compiled_srcs.pic.clang_remarks:
sub_targets["pic-clang-remarks"] = [DefaultInfo(
default_outputs = compiled_srcs.pic.clang_remarks,
)]
if impl_params.generate_sub_targets.clang_traces:
if compiled_srcs.non_pic and compiled_srcs.non_pic.clang_traces:
sub_targets["clang-trace"] = [DefaultInfo(
default_outputs = compiled_srcs.non_pic.clang_traces,
)]
if compiled_srcs.pic.clang_traces:
sub_targets["pic-clang-trace"] = [DefaultInfo(
default_outputs = compiled_srcs.pic.clang_traces,
)]
if impl_params.generate_sub_targets.objects:
objects_sub_targets = compiled_srcs.pic.objects_sub_targets
if compiled_srcs.non_pic:
objects_sub_targets = objects_sub_targets | compiled_srcs.non_pic.objects_sub_targets
sub_targets[OBJECTS_SUBTARGET] = [DefaultInfo(sub_targets = objects_sub_targets)]
# Compilation DB.
if impl_params.generate_sub_targets.compilation_database:
comp_db = create_compilation_database(ctx, compiled_srcs.compile_cmds.src_compile_cmds, "compilation-database")
sub_targets["compilation-database"] = [comp_db]
# Compilation DB including headers.
comp_db = create_compilation_database(ctx, compiled_srcs.compile_cmds.comp_db_compile_cmds, "full-compilation-database")
sub_targets["full-compilation-database"] = [comp_db]
# comp_db_compile_cmds can include header files being compiled as C++ which should not be exposed in the [compilation-database] subtarget
comp_db_info = None
if impl_params.generate_providers.compilation_database:
comp_db_info = make_compilation_db_info(compiled_srcs.compile_cmds.comp_db_compile_cmds, get_cxx_toolchain_info(ctx), get_cxx_platform_info(ctx))
providers.append(comp_db_info)
# Link Groups
link_group = get_link_group(ctx)
link_group_info = get_link_group_info(ctx)
if link_group_info:
link_groups = link_group_info.groups
link_group_mappings = link_group_info.mappings
link_group_deps = [link_group_info.graph]
link_group_libs = gather_link_group_libs(
deps = non_exported_deps + exported_deps,
)
providers.append(link_group_info)
else:
link_groups = {}
link_group_mappings = {}
link_group_deps = []
link_group_libs = {}
link_group_preferred_linkage = get_link_group_preferred_linkage(link_groups.values())
# Create the linkable graph from the library's deps, exported deps and any link group deps.
linkable_graph_deps = non_exported_deps + exported_deps + link_group_deps
deps_linkable_graph = create_linkable_graph(
ctx,
deps = linkable_graph_deps,
)
frameworks_linkable = apple_create_frameworks_linkable(ctx)
swiftmodule_linkable = impl_params.swiftmodule_linkable
swift_runtime_linkable = create_swift_runtime_linkable(ctx)
dep_infos, link_group_map, link_execution_preference = _get_shared_library_links(
ctx,
get_linkable_graph_node_map_func(deps_linkable_graph),
link_group,
link_group_mappings,
link_group_preferred_linkage,
link_group_libs,
exported_deps,
non_exported_deps,
impl_params.force_link_group_linking,
frameworks_linkable,
swiftmodule_linkable,
force_static_follows_dependents = impl_params.link_groups_force_static_follows_dependents,
swift_runtime_linkable = swift_runtime_linkable,
)
if impl_params.generate_sub_targets.link_group_map and link_group_map:
sub_targets[LINK_GROUP_MAP_DATABASE_SUB_TARGET] = [link_group_map]
extra_static_linkables = []
if frameworks_linkable:
extra_static_linkables.append(frameworks_linkable)
if swiftmodule_linkable:
extra_static_linkables.append(swiftmodule_linkable)
if swift_runtime_linkable:
extra_static_linkables.append(swift_runtime_linkable)
library_outputs = _form_library_outputs(
ctx = ctx,
impl_params = impl_params,
compiled_srcs = compiled_srcs,
preferred_linkage = preferred_linkage,
dep_infos = dep_infos,
extra_static_linkables = extra_static_linkables,
gnu_use_link_groups = cxx_is_gnu(ctx) and bool(link_group_mappings),
link_execution_preference = link_execution_preference,
)
solib_as_dict = {library_outputs.solib[0]: library_outputs.solib[1]} if library_outputs.solib else {}
for _, link_style_output in library_outputs.outputs.items():
for key in link_style_output.sub_targets.keys():
expect(not key in sub_targets, "The subtarget `{}` already exists!".format(key))
sub_targets.update(link_style_output.sub_targets)
providers.extend(library_outputs.providers)
pic_behavior = get_cxx_toolchain_info(ctx).pic_behavior
# This is the output style for the library's own link_strategy+preferred_linkage.
default_output_style = get_lib_output_style(to_link_strategy(cxx_attr_link_style(ctx)), preferred_linkage, pic_behavior)
# Output sub-targets for all output-styles.
if impl_params.generate_sub_targets.link_style_outputs or impl_params.generate_providers.link_style_outputs:
default_output_style_providers = []
for output_style in get_output_styles_for_linkage(preferred_linkage):
output = library_outputs.outputs.get(output_style, None)
output_style_sub_targets, output_style_providers = impl_params.output_style_sub_targets_and_providers_factory(
output_style,
ctx,
output,
)
if output != None and output.unstripped != None:
output_style_providers.append(
UnstrippedLinkOutputInfo(artifact = output.unstripped),
)
if output:
# Add any subtargets for this output style.
output_style_sub_targets.update(output.sub_targets)
if impl_params.generate_sub_targets.link_style_outputs:
if output:
sub_targets[subtarget_for_output_style(output_style)] = [DefaultInfo(
default_output = output.default,
other_outputs = output.other,
sub_targets = output_style_sub_targets,
)] + (output_style_providers if output_style_providers else [])
if output_style == default_output_style:
# If we have additional providers for the current link style,
# add them to the list of default providers
# TODO(cjhopman): This looks like a bug, adding to providers probably shouldn't depend on generate_sub_target.link_style_outputs
default_output_style_providers += output_style_providers
# In addition, ensure any subtargets for the active link style
# can be accessed as a default subtarget
for output_style_sub_target_name, output_style_sub_target_providers in output_style_sub_targets.items():
sub_targets[output_style_sub_target_name] = output_style_sub_target_providers
if impl_params.generate_providers.link_style_outputs:
providers += default_output_style_providers
# Create the default output for the library rule given it's link style and preferred linkage
# It's possible for a library to not produce any output, for example, a header only library doesn't produce any archive or shared lib
default_output = library_outputs.outputs[default_output_style] if default_output_style in library_outputs.outputs else None
if default_output and default_output.bitcode_bundle:
sub_targets["bitcode"] = [DefaultInfo(default_output = default_output.bitcode_bundle.artifact)]
# Define the xcode data sub target
xcode_data_info = None
if impl_params.generate_sub_targets.xcode_data:
xcode_data_default_info, xcode_data_info = generate_xcode_data(
ctx,
rule_type = impl_params.rule_type,
output = default_output.default if default_output else None,
populate_rule_specific_attributes_func = impl_params.cxx_populate_xcode_attributes_func,
srcs = impl_params.srcs + impl_params.additional.srcs,
argsfiles = compiled_srcs.compile_cmds.argsfiles.absolute,
product_name = get_default_cxx_library_product_name(ctx, impl_params),
)
sub_targets[XCODE_DATA_SUB_TARGET] = xcode_data_default_info
providers.append(xcode_data_info)
# Propagate link info provider.
if impl_params.generate_providers.merged_native_link_info or impl_params.generate_providers.template_placeholders:
# Gather link inputs.
inherited_non_exported_link = cxx_inherited_link_info(non_exported_deps)
inherited_exported_link = cxx_inherited_link_info(exported_deps)
merged_native_link_info = create_merged_link_info(
ctx,
pic_behavior,
# Add link info for each link style,
library_outputs.link_infos,
preferred_linkage = preferred_linkage,
# Export link info from non-exported deps (when necessary).
deps = inherited_non_exported_link,
# Export link info from out (exported) deps.
exported_deps = inherited_exported_link,
frameworks_linkable = frameworks_linkable,
swiftmodule_linkable = swiftmodule_linkable,
swift_runtime_linkable = swift_runtime_linkable,
)
if impl_params.generate_providers.merged_native_link_info:
providers.append(merged_native_link_info)
else:
# This code sets merged_native_link_info only in some cases, leaving it unassigned in others.
# Add a fake definition set to None so the assignment checker is satisfied.
merged_native_link_info = None
# Propagate shared libraries up the tree.
if impl_params.generate_providers.shared_libraries:
providers.append(merge_shared_libraries(
ctx.actions,
create_shared_libraries(ctx, solib_as_dict),
filter(None, [x.get(SharedLibraryInfo) for x in non_exported_deps]) +
filter(None, [x.get(SharedLibraryInfo) for x in exported_deps]),
))
propagated_preprocessor_merge_list = inherited_exported_preprocessor_infos
if _attr_reexport_all_header_dependencies(ctx):
propagated_preprocessor_merge_list = inherited_non_exported_preprocessor_infos + propagated_preprocessor_merge_list
propagated_preprocessor = cxx_merge_cpreprocessors(ctx, [own_exported_preprocessor_info], propagated_preprocessor_merge_list)
if impl_params.generate_providers.preprocessors:
providers.append(propagated_preprocessor)
# Propagated_exported_preprocessor_info is used for pcm compilation, which isn't possible for non-modular targets.
propagated_exported_preprocessor_info = propagated_preprocessor if impl_params.rule_type == "apple_library" and ctx.attrs.modular else None
additional_providers = impl_params.additional.additional_providers_factory(propagated_exported_preprocessor_info) if impl_params.additional.additional_providers_factory else []
# For v1's `#headers` functionality.
if impl_params.generate_sub_targets.headers:
sub_targets["headers"] = [propagated_preprocessor, create_merged_link_info(
ctx,
pic_behavior = pic_behavior,
preferred_linkage = Linkage("static"),
frameworks_linkable = frameworks_linkable,
swiftmodule_linkable = swiftmodule_linkable,
), LinkGroupLibInfo(libs = {}), SharedLibraryInfo(set = None)] + additional_providers
if getattr(ctx.attrs, "supports_header_symlink_subtarget", False):
header_symlink_mapping = {}
for records in propagated_preprocessor.set.traverse():
for record in records:
for header in record.headers:
header_path = header.name
if header.namespace:
header_path = paths.join(header.namespace, header_path)
header_symlink_mapping[paths.normalize(header_path)] = header.artifact
sub_targets["header-symlink-tree"] = [DefaultInfo(
default_output = ctx.actions.symlinked_dir("header_symlink_tree", header_symlink_mapping),
)]
for additional_subtarget, subtarget_providers in impl_params.additional.subtargets.items():
sub_targets[additional_subtarget] = subtarget_providers
linker_flags = cxx_attr_linker_flags_all(ctx)
# Omnibus root provider.
linkable_root = None
if impl_params.generate_providers.omnibus_root:
if impl_params.use_soname:
soname = _soname(ctx, impl_params)
else:
soname = None
linker_type = get_cxx_toolchain_info(ctx).linker_info.type
linkable_root = create_linkable_root(
name = soname,
link_infos = LinkInfos(
default = LinkInfo(
pre_flags = linker_flags.flags + linker_flags.exported_flags,
post_flags = linker_flags.post_flags + linker_flags.exported_post_flags,
linkables = [ObjectsLinkable(
objects = compiled_srcs.pic.objects,
linker_type = linker_type,
link_whole = True,
)],
external_debug_info = make_artifact_tset(
actions = ctx.actions,
label = ctx.label,
artifacts = (
compiled_srcs.pic.external_debug_info +
(compiled_srcs.pic.objects if compiled_srcs.pic.objects_have_external_debug_info else [])
),
children = impl_params.additional.static_external_debug_info,
),
),
stripped = LinkInfo(
pre_flags = linker_flags.flags + linker_flags.exported_flags,
post_flags = linker_flags.post_flags + linker_flags.exported_post_flags,
linkables = [ObjectsLinkable(
objects = compiled_srcs.pic.stripped_objects,
linker_type = linker_type,
link_whole = True,
)],
),
),
deps = non_exported_deps + exported_deps,
)
providers.append(linkable_root)
# Mark libraries that support `dlopen`.
if getattr(ctx.attrs, "supports_python_dlopen", False):
providers.append(DlopenableLibraryInfo())
# Augment and provide the linkable graph.
if impl_params.generate_providers.linkable_graph:
merged_linkable_graph = create_linkable_graph(
ctx,
node = create_linkable_graph_node(
ctx,
linkable_node = create_linkable_node(
ctx = ctx,
default_soname = _soname(ctx, impl_params),
preferred_linkage = preferred_linkage,
default_link_strategy = cxx_attr_link_strategy(ctx.attrs),
deps = non_exported_deps,
exported_deps = exported_deps,
# If we don't have link input for this link style, we pass in `None` so
# that omnibus knows to avoid it.
include_in_android_mergemap = getattr(ctx.attrs, "include_in_android_merge_map_output", True) and default_output != None,
link_infos = library_outputs.link_infos,
shared_libs = solib_as_dict,
linker_flags = linker_flags,
can_be_asset = getattr(ctx.attrs, "can_be_asset", False) or False,
),
excluded = {ctx.label: None} if not value_or(ctx.attrs.supports_merged_linking, True) else {},
),
deps = linkable_graph_deps,
)
providers.append(merged_linkable_graph)
# C++ resource.
if impl_params.generate_providers.resources:
resources = cxx_attr_resources(ctx)
cxx_resource_info = ResourceInfo(resources = gather_resources(
label = ctx.label,
resources = resources,
deps = non_exported_deps + exported_deps,
))
providers += [cxx_resource_info]
if impl_params.generate_providers.cxx_resources_as_apple_resources:
apple_resource_graph = create_resource_graph(
ctx = ctx,
labels = ctx.attrs.labels,
deps = non_exported_deps,
exported_deps = exported_deps,
cxx_resource_spec = CxxResourceSpec(resources = resources) if resources else None,
)
providers += [apple_resource_graph]
if impl_params.generate_providers.template_placeholders:
templ_vars = {}
# Some rules, e.g. fbcode//thrift/lib/cpp:thrift-core-module
# define preprocessor flags as things like: -DTHRIFT_PLATFORM_CONFIG=<thrift/facebook/PlatformConfig.h>
# and unless they get quoted, they break shell syntax.
cxx_preprocessor_flags = cmd_args()
cxx_compiler_info = get_cxx_toolchain_info(ctx).cxx_compiler_info
cxx_preprocessor_flags.add(cmd_args(cxx_compiler_info.preprocessor_flags or [], quote = "shell"))
cxx_preprocessor_flags.add(cmd_args(propagated_preprocessor.set.project_as_args("args"), quote = "shell"))
cxx_preprocessor_flags.add(propagated_preprocessor.set.project_as_args("include_dirs"))
templ_vars["cxxppflags"] = cxx_preprocessor_flags
c_preprocessor_flags = cmd_args()
c_compiler_info = get_cxx_toolchain_info(ctx).c_compiler_info
c_preprocessor_flags.add(cmd_args(c_compiler_info.preprocessor_flags or [], quote = "shell"))
c_preprocessor_flags.add(cmd_args(propagated_preprocessor.set.project_as_args("args"), quote = "shell"))
c_preprocessor_flags.add(propagated_preprocessor.set.project_as_args("include_dirs"))
templ_vars["cppflags"] = c_preprocessor_flags
# Add in ldflag macros.
for link_strategy in (LinkStrategy("static"), LinkStrategy("static_pic")):
name = "ldflags-" + link_strategy.value.replace("_", "-")
args = cmd_args()
linker_info = get_cxx_toolchain_info(ctx).linker_info
args.add(linker_info.linker_flags or [])
# Normally, we call get_link_args_for_strategy for getting the args for our own link from our
# deps. This case is a bit different as we are effectively trying to get the args for how this library
# would be represented on a dependent's link line and so it is appropriate to use our own merged_native_link_info.
link_args = get_link_args_for_strategy(
ctx,
[merged_native_link_info],
link_strategy,
)
args.add(unpack_link_args(link_args))
templ_vars[name] = args
# TODO(T110378127): To implement `$(ldflags-shared ...)` properly, we'd need
# to setup a symink tree rule for all transitive shared libs. Since this
# currently would be pretty costly (O(N^2)?), and since it's not that
# commonly used anyway, just use `static-pic` instead. Longer-term, once
# v1 is gone, macros that use `$(ldflags-shared ...)` (e.g. Haskell's
# hsc2hs) can move to a v2 rules-based API to avoid needing this macro.
templ_vars["ldflags-shared"] = templ_vars["ldflags-static-pic"]
providers.append(TemplatePlaceholderInfo(keyed_variables = templ_vars))
# It is possible (e.g. in a java binary or an Android APK) to have C++ libraries that depend
# upon Java libraries (through JNI). In some cases those Java libraries are not depended upon
# anywhere else, so we need to expose them here to ensure that they are packaged into the
# final binary.
if impl_params.generate_providers.java_packaging_info:
providers.append(get_java_packaging_info(ctx, non_exported_deps + exported_deps))
# TODO(T107163344) this shouldn't be in cxx_library itself, use overlays to remove it.
if impl_params.generate_providers.android_packageable_info:
providers.append(merge_android_packageable_info(ctx.label, ctx.actions, non_exported_deps + exported_deps))
bitcode_bundle = default_output.bitcode_bundle if default_output != None else None
if bitcode_bundle:
bc_provider = BitcodeBundleInfo(bitcode = bitcode_bundle, bitcode_bundle = ctx.actions.tset(BitcodeTSet, value = bitcode_bundle))
additional_providers.append(bc_provider)
if impl_params.generate_providers.default:
if False:
# TODO(nga): `default_output.unstripped` is never `None`.
def unknown():
pass
default_output = unknown()
default_info = DefaultInfo(
default_output = default_output.default if default_output != None else None,
other_outputs = default_output.other if default_output != None else [],
sub_targets = sub_targets,
)
providers.append(default_info)
# Propagate all transitive link group lib roots up the tree, so that the
# final executable can use them.
if impl_params.generate_providers.merged_native_link_info:
providers.append(
merge_link_group_lib_info(
label = ctx.label,
name = link_group,
shared_libs = solib_as_dict,
shared_link_infos = library_outputs.link_infos.get(LibOutputStyle("shared_lib")),
deps = exported_deps + non_exported_deps,
),
)
return _CxxLibraryParameterizedOutput(
default_output = default_output,
all_outputs = library_outputs,
sub_targets = sub_targets,
bitcode_bundle = bitcode_bundle,
providers = providers + additional_providers,
xcode_data_info = xcode_data_info,
cxx_compilationdb_info = comp_db_info,
linkable_root = linkable_root,
sanitizer_runtime_files = library_outputs.sanitizer_runtime_files,
)
def get_default_cxx_library_product_name(ctx, impl_params) -> str:
preferred_linkage = cxx_attr_preferred_linkage(ctx)
output_style = get_lib_output_style(
to_link_strategy(cxx_attr_link_style(ctx)),
preferred_linkage,
get_cxx_toolchain_info(ctx).pic_behavior,
)
if output_style == LibOutputStyle("shared_lib"):
return _soname(ctx, impl_params)
else:
return _base_static_library_name(ctx, False)
def _get_library_compile_output(ctx, outs: list[CxxCompileOutput], extra_link_input) -> _CxxLibraryCompileOutput:
objects = [out.object for out in outs]
stripped_objects = _strip_objects(ctx, objects)
bitcode_objects = [
out.object
for out in outs
if is_bitcode_format(out.object_format)
]
if len(bitcode_objects) == 0:
bitcode_objects = None
objects_sub_targets = cxx_objects_sub_targets(outs)
# add this after setting up sub_targets
objects += extra_link_input
stripped_objects += extra_link_input
return _CxxLibraryCompileOutput(
objects = objects,
stripped_objects = stripped_objects,
bitcode_objects = bitcode_objects,
clang_traces = [out.clang_trace for out in outs if out.clang_trace != None],
clang_remarks = [out.clang_remarks for out in outs if out.clang_remarks != None],
external_debug_info = [out.external_debug_info for out in outs if out.external_debug_info != None],
objects_have_external_debug_info = lazy.is_any(lambda out: out.object_has_external_debug_info, outs),
objects_sub_targets = objects_sub_targets,
)
def cxx_compile_srcs(
ctx: AnalysisContext,
impl_params: CxxRuleConstructorParams,
own_preprocessors: list[CPreprocessor],
inherited_non_exported_preprocessor_infos: list[CPreprocessorInfo],
inherited_exported_preprocessor_infos: list[CPreprocessorInfo],
preferred_linkage: Linkage) -> _CxxCompiledSourcesOutput:
"""
Compile objects we'll need for archives and shared libraries.
"""
# Create the commands and argsfiles to use for compiling each source file
compile_cmd_output = create_compile_cmds(
ctx = ctx,
impl_params = impl_params,
own_preprocessors = own_preprocessors,
inherited_preprocessor_infos = inherited_non_exported_preprocessor_infos + inherited_exported_preprocessor_infos,
)
# Define object files.
pic_cxx_outs = compile_cxx(ctx, compile_cmd_output.src_compile_cmds, pic = True, allow_cache_upload = ctx.attrs.allow_cache_upload)
pic = _get_library_compile_output(ctx, pic_cxx_outs, impl_params.extra_link_input)
non_pic = None
if preferred_linkage != Linkage("shared"):
non_pic_cxx_outs = compile_cxx(ctx, compile_cmd_output.src_compile_cmds, pic = False, allow_cache_upload = ctx.attrs.allow_cache_upload)
non_pic = _get_library_compile_output(ctx, non_pic_cxx_outs, impl_params.extra_link_input)
return _CxxCompiledSourcesOutput(
compile_cmds = compile_cmd_output,
pic = pic,
non_pic = non_pic,
)
def _form_library_outputs(
ctx: AnalysisContext,
impl_params: CxxRuleConstructorParams,
compiled_srcs: _CxxCompiledSourcesOutput,
preferred_linkage: Linkage,
dep_infos: LinkArgs,
extra_static_linkables: list[[FrameworksLinkable, SwiftmoduleLinkable, SwiftRuntimeLinkable]],
gnu_use_link_groups: bool,
link_execution_preference: LinkExecutionPreference) -> _CxxAllLibraryOutputs:
# Build static/shared libs and the link info we use to export them to dependents.
outputs = {}
solib = None
link_infos = {}
providers = []
sanitizer_runtime_files = []
linker_flags = cxx_attr_linker_flags_all(ctx)
# Add in exported linker flags.
def ldflags(inner: LinkInfo) -> LinkInfo:
return wrap_link_info(
inner = inner,
pre_flags = linker_flags.exported_flags,
post_flags = linker_flags.exported_post_flags,
)
# We don't know which outputs consumers may want, so we define all the possibilities given our preferred linkage.
for output_style in get_output_styles_for_linkage(preferred_linkage):
output = None
stripped = None
info = None
# Generate the necessary libraries and
# add them to the exported link info.
if output_style != LibOutputStyle("shared_lib"):
pic = _use_pic(output_style)
lib_compile_output = compiled_srcs.pic
if not pic:
lib_compile_output = compiled_srcs.non_pic
if not lib_compile_output:
fail("output_style {} requires non_pic compiled srcs, but didn't have any in {}".format(output_style, compiled_srcs))
# Only generate an archive if we have objects to include
if lib_compile_output.objects:
output, info = _static_library(
ctx,
impl_params,
lib_compile_output.objects,
objects_have_external_debug_info = lib_compile_output.objects_have_external_debug_info,
external_debug_info = make_artifact_tset(
ctx.actions,
label = ctx.label,
artifacts = lib_compile_output.external_debug_info,
children = impl_params.additional.static_external_debug_info,
),
pic = pic,
stripped = False,
extra_linkables = extra_static_linkables,
bitcode_objects = lib_compile_output.bitcode_objects,
)
_, stripped = _static_library(
ctx,
impl_params,
lib_compile_output.stripped_objects,
pic = pic,
stripped = True,
extra_linkables = extra_static_linkables,
bitcode_objects = lib_compile_output.bitcode_objects,
)
else:
# Header only libraries can have `extra_static_linkables`
info = LinkInfo(
name = ctx.label.name,
linkables = extra_static_linkables,