-
-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gcc toolchain example (#355)
Co-authored-by: Alex Eagle <[email protected]>
- Loading branch information
Showing
11 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
common --enable_platform_specific_config | ||
|
||
# Prevent Bazel from detecting the system's C++ toolchain. | ||
build:linux --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 | ||
build:linux --incompatible_strict_action_env=true | ||
# Enable the CC toolchain resolution based on platforms. | ||
build:linux --incompatible_enable_cc_toolchain_resolution |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
|
||
# Stage 0 | ||
|
||
**Linux-only** | ||
|
||
In this initial stage, we'll focus on setting up a hermetic GCC toolchain for | ||
our Bazel build environment, utilizing the hermetic GCC toolchain provided here. | ||
A hermetic toolchain is crucial for achieving deterministic builds, ensuring | ||
that our build outputs are solely dependent on our input sources and build | ||
instructions, unaffected by the external system environment. | ||
|
||
Note that there are several options for hermetic toolchains, depending on which | ||
compiler you use. | ||
See <https://github.com/bazelbuild/rules_cc#using-the-rules_cc-toolchain>. | ||
|
||
## Load the hermetic GCC toolchain | ||
|
||
Add to your WORKSPACE file: | ||
|
||
```python | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "aspect_gcc_toolchain", | ||
sha256 = "<sha256>", # Replace with the SHA-256 hash of the release tarball. | ||
strip_prefix = "gcc-toolchain-<version>", # Replace with the release version. | ||
urls = [ | ||
# Replace with the release version. | ||
"https://github.com/aspect-build/gcc-toolchain/archive/<version>.tar.gz", | ||
], | ||
) | ||
|
||
load("@aspect_gcc_toolchain//toolchain:repositories.bzl", "gcc_toolchain_dependencies") | ||
|
||
gcc_toolchain_dependencies() | ||
|
||
load("@aspect_gcc_toolchain//toolchain:defs.bzl", "gcc_register_toolchain", "ARCHS") | ||
|
||
gcc_register_toolchain( | ||
name = "gcc_toolchain_x86_64", | ||
target_arch = ARCHS.x86_64, | ||
) | ||
``` | ||
|
||
### The X11 sysroot variant | ||
|
||
The hermetic GCC toolchain is available in two variants: the X11 sysroot variant | ||
and the non-X11 sysroot variant. The non-X11 sysroot variant is the default | ||
variant, and is suitable for building command-line applications. The X11 sysroot | ||
variant is suitable for building GUI applications that require X11 libraries. | ||
|
||
To use the X11 sysroot variant, replace the `gcc_toolchain_x86_64` target in | ||
the above snippet with the following: | ||
|
||
```python | ||
gcc_register_toolchain( | ||
name = "gcc_toolchain_x86_64", | ||
sysroot_variant = "x86_64-X11", | ||
target_arch = ARCHS.x86_64, | ||
) | ||
``` | ||
|
||
## Cross-compiling for ARM | ||
|
||
This approach uses `--platforms` to cross-compile for ARM. For more information | ||
on how to use `platforms`, see the documentation here: | ||
https://bazel.build/extending/platforms. | ||
|
||
### aarch64 (a.k.a arm64 or armv8) | ||
|
||
Add to your WORKSPACE file: | ||
|
||
```python | ||
gcc_register_toolchain( | ||
name = "gcc_toolchain_aarch64", | ||
target_arch = ARCHS.aarch64, | ||
) | ||
``` | ||
|
||
### armv7-hf (32-bit) | ||
|
||
Add to your WORKSPACE file: | ||
|
||
```python | ||
gcc_register_toolchain( | ||
name = "gcc_toolchain_armv7", | ||
target_arch = ARCHS.armv7, | ||
) | ||
``` | ||
|
||
## Setting up the required flags in your .bazelrc | ||
|
||
Add to your .bazelrc file: | ||
|
||
``` | ||
# Prevent Bazel from detecting the system's C++ toolchain. | ||
build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 | ||
build --incompatible_strict_action_env=true | ||
# Enable the CC toolchain resolution based on platforms. | ||
build --incompatible_enable_cc_toolchain_resolution | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "aspect_gcc_toolchain", | ||
sha256 = "3341394b1376fb96a87ac3ca01c582f7f18e7dc5e16e8cf40880a31dd7ac0e1e", | ||
strip_prefix = "gcc-toolchain-0.4.2", | ||
urls = [ | ||
"https://github.com/aspect-build/gcc-toolchain/archive/refs/tags/0.4.2.tar.gz", | ||
], | ||
) | ||
|
||
load("@aspect_gcc_toolchain//toolchain:repositories.bzl", "gcc_toolchain_dependencies") | ||
|
||
gcc_toolchain_dependencies() | ||
|
||
load("@aspect_gcc_toolchain//toolchain:defs.bzl", "gcc_register_toolchain", "ARCHS") | ||
|
||
gcc_register_toolchain( | ||
name = "gcc_toolchain_x86_64", | ||
target_arch = ARCHS.x86_64, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
common --enable_platform_specific_config | ||
|
||
# Prevent Bazel from detecting the system's C++ toolchain. | ||
build:linux --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 | ||
build:linux --incompatible_strict_action_env=true | ||
# Enable the CC toolchain resolution based on platforms. | ||
build:linux --incompatible_enable_cc_toolchain_resolution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "aspect_gcc_toolchain", | ||
sha256 = "3341394b1376fb96a87ac3ca01c582f7f18e7dc5e16e8cf40880a31dd7ac0e1e", | ||
strip_prefix = "gcc-toolchain-0.4.2", | ||
urls = [ | ||
"https://github.com/aspect-build/gcc-toolchain/archive/refs/tags/0.4.2.tar.gz", | ||
], | ||
) | ||
|
||
load("@aspect_gcc_toolchain//toolchain:repositories.bzl", "gcc_toolchain_dependencies") | ||
|
||
gcc_toolchain_dependencies() | ||
|
||
load("@aspect_gcc_toolchain//toolchain:defs.bzl", "gcc_register_toolchain", "ARCHS") | ||
|
||
gcc_register_toolchain( | ||
name = "gcc_toolchain_x86_64", | ||
target_arch = ARCHS.x86_64, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
common --enable_platform_specific_config | ||
|
||
# Prevent Bazel from detecting the system's C++ toolchain. | ||
build:linux --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 | ||
build:linux --incompatible_strict_action_env=true | ||
# Enable the CC toolchain resolution based on platforms. | ||
build:linux --incompatible_enable_cc_toolchain_resolution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "aspect_gcc_toolchain", | ||
sha256 = "3341394b1376fb96a87ac3ca01c582f7f18e7dc5e16e8cf40880a31dd7ac0e1e", | ||
strip_prefix = "gcc-toolchain-0.4.2", | ||
urls = [ | ||
"https://github.com/aspect-build/gcc-toolchain/archive/refs/tags/0.4.2.tar.gz", | ||
], | ||
) | ||
|
||
load("@aspect_gcc_toolchain//toolchain:repositories.bzl", "gcc_toolchain_dependencies") | ||
|
||
gcc_toolchain_dependencies() | ||
|
||
load("@aspect_gcc_toolchain//toolchain:defs.bzl", "gcc_register_toolchain", "ARCHS") | ||
|
||
gcc_register_toolchain( | ||
name = "gcc_toolchain_x86_64", | ||
target_arch = ARCHS.x86_64, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
common --enable_platform_specific_config | ||
|
||
# Prevent Bazel from detecting the system's C++ toolchain. | ||
build:linux --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 | ||
build:linux --incompatible_strict_action_env=true | ||
# Enable the CC toolchain resolution based on platforms. | ||
build:linux --incompatible_enable_cc_toolchain_resolution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "aspect_gcc_toolchain", | ||
sha256 = "3341394b1376fb96a87ac3ca01c582f7f18e7dc5e16e8cf40880a31dd7ac0e1e", | ||
strip_prefix = "gcc-toolchain-0.4.2", | ||
urls = [ | ||
"https://github.com/aspect-build/gcc-toolchain/archive/refs/tags/0.4.2.tar.gz", | ||
], | ||
) | ||
|
||
load("@aspect_gcc_toolchain//toolchain:repositories.bzl", "gcc_toolchain_dependencies") | ||
|
||
gcc_toolchain_dependencies() | ||
|
||
load("@aspect_gcc_toolchain//toolchain:defs.bzl", "gcc_register_toolchain", "ARCHS") | ||
|
||
gcc_register_toolchain( | ||
name = "gcc_toolchain_x86_64", | ||
target_arch = ARCHS.x86_64, | ||
) |