Skip to content

Commit 9a81dd8

Browse files
committed
PRYSM-2849 first cut at multi-arch cross compiling toolchain. currently supports arm64 and amd64 via docker cross compiler image
1 parent c2b30cf commit 9a81dd8

8 files changed

+853
-0
lines changed

.bazelrc

+29
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,32 @@ run --incompatible_strict_action_env
2929
build --define kafka_enabled=false
3030
test --define kafka_enabled=false
3131
run --define kafka_enabled=false
32+
33+
34+
# debug
35+
build:debug --sandbox_debug
36+
build:debug --toolchain_resolution_debug
37+
build:debug --verbose_failures
38+
39+
# linux_amd64, not strictly necessary since host/exec env is amd64
40+
build:linux_amd64 --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo
41+
42+
# linux_amd64_debug
43+
build:linux_amd64_debug --config=linux_amd64
44+
build:linux_amd64_debug --config=debug
45+
46+
# linux_arm64
47+
build:linux_arm64 --crosstool_top=//tools/cross-toolchain:multiarch_toolchain
48+
build:linux_arm64 --platforms=@io_bazel_rules_go//go/toolchain:linux_arm64_cgo
49+
build:linux_arm64 --copt=-funsafe-math-optimizations
50+
build:linux_arm64 --copt=-ftree-vectorize
51+
build:linux_arm64 --copt=-fomit-frame-pointer
52+
build:linux_arm64 --cpu=aarch64
53+
build:linux_arm64 --compiler=clang
54+
build:linux_arm64 --copt=-march=armv8-a
55+
build:linux_arm64 --copt=-march=armv8.2-a
56+
57+
#linux_arm64_debug
58+
build:linux_arm64_debug --config=linux_arm64
59+
build:linux_arm64_debug --config=debug
60+

WORKSPACE

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ workspace(name = "prysm")
33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
55

6+
register_toolchains(
7+
"//tools/cross-toolchain:cc-toolchain-clang"
8+
)
9+
610
http_archive(
711
name = "bazel_skylib",
812
sha256 = "2ea8a5ed2b448baf4a6855d3ce049c4c452a6470b1efd1504fdb7c1c134d220a",
@@ -1575,3 +1579,11 @@ go_repository(
15751579
commit = "e180dbdc8da04c4fa04272e875ce64949f38bd3e",
15761580
importpath = "github.com/shibukawa/configdir",
15771581
)
1582+
1583+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies", "go_host_sdk")
1584+
1585+
go_rules_dependencies()
1586+
1587+
#go_host_sdk(name = "go_sdk")
1588+
1589+
go_register_toolchains()

tools/cross-toolchain/BUILD

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load(":cc_toolchain_config_linux_arm64.bzl", "arm64_cc_toolchain_config")
4+
load(":cc_toolchain_config_osx.bzl", "osx_cc_toolchain_config")
5+
6+
cc_toolchain_suite(
7+
name = "multiarch_toolchain",
8+
toolchains = {
9+
"k8|osxcross": ":cc-clang-osx",
10+
"k8|clang": "cc-clang-amd64",
11+
"aarch64|clang": ":cc-clang-arm64",
12+
"k8": "cc-clang-amd64",
13+
"aarch64": ":cc-clang-arm64",
14+
},
15+
)
16+
17+
filegroup(
18+
name = "empty",
19+
srcs = [],
20+
)
21+
22+
config_setting(
23+
name = "osx_amd64",
24+
constraint_values = [
25+
"@platforms//os:osx",
26+
"@platforms//cpu:x86_64",
27+
],
28+
)
29+
30+
config_setting(
31+
name = "linux_arm64",
32+
constraint_values = [
33+
"@platforms//os:linux",
34+
"@platforms//cpu:aarch64",
35+
],
36+
)
37+
38+
config_setting(
39+
name = "linux_amd64",
40+
constraint_values = [
41+
"@platforms//os:linux",
42+
"@platforms//cpu:x86_64",
43+
],
44+
)
45+
46+
config_setting(
47+
name = "windows_amd64",
48+
constraint_values = [
49+
"@platforms//os:windows",
50+
"@platforms//cpu:x86_64",
51+
],
52+
)
53+
54+
arm64_cc_toolchain_config(
55+
name = "local-arm64",
56+
target = "aarch64-linux-gnu",
57+
)
58+
59+
arm64_cc_toolchain_config(
60+
name = "local-amd64",
61+
target = "x86_64-unknown-linux-gnu",
62+
)
63+
64+
osx_cc_toolchain_config(
65+
name = "local-osxcross",
66+
target = "darwin_x86_64",
67+
)
68+
69+
cc_toolchain(
70+
name = "cc-clang-arm64",
71+
all_files = ":empty",
72+
compiler_files = ":empty",
73+
dwp_files = ":empty",
74+
linker_files = ":empty",
75+
objcopy_files = ":empty",
76+
strip_files = ":empty",
77+
supports_param_files = 1,
78+
toolchain_config = ":local-arm64",
79+
)
80+
81+
cc_toolchain(
82+
name = "cc-clang-osx",
83+
all_files = ":empty",
84+
compiler_files = ":empty",
85+
dwp_files = ":empty",
86+
linker_files = ":empty",
87+
objcopy_files = ":empty",
88+
strip_files = ":empty",
89+
supports_param_files = 1,
90+
toolchain_config = ":local-osxcross",
91+
)
92+
93+
cc_toolchain(
94+
name = "cc-clang-amd64",
95+
all_files = ":empty",
96+
compiler_files = ":empty",
97+
dwp_files = ":empty",
98+
linker_files = ":empty",
99+
objcopy_files = ":empty",
100+
strip_files = ":empty",
101+
supports_param_files = 1,
102+
toolchain_config = ":local-amd64",
103+
)
104+
105+
toolchain(
106+
name = "cc-toolchain-clang",
107+
exec_compatible_with = [
108+
"@platforms//os:linux",
109+
"@platforms//cpu:x86_64",
110+
],
111+
target_compatible_with = [],
112+
toolchain = select({
113+
":linux_arm64": ":cc-clang-arm64",
114+
":linux_amd64": ":cc-clang-amd64",
115+
":osx_amd64": ":cc-clang-osx",
116+
}),
117+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
118+
)

tools/cross-toolchain/Dockerfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# containerized build environment
2+
FROM debian:buster as build
3+
4+
# install gnu/gcc cross-build toolchain (gcc 8.3)
5+
ENV DOCKER_CLI_EXPERIMENTAL=enabled
6+
RUN apt-get update && \
7+
apt-get install -y \
8+
curl xz-utils \
9+
gcc g++ \
10+
gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf \
11+
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
12+
cmake libssl-dev libxml2-dev vim apt-transport-https \
13+
zip unzip libtinfo5 patch zlib1g-dev autoconf libtool \
14+
pkg-config make docker.io gnupg2 && \
15+
curl https://bazel.build/bazel-release.pub.gpg | apt-key add - && \
16+
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list && \
17+
apt update && apt install -y bazel && \
18+
apt-get clean && \
19+
rm -rf /var/lib/apt/lists/*
20+
21+
# install llvm/clang cross-build toolchains
22+
ENV INSTALL_LLVM_VERSION=10.0.0-rc2
23+
ADD install_clang_cross10.sh /tmp/install_clang_cross.sh
24+
RUN /tmp/install_clang_cross.sh
25+
26+
# install osxcross
27+
RUN git clone https://github.com/tpoechtrager/osxcross.git && \
28+
cd osxcross/tarballs && \
29+
curl -LO https://www.dropbox.com/s/yfbesd249w10lpc/MacOSX10.10.sdk.tar.xz && \
30+
cd .. && chmod 755 build.sh tools/*.sh && UNATTENDED=1 ./build.sh && \
31+
mv target /usr/x86_64-apple-darwin/osxcross
32+
# containerized development environment
33+
FROM build as devel
34+
RUN mkdir /workdir
35+
WORKDIR /workdir
36+
RUN echo 'PS1="\[$(tput setaf 3)$(tput bold)[\]devel@\\h:\\W]#\[$(tput sgr0) \]"' >> /root/.bashrc

tools/cross-toolchain/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# WIP Multiarch Cross Compiling Toolchain
2+
3+
### Containerized Builds
4+
This project declares a c++ toolchain suite with cross compilers for targeting four platforms:
5+
* linux_amd64
6+
* linux_arm64
7+
* osx_amd64
8+
* windows_amd64
9+
10+
The toolchain suite describes cross compilers defined in a docker container described by a Dockerfile, also included in this project.
11+
12+
13+
### Using Published Docker Container for Cross Compilation Targets:
14+
This is still a WIP, at the time of this writing only linux_amd64 and linux_arm64 are working targets.
15+
16+
#### Run the cross compiler image
17+
1. checkout prysm, `git clone https://github.com/prysmaticlabs/prysm`
18+
2. cd prysm
19+
3. ``docker run -it -v $(pwd):/workdir suburbandad/cross-clang-10:latest`
20+
21+
From here you can run builds inside the linux x86_64 container image, e.g.:
22+
23+
| arch | os | config | working? | example cmd |
24+
|--------|----------|---------------|----------|---------------|
25+
| arm64 | linux | linux_arm64 | Y | `bazel build --config=linux_arm64 //beacon-chain` |
26+
| x86_64 | linux | linux_amd64 | Y | `bazel build --config=linux_amd64 //beacon-chain` |
27+
| x86_64 | osx | osx_amd64 | N | `bazel build --config=osx_arm64 //beacon-chain` |
28+
| x86_64 | windows | windows_amd64 | N | `bazel build --config=windows_arm64 //beacon-chain` |
29+
30+
31+
#### Or, if you just want to run a particular target, this is handy:
32+
For example, to build the beacon chain for linux_arm64:
33+
`docker run -it -v $(pwd):/workdir suburbandad/cross-clang-10:latest bazel build --config=linux_arm64 //beacon-chain`
34+
`
35+
36+
Also fun, if you are on OSX can build and run a linux_amd64 beacon-chain:
37+
`docker run -it -v $(pwd):/workdir suburbandad/cross-clang-10:latest bazel run //beacon-chain`
38+
39+
40+
### Coming soon
41+
* Mac OSX x86_64 builds
42+
* Windows x86_64 builds
43+

0 commit comments

Comments
 (0)