-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathcxx_rules.bzl
1395 lines (1202 loc) · 64.5 KB
/
cxx_rules.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.
# TODO(cjhopman): This was generated by scripts/hacks/rules_shim_with_docs.py,
# but should be manually edited going forward. There may be some errors in
# the generated docs, and so those should be verified to be accurate and
# well-formatted (and then delete this TODO)
load("@prelude//apple:apple_common.bzl", "apple_common")
load("@prelude//cxx:link_groups_types.bzl", "LINK_GROUP_MAP_ATTR")
load("@prelude//decls:test_common.bzl", "test_common")
load("@prelude//linking:link_info.bzl", "LinkStyle")
load("@prelude//linking:types.bzl", "Linkage")
load(":common.bzl", "CxxRuntimeType", "CxxSourceType", "HeadersAsRawHeadersMode", "buck", "prelude_rule")
load(":cxx_common.bzl", "cxx_common")
load(":genrule_common.bzl", "genrule_common")
load(":native_common.bzl", "native_common")
ArchiveContents = ["normal", "thin"]
ArchiverProviderType = ["bsd", "gnu", "llvm", "windows", "windows_clang"]
CxxTestType = ["gtest", "boost"]
CxxToolProviderType = ["clang", "clang_cl", "clang_windows", "gcc", "windows", "windows_ml64"]
LinkerProviderType = ["darwin", "gnu", "windows", "unknown"]
PicType = ["pic", "pdc"]
SharedLibraryInterfaceParamsType = ["disabled", "enabled", "defined_only"]
cxx_binary = prelude_rule(
name = "cxx_binary",
docs = """
A cxx\\_binary() rule builds a native executable from the supplied set of C/C++ source files and
dependencies. If C/C++ library dependencies are listed, the generated native executable will request
and link against their static archives (which are \\*not\\* built using [PIC](http://en.wikipedia.org/wiki/Position-independent_code)).
""",
examples = """
```
# A rule that builds a C/C++ native executable from a single .cpp file
# its corresponding header, and a C/C++ library dependency.
cxx_binary(
name = 'echo',
srcs = [
'echo.cpp',
],
headers = [
'echo.h',
],
deps = [
':util',
],
)
cxx_library(
name = 'util',
srcs = [
'util.cpp',
],
headers = [
'util.h',
],
)
# To build without stripping:
buck build :echo
# To build with stripping debug symbols only:
buck build :echo#strip-debug
```
""",
further = None,
attrs = (
# @unsorted-dict-items
cxx_common.srcs_arg() |
cxx_common.platform_srcs_arg() |
cxx_common.headers_arg() |
cxx_common.platform_headers_arg() |
cxx_common.header_namespace_arg() |
cxx_common.preprocessor_flags_arg() |
cxx_common.platform_preprocessor_flags_arg() |
cxx_common.compiler_flags_arg() |
cxx_common.platform_compiler_flags_arg() |
cxx_common.linker_extra_outputs_arg() |
cxx_common.linker_flags_arg() |
cxx_common.platform_linker_flags_arg() |
cxx_common.precompiled_header_arg() |
native_common.link_style() |
native_common.link_group_deps() |
native_common.link_group_public_deps_label() |
buck.deps_query_arg() |
cxx_common.raw_headers_arg() |
cxx_common.include_directories_arg() |
cxx_common.raw_headers_as_headers_mode_arg() |
cxx_common.runtime_dependency_handling_arg() |
{
"contacts": attrs.list(attrs.string(), default = []),
"cxx_runtime_type": attrs.option(attrs.enum(CxxRuntimeType), default = None),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"default_platform": attrs.option(attrs.string(), default = None),
"defaults": attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False, default = {}),
"deps": attrs.list(attrs.dep(), default = []),
"devirt_enabled": attrs.bool(default = False),
"executable_name": attrs.option(attrs.string(), default = None),
"fat_lto": attrs.bool(default = False),
"focused_list_target": attrs.option(attrs.dep(), default = None),
"frameworks": attrs.list(attrs.string(), default = []),
"headers_as_raw_headers_mode": attrs.option(attrs.enum(HeadersAsRawHeadersMode), default = None),
"labels": attrs.list(attrs.string(), default = []),
"lang_compiler_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.arg()), sorted = False, default = {}),
"lang_platform_compiler_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg()))), sorted = False, default = {}),
"lang_platform_preprocessor_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg()))), sorted = False, default = {}),
"lang_preprocessor_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.arg()), sorted = False, default = {}),
"libraries": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"link_deps_query_whole": attrs.bool(default = False),
"link_group": attrs.option(attrs.string(), default = None),
"link_group_map": LINK_GROUP_MAP_ATTR,
"platform_deps": attrs.list(attrs.tuple(attrs.regex(), attrs.set(attrs.dep(), sorted = True)), default = []),
"post_linker_flags": attrs.list(attrs.arg(anon_target_compatible = True), default = []),
"post_platform_linker_flags": attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg(anon_target_compatible = True))), default = []),
"prefer_stripped_objects": attrs.bool(default = False),
"prefix_header": attrs.option(attrs.source(), default = None),
"resources": attrs.named_set(attrs.source(), sorted = True, default = []),
"thin_lto": attrs.bool(default = False),
"version_universe": attrs.option(attrs.string(), default = None),
"weak_framework_names": attrs.list(attrs.string(), default = []),
"use_header_units": attrs.bool(default = False),
} |
buck.allow_cache_upload_arg()
),
)
cxx_genrule = prelude_rule(
name = "cxx_genrule",
docs = """
A `cxx_genrule()` enables you to run shell commands as part
of the Buck build process. A `cxx_genrule()` exposes - through
a set of string parameter macros and variables - information about the
tools and configuration options used by the
Buck environment, specifically those related to the C/C++ toolchain.
The information exposed through these tools and configuration options is a reflection of:
Buck's built-in settings,
the settings in `.buckconfig`
and `.buckconfig.local`,
and the result of various command-line overrides specified through
the `common_parameters` command-line option.
This information is available only
to the shell commands specified in the `cxx_genrule`.
The information is not available to other arguments of the rule.
A `cxx_genrule()` can be an input to
another `cxx_genrule()`.
Note that if you specify the `cxx_genrule` as a command-line
target to `buck build`, you must include a platform flavor.
For example:
```
buck build :cxx_gr_name#iphonesimulator-x86_64
```
You could also just specify the default platform flavor explicitly:
```
buck build :cxx_gr_name#default
```
""",
examples = None,
further = None,
attrs = (
# @unsorted-dict-items
genrule_common.srcs_arg() |
{
"cmd": attrs.option(attrs.arg(), default = None, doc = """
The shell command to run to generate the output file. It is the fallback of `bash`
and `cmd_exe`. The shell command can access information
about the buck build environment through a set
of *macros*, *parameterized macros*, and *variables*.
#### Macros
The following macros are available to the shell command and are
accessed using the following syntax.
```
$(<macro>)
```
Example:
```
$(cc)
```
`$(cc)`
Path to the C compiler.
`$(cxx)`
Path to the C++ compiler.
`$(cflags)`
Flags passed to the C compiler.
`$(cppflags)`
Flags passed to the C preprocessor.
`$(cxxflags)`
Flags passed to the C++ compiler.
`$(cxxppflags)`
Flags to pass to the C++ preprocessor.
`$(ld)`
Path to the linker.
`$(ldflags-pic)`
Flags passed to the linker for binaries that use
position-independent code (PIC).
`$(ldflags-pic-filter <pattern>)`
Flags passed to the linker for binaries that use position-independent code (PIC).
Use the *pattern* parameter to specify a regular expression that matches the build targets that use these flags.
`$(ldflags-shared)`
Flags passed to the linker for shared libraries, such as dynamic-link libraries (DLLs).
`$(ldflags-shared-filter <pattern>)`
Flags passed to the linker for shared libraries, such as dynamic-link libraries (DLLs).
Use the *pattern* parameter to specify a regular expression that matches the build targets that use these flags.
`$(ldflags-static)`
Flags passed to the linker for statically-linked libraries.
`$(ldflags-static-filter <pattern>)`
Flags passed to the linker for statically-linked libraries.
Use the *pattern* parameter to specify a regular expression that matches the build targets that use these flags.
`$(platform-name)`
The platform flavor with which this `cxx_genrule` was specified.
#### Parameterized Macros
It is also possible to expand references to other rules within the
shell command, using the following subset of the
builtin `string parameter macros`
.
Note that all build rules expanded in the command are automatically
considered to be dependencies of the `genrule()`.
Note that the paths returned by these macros are *absolute* paths. You should convert these paths to be relative paths before
embedding them in, for example, a shell script or batch file. Using
relative paths ensures that your builds are *hermetic*, that
is, they are reproducible across different machine environments.
Additionally, if you embed these paths in a shell script, you should
execute that script using the `sh_binary()` rule and include
the targets for these paths in the `resources` argument of
that `sh_binary` rule. These are the same targets that you
pass to the string parameter macros.
`$(exe //path/to:target)`
Expands to the commands necessary to run the executable
generated by the specified build rule. For a C++ executable, this
will typically just be the name of the output executable itself,
such as `main`. If the specified build rule does not generate an
executable output, an exception will be thrown and the build will
fail.
`$(location //path/to:target)`
Expands to the path of the output of the build rule. This
means that you can refer to these without needing to be aware of
how Buck is storing data on the disk mid-build.
#### Variables
Finally, Buck adds the following variables to the environment in
which the shell command runs. They are accessed using the following syntax.
Note the use of braces rather than parentheses.
```
${<variable>}
```
Example:
```
${SRCS}
```
`${SRCS}`
A string expansion of the `srcs` argument delimited by
the `environment_expansion_separator` argument where each element
of `srcs` will be translated into an absolute path.
`${SRCDIR}`
The absolute path to the to which sources are copied
prior to running the command.
`${OUT}`
The output file for the `genrule()`. The file
specified by this variable must always be written by this
command. If not, the execution of this rule will be considered a
failure, halting the build process.
`${TMP}`
A temporary directory which can be used for intermediate results and will not be
bundled into the output.
"""),
} |
genrule_common.bash_arg() |
genrule_common.cmd_exe_arg() |
genrule_common.type_arg() |
genrule_common.weight_arg() |
genrule_common.out_arg() |
genrule_common.env_arg() |
genrule_common.environment_expansion_separator() |
{
"enable_sandbox": attrs.option(attrs.bool(), default = None, doc = """
Whether this target should be executed in a sandbox or not.
"""),
"cacheable": attrs.option(attrs.bool(), default = None),
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"default_outs": attrs.option(attrs.set(attrs.string(), sorted = False), default = None),
"labels": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"need_android_tools": attrs.bool(default = False),
"outs": attrs.option(attrs.dict(key = attrs.string(), value = attrs.set(attrs.string(), sorted = False), sorted = False), default = None),
"remote": attrs.option(attrs.bool(), default = None),
}
),
)
cxx_library = prelude_rule(
name = "cxx_library",
docs = """
A `cxx_library()` rule specifies a set of C/C++ source files
and also provides flags that specify how those files should be built.
#### Building requires a specified top-level target
Whether a Buck command builds the `cxx_library` is
determined by the inclusion of a top-level target, such as
a `cxx_binary()` or `android_binary()`, that
transitively depends on the `cxx_library`. The set of
targets specified to the Buck command (`buck build`, `buck run`, etc) must
include one of these top-level targets in order for Buck to build
the `cxx_library`. Note that you could specify the top-level target
implicitly using a `build target pattern` or you could also specify
the top-level target using a buckconfig `alias` defined in `.buckconfig`.
*How* Buck builds the library also depends on the specified top-level target.
For example, a C/C++ binary (`cxx_binary`) would require a static non-PIC build of the library,
whereas an Android APK (`android_binary`) would require a shared PIC-enabled build.
(PIC stands for position-independent code.)
#### Dependencies of the cxx\\_library also require a top-level target
Similarly, in order for Buck to build a target that
the `cxx_library` depends on, such as a `cxx_genrule()`,
you must specify in the Buck command a top-level target that depends on
the `cxx_library`. For example, you could specify
to `build` a `cxx_binary` that
depends on the `cxx_library`. If you specify as
your build target the `cxx_library` itself, the build targets
that the `cxx_library` depends on *might not be built*.
""",
examples = """
```
# A rule that includes a single .cpp file and its corresponding header and
# also supplies an additional flag for compilation.
cxx_library(
name = 'fileutil',
srcs = [
'fileutil.cpp',
],
exported_headers = [
'fileutil.h',
],
compiler_flags = [
'-fno-omit-frame-pointer',
],
)
# A rule that defines explicit names for its headers
cxx_library(
name = 'mathutils',
header_namespace = 'math',
srcs = [
'trig/src/cos.cpp',
'trig/src/tan.cpp',
],
exported_headers = {
# These are included as <math/trig/cos.h> and <math/trig/tan.h>
'trig/cos.h': 'trig/include/cos.h',
'trig/tan.h': 'trig/include/tan.h',
},
compiler_flags = [
'-fno-omit-frame-pointer',
],
)
# A rule that uses different headers and sources per platform
cxx_library(
name = 'vector',
# Because of platform_headers, this file can include "config.h"
# and get the architecture specific header
srcs = ['vector.cpp'],
platform_srcs = [
('.*armv7$', 'armv7.S'),
('.*x86_64$', 'x86_64.S'),
],
exported_headers = [
'vector.h',
],
platform_headers = [
(
'.*armv7$',
{
'config.h': 'config-armv7.h',
}
),
(
'.*x86_64$',
{
'config.h': 'config-x86_64.h',
}
),
],
)
```
""",
further = None,
attrs = (
# @unsorted-dict-items
cxx_common.srcs_arg() |
cxx_common.platform_srcs_arg() |
cxx_common.headers_arg() |
cxx_common.platform_headers_arg() |
cxx_common.exported_headers_arg() |
cxx_common.exported_header_style_arg() |
cxx_common.exported_platform_headers_arg() |
cxx_common.header_namespace_arg() |
cxx_common.preprocessor_flags_arg() |
cxx_common.lang_preprocessor_flags_arg() |
cxx_common.platform_preprocessor_flags_arg() |
cxx_common.lang_platform_preprocessor_flags_arg() |
cxx_common.exported_preprocessor_flags_arg(exported_preprocessor_flags_type = attrs.list(attrs.arg(), default = [])) |
cxx_common.exported_lang_preprocessor_flags_arg() |
cxx_common.exported_platform_preprocessor_flags_arg() |
cxx_common.exported_lang_platform_preprocessor_flags_arg() |
cxx_common.compiler_flags_arg() |
cxx_common.lang_compiler_flags_arg() |
cxx_common.platform_compiler_flags_arg() |
cxx_common.lang_platform_compiler_flags_arg() |
cxx_common.linker_extra_outputs_arg() |
cxx_common.linker_flags_arg() |
cxx_common.local_linker_flags_arg() |
cxx_common.platform_linker_flags_arg() |
cxx_common.exported_linker_flags_arg() |
cxx_common.exported_post_linker_flags_arg() |
cxx_common.exported_platform_linker_flags_arg() |
cxx_common.exported_post_platform_linker_flags_arg() |
native_common.link_style() |
native_common.link_whole(link_whole_type = attrs.option(attrs.bool(), default = None)) |
native_common.soname() |
cxx_common.raw_headers_arg() |
cxx_common.raw_headers_as_headers_mode_arg() |
cxx_common.include_directories_arg() |
cxx_common.public_include_directories_arg() |
cxx_common.public_system_include_directories_arg() |
{
"deffile": attrs.option(attrs.source(), default = None, doc = """
Specifies the *.def file used on windows to modify a dll's exports in place of explicit `__declspec(dllexport)` declarations.
The default is to not use a defile.
"""),
"used_by_wrap_script": attrs.bool(default = False, doc = """
When using an exopackage
Android, if this parameter is set to `True`, then the library is
included in the primary APK even if native libraries would otherwise not be
placed in it. This is intended for native libraries that are used by a
[wrap.sh](https://developer.android.com/ndk/guides/wrap-script)
script, which must be placed in the primary APK. Only one of
`can_be_asset` and `used_by_wrap_script` can be set
for a rule.
"""),
} |
cxx_common.supported_platforms_regex_arg() |
cxx_common.force_static(force_static_type = attrs.option(attrs.bool(), default = None)) |
native_common.preferred_linkage(preferred_linkage_type = attrs.option(attrs.enum(Linkage.values()), default = None)) |
cxx_common.reexport_all_header_dependencies_arg() |
cxx_common.exported_deps_arg() |
cxx_common.exported_platform_deps_arg() |
cxx_common.precompiled_header_arg() |
apple_common.extra_xcode_sources() |
apple_common.extra_xcode_files() |
apple_common.uses_explicit_modules_arg() |
apple_common.meta_apple_library_validation_enabled_arg() |
cxx_common.version_arg() |
{
"archive_allow_cache_upload": attrs.bool(default = False),
"bridging_header": attrs.option(attrs.source(), default = None),
"can_be_asset": attrs.option(attrs.bool(), default = None),
"contacts": attrs.list(attrs.string(), default = []),
"cxx_runtime_type": attrs.option(attrs.enum(CxxRuntimeType), default = None),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"default_platform": attrs.option(attrs.string(), default = None),
"defaults": attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False, default = {}),
"deps": attrs.list(attrs.dep(), default = []),
"devirt_enabled": attrs.bool(default = False),
"diagnostics": attrs.dict(key = attrs.string(), value = attrs.source(), sorted = False, default = {}),
"executable_name": attrs.option(attrs.string(), default = None),
"fat_lto": attrs.bool(default = False),
"focused_list_target": attrs.option(attrs.dep(), default = None),
"frameworks": attrs.list(attrs.string(), default = []),
"headers_as_raw_headers_mode": attrs.option(attrs.enum(HeadersAsRawHeadersMode), default = None),
"include_in_android_merge_map_output": attrs.bool(default = True),
"labels": attrs.list(attrs.string(), default = []),
"libraries": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"link_group": attrs.option(attrs.string(), default = None),
"link_group_map": LINK_GROUP_MAP_ATTR,
"module_name": attrs.option(attrs.string(), default = None),
"platform_deps": attrs.list(attrs.tuple(attrs.regex(), attrs.set(attrs.dep(), sorted = True)), default = []),
"post_linker_flags": attrs.list(attrs.arg(anon_target_compatible = True), default = []),
"post_platform_linker_flags": attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg(anon_target_compatible = True))), default = []),
"prefix_header": attrs.option(attrs.source(), default = None),
"resources": attrs.named_set(attrs.source(), sorted = True, default = []),
"sdk_modules": attrs.list(attrs.string(), default = []),
"static_library_basename": attrs.option(attrs.string(), default = None),
"supports_merged_linking": attrs.option(attrs.bool(), default = None),
"thin_lto": attrs.bool(default = False),
"use_archive": attrs.option(attrs.bool(), default = None),
"uses_cxx_explicit_modules": attrs.bool(default = False),
"version_universe": attrs.option(attrs.string(), default = None),
"weak_framework_names": attrs.list(attrs.string(), default = []),
"use_header_units": attrs.bool(default = False, doc = """
If True, makes any header unit exported by a dependency (including
recursively) through export_header_unit available to the compiler. If
false, the compilation ignores header units, regardless of what is
exported by dependencies.
"""),
"export_header_unit": attrs.option(attrs.enum(["include", "preload"]), default = None, doc = """
If not None, export a C++20 header unit visible to dependants (including
recursively) with use_header_units set to True.
"include": replace includes of each file in exported_headers or
raw_headers with an import of the precompiled header unit; files
that do not include any of those headers do not load the header
unit.
"preload": automatically load the precompiled header unit in any
dependant that uses header units.
"""),
"export_header_unit_filter": attrs.list(attrs.string(), default = [], doc = """
A list of regexes. Each regex should match a set of headers in
exported_headers or raw_headers to be precompiled together into one
C++20 header unit.
When used with export_header_unit="include", this allows different
subsets of headers to be loaded only by files that use them. Each group
should only depend on headers in previous groups.
If a header is not matched by any group, it is not precompiled and will
be included textually. If no filter is specified, the rule excludes
inline headers based on a name heuristics (e.g. "-inl.h").
"""),
} |
buck.allow_cache_upload_arg()
),
)
cxx_precompiled_header = prelude_rule(
name = "cxx_precompiled_header",
docs = """
A `cxx_precompiled_header` rule specifies a single header file that can be
precompiled and made available for use in other build rules such as
a `cxx_library()` or a `cxx_binary()`.
This header file is precompiled by the preprocessor on behalf of the
C, C++, Objective-C, or Objective-C++ rule using it, via its `precompiled_header` parameter.
Afterwards the precompiled header is applied during the rule's own compilation
(often with an appreciable reduction in build time, the main benefit of PCH).
This PCH is built once per combination of build flags which might affect the PCH's compatibility.
For example, a distinct pre-compilation of the header occurs per combination of flags related to
optimization, debug, architecture, and so on, used by rules which employ PCH.
The flags used during the build of the dependent rule (that is, the "PCH-using rule")
are in effect while building the PCH itself. Similarly, to the same end, the include paths used
when building the PCH are applied to the dependent rule. For example, `deps` in the
PCH rule are propagated back to the dependent rule, and the PCH's header search paths
(e.g. `-I` or `-isystem` options) are prefixed onto the list of
include paths for the dependent rule.
""",
examples = """
The best way to see how the `cxx_precompiled_header()` rule works is with an
example. Let there be a header called `common.h` which has the following:
```
#pragma once
/* Include common C++ files. */
#include <string>
#include <map>
#include <set>
#include <type_traits>
#include <vector>
/* Some frequently-used headers from the Folly project. */
#include <folly/Conv.h>
#include <folly/Executor.h>
#include <folly/io/async/EventBase.h>
```
```
cxx_precompiled_header(
name = 'common_pch',
src = 'common.h',
deps = [
# Needed for standard C++ headers:
'//external/libcxx:headers',
# Needed for the Folly includes:
'//folly:folly',
'//folly/io/async:async',
],
)
cxx_binary(
name = 'main',
srcs = ['main.cpp'],
precompiled_header = ':common_pch',
deps = [ ... ],
compiler_flags = ['-g', '-O2', '-fPIC'],
)
```
The `cxx_precompiled_header` rule declares a precompiled header "template"
containing the header file path, and dependencies.
In this example we indicate that `common.h` is to be precompiled when used by another build rule.
Note that, by itself, this `cxx_precompiled_header` rule will not result
in anything being built. The *usage* of this rule from another rule --
an "instantiation" of this precompiled header template -- is what will trigger the
PCH build.
In the example above, the build for the binary named `"main"` will depend on
the header being precompiled in a separate step, prior to compiling `main.cpp`,
and the resulting PCH will be used in `main`'s compilation.
The dependencies specified in this precompiled header rule's `deps` are transitive; they
will propagate to rules using this PCH, so that during link time, any libraries which are
required by the code made available in the header will be included in the final binary build.
The precompiled header dynamically created from the "template" will be built with flags
which would be used in the dependent rule. In this case, `main`'s use of specific
compiler flags `-g -O2 -fPIC` will result in the production of a precompiled header
with the same flags. This is so the precompiled code fully jives with rules using the PCH,
i.e. they will have the same debug, optimization, CPU, etc. options. (The compiler is usually
smart enough to reject a bad PCH, fortunately. But we want to ensure we take the appropriate
steps to ensure we *always have* a PCH which works with any build that uses it.)
Another effect of a rule using a precompiled header is that the rule's list of
build flags will change; not just to employ PCH with e.g. `-include-pch` (if using Clang), but also, to alter the sequence of header search paths.
The rule using the precompiled header will "inherit" the lists of paths used
during the PCH build, applying them *first* in its own search paths.
This is to ensure that an `#include` directive will resolve in exactly
the same way in this build as it would have in the PCH, to ensure full compatibility
between the PCH and other rule's builds. For example, if the PCH were to use one version
of `stdcxx` and another rule use a different version, the version differences
won't clash, thereby avoiding different versions of the `<cstring>` header
used between the precompiled header and the dependent rule, and preventing confused
structure definitions, ABI incompatibility, and so on (catastrophe, in other words).
""",
further = None,
attrs = (
{
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"deps": attrs.list(attrs.dep(), default = [], doc = """
Dependency rules which export headers used by the header specified in `src`.
"""),
"labels": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"src": attrs.source(doc = """
The path to the header file that should be precompiled.
Only one header file can be specified. But of course this header could include
any number of other headers. The included headers could belong to -- that is,
be `exported_headers` from -- another rule, in which case, the rule would
have to be added to `deps` as usual.
"""),
"version_universe": attrs.option(attrs.string(), default = None),
}
),
)
windows_resource = prelude_rule(
name = "windows_resource",
docs = """
A `windows_resource()` rule specifies a set of Window's Resource File (.rc) that
are compiled into object files.
The files are compiled into .res files using rc.exe and then compiled into object files
using cvtres.exe.
They are not part of cxx_library because Microsoft's linker ignores the resources
unless they are specified as an object file, meaning including them in a possibly static
library is unintuitive.
""",
examples = """
```
# A rule that includes a single .rc file and compiles it into an object file.
windows_resource(
name = "resources",
srcs = [
"resources.rc",
],
)
# A rule that links against the above windows_resource rule.
cxx_binary(
name = "app",
srcs = [
"main.cpp",
],
deps = [
":resources"
],
)
```
""",
further = None,
attrs = (
cxx_common.srcs_arg() |
cxx_common.headers_arg() |
cxx_common.platform_headers_arg() |
cxx_common.header_namespace_arg() |
cxx_common.raw_headers_arg() |
cxx_common.include_directories_arg() |
{
"labels": attrs.list(attrs.string(), default = []),
}
),
)
cxx_test = prelude_rule(
name = "cxx_test",
docs = """
A cxx\\_test() rule builds a C/C++ binary against a C/C++ testing framework and runs
it as part of `test`.
""",
examples = """
```
# A rule that builds and runs C/C++ test using gtest.
cxx_test(
name = 'echo_test',
srcs = [
'echo_test.cpp',
],
)
```
""",
further = None,
attrs = (
# @unsorted-dict-items
buck.inject_test_env_arg() |
cxx_common.srcs_arg() |
cxx_common.headers_arg() |
cxx_common.preprocessor_flags_arg() |
cxx_common.compiler_flags_arg() |
cxx_common.linker_flags_arg() |
cxx_common.precompiled_header_arg() |
buck.deps_query_arg() |
{
"resources": attrs.named_set(attrs.source(), sorted = True, default = [], doc = """
This attribute is currently not implemented, and just causes buck to rebuild
the test file if any of the resources change. This will change in the future
to provide a more reliable interface for resource files.
Additional data or source files which this test uses.
"""),
} |
cxx_common.raw_headers_arg() |
cxx_common.raw_headers_as_headers_mode_arg() |
cxx_common.include_directories_arg() |
cxx_common.runtime_dependency_handling_arg() |
{
"framework": attrs.option(attrs.enum(CxxTestType), default = None, doc = """
Unused.
"""),
"env": attrs.dict(key = attrs.string(), value = attrs.arg(), sorted = False, default = {}, doc = """
A map of environment names and values to set when running the test.
It is also possible to expand references to other rules within the **values** of
these environment variables, using builtin `string parameter macros`
:
`$(location //path/to:target)`
Expands to the location of the output of the build rule. This
means that you can refer to these without needing to be aware of how
Buck is storing data on the disk mid-build.
"""),
"args": attrs.list(attrs.arg(), default = [], doc = """
A list of additional arguments to pass to the test when it's run.
It is also possible to expand references to other rules within these
arguments, using builtin `string parameter macros`
:
`$(location //path/to:target)`
Expands to the location of the output of the build rule. This
means that you can refer to these without needing to be aware of how
Buck is storing data on the disk mid-build.
"""),
} |
buck.run_test_separately_arg(run_test_separately_type = attrs.option(attrs.bool(), default = None)) |
buck.test_rule_timeout_ms() |
native_common.link_group_deps() |
native_common.link_group_public_deps_label() |
native_common.link_style() |
{
"additional_coverage_targets": attrs.list(attrs.source(), default = []),
"contacts": attrs.list(attrs.string(), default = []),
"cxx_runtime_type": attrs.option(attrs.enum(CxxRuntimeType), default = None),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"default_platform": attrs.option(attrs.string(), default = None),
"defaults": attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False, default = {}),
"deps": attrs.list(attrs.dep(), default = []),
"devirt_enabled": attrs.bool(default = False),
"executable_name": attrs.option(attrs.string(), default = None),
"fat_lto": attrs.bool(default = False),
"focused_list_target": attrs.option(attrs.dep(), default = None),
"frameworks": attrs.list(attrs.string(), default = []),
"header_namespace": attrs.option(attrs.string(), default = None),
"headers_as_raw_headers_mode": attrs.option(attrs.enum(HeadersAsRawHeadersMode), default = None),
"labels": attrs.list(attrs.string(), default = []),
"lang_compiler_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.arg()), sorted = False, default = {}),
"lang_platform_compiler_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg()))), sorted = False, default = {}),
"lang_platform_preprocessor_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg()))), sorted = False, default = {}),
"lang_preprocessor_flags": attrs.dict(key = attrs.enum(CxxSourceType), value = attrs.list(attrs.arg()), sorted = False, default = {}),
"libraries": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"link_deps_query_whole": attrs.bool(default = False),
"link_group": attrs.option(attrs.string(), default = None),
"link_group_map": LINK_GROUP_MAP_ATTR,
"linker_extra_outputs": attrs.list(attrs.string(), default = []),
"platform_compiler_flags": attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg())), default = []),
"platform_deps": attrs.list(attrs.tuple(attrs.regex(), attrs.set(attrs.dep(), sorted = True)), default = []),
"platform_headers": attrs.list(attrs.tuple(attrs.regex(), attrs.named_set(attrs.source(), sorted = True)), default = []),
"platform_linker_flags": attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg(anon_target_compatible = True))), default = []),
"platform_preprocessor_flags": attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg())), default = []),
"platform_srcs": attrs.list(attrs.tuple(attrs.regex(), attrs.set(attrs.one_of(attrs.source(), attrs.tuple(attrs.source(), attrs.list(attrs.arg()))), sorted = True)), default = []),
"post_linker_flags": attrs.list(attrs.arg(anon_target_compatible = True), default = []),
"post_platform_linker_flags": attrs.list(attrs.tuple(attrs.regex(), attrs.list(attrs.arg(anon_target_compatible = True))), default = []),
"prefer_stripped_objects": attrs.bool(default = False),
"prefix_header": attrs.option(attrs.source(), default = None),
"thin_lto": attrs.bool(default = False),
"use_default_test_main": attrs.option(attrs.bool(), default = None),
"version_universe": attrs.option(attrs.string(), default = None),
"weak_framework_names": attrs.list(attrs.string(), default = []),
"use_header_units": attrs.bool(default = False, doc = """
If True, makes any header unit exported by a dependency (including
recursively) through export_header_unit available to the compiler. If
false, the compilation ignores header units, regardless of what is
exported by dependencies.
"""),
} |
buck.allow_cache_upload_arg() |
test_common.attributes()
),
)
cxx_toolchain = prelude_rule(
name = "cxx_toolchain",
docs = "",
examples = None,
further = None,
attrs = (
cxx_common.raw_headers_as_headers_mode_arg() |
{
"archive_contents": attrs.enum(ArchiveContents, default = "normal"),
"archiver": attrs.source(),
"archiver_flags": attrs.list(attrs.arg(), default = []),
"archiver_type": attrs.enum(ArchiverProviderType),
"asm_compiler": attrs.option(attrs.source(), default = None),
"asm_compiler_flags": attrs.list(attrs.arg(), default = []),
"asm_compiler_type": attrs.option(attrs.enum(CxxToolProviderType), default = None),
"asm_preprocessor": attrs.option(attrs.source(), default = None),
"asm_preprocessor_flags": attrs.list(attrs.arg(), default = []),
"asm_preprocessor_type": attrs.option(attrs.enum(CxxToolProviderType), default = None),
"assembler": attrs.source(),
"assembler_flags": attrs.list(attrs.arg(), default = []),
"assembler_preprocessor": attrs.option(attrs.source(), default = None),
"assembler_preprocessor_flags": attrs.list(attrs.arg(), default = []),
"assembler_preprocessor_type": attrs.option(attrs.enum(CxxToolProviderType), default = None),
"assembler_type": attrs.option(attrs.enum(CxxToolProviderType), default = None),
"binary_extension": attrs.option(attrs.string(), default = None),
"binary_linker_flags": attrs.list(
attrs.arg(anon_target_compatible = True),
default = [],
doc = """
Linker flags that apply to all links coordinated by a binary
rule. One key distinction between these and `executable_linker_flags`
is that these will also apply to library links coordinated by
binary rules (e.g. linking roots/deps when using native python or
omnibus link strategies).
""",
),
"bolt": attrs.source(),
"c_compiler": attrs.source(),
"c_compiler_flags": attrs.list(attrs.arg(), default = []),
"c_compiler_type": attrs.option(attrs.enum(CxxToolProviderType), default = None),
"c_preprocessor_flags": attrs.list(attrs.arg(), default = []),
"cache_links": attrs.bool(default = False),
"compiler_type": attrs.option(attrs.enum(CxxToolProviderType), default = None),