f0rmiga / gcc-toolchain

A fully-hermetic Bazel GCC toolchain for Linux.
Apache License 2.0
103 stars 24 forks source link

[Bug]: `gcc_toolchain` parameters like `includes` replaces all items instead of being extras as documented #134

Open kriswuollett opened 1 year ago

kriswuollett commented 1 year ago

What happened?

Tried registering toolchain with includes to add something like /usr/include/linux so <stddef.h> could be found. Using the includes parameter did include that directory for includes, but removed all the defaults.

Version

Development (host) and target OS/architectures:

host: linux/amd64 targets: linux/amd64, linux/aarch64

Output of bazel --version: bazel 6.1.1

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file:

workspace(name = "my-code")

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

http_archive(
    name = "rules_cc",
    sha256 = "3d9e271e2876ba42e114c9b9bc51454e379cbf0ec9ef9d40e2ae4cec61a31b40",
    strip_prefix = "rules_cc-0.0.6",
    urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.6/rules_cc-0.0.6.tar.gz"],
)

http_archive(
    name = "rules_pkg",
    sha256 = "8c20f74bca25d2d442b327ae26768c02cf3c99e93fad0381f32be9aab1967675",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.8.1/rules_pkg-0.8.1.tar.gz",
        "https://github.com/bazelbuild/rules_pkg/releases/download/0.8.1/rules_pkg-0.8.1.tar.gz",
    ],
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()

git_repository(
    name = "aspect_gcc_toolchain",
    commit = "4bd1f94536ee92b7c49673931773038d923ee86e",
    remote = "https://github.com/aspect-build/gcc-toolchain",
)

#local_repository(
#    name = "aspect_gcc_toolchain",
#    path = "/home/kris/code/kriswuollett/gcc-toolchain",
#)

load("@aspect_gcc_toolchain//toolchain:repositories.bzl", "gcc_toolchain_dependencies")

gcc_toolchain_dependencies()

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

gcc_register_toolchain(
    name = "gcc_toolchain_aarch64",
    target_arch = ARCHS.aarch64,
    includes = [
        "%sysroot%/usr/include/linux",
    ],
)

gcc_register_toolchain(
    name = "gcc_toolchain_armv7",
    target_arch = ARCHS.armv7,
    includes = [
        "%sysroot%/usr/include/linux",
    ],
)

gcc_register_toolchain(
    name = "gcc_toolchain_x86_64",
    sysroot_variant = "x86_64-X11",
    target_arch = ARCHS.x86_64,
    includes = [
        "%sysroot%/usr/include/linux",
    ],
)

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "dd926a88a564a9246713a9c00b35315f54cbd46b31a26d5d8fb264c07045f05d",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_download_sdk(
    name = "go_1_20_2_host",
    version = "1.20.2",
)

go_download_sdk(
    name = "go_1_20_2_linux_amd64",
    goarch = "amd64",
    goos = "linux",
    version = "1.20.2",
)

go_download_sdk(
    name = "go_1_20_2_linux_arm64",
    goarch = "arm64",
    goos = "linux",
    version = "1.20.2",
)

go_register_toolchains()

http_archive(
    name = "bazel_gazelle",
    sha256 = "ecba0f04f96b4960a5b250c8e8eeec42281035970aa8852dda73098274d14a1d",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
    ],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("//:go.bzl", "go_dependencies")

# gazelle:repository_macro go.bzl%go_dependencies
go_dependencies()

gazelle_dependencies(go_sdk = "go_1_20_2_host")

http_archive(
    name = "rules_rust",
    sha256 = "b4e622a36904b5dd2d2211e40008fc473421c8b51c9efca746ab2ecf11dca08e",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.19.1/rules_rust-v0.19.1.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(
    edition = "2021",
    versions = [
        "nightly/2023-03-24",
    ],
    extra_target_triples = [
        "aarch64-unknown-linux-gnu",
        "x86_64-unknown-linux-gnu",
        "wasm32-unknown-unknown",
        "wasm32-wasi",
    ],
)

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies()

load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "render_config")
load("//bazel:rust.bzl", "RUST_PACKAGES")

crates_repository(
    name = "crate_index",
    cargo_lockfile = "//:Cargo.lock",
    generate_binaries = True,
    lockfile = "//:Cargo.Bazel.lock",
    packages = RUST_PACKAGES,
    render_config = render_config(
        default_package_name = "",
    ),
    rust_version = "nightly/2023-03-05",
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")

rust_analyzer_dependencies()

http_archive(
    name = "rules_oci",
    patch_args = ["-p1"],
    patches = ["//bazel:rules_oci_registry.patch"],
    sha256 = "48642588e91e992772b94de06234da6601854fda0ee32a91ce8ef303cf5e5837",
    strip_prefix = "rules_oci-0.3.7",
    url = "https://github.com/bazel-contrib/rules_oci/releases/download/v0.3.7/rules_oci-v0.3.7.tar.gz",
)

load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies")

rules_oci_dependencies()

load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "LATEST_ZOT_VERSION", "oci_register_toolchains")

register_toolchains("//registry:registry_toolchain")

oci_register_toolchains(
    name = "oci",
    crane_version = LATEST_CRANE_VERSION,
    zot_version = LATEST_ZOT_VERSION,
)

load("@rules_oci//oci:pull.bzl", "oci_pull")

oci_pull(
    name = "distroless_base",
    digest = "sha256:5812871f5d87d6d4c226c536be70f7a8232a77230675b4b574c9866c8dc982fa",
    image = "gcr.io/distroless/base",
    platforms = [
        "linux/amd64",
        "linux/arm64",
    ],
)

