Skip to content

Commit b80bdab

Browse files
committed
Enable users to customize cmake and ninja versions with bzlmod
Avoid hardcoding cmake and ninja versions in repository names that the bzlmod extension produces.
1 parent 265bbb9 commit b80bdab

File tree

4 files changed

+95
-86
lines changed

4 files changed

+95
-86
lines changed

MODULE.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ bazel_dep(name = "rules_cc", version = "0.0.9", dev_dependency = True)
1818
tools = use_extension("@rules_foreign_cc//foreign_cc:extensions.bzl", "tools")
1919
use_repo(
2020
tools,
21-
"cmake_3.23.2_toolchains",
2221
"cmake_src",
22+
"cmake_toolchains",
2323
"gettext_runtime",
2424
"glib_dev",
2525
"glib_src",
2626
"gnumake_src",
2727
"meson_src",
28-
"ninja_1.11.1_toolchains",
2928
"ninja_build_src",
29+
"ninja_toolchains",
3030
"pkgconfig_src",
3131
"rules_foreign_cc_framework_toolchains",
3232
)
@@ -40,6 +40,6 @@ register_toolchains(
4040
"@rules_foreign_cc//toolchains:preinstalled_automake_toolchain",
4141
"@rules_foreign_cc//toolchains:preinstalled_m4_toolchain",
4242
"@rules_foreign_cc//toolchains:preinstalled_pkgconfig_toolchain",
43-
"@cmake_3.23.2_toolchains//:all",
44-
"@ninja_1.11.1_toolchains//:all",
43+
"@cmake_toolchains//:all",
44+
"@ninja_toolchains//:all",
4545
)

foreign_cc/extensions.bzl

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,31 @@ def _init(module_ctx):
2323
register_built_pkgconfig_toolchain = True,
2424
)
2525

26-
versions = {
27-
"cmake": _DEFAULT_CMAKE_VERSION,
28-
"ninja": _DEFAULT_NINJA_VERSION,
29-
}
26+
cmake_version = _DEFAULT_CMAKE_VERSION
27+
ninja_version = _DEFAULT_NINJA_VERSION
3028

29+
# Traverse all modules starting from the root one (the first in
30+
# module_ctx.modules). The first occurrence of cmake or ninja tag wins.
31+
# Multiple versions requested from the same module are rejected.
3132
for mod in module_ctx.modules:
32-
if not mod.is_root:
33-
for toolchain in mod.tags.cmake:
34-
versions["cmake"] = toolchain.version
33+
cmake_versions_count = len(mod.tags.cmake)
34+
if cmake_versions_count == 1:
35+
cmake_version = mod.tags.cmake[0].version
36+
break
37+
elif cmake_versions_count > 1:
38+
fail("More than one cmake version requested: {}".format(mod.tags.cmake))
3539

36-
for toolchain in mod.tags.ninja:
37-
versions["ninja"] = toolchain.version
40+
for mod in module_ctx.modules:
41+
ninja_versions_count = len(mod.tags.ninja)
42+
if ninja_versions_count == 1:
43+
ninja_version = mod.tags.ninja[0].version
44+
break
45+
elif ninja_versions_count > 1:
46+
fail("More than one ninja version requested: {}".format(mod.tags.ninja))
3847

3948
prebuilt_toolchains(
40-
cmake_version = versions["cmake"],
41-
ninja_version = versions["ninja"],
49+
cmake_version = cmake_version,
50+
ninja_version = ninja_version,
4251
register_toolchains = False,
4352
)
4453

0 commit comments

Comments
 (0)