-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdefs.bzl
More file actions
1198 lines (1090 loc) · 40.2 KB
/
Copy pathdefs.bzl
File metadata and controls
1198 lines (1090 loc) · 40.2 KB
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) Joby Aviation 2022
# Original authors: Thulio Ferraz Assis (thulio@aspect.dev), Aspect.dev
#
# Copyright (c) Thulio Ferraz Assis 2024
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This module provides the definitions for registering a GCC toolchain for C and C++.
"""
load("@bazel_lib//lib:utils.bzl", "is_bzlmod_enabled")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_user_netrc", "use_netrc")
_AUTO_BINARY_PREFIX = "auto"
_TRIPLE_BINARY_PREFIXES = {
"aarch64": "aarch64-linux-",
"armv7": "arm-linux-gnueabihf-",
"x86_64": "x86_64-linux-",
}
def _detect_binary_prefix(rctx, target_arch):
"""Resolves the prefix that `bin/` binaries carry in the extracted toolchain.
Some gcc-builds releases lay the binaries out under the target triple prefix and
others leave them unprefixed. `as` tells the two layouts apart, since the
unprefixed layout never provides a prefixed alias for it.
Args:
rctx: The repository context, with the toolchain already extracted.
target_arch: The target architecture of the toolchain.
Returns:
The binary prefix, either the target triple prefix or the empty string.
"""
triple_prefix = _TRIPLE_BINARY_PREFIXES[target_arch]
if rctx.path("bin/{}as".format(triple_prefix)).exists:
return triple_prefix
return ""
def _gcc_toolchain_impl(rctx):
versions = json.decode(rctx.attr.gcc_versions)
url = versions[rctx.attr.gcc_version][rctx.attr.target_arch]["url"]
sha256 = versions[rctx.attr.gcc_version][rctx.attr.target_arch]["sha256"]
rctx.download_and_extract(
url = url,
sha256 = sha256,
auth = use_netrc(read_user_netrc(rctx), [url], {}),
)
absolute_toolchain_root = str(rctx.path("."))
execroot = paths.normalize(paths.join(absolute_toolchain_root, "..", ".."))
toolchain_root = paths.relativize(absolute_toolchain_root, execroot)
def _format_flags(flags):
return [
flag.replace("%workspace%", toolchain_root)
for flag in flags
]
def _format_builtins(builtins):
# In bzlmod, external dependencies have their own canonical subdirectories, so we can't rely on %workspace%.
# Instead, we want to resolve paths relative to the root of the module where the toolchain is installed.
if is_bzlmod_enabled():
return [d.replace("%workspace%", toolchain_root) for d in builtins]
return builtins
target_arch = rctx.attr.target_arch
binary_prefix = rctx.attr.binary_prefix
if binary_prefix == _AUTO_BINARY_PREFIX:
binary_prefix = _detect_binary_prefix(rctx, target_arch)
tool_paths = _render_tool_paths(rctx, toolchain_root, binary_prefix)
rctx.file("tool_paths.bzl", "tool_paths = {}".format(str(tool_paths)))
include_prefix = None
if target_arch == ARCHS.aarch64:
include_prefix = "aarch64-linux/"
elif target_arch == ARCHS.armv7:
include_prefix = "arm-linux-gnueabihf/"
elif target_arch == ARCHS.x86_64:
include_prefix = "x86_64-linux/"
c_builtin_includes = [
include.format(
gcc_version = rctx.attr.gcc_version,
include_prefix = include_prefix,
)
for include in [
"%workspace%/lib/gcc/{include_prefix}{gcc_version}/include",
"%workspace%/lib/gcc/{include_prefix}{gcc_version}/include-fixed",
] + ([
"%workspace%/{include_prefix}include",
] if target_arch != ARCHS.x86_64 else []) + [
"%workspace%/sysroot/usr/include",
]
]
cxx_builtin_includes = []
if target_arch == ARCHS.x86_64:
cxx_builtin_includes.extend([
include.format(
gcc_version = rctx.attr.gcc_version,
include_prefix = include_prefix,
)
for include in [
"%workspace%/include/c++/{gcc_version}",
"%workspace%/include/c++/{gcc_version}/{include_prefix}",
"%workspace%/include/c++/{gcc_version}/backward",
]
])
else:
cxx_builtin_includes.extend([
include.format(
gcc_version = rctx.attr.gcc_version,
include_prefix = include_prefix,
)
for include in [
"%workspace%/{include_prefix}include/c++/{gcc_version}",
"%workspace%/{include_prefix}include/c++/{gcc_version}/{include_prefix}",
"%workspace%/{include_prefix}include/c++/{gcc_version}/backward",
]
])
f_builtin_includes = [
include.format(
gcc_version = rctx.attr.gcc_version,
include_prefix = include_prefix,
)
for include in [
"%workspace%/lib/gcc/{include_prefix}{gcc_version}/finclude",
]
]
builtin_include_directories = []
builtin_include_directories.extend(c_builtin_includes)
builtin_include_directories.extend(cxx_builtin_includes)
if rctx.attr.enable_fortran:
builtin_include_directories.extend(f_builtin_includes)
builtin_include_directories.extend(rctx.attr.includes)
if rctx.attr.enable_fortran:
builtin_include_directories.extend(rctx.attr.fincludes)
extra_cflags = [
"-nostdinc",
"-B%workspace%/bin",
"-B%workspace%/xbin",
]
extra_cflags.extend([
"-isystem{}".format(include)
for include in c_builtin_includes
])
extra_cflags.extend([
"-I{}".format(include)
for include in rctx.attr.includes
])
extra_cflags.extend(rctx.attr.extra_cflags)
extra_cxxflags = [
"-nostdinc",
"-nostdinc++",
"-B%workspace%/bin",
"-B%workspace%/xbin",
]
extra_cxxflags.extend([
"-isystem{}".format(include)
for include in cxx_builtin_includes
])
extra_cxxflags.extend([
"-isystem{}".format(include)
for include in c_builtin_includes
])
extra_cxxflags.extend([
"-I{}".format(include)
for include in rctx.attr.includes
])
extra_cxxflags.extend(rctx.attr.extra_cxxflags)
extra_fflags = [
"-nostdinc",
"-B%workspace%/bin",
"-B%workspace%/xbin",
]
extra_fflags.extend([
"-I{}".format(include)
for include in f_builtin_includes
])
extra_fflags.extend([
"-I{}".format(include)
for include in c_builtin_includes
])
extra_fflags.extend([
"-I{}".format(finclude)
for finclude in rctx.attr.fincludes
])
extra_fflags.extend(rctx.attr.extra_fflags)
# Note: we deliberately do NOT add `-B`/`-L` flags for the sysroot lib dirs
# (`%workspace%/sysroot/lib`, `%workspace%/sysroot/usr/lib`). GCC already
# searches them via its built-in sysroot, so they are redundant. They are
# also harmful to `lld` (the `linker-lld` feature): these flags are relative,
# so in the sandbox they resolve against the action's cwd, and `lld` then
# finds glibc's `libm.so`/`libc.so` linker scripts via a path that is not
# under the absolute sysroot. Unlike BFD, `lld` then refuses to rewrite the
# absolute `GROUP(/lib/libm.so.6)` entries in those scripts and the link
# fails with `cannot open /lib/libm.so.6`. Relying on GCC's built-in sysroot
# keeps both linkers working.
extra_ldflags = [
lib.format(
include_prefix = include_prefix,
)
for lib in [
"-B%workspace%/bin",
"-B%workspace%/xbin",
"-B%workspace%/lib",
"-B%workspace%/{include_prefix}lib",
"-B%workspace%/lib64",
"-B%workspace%/{include_prefix}lib64",
"-L%workspace%/lib",
"-L%workspace%/{include_prefix}lib",
"-L%workspace%/lib64",
"-L%workspace%/{include_prefix}lib64",
]
]
extra_ldflags.extend(rctx.attr.extra_ldflags)
extra_asmflags = []
extra_asmflags.extend([
"-isystem{}".format(include)
for include in c_builtin_includes
])
extra_asmflags.extend([
"-I{}".format(include)
for include in rctx.attr.includes
])
extra_asmflags.extend(rctx.attr.extra_asmflags)
rctx.file("BUILD.bazel", _TOOLCHAIN_BUILD_FILE_CONTENT.format(
gcc_toolchain_workspace_name = rctx.attr.gcc_toolchain_workspace_name,
enable_fortran = str(rctx.attr.enable_fortran),
binary_prefix = binary_prefix,
include_prefix = include_prefix,
# Includes
cxx_builtin_include_directories = _format_builtins(builtin_include_directories),
# Flags
extra_cflags = _format_flags(extra_cflags),
extra_cxxflags = _format_flags(extra_cxxflags),
extra_fflags = _format_flags(extra_fflags) if rctx.attr.enable_fortran else [],
extra_ldflags = _format_flags(extra_ldflags),
extra_asmflags = _format_flags(extra_asmflags),
# cc_toolchain attributes
supports_param_files = 1 if rctx.attr.supports_param_files else 0,
# Fortran, if enabled
**_fortran_vars(
rctx.attr.enable_fortran,
rctx.attr.gcc_toolchain_workspace_name,
include_prefix,
binary_prefix,
)
))
def _fortran_vars(enable_fortran, gcc_toolchain_workspace_name, include_prefix, binary_prefix):
load_ = ""
toolchain = ""
includes = ""
bin_gfortran = ""
xbin_gfortran = ""
f951 = ""
specs = ""
if enable_fortran:
load_ = 'load("@{gcc_toolchain_workspace_name}//toolchain/fortran:defs.bzl", "fortran_toolchain")'.format(
gcc_toolchain_workspace_name = gcc_toolchain_workspace_name,
)
toolchain = '''fortran_toolchain(
name = "_fortran_toolchain",
cc_toolchain = ":_cc_toolchain",
)'''
includes = '''
# Fortran includes
"lib/gcc/{include_prefix}*/finclude/**",'''.format(include_prefix = include_prefix)
bin_gfortran = ' "bin/{binary_prefix}gfortran",'.format(binary_prefix = binary_prefix)
xbin_gfortran = ' "xbin/gfortran",'
f951 = ' "**/libexec/gcc/**/f951",'
specs = ''' # Fortran spec files.
"**/lib*/libgfortran.spec",
"**/lib*/libgomp.spec",'''
return dict(
fortran_load = load_,
fortran_toolchain = toolchain,
fortran_includes = includes,
fortran_bin_gfortran = bin_gfortran,
fortran_xbin_gfortran = xbin_gfortran,
fortran_f951 = f951,
fortran_specs = specs,
)
AVAILABLE_GCC_VERSIONS = {
"12.5.0": {
"aarch64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/18082025/gcc-toolchain-12.5.0-aarch64.tar.xz",
"sha256": "7b0e25133a98d44b648a925ba11f64a3adc470e87668af80ce2c3af389ebe9be",
},
"armv7": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/18082025/gcc-toolchain-12.5.0-armv7.tar.xz",
"sha256": "a0ef76c8cc517b3d76dd2f09b1a371975b2ff1082e2f9372ed79af01b9292934",
},
"x86_64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/18082025/gcc-toolchain-12.5.0-x86_64.tar.xz",
"sha256": "51076e175839b434bb2dc0006c0096916df585e8c44666d35b0e3ce821d535db",
},
},
"13.4.0": {
"aarch64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/18082025/gcc-toolchain-13.4.0-aarch64.tar.xz",
"sha256": "770cf6bf62bdf78763de526d3a9f5cae4c19f1a3aca0ef8f18b05f1a46d1ffaf",
},
"armv7": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/18082025/gcc-toolchain-13.4.0-armv7.tar.xz",
"sha256": "1b2739b5003c5a3f0ab7c4cc7fb95cc99c0e933982512de7255c2bd9ced757ad",
},
"x86_64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/18082025/gcc-toolchain-13.4.0-x86_64.tar.xz",
"sha256": "d96071c1b98499afd7b7b56ebd69ad414020edf66e982004acffe7df8aaf7e02",
},
},
"14.3.0": {
"aarch64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-14.3.0-aarch64.tar.xz",
"sha256": "b5a73bc840938c8dbb49e2f15b8b8d63e5c33beae0be2aa9a7c52b593522cdcd",
},
"armv7": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-14.3.0-armv7.tar.xz",
"sha256": "376684c062a23e84b619a717fa4c7bba336211c8b48169f76eb5aa6ed4da6bb8",
},
"x86_64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-14.3.0-x86_64.tar.xz",
"sha256": "d155a38e4acff9588df466f0edc98c1a2c54d09bc0162805ba38908cfd7a1d28",
},
},
"15.2.0": {
"aarch64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-15.2.0-aarch64.tar.xz",
"sha256": "f38696590786e7c99d3bb5b8ce9ed66be6224d505fa45dcae0b2a6ec07eb0570",
},
"armv7": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-15.2.0-armv7.tar.xz",
"sha256": "8ecb5b35aa25efe78772701c70ed27eda727264303d03cffc372a6f24f18be90",
},
"x86_64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-15.2.0-x86_64.tar.xz",
"sha256": "f31edf791877935258dcc864afbfb7c9f9238be9f7d9da0ee57f5bc121074457",
},
},
"16.1.0": {
"aarch64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-16.1.0-aarch64.tar.xz",
"sha256": "4331e513156a699c1015fb94021497306f0a896520efbad8b3e16418eb683468",
},
"armv7": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-16.1.0-armv7.tar.xz",
"sha256": "8d42c1fd130ccb170fe8d5d17148ae2d3f97134ac0009fdcac87550fec6a6289",
},
"x86_64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/08072026/gcc-toolchain-16.1.0-x86_64.tar.xz",
"sha256": "cfd8ca5bc365c1c838825ed6e44c7b2c309aada25791f76ef73b1aec819e362e",
},
},
"16.2.0": {
"aarch64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/10082026/gcc-toolchain-16.2.0-aarch64.tar.xz",
"sha256": "6407b35116f21c59e98ab5ad68ddd8ff5f3e0469723a5d465ec08ed615b11a55",
},
"armv7": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/10082026/gcc-toolchain-16.2.0-armv7.tar.xz",
"sha256": "515da8a22fa560002d3d149ad701acb725502c70f7bf8d4905f90b0830d38238",
},
"x86_64": {
"url": "https://github.com/f0rmiga/gcc-builds/releases/download/10082026/gcc-toolchain-16.2.0-x86_64.tar.xz",
"sha256": "54af34c821e59b03ded8f82d3a1104426ec4baaf3b226233e1fd76ad5dcb78cf",
},
},
}
DEFAULT_GCC_VERSION = "16.2.0"
_FEATURE_ATTRS = {
"binary_prefix": attr.string(
doc = "An explicit prefix used by each binary in bin/. Defaults to detecting the prefix from the extracted toolchain.",
default = _AUTO_BINARY_PREFIX,
),
"enable_fortran": attr.bool(
doc = "Enable Fortran support in the toolchain (the default).",
default = True,
),
"extra_cflags": attr.string_list(
doc = "Extra flags for compiling C.",
default = [],
),
"extra_cxxflags": attr.string_list(
doc = "Extra flags for compiling C++.",
default = [],
),
"extra_fflags": attr.string_list(
doc = "Extra flags for compiling Fortran, if enabled.",
default = [],
),
"extra_ldflags": attr.string_list(
doc = "Extra flags for linking." +
" %workspace% is rendered to the toolchain root path." +
" See https://github.com/bazelbuild/bazel/blob/a48e246e/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java#L234-L254.",
default = [],
),
"extra_asmflags": attr.string_list(
doc = "Extra flags for the assembly preprocessor.",
default = [],
),
"supports_param_files": attr.bool(
doc = "Set `supports_param_files = 1` on the generated `cc_toolchain`," +
" which lets Bazel pass linker arguments via an `@params` file." +
" Enable this when link command lines for large targets overflow `ARG_MAX`" +
" (e.g. `collect2: posix_spawn: Argument list too long`)." +
" Off by default to preserve historical behavior.",
default = False,
),
"gcc_toolchain_workspace_name": attr.string(
doc = "The name given to the gcc-toolchain repository, if the default was not used.",
default = "gcc_toolchain",
),
"gcc_version": attr.string(
default = DEFAULT_GCC_VERSION,
doc = "The version of GCC.",
),
"gcc_versions": attr.string(
default = json.encode(AVAILABLE_GCC_VERSIONS),
doc = "A JSON dictionary of GCC versions to their download URLs and SHA256 hashes." +
" The structure is {<gcc_version>: {<target_arch>: {url: <url>, sha256: <sha256>}}}.",
),
"includes": attr.string_list(
doc = "Extra includes for compiling C and C++." +
" %workspace% is rendered to the toolchain root path." +
" See https://github.com/bazelbuild/bazel/blob/a48e246e/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderHelper.java#L234-L254.",
default = [],
),
"fincludes": attr.string_list(
doc = "Extra includes for compiling Fortran, if enabled." +
" %workspace% is rendered to the toolchain root path.",
default = [],
),
"target_arch": attr.string(
doc = "The target architecture this toolchain produces. E.g. x86_64.",
mandatory = True,
),
}
# Attributes of the `toolchain` declarations, which live in the hub rather than in the repository
# holding the compiler itself.
_TOOLCHAIN_DECLARATION_ATTRS = {
"extra_target_compatible_with": attr.label_list(
doc = "Additional constraint_values appended to target_compatible_with of the toolchain," +
" on top of the values from the target_compatible_with attribute (including its defaults)." +
" Unlike target_compatible_with, {target_arch} is not rendered.",
mandatory = False,
),
"target_compatible_with": attr.string_list(
default = [
"@platforms//os:linux",
"@platforms//cpu:{target_arch}",
],
doc = "constraint_values passed to target_compatible_with of the toolchain. {target_arch} is rendered to the target_arch attribute value.",
mandatory = False,
),
"target_settings": attr.string_list(
default = [],
doc = "Additional config_settings passed to target_settings of the toolchain, on top of the GCC version selection. {target_arch} is rendered to the target_arch attribute value.",
mandatory = False,
),
}
_PRIVATE_ATTRS = {
"_wrapper_sh_template": attr.label(
default = Label("//toolchain:wrapper.sh.tpl"),
),
}
gcc_toolchain = repository_rule(
_gcc_toolchain_impl,
attrs = dicts.add(
_FEATURE_ATTRS,
_PRIVATE_ATTRS,
),
)
def _sanitize_version(gcc_version):
return gcc_version.replace(".", "_")
def _gcc_toolchains_hub_impl(rctx):
target_arch = rctx.attr.target_arch
gcc_version_flag = str(rctx.attr._gcc_version_flag)
target_compatible_with = [
v.format(target_arch = target_arch)
for v in rctx.attr.target_compatible_with
]
target_compatible_with.extend([str(c) for c in rctx.attr.extra_target_compatible_with])
extra_target_settings = [
v.format(target_arch = target_arch)
for v in rctx.attr.target_settings
]
toolchain_repos = rctx.attr.toolchain_repos
default_gcc_version = rctx.attr.default_gcc_version
if default_gcc_version not in toolchain_repos:
fail("The default GCC version {} has no toolchain for {}.".format(default_gcc_version, target_arch))
content = [_HUB_BUILD_FILE_HEADER]
content.append('''config_setting(
name = "_gcc_version_unset",
flag_values = {{"{gcc_version_flag}": ""}},
)'''.format(gcc_version_flag = gcc_version_flag))
for gcc_version in sorted(toolchain_repos.keys()):
content.append('''config_setting(
name = "_gcc_version_{suffix}",
flag_values = {{"{gcc_version_flag}": "{gcc_version}"}},
)'''.format(
suffix = _sanitize_version(gcc_version),
gcc_version = gcc_version,
gcc_version_flag = gcc_version_flag,
))
# The default version is selected both when the flag is left at its empty default and when it
# is set to that version explicitly.
content.append('''selects.config_setting_group(
name = "_gcc_version_default",
match_any = [
":_gcc_version_unset",
":_gcc_version_{suffix}",
],
)'''.format(suffix = _sanitize_version(default_gcc_version)))
for gcc_version in sorted(toolchain_repos.keys()):
is_default = gcc_version == default_gcc_version
suffix = "" if is_default else "_" + _sanitize_version(gcc_version)
setting = ":_gcc_version_default" if is_default else ":_gcc_version_" + _sanitize_version(gcc_version)
content.append(_HUB_TOOLCHAIN_TEMPLATE.format(
suffix = suffix,
target_compatible_with = target_compatible_with,
target_settings = [setting] + extra_target_settings,
toolchain_repo = toolchain_repos[gcc_version],
))
if rctx.attr.enable_fortran:
content.append(_HUB_FORTRAN_TOOLCHAIN_TEMPLATE.format(
suffix = suffix,
target_compatible_with = target_compatible_with,
target_settings = [setting] + extra_target_settings,
toolchain_repo = toolchain_repos[gcc_version],
fortran_toolchain_type = str(rctx.attr._fortran_toolchain_type),
))
# Keep the targets of the selected toolchain reachable under the hub name, so that labels like
# @gcc_toolchain_x86_64//:libstdcxx keep resolving and follow the selected version.
aliases = list(_HUB_ALIASED_TARGETS)
if rctx.attr.enable_fortran:
aliases.append("_fortran_toolchain")
for target in aliases:
actual = {
":_gcc_version_" + _sanitize_version(gcc_version): "@{}//:{}".format(toolchain_repo, target)
for gcc_version, toolchain_repo in toolchain_repos.items()
if gcc_version != default_gcc_version
}
actual["//conditions:default"] = "@{}//:{}".format(toolchain_repos[default_gcc_version], target)
content.append('''alias(
name = "{target}",
actual = select({actual}),
)'''.format(target = target, actual = actual))
rctx.file("BUILD.bazel", "\n\n".join(content) + "\n")
_gcc_toolchains_hub = repository_rule(
_gcc_toolchains_hub_impl,
attrs = dicts.add(
_TOOLCHAIN_DECLARATION_ATTRS,
{
"default_gcc_version": attr.string(
doc = "The GCC version used when the gcc_version flag is not set.",
mandatory = True,
),
"enable_fortran": attr.bool(
doc = "Whether to declare the Fortran toolchains.",
default = True,
),
"target_arch": attr.string(
doc = "The target architecture the toolchains produce. E.g. x86_64.",
mandatory = True,
),
"toolchain_repos": attr.string_dict(
doc = "Maps each available GCC version to the repository holding its cc_toolchain.",
mandatory = True,
),
"_fortran_toolchain_type": attr.label(
default = Label("//toolchain/fortran:toolchain_type"),
),
"_gcc_version_flag": attr.label(
default = Label("//toolchain:gcc_version"),
),
},
),
)
_SHAREABLE_ATTRS = dicts.add(_FEATURE_ATTRS, _TOOLCHAIN_DECLARATION_ATTRS)
ATTRS_SHARED_WITH_MODULE_EXTENSION = {
attr_name: _SHAREABLE_ATTRS[attr_name]
for attr_name in [
"gcc_version",
"gcc_versions",
"enable_fortran",
"extra_cflags",
"extra_cxxflags",
"extra_ldflags",
"extra_fflags",
"extra_asmflags",
"extra_target_compatible_with",
"supports_param_files",
]
}
def _render_tool_paths(rctx, path_prefix, binary_prefix):
relative_tool_paths = {
"ar": "{path_prefix}/bin/{binary_prefix}ar".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"as": "{path_prefix}/bin/{binary_prefix}as".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"cpp": "{path_prefix}/bin/{binary_prefix}cpp".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"g++": "{path_prefix}/bin/{binary_prefix}g++".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"gcc": "{path_prefix}/bin/{binary_prefix}gcc".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"gcov": "{path_prefix}/bin/{binary_prefix}gcov".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"gfortran": "{path_prefix}/bin/{binary_prefix}gfortran".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"ld": "{path_prefix}/bin/{binary_prefix}ld".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"nm": "{path_prefix}/bin/{binary_prefix}nm".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"objcopy": "{path_prefix}/bin/{binary_prefix}objcopy".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"objdump": "{path_prefix}/bin/{binary_prefix}objdump".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
"strip": "{path_prefix}/bin/{binary_prefix}strip".format(
path_prefix = path_prefix,
binary_prefix = binary_prefix,
),
}
path_env = ":".join([
path.format(
path_prefix = path_prefix,
)
for path in [
# xbin first so that wrappers are found first in PATH when called
# indirectly by other tools.
"${{EXECROOT}}/{path_prefix}/xbin",
"${{EXECROOT}}/{path_prefix}/bin",
]
])
tool_paths = {}
for name, tool_path in relative_tool_paths.items():
wrapped_tool_path = paths.join("xbin", name)
rctx.template(
wrapped_tool_path,
rctx.attr._wrapper_sh_template,
substitutions = {
"__PATH__": path_env,
"__binary__": tool_path,
},
executable = True,
)
tool_paths[name] = wrapped_tool_path
return tool_paths
def gcc_declare_toolchain(
name,
target_arch,
**kwargs):
"""Declares a `gcc_toolchain` for every available GCC version.
`name` is the hub repository holding the `toolchain` declarations. Each GCC version gets its
own repository holding the `cc_toolchain`, fetched only when that version is selected through
the `@gcc_toolchain//toolchain:gcc_version` flag.
You should use `gcc_register_toolchain` unless you need to register toolchains manually,
e.g. if you are consuming this repository as a Bzlmod dependency.
Args:
name: The name of the hub repository holding the toolchain declarations.
target_arch: The target architecture of the toolchain.
**kwargs: The extra arguments passed to `gcc_toolchain`. See `gcc_toolchain` for more info.
The attributes of the `toolchain` declarations themselves are also accepted here,
since they apply to the hub rather than to `gcc_toolchain`:
`target_compatible_with`: constraint_values passed to `target_compatible_with` of the
toolchain. `{target_arch}` is rendered to the `target_arch` argument value. Defaults to
`["@platforms//os:linux", "@platforms//cpu:{target_arch}"]`.
`extra_target_compatible_with`: Additional constraint_values appended to
`target_compatible_with` of the toolchain, on top of the values from the
`target_compatible_with` argument (including its defaults). Unlike
`target_compatible_with`, `{target_arch}` is not rendered.
`target_settings`: Additional config_settings passed to `target_settings` of the
toolchain, on top of the GCC version selection. `{target_arch}` is rendered to the
`target_arch` argument value.
"""
binary_prefix = kwargs.pop("binary_prefix", None)
if binary_prefix == None:
if target_arch not in _TRIPLE_BINARY_PREFIXES:
fail("Unsupported target architecture: {}".format(target_arch))
binary_prefix = _AUTO_BINARY_PREFIX
default_gcc_version = kwargs.pop("gcc_version", DEFAULT_GCC_VERSION)
gcc_versions = kwargs.pop("gcc_versions", json.encode(AVAILABLE_GCC_VERSIONS))
enable_fortran = kwargs.pop("enable_fortran", True)
extra_target_compatible_with = kwargs.pop("extra_target_compatible_with", [])
target_compatible_with = kwargs.pop("target_compatible_with", None)
target_settings = kwargs.pop("target_settings", [])
# Left in kwargs so that the per-version repositories keep receiving it.
repo_mapping = kwargs.get("repo_mapping", None)
extra_cflags = kwargs.pop("extra_cflags", [])
extra_cxxflags = kwargs.pop("extra_cxxflags", [])
extra_fflags = kwargs.pop("extra_fflags", [])
extra_ldflags = kwargs.pop("extra_ldflags", [])
extra_asmflags = kwargs.pop("extra_asmflags", [])
includes = kwargs.pop("includes", [])
fincludes = kwargs.pop("fincludes", [])
toolchain_repos = {}
for gcc_version in json.decode(gcc_versions):
toolchain_repo = "{}_{}".format(name, _sanitize_version(gcc_version))
toolchain_repos[gcc_version] = toolchain_repo
gcc_toolchain(
name = toolchain_repo,
binary_prefix = binary_prefix,
enable_fortran = enable_fortran,
extra_cflags = extra_cflags,
extra_cxxflags = extra_cxxflags,
extra_fflags = extra_fflags,
extra_ldflags = extra_ldflags,
extra_asmflags = extra_asmflags,
includes = includes,
fincludes = fincludes,
gcc_version = gcc_version,
gcc_versions = gcc_versions,
target_arch = target_arch,
**kwargs
)
hub_kwargs = {}
# An explicitly empty target_compatible_with drops the default constraints, so only an omitted
# one may fall back to the attribute default.
if target_compatible_with != None:
hub_kwargs["target_compatible_with"] = target_compatible_with
# The hub BUILD file resolves @bazel_skylib, @platforms and @rules_cc as well, so it needs the
# same repository mapping as the per-version repositories.
if repo_mapping != None:
hub_kwargs["repo_mapping"] = repo_mapping
_gcc_toolchains_hub(
name = name,
default_gcc_version = default_gcc_version,
enable_fortran = enable_fortran,
extra_target_compatible_with = extra_target_compatible_with,
target_arch = target_arch,
target_settings = target_settings,
toolchain_repos = toolchain_repos,
**hub_kwargs
)
def gcc_register_toolchain(
name,
target_arch,
**kwargs):
"""Declares a `gcc_toolchain` for every available GCC version and registers all of them.
Which one resolves is selected by the `@gcc_toolchain//toolchain:gcc_version` flag.
Args:
name: The name of the hub repository holding the toolchain declarations.
target_arch: The target architecture of the toolchain.
**kwargs: The extra arguments passed to `gcc_declare_toolchain`. See
`gcc_declare_toolchain` for more info.
"""
enable_fortran = kwargs.pop("enable_fortran", True)
gcc_declare_toolchain(name, target_arch, enable_fortran = enable_fortran, **kwargs)
native.register_toolchains("@{}//:all".format(name))
ARCHS = struct(
aarch64 = "aarch64",
armv7 = "armv7",
x86_64 = "x86_64",
)
# The public targets of a toolchain repository, aliased by the hub so that they track the GCC
# version selected by the flag.
_HUB_ALIASED_TARGETS = [
"_cc_toolchain",
"all_files",
"ar",
"ar_files",
"as",
"as_files",
"cc_toolchain_config",
"compiler_files",
"coverage_files",
"dwp_files",
"gcc",
"gcov",
"include",
"ld",
"ld.bfd",
"ld_files",
"lib",
"libasan",
"liblsan",
"libstdcxx",
"libstdcxx_static",
"libtsan",
"libubsan",
"linker_files",
"lld_files",
"nm",
"objcopy",
"objcopy_files",
"objdump",
"ranlib",
"readelf",
"strip",
"strip_files",
]
_HUB_BUILD_FILE_HEADER = '''\
load("@bazel_skylib//lib:selects.bzl", "selects")
package(default_visibility = ["//visibility:public"])'''
_HUB_TOOLCHAIN_TEMPLATE = '''\
toolchain(
name = "cc_toolchain{suffix}",
exec_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
target_compatible_with = {target_compatible_with},
target_settings = {target_settings},
toolchain = "@{toolchain_repo}//:_cc_toolchain",
toolchain_type = "@rules_cc//cc:toolchain_type",
)'''
_HUB_FORTRAN_TOOLCHAIN_TEMPLATE = '''\
toolchain(
name = "fortran_toolchain{suffix}",
exec_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
target_compatible_with = {target_compatible_with},
target_settings = {target_settings},
toolchain = "@{toolchain_repo}//:_fortran_toolchain",
toolchain_type = "{fortran_toolchain_type}",
)'''
_TOOLCHAIN_BUILD_FILE_CONTENT = """\
load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_library")
load("@{gcc_toolchain_workspace_name}//toolchain:cc_toolchain_config.bzl", "cc_toolchain_config")
{fortran_load}
load("//:tool_paths.bzl", "tool_paths")
package(default_visibility = ["//visibility:public"])
{fortran_toolchain}
cc_toolchain(
name = "_cc_toolchain",
all_files = ":all_files",
ar_files = ":ar_files",
as_files = ":as_files",
compiler_files = ":compiler_files",
dwp_files = ":dwp_files",
linker_files = ":linker_files",
objcopy_files = ":objcopy_files",
strip_files = ":strip_files",
coverage_files = ":coverage_files",
supports_param_files = {supports_param_files},
toolchain_config = ":cc_toolchain_config",
toolchain_identifier = "gcc-toolchain",
)
cc_toolchain_config(
name = "cc_toolchain_config",
cxx_builtin_include_directories = {cxx_builtin_include_directories},
enable_fortran = {enable_fortran},
extra_cflags = {extra_cflags},
extra_cxxflags = {extra_cxxflags},
extra_fflags = {extra_fflags},
extra_ldflags = {extra_ldflags},
extra_asmflags = {extra_asmflags},
tool_paths = tool_paths,
)
filegroup(
name = "all_files",
srcs = [
":ar_files",
":as_files",
":compiler_files",
":coverage_files",
":dwp_files",
":linker_files",
":objcopy_files",
":strip_files",
],
)
filegroup(
name = "compiler_files",
srcs = [
":as_files",
":gcc",
":include",
],
visibility = ["//visibility:public"],
)
filegroup(
name = "linker_files",
srcs = [
":ar",
":gcc",
":lib",
":ld_files",
":lld_files",
],
visibility = ["//visibility:public"],
)
filegroup(
name = "ld_files",
srcs = [
":ld",
":ld.bfd",
"xbin/ld",