bazel-contrib / toolchains_llvm

LLVM toolchain for bazel
Apache License 2.0
291 stars 209 forks source link

Use LLVM toolchain in a module which can be used as both a dependency and a root module #243

Closed mering closed 8 months ago

mering commented 8 months ago

I have a Bzlmod module "A" which I want to be able to build standalone but also be depended on by module "B".

If I define

bazel_dep(name = "toolchains_llvm", version = "0.10.3")
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm.toolchain(
    llvm_version = "14.0.0",
    sysroot = {
        "linux-x86_64": "chromium_x64_sysroot",
    },
    sysroot_label_map = {
        "@com_googleapis_storage_chrome_linux_amd64_sysroot//:all_files": "chromium_x64_sysroot",
    },
)
use_repo(llvm, "llvm_toolchain")
register_toolchains("@llvm_toolchain//:all")

in MODULE.bazel of both module "A" and "B", then I get the following error when compiling module "B" which depends on module "A":

ERROR: Traceback (most recent call last):
        File "/home/USER/.cache/bazel/_bazel_builder/395eea75460def8a11dd35795d615210/external/toolchains_llvm~override/toolchain/extensions/llvm.bzl", line 13, column 17, in _llvm_impl_
                fail("Only the root module can use the 'llvm' extension")
Error in fail: Only the root module can use the 'llvm' extension

But if I remove it from module "A", I can no longer build module "A" correctly standalone.

How can I define the llvm_toolchain such that I can use it in both modules "A" and "B"?


Based on #235.

mering commented 8 months ago

Would it be possible to just ignore non-root tool chain calls and print a warning instead of the fail()? This is what the Python tool chain is doing as well.

fmeum commented 8 months ago

You can add dev_dependency = True to your use_extension and register_toolchains calls. Then the toolchain definition in A will be ignored when building from B. If needed, B can define its own toolchain in this way.

mering commented 8 months ago

Using dev_dependency worked. Thanks!