oci_pull(
    name = "distroless_cc",
    digest = "sha256:f252d3ca44b7f2c718c67c2020feee7b7fd4ee6bff8a192dfea6e599b7d820ad",
    # TODO(https://github.com/bazel-contrib/rules_oci/issues/74) Use latest images
    # digest = "sha256:fb402c45f3ef485ccd56ca2af2a58615fc47c4978bb3004e8663a83456791f48",
    image = "gcr.io/distroless/cc",
    platforms = [
        "linux/amd64",
        "linux/arm64",
    ],
)

Language(s) and/or frameworks involved:

Rust

How to reproduce

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

gcc_register_toolchain(
    name = "gcc_toolchain_aarch64",
    target_arch = ARCHS.aarch64,
    includes = [
        "%sysroot%/usr/include/linux",
    ],
)

Any other information?

The documentation says that parameters like includes are supposed to be extra values:

https://github.com/aspect-build/gcc-toolchain/blob/4bd1f94536ee92b7c49673931773038d923ee86e/docs/defs.md?plain=1#L25-L28

The invocation of gcc-toolchain uses dict::pop that has the base values as the default which is only used if the key like "includes" is not in the dict.

https://github.com/aspect-build/gcc-toolchain/blob/4bd1f94536ee92b7c49673931773038d923ee86e/toolchain/defs.bzl#L286-L298

So it is not possible to add extra include directories without copy and pasting the directories from the following code:

https://github.com/aspect-build/gcc-toolchain/blob/4bd1f94536ee92b7c49673931773038d923ee86e/toolchain/defs.bzl#L418-L430

Original error without specifying includes:

error occurred: Command "/home/kris/.cache/bazel/_bazel_kris/ebd20a16b5bb6af41a7a481997cc3984/sandbox/linux-sandbox/556/execroot/my-code/external/gcc_toolchain_aarch64/bin/gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "--sysroot" "external/sysroot_aarch64" "-no-canonical-prefixes" "-fno-canonical-system-headers" "-Wno-builtin-macro-redefined" "-D__DATE__=\"redacted\"" "-D__TIMESTAMP__=\"redacted\"" "-D__TIME__=\"redacted\"" "-fdiagnostics-color=always" "-nostdinc" "-Bexternal/gcc_toolchain_aarch64/bin" "-isystemexternal/sysroot_aarch64//aarch64-linux/include/c++/10.3.0" "-isystemexternal/sysroot_aarch64//aarch64-linux/include/c++/10.3.0/aarch64-linux" "-isystemexternal/sysroot_aarch64//lib/gcc/aarch64-linux/10.3.0/include-fixed" "-isystemexternal/sysroot_aarch64//lib/gcc/aarch64-linux/10.3.0/include" "-isystemexternal/sysroot_aarch64//usr/include" "-I" "zstd/lib/" "-I" "zstd/lib/common" "-fvisibility=hidden" "-ffunction-sections" "-fdata-sections" "-fmerge-all-constants" "-DZSTD_LIB_DEPRECATED=0" "-DXXH_PRIVATE_API=" "-DZSTDLIB_VISIBILITY=" "-DZSTDERRORLIB_VISIBILITY=" "-o" "/home/kris/.cache/bazel/_bazel_kris/ebd20a16b5bb6af41a7a481997cc3984/sandbox/linux-sandbox/556/execroot/my-code/bazel-out/k8-fastbuild-ST-87f845362927/bin/external/crate_index__zstd-sys-2.0.7-zstd.1.5.4/zstd-sys_build_script.out_dir/zstd/lib/common/ent

Error with specifying includes, the original base system includes are missing:

error occurred: Command "/home/kris/.cache/bazel/_bazel_kris/ebd20a16b5bb6af41a7a481997cc3984/sandbox/linux-sandbox/612/execroot/my-code/external/gcc_toolchain_aarch64/bin/gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "--sysroot" "external/sysroot_aarch64" "-no-canonical-prefixes" "-fno-canonical-system-headers" "-Wno-builtin-macro-redefined" "-D__DATE__=\"redacted\"" "-D__TIMESTAMP__=\"redacted\"" "-D__TIME__=\"redacted\"" "-fdiagnostics-color=always" "-nostdinc" "-Bexternal/gcc_toolchain_aarch64/bin" "-isystemexternal/sysroot_aarch64//usr/include/linux" "-I" "zstd/lib/" "-I" "zstd/lib/common" "-fvisibility=hidden" "-ffunction-sections" "-fdata-sections" "-fmerge-all-constants" "-DZSTD_LIB_DEPRECATED=0" "-DXXH_PRIVATE_API=" "-DZSTDLIB_VISIBILITY=" "-DZSTDERRORLIB_VISIBILITY=" "-o" "/home/kris/.cache/bazel/_bazel_kris/ebd20a16b5bb6af41a7a481997cc3984/sandbox/linux-sandbox/612/execroot/my-code/bazel-out/k8-fastbuild-ST-87f845362927/bin/external/crate_index__zstd-sys-2.0.7-zstd.1.5.4/zstd-sys_build_script.out_dir/zstd/lib/common/entropy_common.o" "-c" "zstd/lib/common/entropy_common.c" with args "gcc" did not execute successfully (status code exit status: 1).
blackgnezdo commented 10 months ago

I also ran into this problem while trying to use aspect_gcc_toolchain with rules-rust. It looks like the story of using the two together is not fully developed.