Skip to content

Commit

Permalink
Allow configuring meson, make and pkgconfig versions via bzlmod exten…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
jsharpe committed Mar 31, 2024
1 parent 99d018f commit 4190892
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 45 deletions.
70 changes: 44 additions & 26 deletions foreign_cc/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,51 +1,69 @@
"""Entry point for extensions used by bzlmod."""

load("//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
load("//toolchains:prebuilt_toolchains.bzl", "prebuilt_toolchains")

_DEFAULT_CMAKE_VERSION = "3.23.2"
_DEFAULT_NINJA_VERSION = "1.11.1"
load("//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies", "DEFAULT_CMAKE_VERSION", "DEFAULT_MAKE_VERSION", "DEFAULT_MESON_VERSION", "DEFAULT_NINJA_VERSION", "DEFAULT_PKGCONFIG_VERSION")
load("//toolchains:built_toolchains.bzl", "make_toolchain", "meson_toolchain", "pkgconfig_toolchain")
load("//toolchains:prebuilt_toolchains.bzl", "cmake_toolchains", "ninja_toolchains")

cmake_toolchain_version = tag_class(attrs = {
"version": attr.string(doc = "The cmake version", default = _DEFAULT_CMAKE_VERSION),
"version": attr.string(doc = "The cmake version", default = DEFAULT_CMAKE_VERSION),
})

ninja_toolchain_version = tag_class(attrs = {
"version": attr.string(doc = "The ninja version", default = _DEFAULT_NINJA_VERSION),
make_toolchain_version = tag_class(attrs = {
"version": attr.string(doc = "The GNU Make version", default = DEFAULT_MAKE_VERSION),
})

def _init(module_ctx):
rules_foreign_cc_dependencies(
register_toolchains = False,
register_built_tools = True,
register_default_tools = False,
register_preinstalled_tools = False,
register_built_pkgconfig_toolchain = True,
)
meson_toolchain_version = tag_class(attrs = {
"version": attr.string(doc = "The meson version", default = DEFAULT_MESON_VERSION),
})

ninja_toolchain_version = tag_class(attrs = {
"version": attr.string(doc = "The ninja version", default = DEFAULT_NINJA_VERSION),
})

versions = {
"cmake": _DEFAULT_CMAKE_VERSION,
"ninja": _DEFAULT_NINJA_VERSION,
}
pkgconfig_toolchain_version = tag_class(attrs = {
"version": attr.string(doc = "The pkgconfig version", default = DEFAULT_PKGCONFIG_VERSION),
})

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
cmake_toolchains(toolchain.version, register_toolchains = False)

for toolchain in mod.tags.make:
make_toolchain(toolchain.version, register_toolchains = False)

for toolchain in mod.tags.meson:
meson_toolchain(toolchain.version, register_toolchains = False)

for toolchain in mod.tags.ninja:
versions["ninja"] = toolchain.version
ninja_toolchains(toolchain.version, register_toolchains = False)

for toolchain in mod.tags.pkgconfig:
pkgconfig_toolchain(toolchain.version, register_toolchains = False)

prebuilt_toolchains(
cmake_version = versions["cmake"],
ninja_version = versions["ninja"],
cmake_toolchains(DEFAULT_CMAKE_VERSION, register_toolchains = False)
make_toolchain(DEFAULT_MAKE_VERSION, register_toolchains = False)
meson_toolchain(DEFAULT_MESON_VERSION, register_toolchains = False)
ninja_toolchains(DEFAULT_NINJA_VERSION, register_toolchains = False)
pkgconfig_toolchain(DEFAULT_PKGCONFIG_VERSION, register_toolchains = False)

rules_foreign_cc_dependencies(
register_toolchains = False,
register_built_tools = True,
register_default_tools = False,
register_preinstalled_tools = False,
register_built_pkgconfig_toolchain = False,
)


tools = module_extension(
implementation = _init,
tag_classes = {
"cmake": cmake_toolchain_version,
"make": make_toolchain_version,
"meson": meson_toolchain_version,
"ninja": ninja_toolchain_version,
"pkgconfig": pkgconfig_toolchain_version,
},
)
16 changes: 11 additions & 5 deletions foreign_cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//foreign_cc/private/framework:toolchain.bzl", "register_framework_toolchains")
load("//toolchains:toolchains.bzl", "built_toolchains", "prebuilt_toolchains", "preinstalled_toolchains")

DEFAULT_CMAKE_VERSION = "3.23.2"
DEFAULT_MAKE_VERSION = "4.4.1"
DEFAULT_MESON_VERSION = "1.1.1"
DEFAULT_NINJA_VERSION = "1.11.1"
DEFAULT_PKGCONFIG_VERSION = "0.29.2"

# buildifier: disable=unnamed-macro
def rules_foreign_cc_dependencies(
native_tools_toolchains = [],
register_default_tools = True,
cmake_version = "3.23.2",
make_version = "4.4.1",
ninja_version = "1.11.1",
meson_version = "1.1.1",
pkgconfig_version = "0.29.2",
cmake_version = DEFAULT_CMAKE_VERSION,
make_version = DEFAULT_MAKE_VERSION,
ninja_version = DEFAULT_NINJA_VERSION,
meson_version = DEFAULT_MESON_VERSION,
pkgconfig_version = DEFAULT_PKGCONFIG_VERSION,
register_preinstalled_tools = True,
register_built_tools = True,
register_toolchains = True,
Expand Down
8 changes: 4 additions & 4 deletions toolchains/built_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def built_toolchains(cmake_version, make_version, ninja_version, meson_version,
register_built_pkgconfig_toolchain: If true, the built pkgconfig toolchain will be registered.
"""
_cmake_toolchain(cmake_version, register_toolchains)
_make_toolchain(make_version, register_toolchains)
make_toolchain(make_version, register_toolchains)
_ninja_toolchain(ninja_version, register_toolchains)
_meson_toolchain(meson_version, register_toolchains)
meson_toolchain(meson_version, register_toolchains)

if register_built_pkgconfig_toolchain:
_pkgconfig_toolchain(pkgconfig_version, register_toolchains)
Expand Down Expand Up @@ -80,7 +80,7 @@ def _cmake_toolchain(version, register_toolchains):

fail("Unsupported cmake version: " + str(version))

def _make_toolchain(version, register_toolchains):
def make_toolchain(version, register_toolchains):
if register_toolchains:
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_make_toolchain",
Expand Down Expand Up @@ -177,7 +177,7 @@ def _ninja_toolchain(version, register_toolchains):

fail("Unsupported ninja version: " + str(version))

def _meson_toolchain(version, register_toolchains):
def meson_toolchain(version, register_toolchains):
if register_toolchains:
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_meson_toolchain",
Expand Down
8 changes: 4 additions & 4 deletions toolchains/prebuilt_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ def prebuilt_toolchains(cmake_version, ninja_version, register_toolchains):
ninja_version (string): The target ninja-build version
register_toolchains (boolean): Whether to call native.register_toolchains or not
"""
_cmake_toolchains(cmake_version, register_toolchains)
_ninja_toolchains(ninja_version, register_toolchains)
cmake_toolchains(cmake_version, register_toolchains)
ninja_toolchains(ninja_version, register_toolchains)

def _cmake_toolchains(version, register_toolchains):
def cmake_toolchains(version, register_toolchains):
if "3.28.1" == version:
maybe(
http_archive,
Expand Down Expand Up @@ -8271,7 +8271,7 @@ def _cmake_toolchains(version, register_toolchains):

fail("Unsupported version: " + str(version))

def _ninja_toolchains(version, register_toolchains):
def ninja_toolchains(version, register_toolchains):
if "1.11.1" == version:
maybe(
http_archive,
Expand Down
12 changes: 6 additions & 6 deletions toolchains/prebuilt_toolchains.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ def prebuilt_toolchains(cmake_version, ninja_version, register_toolchains):
ninja_version (string): The target ninja-build version
register_toolchains (boolean): Whether to call native.register_toolchains or not
\"\"\"
_cmake_toolchains(cmake_version, register_toolchains)
_ninja_toolchains(ninja_version, register_toolchains)
cmake_toolchains(cmake_version, register_toolchains)
ninja_toolchains(ninja_version, register_toolchains)
def _cmake_toolchains(version, register_toolchains):
def cmake_toolchains(version, register_toolchains):
{cmake_definitions}
def _ninja_toolchains(version, register_toolchains):
def ninja_toolchains(version, register_toolchains):
{ninja_definitions}
"""

Expand All @@ -296,7 +296,7 @@ def get_cmake_definitions() -> str:
"""Define a set of repositories and calls for registering `cmake` toolchains
Returns:
str: The Implementation of `_cmake_toolchains`
str: The Implementation of `cmake_toolchains`
"""

archives = []
Expand Down Expand Up @@ -415,7 +415,7 @@ def get_ninja_definitions() -> str:
"""Define a set of repositories and calls for registering `ninja` toolchains
Returns:
str: The Implementation of `_ninja_toolchains`
str: The Implementation of `ninja_toolchains`
"""

archives = []
Expand Down

0 comments on commit 4190892

Please sign in to comment.