Skip to content

Commit 6bf5e94

Browse files
committed
Adopt Protobuf's toolchain setup
1 parent e20272e commit 6bf5e94

9 files changed

Lines changed: 527 additions & 34 deletions

.bazelrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ common --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
3838
build --define=protobuf_allow_msvc=true
3939

4040
# To facilitate testing in bazelci incompatible flags
41-
build --incompatible_autoload_externally=
41+
build --incompatible_autoload_externally=
42+
43+
import %workspace%/toolchain/toolchains.bazelrc
44+
45+
build:offline --nofetch

BUILD.bazel

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,8 @@ load("//:protobuf_javascript_release.bzl", "package_naming")
88

99
exports_files(["package.json"])
1010

11-
config_setting(
12-
name = "x64_x86_windows",
13-
values = {"cpu": "x64_x86_windows"},
14-
)
15-
16-
config_setting(
17-
name = "x64_windows",
18-
values = {"cpu": "x64_windows"},
19-
)
20-
21-
config_setting(
22-
name = "k8",
23-
values = {"cpu": "k8"},
24-
)
25-
26-
config_setting(
27-
name = "darwin_arm64",
28-
values = {"cpu": "darwin_arm64"},
29-
)
30-
31-
config_setting(
32-
name = "darwin_x86_64",
33-
values = {"cpu": "darwin_x86_64"},
34-
)
35-
3611
package_naming(
3712
name = "protobuf_javascript_pkg_naming",
38-
platform = select({
39-
":k8": "linux-x86_64", # currently the only supported build type in Github Actions
40-
":x64_x86_windows": "win32",
41-
":x64_windows": "win64",
42-
":darwin_arm64": "osx-aarch_64",
43-
":darwin_x86_64": "osx-x86_64",
44-
"//conditions:default": "", # continues with current behavior when no --cpu is specified allowing existing internal builds to function
45-
}),
4613
)
4714

