blob: 5dbaa86ab2ac0b5411c629cb11cd8a63710cf4f2 [file] [log] [blame]
rosica71bc38f2019-02-04 02:39:30 -08001# Copyright 2019 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""A Starlark cc_toolchain configuration rule"""
rosica71bc38f2019-02-04 02:39:30 -080016
rosica96500b72019-05-21 03:25:53 -070017load(
18 "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
plf21b5eb62020-10-21 06:53:30 -070019 "action_config",
rosica96500b72019-05-21 03:25:53 -070020 "feature",
21 "feature_set",
22 "flag_group",
23 "flag_set",
plf21b5eb62020-10-21 06:53:30 -070024 "tool",
rosica96500b72019-05-21 03:25:53 -070025 "tool_path",
26 "variable_with_value",
27 "with_feature_set",
28)
29load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
rosica71bc38f2019-02-04 02:39:30 -080030
hlopko8b9f7462020-05-26 05:39:52 -070031def layering_check_features(compiler):
32 if compiler != "clang":
33 return []
34 return [
35 feature(
36 name = "use_module_maps",
37 requires = [feature_set(features = ["module_maps"])],
38 flag_sets = [
39 flag_set(
40 actions = [
41 ACTION_NAMES.c_compile,
42 ACTION_NAMES.cpp_compile,
43 ACTION_NAMES.cpp_header_parsing,
44 ACTION_NAMES.cpp_module_compile,
45 ],
46 flag_groups = [
47 flag_group(
48 flags = [
49 "-fmodule-name=%{module_name}",
50 "-fmodule-map-file=%{module_map_file}",
51 ],
52 ),
53 ],
54 ),
55 ],
56 ),
57
58 # Tell blaze we support module maps in general, so they will be generated
59 # for all c/c++ rules.
60 # Note: not all C++ rules support module maps; thus, do not imply this
61 # feature from other features - instead, require it.
62 feature(name = "module_maps", enabled = True),
63 feature(
64 name = "layering_check",
65 implies = ["use_module_maps"],
66 flag_sets = [
67 flag_set(
68 actions = [
69 ACTION_NAMES.c_compile,
70 ACTION_NAMES.cpp_compile,
71 ACTION_NAMES.cpp_header_parsing,
72 ACTION_NAMES.cpp_module_compile,
73 ],
74 flag_groups = [
75 flag_group(flags = [
76 "-fmodules-strict-decluse",
77 "-Wprivate-header",
78 ]),
79 flag_group(
80 iterate_over = "dependent_module_map_files",
81 flags = [
82 "-fmodule-map-file=%{dependent_module_map_files}",
83 ],
84 ),
85 ],
86 ),
87 ],
88 ),
89 ]
90
rosica71bc38f2019-02-04 02:39:30 -080091all_compile_actions = [
92 ACTION_NAMES.c_compile,
93 ACTION_NAMES.cpp_compile,
94 ACTION_NAMES.linkstamp_compile,
95 ACTION_NAMES.assemble,
96 ACTION_NAMES.preprocess_assemble,
97 ACTION_NAMES.cpp_header_parsing,
98 ACTION_NAMES.cpp_module_compile,
99 ACTION_NAMES.cpp_module_codegen,
100 ACTION_NAMES.clif_match,
101 ACTION_NAMES.lto_backend,
102]
103
104all_cpp_compile_actions = [
105 ACTION_NAMES.cpp_compile,
106 ACTION_NAMES.linkstamp_compile,
107 ACTION_NAMES.cpp_header_parsing,
108 ACTION_NAMES.cpp_module_compile,
109 ACTION_NAMES.cpp_module_codegen,
110 ACTION_NAMES.clif_match,
111]
112
113preprocessor_compile_actions = [
114 ACTION_NAMES.c_compile,
115 ACTION_NAMES.cpp_compile,
116 ACTION_NAMES.linkstamp_compile,
117 ACTION_NAMES.preprocess_assemble,
118 ACTION_NAMES.cpp_header_parsing,
119 ACTION_NAMES.cpp_module_compile,
120 ACTION_NAMES.clif_match,
121]
122
123codegen_compile_actions = [
124 ACTION_NAMES.c_compile,
125 ACTION_NAMES.cpp_compile,
126 ACTION_NAMES.linkstamp_compile,
127 ACTION_NAMES.assemble,
128 ACTION_NAMES.preprocess_assemble,
129 ACTION_NAMES.cpp_module_codegen,
130 ACTION_NAMES.lto_backend,
131]
132
133all_link_actions = [
134 ACTION_NAMES.cpp_link_executable,
135 ACTION_NAMES.cpp_link_dynamic_library,
136 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
137]
138
rosica1112b552019-07-05 06:35:15 -0700139lto_index_actions = [
140 ACTION_NAMES.lto_index_for_executable,
141 ACTION_NAMES.lto_index_for_dynamic_library,
142 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
143]
144
rosica71bc38f2019-02-04 02:39:30 -0800145def _impl(ctx):
rosica71bc38f2019-02-04 02:39:30 -0800146 tool_paths = [
rosica96500b72019-05-21 03:25:53 -0700147 tool_path(name = name, path = path)
148 for name, path in ctx.attr.tool_paths.items()
rosica71bc38f2019-02-04 02:39:30 -0800149 ]
rosica71bc38f2019-02-04 02:39:30 -0800150 action_configs = []
151
plf21b5eb62020-10-21 06:53:30 -0700152 llvm_cov_action = action_config(
153 action_name = ACTION_NAMES.llvm_cov,
154 tools = [
155 tool(
156 path = ctx.attr.tool_paths["llvm-cov"],
157 ),
158 ],
159 )
160
161 action_configs.append(llvm_cov_action)
162
rosica71bc38f2019-02-04 02:39:30 -0800163 supports_pic_feature = feature(
164 name = "supports_pic",
165 enabled = True,
166 )
167 supports_start_end_lib_feature = feature(
168 name = "supports_start_end_lib",
169 enabled = True,
170 )
171
172 default_compile_flags_feature = feature(
173 name = "default_compile_flags",
174 enabled = True,
175 flag_sets = [
176 flag_set(
rosica1112b552019-07-05 06:35:15 -0700177 actions = all_compile_actions,
rosica96500b72019-05-21 03:25:53 -0700178 flag_groups = ([
179 flag_group(
180 flags = ctx.attr.compile_flags,
181 ),
182 ] if ctx.attr.compile_flags else []),
rosica71bc38f2019-02-04 02:39:30 -0800183 ),
184 flag_set(
rosica1112b552019-07-05 06:35:15 -0700185 actions = all_compile_actions,
rosica96500b72019-05-21 03:25:53 -0700186 flag_groups = ([
187 flag_group(
188 flags = ctx.attr.dbg_compile_flags,
189 ),
190 ] if ctx.attr.dbg_compile_flags else []),
rosica71bc38f2019-02-04 02:39:30 -0800191 with_features = [with_feature_set(features = ["dbg"])],
192 ),
193 flag_set(
rosica1112b552019-07-05 06:35:15 -0700194 actions = all_compile_actions,
rosica96500b72019-05-21 03:25:53 -0700195 flag_groups = ([
196 flag_group(
197 flags = ctx.attr.opt_compile_flags,
198 ),
199 ] if ctx.attr.opt_compile_flags else []),
rosica71bc38f2019-02-04 02:39:30 -0800200 with_features = [with_feature_set(features = ["opt"])],
201 ),
202 flag_set(
rosica1112b552019-07-05 06:35:15 -0700203 actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
rosica96500b72019-05-21 03:25:53 -0700204 flag_groups = ([
205 flag_group(
206 flags = ctx.attr.cxx_flags,
207 ),
208 ] if ctx.attr.cxx_flags else []),
rosica71bc38f2019-02-04 02:39:30 -0800209 ),
210 ],
211 )
212
213 default_link_flags_feature = feature(
214 name = "default_link_flags",
215 enabled = True,
216 flag_sets = [
217 flag_set(
rosica1112b552019-07-05 06:35:15 -0700218 actions = all_link_actions + lto_index_actions,
rosica96500b72019-05-21 03:25:53 -0700219 flag_groups = ([
220 flag_group(
221 flags = ctx.attr.link_flags,
222 ),
223 ] if ctx.attr.link_flags else []),
rosica71bc38f2019-02-04 02:39:30 -0800224 ),
225 flag_set(
rosica1112b552019-07-05 06:35:15 -0700226 actions = all_link_actions + lto_index_actions,
rosica96500b72019-05-21 03:25:53 -0700227 flag_groups = ([
228 flag_group(
229 flags = ctx.attr.opt_link_flags,
230 ),
231 ] if ctx.attr.opt_link_flags else []),
rosica71bc38f2019-02-04 02:39:30 -0800232 with_features = [with_feature_set(features = ["opt"])],
233 ),
234 ],
235 )
236
237 dbg_feature = feature(name = "dbg")
238
239 opt_feature = feature(name = "opt")
240
241 sysroot_feature = feature(
242 name = "sysroot",
243 enabled = True,
244 flag_sets = [
245 flag_set(
246 actions = [
rosica96500b72019-05-21 03:25:53 -0700247 ACTION_NAMES.preprocess_assemble,
248 ACTION_NAMES.linkstamp_compile,
249 ACTION_NAMES.c_compile,
250 ACTION_NAMES.cpp_compile,
251 ACTION_NAMES.cpp_header_parsing,
252 ACTION_NAMES.cpp_module_compile,
253 ACTION_NAMES.cpp_module_codegen,
254 ACTION_NAMES.lto_backend,
255 ACTION_NAMES.clif_match,
rosica1112b552019-07-05 06:35:15 -0700256 ] + all_link_actions + lto_index_actions,
rosica71bc38f2019-02-04 02:39:30 -0800257 flag_groups = [
rosica96500b72019-05-21 03:25:53 -0700258 flag_group(
259 flags = ["--sysroot=%{sysroot}"],
260 expand_if_available = "sysroot",
261 ),
rosica71bc38f2019-02-04 02:39:30 -0800262 ],
263 ),
264 ],
265 )
266
267 fdo_optimize_feature = feature(
268 name = "fdo_optimize",
269 flag_sets = [
270 flag_set(
271 actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
272 flag_groups = [
rosica96500b72019-05-21 03:25:53 -0700273 flag_group(
274 flags = [
275 "-fprofile-use=%{fdo_profile_path}",
276 "-fprofile-correction",
277 ],
278 expand_if_available = "fdo_profile_path",
279 ),
rosica71bc38f2019-02-04 02:39:30 -0800280 ],
281 ),
282 ],
283 provides = ["profile"],
284 )
285
286 supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True)
287
288 user_compile_flags_feature = feature(
289 name = "user_compile_flags",
290 enabled = True,
291 flag_sets = [
292 flag_set(
rosica1112b552019-07-05 06:35:15 -0700293 actions = all_compile_actions,
rosica71bc38f2019-02-04 02:39:30 -0800294 flag_groups = [
rosica96500b72019-05-21 03:25:53 -0700295 flag_group(
296 flags = ["%{user_compile_flags}"],
297 iterate_over = "user_compile_flags",
298 expand_if_available = "user_compile_flags",
299 ),
rosica71bc38f2019-02-04 02:39:30 -0800300 ],
301 ),
rosica96500b72019-05-21 03:25:53 -0700302 ],
rosica71bc38f2019-02-04 02:39:30 -0800303 )
304
305 unfiltered_compile_flags_feature = feature(
306 name = "unfiltered_compile_flags",
307 enabled = True,
308 flag_sets = [
309 flag_set(
rosica1112b552019-07-05 06:35:15 -0700310 actions = all_compile_actions,
rosica96500b72019-05-21 03:25:53 -0700311 flag_groups = ([
312 flag_group(
313 flags = ctx.attr.unfiltered_compile_flags,
314 ),
315 ] if ctx.attr.unfiltered_compile_flags else []),
rosica71bc38f2019-02-04 02:39:30 -0800316 ),
317 ],
318 )
319
plf0ff6c092019-05-14 05:22:12 -0700320 library_search_directories_feature = feature(
321 name = "library_search_directories",
322 flag_sets = [
323 flag_set(
rosica1112b552019-07-05 06:35:15 -0700324 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700325 flag_groups = [
326 flag_group(
327 flags = ["-L%{library_search_directories}"],
328 iterate_over = "library_search_directories",
329 expand_if_available = "library_search_directories",
330 ),
331 ],
332 ),
333 ],
334 )
335
336 static_libgcc_feature = feature(
337 name = "static_libgcc",
338 enabled = True,
339 flag_sets = [
340 flag_set(
341 actions = [
342 ACTION_NAMES.cpp_link_executable,
343 ACTION_NAMES.cpp_link_dynamic_library,
rosica1112b552019-07-05 06:35:15 -0700344 ACTION_NAMES.lto_index_for_executable,
345 ACTION_NAMES.lto_index_for_dynamic_library,
plf0ff6c092019-05-14 05:22:12 -0700346 ],
347 flag_groups = [flag_group(flags = ["-static-libgcc"])],
348 with_features = [
rosica96500b72019-05-21 03:25:53 -0700349 with_feature_set(features = ["static_link_cpp_runtimes"]),
plf0ff6c092019-05-14 05:22:12 -0700350 ],
351 ),
352 ],
353 )
354
355 pic_feature = feature(
356 name = "pic",
357 enabled = True,
358 flag_sets = [
359 flag_set(
360 actions = [
361 ACTION_NAMES.assemble,
362 ACTION_NAMES.preprocess_assemble,
363 ACTION_NAMES.linkstamp_compile,
364 ACTION_NAMES.c_compile,
365 ACTION_NAMES.cpp_compile,
366 ACTION_NAMES.cpp_module_codegen,
367 ACTION_NAMES.cpp_module_compile,
368 ],
369 flag_groups = [
370 flag_group(flags = ["-fPIC"], expand_if_available = "pic"),
371 ],
372 ),
373 ],
374 )
375
376 per_object_debug_info_feature = feature(
377 name = "per_object_debug_info",
378 flag_sets = [
379 flag_set(
380 actions = [
381 ACTION_NAMES.assemble,
382 ACTION_NAMES.preprocess_assemble,
383 ACTION_NAMES.c_compile,
384 ACTION_NAMES.cpp_compile,
385 ACTION_NAMES.cpp_module_codegen,
386 ],
387 flag_groups = [
388 flag_group(
389 flags = ["-gsplit-dwarf"],
390 expand_if_available = "per_object_debug_info_file",
391 ),
392 ],
393 ),
394 ],
395 )
396
397 preprocessor_defines_feature = feature(
398 name = "preprocessor_defines",
399 enabled = True,
400 flag_sets = [
401 flag_set(
402 actions = [
403 ACTION_NAMES.preprocess_assemble,
404 ACTION_NAMES.linkstamp_compile,
405 ACTION_NAMES.c_compile,
406 ACTION_NAMES.cpp_compile,
407 ACTION_NAMES.cpp_header_parsing,
408 ACTION_NAMES.cpp_module_compile,
409 ACTION_NAMES.clif_match,
410 ],
411 flag_groups = [
412 flag_group(
413 flags = ["-D%{preprocessor_defines}"],
414 iterate_over = "preprocessor_defines",
415 ),
416 ],
417 ),
418 ],
419 )
420
421 cs_fdo_optimize_feature = feature(
422 name = "cs_fdo_optimize",
423 flag_sets = [
424 flag_set(
425 actions = [ACTION_NAMES.lto_backend],
426 flag_groups = [
427 flag_group(
428 flags = [
429 "-fprofile-use=%{fdo_profile_path}",
Googlera5b16932020-04-13 13:06:59 -0700430 "-Wno-profile-instr-unprofiled",
431 "-Wno-profile-instr-out-of-date",
plf0ff6c092019-05-14 05:22:12 -0700432 "-fprofile-correction",
433 ],
434 expand_if_available = "fdo_profile_path",
435 ),
436 ],
437 ),
438 ],
439 provides = ["csprofile"],
440 )
441
442 autofdo_feature = feature(
443 name = "autofdo",
444 flag_sets = [
445 flag_set(
446 actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
447 flag_groups = [
448 flag_group(
449 flags = [
450 "-fauto-profile=%{fdo_profile_path}",
451 "-fprofile-correction",
452 ],
453 expand_if_available = "fdo_profile_path",
454 ),
455 ],
456 ),
457 ],
458 provides = ["profile"],
459 )
460
461 runtime_library_search_directories_feature = feature(
462 name = "runtime_library_search_directories",
463 flag_sets = [
464 flag_set(
rosica1112b552019-07-05 06:35:15 -0700465 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700466 flag_groups = [
467 flag_group(
468 iterate_over = "runtime_library_search_directories",
469 flag_groups = [
470 flag_group(
471 flags = [
472 "-Wl,-rpath,$EXEC_ORIGIN/%{runtime_library_search_directories}",
473 ],
474 expand_if_true = "is_cc_test",
475 ),
476 flag_group(
477 flags = [
478 "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
479 ],
480 expand_if_false = "is_cc_test",
481 ),
482 ],
483 expand_if_available =
484 "runtime_library_search_directories",
485 ),
486 ],
487 with_features = [
rosica96500b72019-05-21 03:25:53 -0700488 with_feature_set(features = ["static_link_cpp_runtimes"]),
plf0ff6c092019-05-14 05:22:12 -0700489 ],
490 ),
491 flag_set(
rosica1112b552019-07-05 06:35:15 -0700492 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700493 flag_groups = [
494 flag_group(
495 iterate_over = "runtime_library_search_directories",
496 flag_groups = [
497 flag_group(
498 flags = [
499 "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}",
500 ],
501 ),
502 ],
503 expand_if_available =
504 "runtime_library_search_directories",
505 ),
506 ],
507 with_features = [
508 with_feature_set(
509 not_features = ["static_link_cpp_runtimes"],
510 ),
511 ],
512 ),
513 ],
514 )
515
516 fission_support_feature = feature(
517 name = "fission_support",
518 flag_sets = [
519 flag_set(
rosica1112b552019-07-05 06:35:15 -0700520 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700521 flag_groups = [
522 flag_group(
523 flags = ["-Wl,--gdb-index"],
524 expand_if_available = "is_using_fission",
525 ),
526 ],
527 ),
528 ],
529 )
530
531 shared_flag_feature = feature(
532 name = "shared_flag",
533 flag_sets = [
534 flag_set(
535 actions = [
536 ACTION_NAMES.cpp_link_dynamic_library,
537 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
rosica1112b552019-07-05 06:35:15 -0700538 ACTION_NAMES.lto_index_for_dynamic_library,
539 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
plf0ff6c092019-05-14 05:22:12 -0700540 ],
541 flag_groups = [flag_group(flags = ["-shared"])],
542 ),
543 ],
544 )
545
plf0ff6c092019-05-14 05:22:12 -0700546 random_seed_feature = feature(
547 name = "random_seed",
548 enabled = True,
549 flag_sets = [
550 flag_set(
551 actions = [
552 ACTION_NAMES.c_compile,
553 ACTION_NAMES.cpp_compile,
554 ACTION_NAMES.cpp_module_codegen,
555 ACTION_NAMES.cpp_module_compile,
556 ],
557 flag_groups = [
558 flag_group(
559 flags = ["-frandom-seed=%{output_file}"],
560 expand_if_available = "output_file",
561 ),
562 ],
563 ),
564 ],
565 )
566
567 includes_feature = feature(
568 name = "includes",
569 enabled = True,
570 flag_sets = [
571 flag_set(
572 actions = [
573 ACTION_NAMES.preprocess_assemble,
574 ACTION_NAMES.linkstamp_compile,
575 ACTION_NAMES.c_compile,
576 ACTION_NAMES.cpp_compile,
577 ACTION_NAMES.cpp_header_parsing,
578 ACTION_NAMES.cpp_module_compile,
579 ACTION_NAMES.clif_match,
580 ACTION_NAMES.objc_compile,
581 ACTION_NAMES.objcpp_compile,
582 ],
583 flag_groups = [
584 flag_group(
585 flags = ["-include", "%{includes}"],
586 iterate_over = "includes",
587 expand_if_available = "includes",
588 ),
589 ],
590 ),
591 ],
592 )
593
594 fdo_instrument_feature = feature(
595 name = "fdo_instrument",
596 flag_sets = [
597 flag_set(
598 actions = [
599 ACTION_NAMES.c_compile,
600 ACTION_NAMES.cpp_compile,
rosica1112b552019-07-05 06:35:15 -0700601 ] + all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700602 flag_groups = [
603 flag_group(
604 flags = [
605 "-fprofile-generate=%{fdo_instrument_path}",
606 "-fno-data-sections",
607 ],
608 expand_if_available = "fdo_instrument_path",
609 ),
610 ],
611 ),
612 ],
613 provides = ["profile"],
614 )
615
616 cs_fdo_instrument_feature = feature(
617 name = "cs_fdo_instrument",
618 flag_sets = [
619 flag_set(
620 actions = [
621 ACTION_NAMES.c_compile,
622 ACTION_NAMES.cpp_compile,
623 ACTION_NAMES.lto_backend,
rosica1112b552019-07-05 06:35:15 -0700624 ] + all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700625 flag_groups = [
626 flag_group(
627 flags = [
rosica96500b72019-05-21 03:25:53 -0700628 "-fcs-profile-generate=%{cs_fdo_instrument_path}",
plf0ff6c092019-05-14 05:22:12 -0700629 ],
630 expand_if_available = "cs_fdo_instrument_path",
631 ),
632 ],
633 ),
634 ],
635 provides = ["csprofile"],
636 )
637
638 include_paths_feature = feature(
639 name = "include_paths",
640 enabled = True,
641 flag_sets = [
642 flag_set(
643 actions = [
644 ACTION_NAMES.preprocess_assemble,
645 ACTION_NAMES.linkstamp_compile,
646 ACTION_NAMES.c_compile,
647 ACTION_NAMES.cpp_compile,
648 ACTION_NAMES.cpp_header_parsing,
649 ACTION_NAMES.cpp_module_compile,
650 ACTION_NAMES.clif_match,
651 ACTION_NAMES.objc_compile,
652 ACTION_NAMES.objcpp_compile,
653 ],
654 flag_groups = [
655 flag_group(
656 flags = ["-iquote", "%{quote_include_paths}"],
657 iterate_over = "quote_include_paths",
658 ),
659 flag_group(
660 flags = ["-I%{include_paths}"],
661 iterate_over = "include_paths",
662 ),
663 flag_group(
664 flags = ["-isystem", "%{system_include_paths}"],
665 iterate_over = "system_include_paths",
666 ),
667 ],
668 ),
669 ],
670 )
671
672 symbol_counts_feature = feature(
673 name = "symbol_counts",
674 flag_sets = [
675 flag_set(
rosica1112b552019-07-05 06:35:15 -0700676 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700677 flag_groups = [
678 flag_group(
679 flags = [
rosica96500b72019-05-21 03:25:53 -0700680 "-Wl,--print-symbol-counts=%{symbol_counts_output}",
plf0ff6c092019-05-14 05:22:12 -0700681 ],
682 expand_if_available = "symbol_counts_output",
683 ),
684 ],
685 ),
686 ],
687 )
688
689 llvm_coverage_map_format_feature = feature(
690 name = "llvm_coverage_map_format",
691 flag_sets = [
692 flag_set(
693 actions = [
694 ACTION_NAMES.preprocess_assemble,
695 ACTION_NAMES.c_compile,
696 ACTION_NAMES.cpp_compile,
697 ACTION_NAMES.cpp_module_compile,
698 ACTION_NAMES.objc_compile,
699 ACTION_NAMES.objcpp_compile,
700 ],
701 flag_groups = [
702 flag_group(
703 flags = [
704 "-fprofile-instr-generate",
rosica96500b72019-05-21 03:25:53 -0700705 "-fcoverage-mapping",
plf0ff6c092019-05-14 05:22:12 -0700706 ],
707 ),
708 ],
709 ),
710 flag_set(
rosica1112b552019-07-05 06:35:15 -0700711 actions = all_link_actions + lto_index_actions + [
plf0ff6c092019-05-14 05:22:12 -0700712 "objc-executable",
713 "objc++-executable",
714 ],
715 flag_groups = [
rosica96500b72019-05-21 03:25:53 -0700716 flag_group(flags = ["-fprofile-instr-generate"]),
plf0ff6c092019-05-14 05:22:12 -0700717 ],
718 ),
719 ],
720 requires = [feature_set(features = ["coverage"])],
721 provides = ["profile"],
722 )
723
724 strip_debug_symbols_feature = feature(
725 name = "strip_debug_symbols",
726 flag_sets = [
727 flag_set(
rosica1112b552019-07-05 06:35:15 -0700728 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700729 flag_groups = [
730 flag_group(
731 flags = ["-Wl,-S"],
732 expand_if_available = "strip_debug_symbols",
733 ),
734 ],
735 ),
736 ],
737 )
738
739 build_interface_libraries_feature = feature(
740 name = "build_interface_libraries",
741 flag_sets = [
742 flag_set(
743 actions = [
744 ACTION_NAMES.cpp_link_dynamic_library,
745 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
rosica1112b552019-07-05 06:35:15 -0700746 ACTION_NAMES.lto_index_for_dynamic_library,
747 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
plf0ff6c092019-05-14 05:22:12 -0700748 ],
749 flag_groups = [
750 flag_group(
751 flags = [
752 "%{generate_interface_library}",
753 "%{interface_library_builder_path}",
754 "%{interface_library_input_path}",
755 "%{interface_library_output_path}",
756 ],
757 expand_if_available = "generate_interface_library",
758 ),
759 ],
760 with_features = [
761 with_feature_set(
762 features = ["supports_interface_shared_libraries"],
763 ),
764 ],
765 ),
766 ],
767 )
768
769 libraries_to_link_feature = feature(
770 name = "libraries_to_link",
771 flag_sets = [
772 flag_set(
rosica1112b552019-07-05 06:35:15 -0700773 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700774 flag_groups = [
775 flag_group(
776 iterate_over = "libraries_to_link",
777 flag_groups = [
778 flag_group(
779 flags = ["-Wl,--start-lib"],
780 expand_if_equal = variable_with_value(
781 name = "libraries_to_link.type",
782 value = "object_file_group",
783 ),
784 ),
785 flag_group(
786 flags = ["-Wl,-whole-archive"],
787 expand_if_true =
788 "libraries_to_link.is_whole_archive",
789 ),
790 flag_group(
791 flags = ["%{libraries_to_link.object_files}"],
792 iterate_over = "libraries_to_link.object_files",
793 expand_if_equal = variable_with_value(
794 name = "libraries_to_link.type",
795 value = "object_file_group",
796 ),
797 ),
798 flag_group(
799 flags = ["%{libraries_to_link.name}"],
800 expand_if_equal = variable_with_value(
801 name = "libraries_to_link.type",
802 value = "object_file",
803 ),
804 ),
805 flag_group(
806 flags = ["%{libraries_to_link.name}"],
807 expand_if_equal = variable_with_value(
808 name = "libraries_to_link.type",
809 value = "interface_library",
810 ),
811 ),
812 flag_group(
813 flags = ["%{libraries_to_link.name}"],
814 expand_if_equal = variable_with_value(
815 name = "libraries_to_link.type",
816 value = "static_library",
817 ),
818 ),
819 flag_group(
820 flags = ["-l%{libraries_to_link.name}"],
821 expand_if_equal = variable_with_value(
822 name = "libraries_to_link.type",
823 value = "dynamic_library",
824 ),
825 ),
826 flag_group(
827 flags = ["-l:%{libraries_to_link.name}"],
828 expand_if_equal = variable_with_value(
829 name = "libraries_to_link.type",
830 value = "versioned_dynamic_library",
831 ),
832 ),
833 flag_group(
834 flags = ["-Wl,-no-whole-archive"],
835 expand_if_true = "libraries_to_link.is_whole_archive",
836 ),
837 flag_group(
838 flags = ["-Wl,--end-lib"],
839 expand_if_equal = variable_with_value(
840 name = "libraries_to_link.type",
841 value = "object_file_group",
842 ),
843 ),
844 ],
845 expand_if_available = "libraries_to_link",
846 ),
847 flag_group(
848 flags = ["-Wl,@%{thinlto_param_file}"],
849 expand_if_true = "thinlto_param_file",
850 ),
851 ],
852 ),
853 ],
854 )
855
856 user_link_flags_feature = feature(
857 name = "user_link_flags",
858 flag_sets = [
859 flag_set(
rosica1112b552019-07-05 06:35:15 -0700860 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700861 flag_groups = [
862 flag_group(
863 flags = ["%{user_link_flags}"],
864 iterate_over = "user_link_flags",
865 expand_if_available = "user_link_flags",
866 ),
Marcel Hlopkoab9c1f52019-06-19 00:45:58 -0700867 ] + ([flag_group(flags = ctx.attr.link_libs)] if ctx.attr.link_libs else []),
plf0ff6c092019-05-14 05:22:12 -0700868 ),
869 ],
870 )
871
872 fdo_prefetch_hints_feature = feature(
873 name = "fdo_prefetch_hints",
874 flag_sets = [
875 flag_set(
876 actions = [
877 ACTION_NAMES.c_compile,
878 ACTION_NAMES.cpp_compile,
879 ACTION_NAMES.lto_backend,
880 ],
881 flag_groups = [
882 flag_group(
883 flags = [
Googlera5b16932020-04-13 13:06:59 -0700884 "-mllvm",
885 "-prefetch-hints-file=%{fdo_prefetch_hints_path}",
plf0ff6c092019-05-14 05:22:12 -0700886 ],
887 expand_if_available = "fdo_prefetch_hints_path",
888 ),
889 ],
890 ),
891 ],
892 )
893
894 linkstamps_feature = feature(
895 name = "linkstamps",
896 flag_sets = [
897 flag_set(
rosica1112b552019-07-05 06:35:15 -0700898 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700899 flag_groups = [
900 flag_group(
901 flags = ["%{linkstamp_paths}"],
902 iterate_over = "linkstamp_paths",
903 expand_if_available = "linkstamp_paths",
904 ),
905 ],
906 ),
907 ],
908 )
909
910 gcc_coverage_map_format_feature = feature(
911 name = "gcc_coverage_map_format",
912 flag_sets = [
913 flag_set(
914 actions = [
915 ACTION_NAMES.preprocess_assemble,
916 ACTION_NAMES.c_compile,
917 ACTION_NAMES.cpp_compile,
918 ACTION_NAMES.cpp_module_compile,
919 ACTION_NAMES.objc_compile,
920 ACTION_NAMES.objcpp_compile,
921 "objc-executable",
922 "objc++-executable",
923 ],
924 flag_groups = [
925 flag_group(
926 flags = ["-fprofile-arcs", "-ftest-coverage"],
927 expand_if_available = "gcov_gcno_file",
928 ),
929 ],
930 ),
931 flag_set(
rosica1112b552019-07-05 06:35:15 -0700932 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -0700933 flag_groups = [flag_group(flags = ["--coverage"])],
934 ),
935 ],
936 requires = [feature_set(features = ["coverage"])],
937 provides = ["profile"],
938 )
939
940 archiver_flags_feature = feature(
941 name = "archiver_flags",
942 flag_sets = [
943 flag_set(
944 actions = [ACTION_NAMES.cpp_link_static_library],
945 flag_groups = [
946 flag_group(flags = ["rcsD"]),
947 flag_group(
948 flags = ["%{output_execpath}"],
949 expand_if_available = "output_execpath",
950 ),
951 ],
952 ),
953 flag_set(
954 actions = [ACTION_NAMES.cpp_link_static_library],
955 flag_groups = [
956 flag_group(
957 iterate_over = "libraries_to_link",
958 flag_groups = [
959 flag_group(
960 flags = ["%{libraries_to_link.name}"],
961 expand_if_equal = variable_with_value(
962 name = "libraries_to_link.type",
963 value = "object_file",
964 ),
965 ),
966 flag_group(
967 flags = ["%{libraries_to_link.object_files}"],
968 iterate_over = "libraries_to_link.object_files",
969 expand_if_equal = variable_with_value(
970 name = "libraries_to_link.type",
971 value = "object_file_group",
972 ),
973 ),
974 ],
975 expand_if_available = "libraries_to_link",
976 ),
977 ],
978 ),
979 ],
980 )
981
982 force_pic_flags_feature = feature(
983 name = "force_pic_flags",
984 flag_sets = [
985 flag_set(
rosica1112b552019-07-05 06:35:15 -0700986 actions = [
987 ACTION_NAMES.cpp_link_executable,
988 ACTION_NAMES.lto_index_for_executable,
989 ],
plf0ff6c092019-05-14 05:22:12 -0700990 flag_groups = [
991 flag_group(
992 flags = ["-pie"],
993 expand_if_available = "force_pic",
994 ),
995 ],
996 ),
997 ],
998 )
999
1000 dependency_file_feature = feature(
1001 name = "dependency_file",
1002 enabled = True,
1003 flag_sets = [
1004 flag_set(
1005 actions = [
1006 ACTION_NAMES.assemble,
1007 ACTION_NAMES.preprocess_assemble,
1008 ACTION_NAMES.c_compile,
1009 ACTION_NAMES.cpp_compile,
1010 ACTION_NAMES.cpp_module_compile,
1011 ACTION_NAMES.objc_compile,
1012 ACTION_NAMES.objcpp_compile,
1013 ACTION_NAMES.cpp_header_parsing,
1014 ACTION_NAMES.clif_match,
1015 ],
1016 flag_groups = [
1017 flag_group(
1018 flags = ["-MD", "-MF", "%{dependency_file}"],
1019 expand_if_available = "dependency_file",
1020 ),
1021 ],
1022 ),
1023 ],
1024 )
1025
rosica1112b552019-07-05 06:35:15 -07001026 dynamic_library_linker_tool_path = tool_paths
plf0ff6c092019-05-14 05:22:12 -07001027 dynamic_library_linker_tool_feature = feature(
1028 name = "dynamic_library_linker_tool",
1029 flag_sets = [
1030 flag_set(
1031 actions = [
1032 ACTION_NAMES.cpp_link_dynamic_library,
1033 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
rosica1112b552019-07-05 06:35:15 -07001034 ACTION_NAMES.lto_index_for_dynamic_library,
1035 ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
plf0ff6c092019-05-14 05:22:12 -07001036 ],
1037 flag_groups = [
1038 flag_group(
1039 flags = [" + cppLinkDynamicLibraryToolPath + "],
1040 expand_if_available = "generate_interface_library",
1041 ),
1042 ],
1043 with_features = [
1044 with_feature_set(
1045 features = ["supports_interface_shared_libraries"],
1046 ),
1047 ],
1048 ),
1049 ],
1050 )
1051
1052 output_execpath_flags_feature = feature(
1053 name = "output_execpath_flags",
1054 flag_sets = [
1055 flag_set(
rosica1112b552019-07-05 06:35:15 -07001056 actions = all_link_actions + lto_index_actions,
plf0ff6c092019-05-14 05:22:12 -07001057 flag_groups = [
1058 flag_group(
1059 flags = ["-o", "%{output_execpath}"],
1060 expand_if_available = "output_execpath",
1061 ),
1062 ],
1063 ),
1064 ],
1065 )
1066
rosica96500b72019-05-21 03:25:53 -07001067 # Note that we also set --coverage for c++-link-nodeps-dynamic-library. The
1068 # generated code contains references to gcov symbols, and the dynamic linker
1069 # can't resolve them unless the library is linked against gcov.
1070 coverage_feature = feature(
1071 name = "coverage",
1072 provides = ["profile"],
1073 flag_sets = [
1074 flag_set(
1075 actions = [
1076 ACTION_NAMES.preprocess_assemble,
1077 ACTION_NAMES.c_compile,
1078 ACTION_NAMES.cpp_compile,
1079 ACTION_NAMES.cpp_header_parsing,
1080 ACTION_NAMES.cpp_module_compile,
1081 ],
1082 flag_groups = ([
1083 flag_group(flags = ctx.attr.coverage_compile_flags),
1084 ] if ctx.attr.coverage_compile_flags else []),
1085 ),
1086 flag_set(
rosica1112b552019-07-05 06:35:15 -07001087 actions = all_link_actions + lto_index_actions,
rosica96500b72019-05-21 03:25:53 -07001088 flag_groups = ([
1089 flag_group(flags = ctx.attr.coverage_link_flags),
1090 ] if ctx.attr.coverage_link_flags else []),
1091 ),
1092 ],
1093 )
plf0ff6c092019-05-14 05:22:12 -07001094
rosicae2a9e4a2019-08-27 03:33:31 -07001095 thinlto_feature = feature(
1096 name = "thin_lto",
1097 flag_sets = [
1098 flag_set(
1099 actions = [
1100 ACTION_NAMES.c_compile,
1101 ACTION_NAMES.cpp_compile,
1102 ] + all_link_actions + lto_index_actions,
1103 flag_groups = [
1104 flag_group(flags = ["-flto=thin"]),
1105 flag_group(
1106 expand_if_available = "lto_indexing_bitcode_file",
1107 flags = [
1108 "-Xclang",
1109 "-fthin-link-bitcode=%{lto_indexing_bitcode_file}",
1110 ],
1111 ),
1112 ],
1113 ),
1114 flag_set(
1115 actions = [ACTION_NAMES.linkstamp_compile],
1116 flag_groups = [flag_group(flags = ["-DBUILD_LTO_TYPE=thin"])],
1117 ),
1118 flag_set(
1119 actions = lto_index_actions,
1120 flag_groups = [
1121 flag_group(flags = [
1122 "-flto=thin",
1123 "-Wl,-plugin-opt,thinlto-index-only%{thinlto_optional_params_file}",
1124 "-Wl,-plugin-opt,thinlto-emit-imports-files",
1125 "-Wl,-plugin-opt,thinlto-prefix-replace=%{thinlto_prefix_replace}",
1126 ]),
1127 flag_group(
1128 expand_if_available = "thinlto_object_suffix_replace",
1129 flags = [
1130 "-Wl,-plugin-opt,thinlto-object-suffix-replace=%{thinlto_object_suffix_replace}",
1131 ],
1132 ),
1133 flag_group(
1134 expand_if_available = "thinlto_merged_object_file",
1135 flags = [
1136 "-Wl,-plugin-opt,obj-path=%{thinlto_merged_object_file}",
1137 ],
1138 ),
1139 ],
1140 ),
1141 flag_set(
1142 actions = [ACTION_NAMES.lto_backend],
1143 flag_groups = [
1144 flag_group(flags = [
1145 "-c",
1146 "-fthinlto-index=%{thinlto_index}",
1147 "-o",
1148 "%{thinlto_output_object_file}",
1149 "-x",
1150 "ir",
1151 "%{thinlto_input_bitcode_file}",
1152 ]),
1153 ],
1154 ),
1155 ],
1156 )
1157
rosica96500b72019-05-21 03:25:53 -07001158 is_linux = ctx.attr.target_libc != "macosx"
1159
1160 # TODO(#8303): Mac crosstool should also declare every feature.
plf0ff6c092019-05-14 05:22:12 -07001161 if is_linux:
1162 features = [
1163 dependency_file_feature,
1164 random_seed_feature,
1165 pic_feature,
1166 per_object_debug_info_feature,
1167 preprocessor_defines_feature,
1168 includes_feature,
1169 include_paths_feature,
1170 fdo_instrument_feature,
1171 cs_fdo_instrument_feature,
1172 cs_fdo_optimize_feature,
rosicae2a9e4a2019-08-27 03:33:31 -07001173 thinlto_feature,
plf0ff6c092019-05-14 05:22:12 -07001174 fdo_prefetch_hints_feature,
1175 autofdo_feature,
1176 build_interface_libraries_feature,
1177 dynamic_library_linker_tool_feature,
1178 symbol_counts_feature,
1179 shared_flag_feature,
1180 linkstamps_feature,
1181 output_execpath_flags_feature,
1182 runtime_library_search_directories_feature,
1183 library_search_directories_feature,
1184 archiver_flags_feature,
plf0ff6c092019-05-14 05:22:12 -07001185 force_pic_flags_feature,
plf0ff6c092019-05-14 05:22:12 -07001186 fission_support_feature,
rosica96500b72019-05-21 03:25:53 -07001187 strip_debug_symbols_feature,
1188 coverage_feature,
plf0ff6c092019-05-14 05:22:12 -07001189 supports_pic_feature,
rosica96500b72019-05-21 03:25:53 -07001190 ] + (
1191 [
1192 supports_start_end_lib_feature,
1193 ] if ctx.attr.supports_start_end_lib else []
1194 ) + [
plf0ff6c092019-05-14 05:22:12 -07001195 default_compile_flags_feature,
1196 default_link_flags_feature,
1197 libraries_to_link_feature,
plf21cf0542019-05-20 09:01:24 -07001198 user_link_flags_feature,
1199 static_libgcc_feature,
plf0ff6c092019-05-14 05:22:12 -07001200 fdo_optimize_feature,
1201 supports_dynamic_linker_feature,
1202 dbg_feature,
1203 opt_feature,
1204 user_compile_flags_feature,
1205 sysroot_feature,
1206 unfiltered_compile_flags_feature,
hlopko8b9f7462020-05-26 05:39:52 -07001207 ] + layering_check_features(ctx.attr.compiler)
plf0ff6c092019-05-14 05:22:12 -07001208 else:
rosicabeaba922019-05-20 02:36:26 -07001209 features = [
plf0ff6c092019-05-14 05:22:12 -07001210 supports_pic_feature,
rosica96500b72019-05-21 03:25:53 -07001211 ] + (
1212 [
1213 supports_start_end_lib_feature,
1214 ] if ctx.attr.supports_start_end_lib else []
1215 ) + [
1216 coverage_feature,
plf0ff6c092019-05-14 05:22:12 -07001217 default_compile_flags_feature,
1218 default_link_flags_feature,
1219 fdo_optimize_feature,
1220 supports_dynamic_linker_feature,
1221 dbg_feature,
1222 opt_feature,
1223 user_compile_flags_feature,
1224 sysroot_feature,
1225 unfiltered_compile_flags_feature,
hlopko8b9f7462020-05-26 05:39:52 -07001226 ] + layering_check_features(ctx.attr.compiler)
rosica71bc38f2019-02-04 02:39:30 -08001227
rosica71bc38f2019-02-04 02:39:30 -08001228 return cc_common.create_cc_toolchain_config_info(
1229 ctx = ctx,
1230 features = features,
1231 action_configs = action_configs,
rosica96500b72019-05-21 03:25:53 -07001232 cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
1233 toolchain_identifier = ctx.attr.toolchain_identifier,
1234 host_system_name = ctx.attr.host_system_name,
1235 target_system_name = ctx.attr.target_system_name,
1236 target_cpu = ctx.attr.cpu,
1237 target_libc = ctx.attr.target_libc,
1238 compiler = ctx.attr.compiler,
1239 abi_version = ctx.attr.abi_version,
1240 abi_libc_version = ctx.attr.abi_libc_version,
rosica71bc38f2019-02-04 02:39:30 -08001241 tool_paths = tool_paths,
erenonda345f12020-10-29 08:25:15 -07001242 builtin_sysroot = ctx.attr.builtin_sysroot,
rosica71bc38f2019-02-04 02:39:30 -08001243 )
1244
1245cc_toolchain_config = rule(
1246 implementation = _impl,
1247 attrs = {
rosica96500b72019-05-21 03:25:53 -07001248 "cpu": attr.string(mandatory = True),
1249 "compiler": attr.string(mandatory = True),
1250 "toolchain_identifier": attr.string(mandatory = True),
1251 "host_system_name": attr.string(mandatory = True),
1252 "target_system_name": attr.string(mandatory = True),
1253 "target_libc": attr.string(mandatory = True),
1254 "abi_version": attr.string(mandatory = True),
1255 "abi_libc_version": attr.string(mandatory = True),
1256 "cxx_builtin_include_directories": attr.string_list(),
1257 "tool_paths": attr.string_dict(),
1258 "compile_flags": attr.string_list(),
1259 "dbg_compile_flags": attr.string_list(),
1260 "opt_compile_flags": attr.string_list(),
1261 "cxx_flags": attr.string_list(),
1262 "link_flags": attr.string_list(),
Marcel Hlopkoab9c1f52019-06-19 00:45:58 -07001263 "link_libs": attr.string_list(),
rosica96500b72019-05-21 03:25:53 -07001264 "opt_link_flags": attr.string_list(),
1265 "unfiltered_compile_flags": attr.string_list(),
1266 "coverage_compile_flags": attr.string_list(),
1267 "coverage_link_flags": attr.string_list(),
1268 "supports_start_end_lib": attr.bool(),
erenonda345f12020-10-29 08:25:15 -07001269 "builtin_sysroot": attr.string(),
rosica71bc38f2019-02-04 02:39:30 -08001270 },
1271 provides = [CcToolchainConfigInfo],
1272)