Skip to content

Commit

Permalink
Enable users to customize cmake and ninja versions with bzlmod
Browse files Browse the repository at this point in the history
Avoid hardcoding cmake and ninja versions in repository names that the
bzlmod extension produces.
  • Loading branch information
mrkkrp committed Jan 12, 2024
1 parent 265bbb9 commit 8cf1aa9
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 397 deletions.
8 changes: 4 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ bazel_dep(name = "rules_cc", version = "0.0.9", dev_dependency = True)
tools = use_extension("@rules_foreign_cc//foreign_cc:extensions.bzl", "tools")
use_repo(
tools,
"cmake_3.23.2_toolchains",
"cmake_src",
"cmake_toolchains",
"gettext_runtime",
"glib_dev",
"glib_src",
"gnumake_src",
"meson_src",
"ninja_1.11.1_toolchains",
"ninja_build_src",
"ninja_toolchains",
"pkgconfig_src",
"rules_foreign_cc_framework_toolchains",
)
Expand All @@ -40,6 +40,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",
"@ninja_1.11.1_toolchains//:all",
"@cmake_toolchains//:all",
"@ninja_toolchains//:all",
)
31 changes: 20 additions & 11 deletions foreign_cc/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,31 @@ def _init(module_ctx):
register_built_pkgconfig_toolchain = True,
)

versions = {
"cmake": _DEFAULT_CMAKE_VERSION,
"ninja": _DEFAULT_NINJA_VERSION,
}
cmake_version = _DEFAULT_CMAKE_VERSION
ninja_version = _DEFAULT_NINJA_VERSION

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

for toolchain in mod.tags.ninja:
versions["ninja"] = toolchain.version
for mod in module_ctx.modules:
ninja_versions_count = len(mod.tags.ninja)
if ninja_versions_count == 1:
ninja_version = mod.tags.ninja[0].version
break
elif ninja_versions_count > 1:
fail("More than one ninja version requested: {}".format(mod.tags.ninja))

prebuilt_toolchains(
cmake_version = versions["cmake"],
ninja_version = versions["ninja"],
cmake_version = cmake_version,
ninja_version = ninja_version,
register_toolchains = False,
)

Expand Down
Loading

0 comments on commit 8cf1aa9

Please sign in to comment.