4815
pkg_files(

MODULE.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,17 @@ bazel_dep(name = "abseil-cpp", version = "20250814.1")
66
bazel_dep(name = "protobuf", version = "33.4", repo_name = "com_google_protobuf")
77
bazel_dep(name = "rules_cc", version = "0.2.15")
88
bazel_dep(name = "rules_pkg", version = "1.0.1", dev_dependency = True)
9+
bazel_dep(name = "platforms", version = "1.0.0")
10+
bazel_dep(name = "apple_support", version = "1.17.1", repo_name = "build_bazel_apple_support")
11+
12+
register_toolchains(
13+
"//toolchain:osx-x86_64-toolchain",
14+
"//toolchain:osx-aarch_64-toolchain",
15+
"//toolchain:linux-aarch_64-toolchain",
16+
"//toolchain:linux-ppcle_64-toolchain",
17+
"//toolchain:linux-s390_64-toolchain",
18+
"//toolchain:linux-x86_32-toolchain",
19+
"//toolchain:linux-x86_64-toolchain",
20+
"//toolchain:win32-toolchain",
21+
"//toolchain:win64-toolchain",
22+
)

MODULE.bazel.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protobuf_javascript_release.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def _package_naming_impl(ctx):
2525
cpu = "aarch_64"
2626
elif cpu == "ppc64":
2727
cpu = "ppcle_64"
28+
elif cpu == "i686":
29+
cpu = "x86_32"
30+
elif cpu == "x86_64":
31+
cpu = "x86_64"
2832

2933
# use the system name to determine the os and then create platform names
3034
if "apple" in system_name:

toolchain/BUILD.bazel

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain")
2+
load("@rules_cc//cc/toolchains:cc_toolchain_suite.bzl", "cc_toolchain_suite")
3+
load(":platforms.bzl", "PROTOBUF_PLATFORMS")
4+
load(":cc_toolchain_config.bzl", "cc_toolchain_config")
5+
6+
package(default_visibility = ["//visibility:public"])
7+
8+
filegroup(name = "empty")
9+
10+
config_setting(
11+
name = "is_cross_compiling",
12+
flag_values = {":release": "True"},
13+
)
14+
15+
[
16+
platform(
17+
name = name,
18+
constraint_values = config["compatible_with"],
19+
)
20+
for name, config in PROTOBUF_PLATFORMS.items()
21+
]
22+
23+
[
24+
cc_toolchain(
25+
name = name + "-cc_toolchain",
26+
all_files = ":empty",
27+
compiler_files = ":empty",
28+
dwp_files = ":empty",
29+
dynamic_runtime_lib = ":empty",
30+
linker_files = ":empty",
31+
objcopy_files = ":empty",
32+
output_licenses = ["restricted"],
33+
static_runtime_lib = ":empty",
34+
strip_files = ":empty",
35+
toolchain_config = ":" + name + "-config",
36+
toolchain_identifier = name + "-cc_toolchain",
37+
)
38+
for name in PROTOBUF_PLATFORMS.keys()
39+
]
40+
41+
[
42+
toolchain(
43+
name = name + "-toolchain",
44+
target_compatible_with = config["compatible_with"],
45+
toolchain = ":" + name + "-cc_toolchain",
46+
toolchain_type = "@rules_cc//cc:toolchain_type",
47+
)
48+
for name, config in PROTOBUF_PLATFORMS.items()
49+
]
50+
51+
cc_toolchain_config(
52+
name = "k8-config",
53+
linker_path = "/usr/bin/ld",
54+
sysroot = "/opt/manylinux/2014/x86_64",
55+
target_cpu = "x86_64",
56+
target_full_name = "x86_64-linux-gnu",
57+
)
58+
59+
cc_toolchain_config(
60+
name = "linux-aarch_64-config",
61+
linker_path = "/usr/bin/ld",
62+
sysroot = "/opt/manylinux/2014/aarch64",
63+
target_cpu = "aarch64",
64+
target_full_name = "aarch64-linux-gnu",
65+
)
66+
67+
cc_toolchain_config(
68+
name = "linux-ppcle_64-config",
69+
linker_path = "/usr/bin/ld",
70+
sysroot = "/opt/manylinux/2014/ppc64le",
71+
target_cpu = "ppc64",
72+
target_full_name = "powerpc64le-linux-gnu",
73+
)
74+
75+
cc_toolchain_config(
76+
name = "linux-s390_64-config",
77+
linker_path = "/usr/bin/ld",
78+
sysroot = "/opt/manylinux/2014/s390x",
79+
target_cpu = "systemz",
80+
target_full_name = "s390x-linux-gnu",
81+
)
82+
83+
cc_toolchain_config(
84+
name = "linux-x86_32-config",
85+
linker_path = "/usr/bin/ld",
86+
sysroot = "/opt/manylinux/2014/i686",
87+
target_cpu = "x86_32",
88+
target_full_name = "i386-linux-gnu",
89+
)
90+
91+
cc_toolchain_config(
92+
name = "linux-x86_64-config",
93+
linker_path = "/usr/bin/ld",
94+
sysroot = "/opt/manylinux/2014/x86_64",
95+
target_cpu = "x86_64",
96+
target_full_name = "x86_64-linux-gnu",
97+
)
98+
99+
cc_toolchain_config(
100+
name = "osx-aarch_64-config",
101+
extra_compiler_flags = [
102+
"-I/usr/tools/xcode_14_0/macosx/usr/include/c++/v1",
103+
"-I/usr/tools/xcode_14_0/macosx/usr/include",
104+
"-F/usr/tools/xcode_14_0/macosx/System/Library/Frameworks",
105+
"-Wno-error=nullability-completeness",
106+
"-Wno-error=availability",
107+
"-Wno-error=elaborated-enum-base",
108+
],
109+
extra_linker_flags = ["-framework CoreFoundation"],
110+
linker_path = "/usr/tools",
111+
sysroot = "/usr/tools/xcode_14_0/macosx",
112+
target_cpu = "aarch64",
113+
target_full_name = "aarch64-apple-macosx12.0",
114+
)
115+
116+
cc_toolchain_config(
117+
name = "osx-x86_64-config",
118+
extra_compiler_flags = [
119+
"-I/usr/tools/xcode_14_0/macosx/usr/include/c++/v1",
120+
"-I/usr/tools/xcode_14_0/macosx/usr/include",
121+
"-F/usr/tools/xcode_14_0/macosx/System/Library/Frameworks",
122+
"-Wno-error=nullability-completeness",
123+
"-Wno-error=availability",
124+
"-Wno-error=elaborated-enum-base",
125+
],
126+
extra_linker_flags = ["-framework CoreFoundation"],
127+
linker_path = "/usr/tools",
128+
sysroot = "/usr/tools/xcode_14_0/macosx",
129+
target_cpu = "x86_64",
130+
target_full_name = "x86_64-apple-macosx12.0",
131+
)
132+
133+
cc_toolchain_config(
134+
name = "win32-config",
135+
extra_compiler_flags = [
136+
"-isystem/usr/lib/gcc/i686-w64-mingw32/10-posix/include/c++",
137+
"-isystem/usr/lib/gcc/i686-w64-mingw32/10-posix/include/c++/i686-w64-mingw32",
138+
],
139+
extra_include = "/usr/lib/gcc/i686-w64-mingw32",
140+
extra_linker_flags = [
141+
"-L/usr/lib/gcc/i686-w64-mingw32/10-posix",
142+
"-ldbghelp",
143+
"-pthread",
144+
],
145+
linker_path = "/usr/bin/ld",
146+
sysroot = "/usr/i686-w64-mingw32",
147+
target_cpu = "x86_32",
148+
target_full_name = "i686-w64-mingw32",
149+
)
150+
151+
cc_toolchain_config(
152+
name = "win64-config",
153+
extra_compiler_flags = [
154+
"-isystem/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include/c++/",
155+
"-isystem/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include/c++/x86_64-w64-mingw32",
156+
],
157+
extra_include = "/usr/lib/gcc/x86_64-w64-mingw32/10-posix/include",
158+
extra_linker_flags = [
159+
"-L/usr/lib/gcc/x86_64-w64-mingw32/10-posix",
160+
"-ldbghelp",
161+
],
162+
linker_path = "/usr/bin/ld",
163+
sysroot = "/usr/x86_64-w64-mingw32",
164+
target_cpu = "x86_64",
165+
target_full_name = "x86_64-w64-mingw32",
166+
)

0 commit comments

Comments
 (0)