Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WORKSPACE snippet to use gcc-toolchain #88

Open
tetsuok opened this issue Aug 20, 2022 · 3 comments
Open

WORKSPACE snippet to use gcc-toolchain #88

tetsuok opened this issue Aug 20, 2022 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@tetsuok
Copy link

tetsuok commented Aug 20, 2022

It seems a WORKSPACE snippet to set up gcc-toolchain is missing from the current documentation. It would be nice to include the WORKSPACE snippet in documentation or the release page so that users can start using gcc-toolchain without troubles by copying the WORKSPACE snippet into their WORKSPACE files.

Based on this repo's WORKSPACE, below is an example of the WORKSPACE snippet and .bazelrc to use gcc-toolchain v0.4.2 to build binaries for x86_64. It would be great to confirm that there is something wrong or redundant with the snippet. I mentioned this because unusual configs, --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 and --incompatible_enable_cc_toolchain_resolution were required to add to .bazelrc so that Bazel can use the C/C++ toolchain managed by gcc-toolchain. If these configs are required indeed, I think it worth noting it in the documentation because these configs not so obvious for non-Bazel experts.

WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Skylib
# gcc-toolchain depends on bazel-skylib
http_archive(
    name = "bazel_skylib",
    sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz",
    ],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
    name = "aspect_gcc_toolchain",
    sha256 = "3341394b1376fb96a87ac3ca01c582f7f18e7dc5e16e8cf40880a31dd7ac0e1e",
    strip_prefix = "gcc-toolchain-0.4.2",
    url = "https://github.com/aspect-build/gcc-toolchain/archive/refs/tags/0.4.2.tar.gz",
)

load("@aspect_gcc_toolchain//toolchain:defs.bzl", "ARCHS", "gcc_register_toolchain")

gcc_register_toolchain(
    name = "gcc_toolchain_x86_64",
    gcc_version = "10.3.0",
    sysroot_variant = "x86_64",
    target_arch = ARCHS.x86_64,
)

.bazelrc:

# Required to use the C/C++ toolchain managed by gcc-toolchain
build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
build --incompatible_enable_cc_toolchain_resolution

# optional, but preferred for performance as per https://github.com/aspect-build/gcc-toolchain/issues/85
build --experimental_reuse_sandbox_directories
@f0rmiga
Copy link
Owner

f0rmiga commented Aug 22, 2022

This is probably the missing piece of #75 after adding bcc2a04 (which triggered the blog post https://blog.aspect.dev/hermetic-c-toolchain).

I'm tempted to just add this snippet as part of the releases with the top-level README.md pointing to the link of the latest release.

@f0rmiga
Copy link
Owner

f0rmiga commented Aug 22, 2022

I agree we should be clear about the extra flags required, they are indeed non-obvious. Before finding BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN buried somewhere deep into the source code, I used to set CC=/bin/false to ensure the toolchain resolution was being enforced, but it started to break some IDEs based on other users reporting directly to me (I haven't experienced it myself).

@tetsuok
Copy link
Author

tetsuok commented Aug 22, 2022

I'm tempted to just add this snippet as part of the releases with the top-level README.md pointing to the link of the latest release.

That sounds good to me.

@cgrindel cgrindel added enhancement New feature or request prioritized labels Sep 28, 2022
@gregmagolan gregmagolan added documentation Improvements or additions to documentation and removed enhancement New feature or request prioritized labels Feb 6, 2023
@gregmagolan gregmagolan moved this to 🔖 Ready in Open Source Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants