Skip to content

[WIP] toolchain: Add cxx_lib attribute to inject libc++ in x-compile#696

Draft
phlax wants to merge 1 commit intobazel-contrib:masterfrom
phlax:cxx_lib
Draft

[WIP] toolchain: Add cxx_lib attribute to inject libc++ in x-compile#696
phlax wants to merge 1 commit intobazel-contrib:masterfrom
phlax:cxx_lib

Conversation

@phlax
Copy link
Contributor

@phlax phlax commented Mar 9, 2026

when cross-compiling you only need the exec toolchain and not the target one, except for the c++ libs themselves

to work around this currently toolchains_llvm falls back to using libstdc++ on the assumption that it is more likely to be present either on the host or in the sysroot

these foreign arch libs are only provided in the huge arch-specific toolchain or release tarballs, so we were keen not to have consumers downloading multiple toolchains for additional arches

to workaround this, we (envoy) now publish tarballs containining only lib/ and include/ folders with the libc++ libs fished out of the llvm release tarball

this PR allows toolchains_llvm to make use of this - example config from current envoy (workspace):

load("@envoy_toolshed//compile:libcxx_libs.bzl", "setup_libcxx_libs")
setup_libcxx_libs()

load("@toolchains_llvm//toolchain:rules.bzl", "llvm_toolchain")

llvm_toolchain(
    name = "llvm_toolchain",
    llvm_version = "18.1.8",
    # These libs are only included for cross-compile targets
    cxx_lib = {
        "linux-aarch64": "@libcxx_libs_aarch64",
        "linux-x86_64": "@libcxx_libs_x86_64",
    },
    cxx_standard = {"": "c++20"},
    sysroot = {} if USE_LOCAL_SYSROOT else {
        "linux-x86_64": "@sysroot_linux_amd64//:sysroot",
        "linux-aarch64": "@sysroot_linux_arm64//:sysroot",
    },
    toolchain_roots = {"": LLVM_PATH} if LLVM_PATH else {},
)
    

it can also be used with bzlmod like so

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency=True)
llvm.cxx_lib(
    name = "llvm_toolchain",
    label = "@libcxx_libs_aarch64",
    targets = ["linux-aarch64"],
)
llvm.cxx_lib(
    name = "llvm_toolchain",
    label = "@libcxx_libs_x86_64",
    targets = ["linux-x86_64"],
)
llvm.toolchain(
    name = "llvm_toolchain",
    llvm_version = "18.1.8",
)
use_repo(llvm, "llvm_toolchain")

partial fix for #694

@phlax phlax marked this pull request as draft March 9, 2026 23:13
@phlax phlax changed the title toolchain: Add cxx_lib attribute to inject libc++ in x-compile [WIP] toolchain: Add cxx_lib attribute to inject libc++ in x-compile Mar 10, 2026
Copy link
Collaborator

@dzbarsky dzbarsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What issues did you hit? In my experience, the c++ standard library include/ doesn't vary between targets (aside from a few minor things like fortran and libomp headers) and when cross-compiling the cxx lib comes from the sysroot, which this toolchain already supports?

@phlax
Copy link
Contributor Author

phlax commented Mar 10, 2026

i didnt really want to put it into sysroot, our sysroot currently does have libstdc++ but i wanted to remove that also - so its just glibc

there is a fix that this pr requires, but otherwise this is working really nicely in patches

Signed-off-by: Ryan Northey <ryan@synca.io>
@phlax
Copy link
Contributor Author

phlax commented Mar 12, 2026

@dzbarsky i probably didnt answer your question very well - i have updated the description which should hopefully address it

if including this is something the project is willing to consider, i would be happy to add some tests/docs

@phlax
Copy link
Contributor Author

phlax commented Mar 12, 2026

fwiw - i think cxx_lib -> cxx_cross_lib to make it clear its only used for cross compile

more fixes from downstream testing being tested atm - will update once that is done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants