Skip to content

Commit 85022a2

Browse files
gabrielrussocSiddhartha Bagaria
authored and
Siddhartha Bagaria
committed
Support for aarch64 cpu
Co-authored by: Ryan Johnson <[email protected]>
1 parent aba3834 commit 85022a2

6 files changed

+88
-29
lines changed

toolchain/BUILD.tpl

+29-7
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,27 @@ filegroup(
3939
cc_toolchain_suite(
4040
name = "toolchain",
4141
toolchains = {
42-
"k8|clang": ":cc-clang-linux",
42+
"k8|clang": ":cc-clang-k8-linux",
43+
"aarch64|clang": ":cc-clang-aarch64-linux",
4344
"darwin|clang": ":cc-clang-darwin",
44-
"k8": ":cc-clang-linux",
45+
"k8": ":cc-clang-k8-linux",
46+
"aarch64": ":cc-clang-aarch64-linux",
4547
"darwin": ":cc-clang-darwin",
4648
},
4749
)
4850

4951
load(":cc_toolchain_config.bzl", "cc_toolchain_config")
5052

5153
cc_toolchain_config(
52-
name = "local_linux",
54+
name = "local_linux_k8",
5355
cpu = "k8",
5456
)
5557

58+
cc_toolchain_config(
59+
name = "local_linux_aarch64",
60+
cpu = "aarch64",
61+
)
62+
5663
cc_toolchain_config(
5764
name = "local_darwin",
5865
cpu = "darwin",
@@ -73,7 +80,7 @@ toolchain(
7380
)
7481

7582
toolchain(
76-
name = "cc-toolchain-linux",
83+
name = "cc-toolchain-k8-linux",
7784
exec_compatible_with = [
7885
"@platforms//cpu:x86_64",
7986
"@platforms//os:linux",
@@ -82,14 +89,29 @@ toolchain(
8289
"@platforms//cpu:x86_64",
8390
"@platforms//os:linux",
8491
],
85-
toolchain = ":cc-clang-linux",
92+
toolchain = ":cc-clang-k8-linux",
93+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
94+
)
95+
96+
toolchain(
97+
name = "cc-toolchain-aarch64-linux",
98+
exec_compatible_with = [
99+
"@platforms//cpu:aarch64",
100+
"@platforms//os:linux",
101+
],
102+
target_compatible_with = [
103+
"@platforms//cpu:aarch64",
104+
"@platforms//os:linux",
105+
],
106+
toolchain = ":cc-clang-aarch64-linux",
86107
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
87108
)
88109

89110
load("@%{parent_repo_name}//toolchain:rules.bzl", "conditional_cc_toolchain")
90111

91-
conditional_cc_toolchain("cc-clang-linux", False, %{absolute_paths})
92-
conditional_cc_toolchain("cc-clang-darwin", True, %{absolute_paths})
112+
conditional_cc_toolchain("cc-clang-k8-linux", "local_linux_k8", False, %{absolute_paths})
113+
conditional_cc_toolchain("cc-clang-aarch64-linux", "local_linux_aarch64", False, %{absolute_paths})
114+
conditional_cc_toolchain("cc-clang-darwin", "local_darwin", True, %{absolute_paths})
93115

94116
## LLVM toolchain files
95117
# Needed when not using absolute paths.

toolchain/cc_toolchain_config.bzl.tpl

+33-10
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,59 @@ def _impl(ctx):
3434
if (ctx.attr.cpu == "darwin"):
3535
toolchain_identifier = "clang-darwin"
3636
elif (ctx.attr.cpu == "k8"):
37-
toolchain_identifier = "clang-linux"
37+
toolchain_identifier = "clang-k8-linux"
38+
elif (ctx.attr.cpu == "aarch64"):
39+
toolchain_identifier = "clang-aarch64-linux"
3840
else:
3941
fail("Unreachable")
4042

4143
if (ctx.attr.cpu == "k8"):
4244
host_system_name = "x86_64"
45+
elif (ctx.attr.cpu == "aarch64"):
46+
host_system_name = "aarch64"
4347
elif (ctx.attr.cpu == "darwin"):
4448
host_system_name = "x86_64-apple-macosx"
4549
else:
4650
fail("Unreachable")
4751

48-
if (ctx.attr.cpu == "darwin"):
49-
target_system_name = "x86_64-apple-macosx"
50-
elif (ctx.attr.cpu == "k8"):
52+
if (ctx.attr.cpu == "k8"):
5153
target_system_name = "x86_64-unknown-linux-gnu"
54+
elif (ctx.attr.cpu == "aarch64"):
55+
target_system_name = "aarch64-unknown-linux-gnu"
56+
elif (ctx.attr.cpu == "darwin"):
57+
target_system_name = "x86_64-apple-macosx"
5258
else:
5359
fail("Unreachable")
5460

5561
if (ctx.attr.cpu == "darwin"):
5662
target_cpu = "darwin"
5763
elif (ctx.attr.cpu == "k8"):
5864
target_cpu = "k8"
65+
elif (ctx.attr.cpu == "aarch64"):
66+
target_cpu = "aarch64"
5967
else:
6068
fail("Unreachable")
6169

6270
if (ctx.attr.cpu == "k8"):
6371
target_libc = "glibc_unknown"
72+
elif (ctx.attr.cpu == "aarch64"):
73+
target_libc = "glibc_unknown"
6474
elif (ctx.attr.cpu == "darwin"):
6575
target_libc = "macosx"
6676
else:
6777
fail("Unreachable")
6878

6979
if (ctx.attr.cpu == "darwin" or
80+
ctx.attr.cpu == "aarch64" or
7081
ctx.attr.cpu == "k8"):
7182
compiler = "clang"
7283
else:
7384
fail("Unreachable")
7485

7586
if (ctx.attr.cpu == "k8"):
7687
abi_version = "clang"
88+
elif (ctx.attr.cpu == "aarch64"):
89+
abi_version = "clang"
7790
elif (ctx.attr.cpu == "darwin"):
7891
abi_version = "darwin_x86_64"
7992
else:
@@ -83,12 +96,15 @@ def _impl(ctx):
8396
abi_libc_version = "darwin_x86_64"
8497
elif (ctx.attr.cpu == "k8"):
8598
abi_libc_version = "glibc_unknown"
99+
elif (ctx.attr.cpu == "aarch64"):
100+
abi_libc_version = "glibc_unknown"
86101
else:
87102
fail("Unreachable")
88103

89104
cc_target_os = None
90105

91106
if (ctx.attr.cpu == "darwin" or
107+
ctx.attr.cpu == "aarch64" or
92108
ctx.attr.cpu == "k8"):
93109
builtin_sysroot = "%{sysroot_path}"
94110
else:
@@ -144,7 +160,7 @@ def _impl(ctx):
144160

145161
action_configs = []
146162

147-
if ctx.attr.cpu == "k8":
163+
if ctx.attr.cpu in ("k8", "aarch64"):
148164
linker_flags = [
149165
# Use the lld linker.
150166
"-fuse-ld=lld",
@@ -228,7 +244,7 @@ def _impl(ctx):
228244
flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])],
229245
with_features = [with_feature_set(features = ["opt"])],
230246
),
231-
] if ctx.attr.cpu == "k8" else []),
247+
] if ctx.attr.cpu in ("k8", "aarch64") else []),
232248
)
233249

