bazelbuild / rules_foreign_cc

Build rules for interfacing with "foreign" (non-Bazel) build systems (CMake, configure-make, GNU Make, boost, ninja, Meson)
https://bazelbuild.github.io/rules_foreign_cc
Apache License 2.0
658 stars 239 forks source link

Enable changing cmake versions with bzlmod #1224

Open brianantony456 opened 1 month ago

brianantony456 commented 1 month ago

Current it is not possible to change the cmake versions with bzlmod.

We were able to change the cmake version by patching MODULE.bazel and using archive_override

Patch

diff --git a/MODULE.bazel b/MODULE.bazel
index da990d8..a1a116a 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -13,7 +13,7 @@ bazel_dep(name = "rules_python", version = "0.20.0")
 tools = use_extension("@rules_foreign_cc//foreign_cc:extensions.bzl", "tools")
 use_repo(
     tools,
-    "cmake_3.23.2_toolchains",
+    # "cmake_3.23.2_toolchains",
     "cmake_src",
     "gnumake_src",
     "meson_src",
@@ -32,6 +32,6 @@ register_toolchains(
     "@rules_foreign_cc//toolchains:preinstalled_automake_toolchain",
     "@rules_foreign_cc//toolchains:preinstalled_m4_toolchain",
     "@rules_foreign_cc//toolchains:preinstalled_pkgconfig_toolchain",
-    "@cmake_3.23.2_toolchains//:all",
+    # "@cmake_3.23.2_toolchains//:all",
     "@ninja_1.11.1_toolchains//:all",
 )
diff --git a/foreign_cc/extensions.bzl b/foreign_cc/extensions.bzl
index 23fd14b..5319b55 100644
--- a/foreign_cc/extensions.bzl
+++ b/foreign_cc/extensions.bzl
@@ -29,7 +29,7 @@ def _init(module_ctx):
     }

     for mod in module_ctx.modules:
-        if not mod.is_root:
+        if mod.is_root:
             for toolchain in mod.tags.cmake:
                 versions["cmake"] = toolchain.version

content in MODULE.bazel

bazel_dep(name = "rules_foreign_cc", version = "0.11.1")

# We patch MODULE.bazel, hence we cannot depend on bcr & have to use archive_override
archive_override(
    module_name = "rules_foreign_cc",
    integrity = "sha256-SzPWLPEJvMzyhrMO1xIRKcw0z09O2dihHzjZEI9AunQ=",
    patch_strip = 1,
    patches = [
        "//:/0002-Enable-CMake-Version-Overwrite-with-Bzlmod.patch",
    ],
    strip_prefix = "rules_foreign_cc-0.11.1",
    urls = [
        "https://github.com/bazelbuild/rules_foreign_cc/releases/download/0.11.1/rules_foreign_cc-0.11.1.tar.gz",
    ],
)

tools = use_extension("@rules_foreign_cc//foreign_cc:extensions.bzl", "tools")

tools.cmake(version = "3.26.4")
use_repo(tools, "cmake_3.26.4_toolchains")

Version: rules_foreign_cc-0.10.1

Are there any plans to support this out of the box ? Is there perhaps a better way to do this ?

jsharpe commented 1 month ago

Yes - this PR: https://github.com/bazelbuild/rules_foreign_cc/pull/1158 - but there are some issues with the design for the meson toolchain that needs addressing before that can land.