Skip to content

Commit 71eae2f

Browse files
authored
refactor: improve toolchain structure (#31)
1 parent ccf0b48 commit 71eae2f

18 files changed

+518
-281
lines changed

cpp/extension.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//cpp:repositories.bzl", "download_llvm")
1+
load("//cpp/private/llvm:download.bzl", "download_llvm")
22

33
def _init_toolchain(ctx):
44
for mod in ctx.modules:

cpp/private/actions/compile.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def _cpp_compile_impl(ctx, sources, headers, includes, modules, features, toolch
4040
features = features,
4141
include_directories = includes,
4242
extra_vars = extra_vars,
43+
action_name = action_name,
4344
)
4445

4546
ctx.actions.run(

cpp/private/common.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def create_compilation_context(ctx, headers = [], is_aspect = False):
5555
Args:
5656
ctx: rule context
5757
headers: optional list of Files with target headers
58+
is_aspect: True if this function is called from an aspect
5859
5960
Returns:
6061
A structure containing compilation context

cpp/private/llvm.BUILD

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
load("@rules_cpp//cpp:rules.bzl", "declare_clang_toolchains")
2-
load("@rules_cpp//cpp/private:tool.bzl", "tool")
1+
load("@rules_cpp//cpp:rules.bzl",
2+
"declare_clang_toolchains",
3+
"compiler",
4+
"standard_library",
5+
"binutils",
6+
"linker",
7+
)
38

49
filegroup(
5-
name = "clang-files",
10+
name = "openmp",
611
srcs = glob([
7-
"bin/clang*",
8-
"lib/clang/**/*",
9-
"lib/*LTO*",
12+
"lib/*omp*",
1013
]),
1114
visibility = ["//visibility:public"],
1215
)
1316

1417
filegroup(
15-
name = "binutils",
18+
name = "clang-files",
1619
srcs = glob([
17-
"bin/llvm-objcopy*",
18-
"bin/llvm-dwp*",
19-
"bin/llvm-cov*",
20-
"bin/llvm-strip*",
21-
"bin/llvm-nm*",
22-
"bin/llvm-ar*",
23-
"bin/llvm-strings*",
20+
"bin/clang*",
21+
"lib/clang/**/*",
22+
"lib/*LTO*",
23+
"lib/libclang*",
24+
"lib/libLLVM*",
2425
]),
25-
visibility = ["//visibility:public"],
2626
)
2727

28-
tool(
29-
name = "strip",
30-
executable = "bin/llvm-strip",
31-
)
32-
33-
tool(
34-
name = "ar",
35-
executable = "bin/llvm-ar",
28+
compiler(
29+
name = "clang",
30+
kind = "clang",
31+
binary = "bin/clang",
32+
deps = [":clang-files", ":openmp"]
3633
)
3734

38-
filegroup(
39-
name = "openmp",
40-
srcs = glob([
41-
"lib/*omp*",
35+
binutils(
36+
name = "binutils",
37+
ar = "bin/llvm-ar",
38+
assembler = "bin/clang",
39+
objcopy = "bin/llvm-objcopy",
40+
strip = "bin/llvm-strip",
41+
dwp = "bin/llvm-dwp",
42+
deps = glob([
43+
"bin/llvm-*",
4244
]),
4345
visibility = ["//visibility:public"],
4446
)
@@ -52,54 +54,40 @@ filegroup(
5254
visibility = ["//visibility:public"],
5355
)
5456

55-
tool(
57+
linker(
5658
name = "lld",
57-
executable = "bin/lld",
59+
kind = "lld",
60+
binary = "bin/ld.lld",
61+
deps = [":lld-files"],
5862
visibility = ["//visibility:public"],
59-
data = [":lld-files"]
6063
)
6164

62-
filegroup(
65+
standard_library(
6366
name = "libc++",
64-
srcs = glob([
67+
kind = "libc++",
68+
headers = glob([
6569
"include/c++/**/*",
6670
"include/x86_64-unknown-linux-gnu/**/*",
67-
"lib/*c++*",
6871
]),
69-
visibility = ["//visibility:public"],
70-
)
71-
72-
filegroup(
73-
name = "static_libc++",
74-
srcs = glob([
72+
shared_libraries = glob([
73+
"lib/**/*c++*.so",
74+
"lib/**/*c++*.dylib",
75+
]),
76+
static_libraries = glob([
7577
"lib/**/libc++.a",
7678
"lib/**/libc++abi.a",
7779
"lib/**/libunwind.a",
7880
]),
79-
visibility = ["//visibility:public"],
80-
)
81-
82-
tool(
83-
name = "clang",
84-
executable = "bin/clang",
85-
data = [
86-
":clang-files",
87-
":binutils",
88-
":openmp",
89-
":lld",
90-
":libc++",
91-
":static_libc++",
92-
],
93-
visibility = ["//visibility:public"],
81+
includes = [
82+
"include/c++/v1",
83+
"include/x86_64-unknown-linux-gnu/c++/v1",
84+
]
9485
)
9586

9687
declare_clang_toolchains(
9788
name = "toolchain",
9889
compiler = ":clang",
9990
linker = ":lld",
10091
stdlib = ":libc++",
101-
static_stdlib = ":static_libc++",
10292
binutils = ":binutils",
103-
strip = ":strip",
104-
archiver = ":ar",
10593
)

cpp/private/llvm/BUILD.bazel

Whitespace-only changes.

cpp/private/llvm/download.bzl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Utilities for downloading LLVM toolchains"""
2+
3+
load(
4+
"@bazel_tools//tools/build_defs/repo:http.bzl",
5+
"http_archive",
6+
)
7+
load("//cpp/private/llvm:releases_aarch64_darwin.bzl", "llvm_aarch64_darwin")
8+
load("//cpp/private/llvm:releases_x64_darwin.bzl", "llvm_x64_darwin")
9+
load("//cpp/private/llvm:releases_x64_linux.bzl", "llvm_x64_linux")
10+
11+
def download_llvm(mctx, repo_name, version):
12+
"""Download entire LLVM toolchain
13+
14+
Args:
15+
mctx: module context
16+
repo_name: name of the repository that'll be made available to the user
17+
version: either main or a supporter released LLVM version
18+
"""
19+
20+
clang_repo = {}
21+
if mctx.os.name == "linux" and mctx.os.arch == "amd64":
22+
clang_repo = llvm_x64_linux
23+
elif mctx.os.name == "mac os x" and mctx.os.arch == "aarch64":
24+
clang_repo = llvm_aarch64_darwin
25+
elif mctx.os.name == "mac os x" and mctx.os.arch == "x86_64":
26+
clang_repo = llvm_x64_darwin
27+
28+
http_archive(
29+
name = repo_name,
30+
urls = clang_repo[version]["urls"],
31+
strip_prefix = clang_repo[version]["strip_prefix"],
32+
sha256 = clang_repo[version]["sha256"],
33+
build_file = "//cpp/private:llvm.BUILD",
34+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""LLVM Releases for macOS on Apple Silicon"""
2+
3+
llvm_aarch64_darwin = {
4+
"17.0.6": {
5+
"urls": [
6+
"https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.6/clang+llvm-17.0.6-arm64-apple-darwin22.0.tar.xz",
7+
],
8+
"strip_prefix": "clang+llvm-17.0.6-arm64-apple-darwin22.0",
9+
"sha256": "1264eb3c2a4a6d5e9354c3e5dc5cb6c6481e678f6456f36d2e0e566e9400fcad",
10+
},
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""LLVM Releases for macOS on x86_64"""
2+
3+
llvm_x64_darwin = {
4+
"17.0.6": {
5+
"urls": [
6+
"https://github.com/alexbatashev/rules_cpp/releases/download/llvmorg-17.0.6-ccf0b487943416578ca0a7abb5cf70e0e24a7957/llvmorg-17.0.6-macos-x86_64.tar.zst",
7+
],
8+
"strip_prefix": "llvmorg-17.0.6-macos-x86_64",
9+
"sha256": "77d5d4654372425e82f20491ab8a7633cdbfed4911bf8af7404490187fbcdcdb",
10+
},
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""LLVM Releases for Linux on x86_64"""
2+
3+
llvm_x64_linux = {
4+
"17.0.6": {
5+
"urls": [
6+
"https://github.com/alexbatashev/rules_cpp/releases/download/llvmorg-17.0.6-31a6a2ac5144effba2c8bd42950c994ba54230ae/llvmorg-17.0.6-linux-x86_64.tar.zst",
7+
],
8+
"strip_prefix": "llvmorg-17.0.6-linux-x86_64",
9+
"sha256": "a9f735bf262c2fd4805e3f66ee58ed5a7725978da7294d6654809a964f92ac82",
10+
},
11+
}

cpp/private/toolchain/BUILD.bazel

Whitespace-only changes.

0 commit comments

Comments
 (0)