234250
default_compile_flags_feature = feature(
@@ -503,14 +519,20 @@ def _impl(ctx):
503519
"%{toolchain_path_prefix}lib/clang/%{llvm_version}/include",
504520
"%{toolchain_path_prefix}lib64/clang/%{llvm_version}/include",
505521
]
506-
if (ctx.attr.cpu == "k8"):
522+
if (ctx.attr.cpu in ["k8", "aarch64"]):
507523
cxx_builtin_include_directories += [
508524
"%{sysroot_prefix}/include",
509525
"%{sysroot_prefix}/usr/include",
510526
"%{sysroot_prefix}/usr/local/include",
511-
] + [
527+
]
528+
if ctx.attr.cpu == "k8":
529+
cxx_builtin_include_directories += [
512530
%{k8_additional_cxx_builtin_include_directories}
513531
]
532+
elif ctx.attr.cpu == "aarch64":
533+
cxx_builtin_include_directories += [
534+
%{aarch64_additional_cxx_builtin_include_directories}
535+
]
514536
elif (ctx.attr.cpu == "darwin"):
515537
cxx_builtin_include_directories += [
516538
"%{sysroot_prefix}/usr/include",
@@ -531,12 +553,12 @@ def _impl(ctx):
531553
value = "-Wframe-larger-than=100000000 -Wno-vla",
532554
),
533555
]
534-
elif (ctx.attr.cpu == "k8"):
556+
elif (ctx.attr.cpu in ("k8", "aarch64")):
535557
make_variables = []
536558
else:
537559
fail("Unreachable")
538560

