Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable users to customize cmake and ninja versions with bzlmod #1157

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ use_repo(python, "python_3_9")
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",
)

register_toolchains(
"@rules_foreign_cc_framework_toolchains//:all",
"@cmake_3.23.2_toolchains//:all",
"@ninja_1.11.1_toolchains//:all",
"@cmake_toolchains//:all",
"@ninja_toolchains//:all",
"@python_3_9//:all",
"@rules_foreign_cc//toolchains:all",
)
2 changes: 1 addition & 1 deletion examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local_path_override(
)

tools = use_extension("@rules_foreign_cc//foreign_cc:extensions.bzl", "tools")
tools.cmake(version = "3.23.1")
tools.cmake(version = "3.25.2")
tools.ninja(version = "1.11.1")
use_repo(
tools,
Expand Down
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:
Copy link
Member

Choose a reason for hiding this comment

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

We need to maintain this check - only the root module should declare the versions of the tooling.
Good catch though on this being inverted; this should be if mod.is_root

Copy link
Author

Choose a reason for hiding this comment

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

The way it is written in this PR, we allow any module to declare a version of the tooling but the module that is closest to the root has the precedence. Is there a reason to strictly limit these version declarations to the root module only?

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))
Comment on lines +37 to +38
Copy link
Member

Choose a reason for hiding this comment

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

I think we should be able to support registering more than one version of cmake in a build. We should probably generate a version specific constraint that we apply to the toolchain targets so that a user can select a version constraint that way if they desire.

Copy link
Author

Choose a reason for hiding this comment

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

We do support declaring more than one version of cmake, but each such declaration should be in a different module. Here we simply guard against multiple declarations in the same module.

Copy link
Member

Choose a reason for hiding this comment

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

By removing the version number from the generated names though you will get duplicate definitions if a module defines one version and then the root requests a different version. This is also the rationale for restricting definitions to the root module at least for the time being.

Copy link
Author

Choose a reason for hiding this comment

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

While we do allow specifying several versions of cmake/ninja, only one version wins—the one closest to the root module (see break in the "then" clause). Also, there can only be one version per module, so this should be very close to the old logic with the exception that the user can actually configure the versions.


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