539-
if (ctx.attr.cpu == "k8"):
561+
if (ctx.attr.cpu in ("k8", "aarch64")):
540562
tool_paths = [
541563
tool_path(
542564
name = "ld",
@@ -647,6 +669,7 @@ cc_toolchain_config = rule(
647669
values = [
648670
"darwin",
649671
"k8",
672+
"aarch64",
650673
],
651674
),
652675
},

toolchain/internal/configure.bzl

+3-5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def llvm_register_toolchains():
7676
"%{absolute_paths}": "True" if rctx.attr.absolute_paths else "False",
7777
"%{makevars_ld_flags}": _makevars_ld_flags(rctx),
7878
"%{k8_additional_cxx_builtin_include_directories}": _include_dirs_str(rctx, "k8"),
79+
"%{aarch64_additional_cxx_builtin_include_directories}": _include_dirs_str(rctx, "aarch64"),
7980
"%{darwin_additional_cxx_builtin_include_directories}": _include_dirs_str(rctx, "darwin"),
8081
}
8182

@@ -120,12 +121,9 @@ def llvm_register_toolchains():
120121
if not _download_llvm(rctx, shortos):
121122
_download_llvm_preconfigured(rctx)
122123

123-
def conditional_cc_toolchain(name, darwin, absolute_paths = False):
124+
def conditional_cc_toolchain(name, toolchain_config, darwin, absolute_paths = False):
124125
# Toolchain macro for BUILD file to use conditional logic.
125126

126-
toolchain_config = "local_darwin" if darwin else "local_linux"
127-
toolchain_identifier = "clang-darwin" if darwin else "clang-linux"
128-
129127
if absolute_paths:
130128
_cc_toolchain(
131129
name = name,
@@ -151,7 +149,7 @@ def conditional_cc_toolchain(name, darwin, absolute_paths = False):
151149
ar_files = name + "-archiver-files",
152150
as_files = name + "-assembler-files",
153151
compiler_files = name + "-compiler-files",
154-
dwp_files = ":empty",
152+
dwp_files = ":dwp",
155153
linker_files = name + "-linker-files",
156154
objcopy_files = ":objcopy",
157155
strip_files = ":empty",

toolchain/internal/llvm_distributions.bzl

+17-3
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,26 @@ def download_llvm_preconfigured(rctx):
256256
auth = _get_auth(rctx, urls),
257257
)
258258

259+
def _arch(rctx):
260+
exec_result = rctx.execute([
261+
_python(rctx),
262+
"-c",
263+
"import platform; print(platform.machine())",
264+
])
265+
if exec_result.return_code:
266+
fail("Failed to detect machine architecture: \n%s\n%s" % (exec_result.stdout, exec_result.stderr))
267+
return exec_result.stdout.strip()
268+
259269
# Download LLVM from the user-provided URLs and return True. If URLs were not provided, return
260270
# False.
261271
def download_llvm(rctx, shortos):
262-
urls = rctx.attr.urls.get(shortos, default = [])
263-
sha256 = rctx.attr.sha256.get(shortos, default = "")
264-
prefix = rctx.attr.strip_prefix.get(shortos, default = "")
272+
if shortos == "linux":
273+
key = "linux-{}".format(_arch(rctx))
274+
else:
275+
key = shortos
276+
urls = rctx.attr.urls.get(key, default = [])
277+
sha256 = rctx.attr.sha256.get(key, default = "")
278+
prefix = rctx.attr.strip_prefix.get(key, default = "")
265279

266280
if not urls:
267281
return False

toolchain/rules.bzl

+4-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ llvm_toolchain = repository_rule(
4545
"cxx_builtin_include_directories": attr.string_list_dict(
4646
mandatory = False,
4747
doc = ("Additional builtin include directories to be added to the default system " +
48-
"directories, keyed by the CPU type (e.g. k8 or darwin). See documentation " +
49-
"for bazel's create_cc_toolchain_config_info."),
48+
"directories, keyed by the CPU type (e.g. k8, aarch64 or darwin). See " +
49+
"documentation for bazel's create_cc_toolchain_config_info."),
5050
),
5151
"llvm_mirror": attr.string(
5252
doc = "Mirror base for LLVM binaries if using the pre-configured URLs.",
@@ -63,7 +63,8 @@ llvm_toolchain = repository_rule(
6363
# Following attributes are needed only when using a non-standard URL scheme.
6464
"urls": attr.string_list_dict(
6565
mandatory = False,
66-
doc = "URLs for each OS type (linux and darwin) if not using the pre-configured URLs.",
66+
doc = ("URLs for each OS type and arch pairs (linux-x86_64, linux-aarch64 and darwin) " +
67+
"if not using the pre-configured URLs."),
6768
),
6869
"sha256": attr.string_dict(
6970
mandatory = False,

toolchain/toolchains.bzl.tpl

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
def llvm_register_toolchains():
1616
native.register_toolchains(
17-
"@%{repo_name}//:cc-toolchain-linux",
17+
"@%{repo_name}//:cc-toolchain-k8-linux",
18+
"@%{repo_name}//:cc-toolchain-aarch64-linux",
1819
"@%{repo_name}//:cc-toolchain-darwin",
1920
)

0 commit comments

Comments
 (0)