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 Nov 18, 2024
1 parent a8421af commit 55ea21b
Show file tree
Hide file tree
Showing 15 changed files with 506 additions and 1,313 deletions.
23 changes: 8 additions & 15 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module(
)

bazel_dep(name = "bazel_features", version = "1.15.0")
bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "rules_python", version = "0.23.1")

# Dev dependencies
Expand All @@ -28,23 +28,16 @@ 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",
"gettext_runtime",
"glib_dev",
"glib_src",
"gnumake_src",
"meson_src",
"ninja_1.12.1_toolchains",
"ninja_build_src",
"pkgconfig_src",
"prebuilt_cmake_toolchains",
"prebuilt_ninja_toolchains",
"rules_foreign_cc_framework_toolchains",
"toolchain_hub",
)

register_toolchains(
"@toolchain_hub//:all",
"@prebuilt_cmake_toolchains//:all",
"@prebuilt_ninja_toolchains//:all",
"@rules_foreign_cc_framework_toolchains//:all",
"@cmake_3.23.2_toolchains//:all",
"@ninja_1.12.1_toolchains//:all",
"@python_3_9//:all",
"@rules_foreign_cc//toolchains:all",
)
15 changes: 7 additions & 8 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ local_path_override(
tools = use_extension("@rules_foreign_cc//foreign_cc:extensions.bzl", "tools")
tools.cmake(version = "3.23.1")
tools.ninja(version = "1.11.1")
use_repo(
tools,
"meson_src",
)
tools.meson(version = "1.1.1")

use_repo(tools, "meson_1.1.1_src")

bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "rules_swift", version = "1.6.0", repo_name = "build_bazel_rules_swift")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "rules_swift", version = "1.17.0", repo_name = "build_bazel_rules_swift")
bazel_dep(name = "rules_apple", version = "3.4.0", repo_name = "build_bazel_rules_apple")
bazel_dep(name = "apple_support", version = "1.12.0", repo_name = "build_bazel_apple_support")
bazel_dep(name = "rules_python", version = "0.23.0")
bazel_dep(name = "apple_support", version = "1.14.0", repo_name = "build_bazel_apple_support")
bazel_dep(name = "rules_python", version = "0.23.1")
bazel_dep(name = "rules_cc", version = "0.0.9")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Expand Down
8 changes: 4 additions & 4 deletions examples/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ load("//deps:repositories.bzl", "repositories")

repositories()

load("//deps:deps_android.bzl", "deps_android")

deps_android()

# load("//deps:deps_android.bzl", "deps_android")
#
# deps_android()
#
load("//deps:deps_jvm_external.bzl", "deps_jvm_external")

deps_jvm_external()
Expand Down
1 change: 1 addition & 0 deletions examples/third_party/zlib/zlib_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def zlib_repositories():
],
urls = [
"https://zlib.net/zlib-1.2.13.tar.gz",
"https://zlib.net/archive/zlib-1.2.13.tar.gz",
"https://storage.googleapis.com/mirror.tensorflow.org/zlib.net/zlib-1.2.13.tar.gz",
],
)
126 changes: 99 additions & 27 deletions foreign_cc/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,51 +1,123 @@
"""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.12.1"
load("//foreign_cc:repositories.bzl", "DEFAULT_CMAKE_VERSION", "DEFAULT_MAKE_VERSION", "DEFAULT_MESON_VERSION", "DEFAULT_NINJA_VERSION", "DEFAULT_PKGCONFIG_VERSION")
load("//foreign_cc/private/bzlmod:toolchain_hub.bzl", "hub_repo")
load("//foreign_cc/private/framework:toolchain.bzl", "register_framework_toolchains")
load("//toolchains:built_toolchains.bzl", "make_toolchain", "meson_toolchain", "pkgconfig_toolchain", cmake_toolchains_src = "cmake_toolchain", ninja_toolchains_src = "ninja_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),
})

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

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),
"version": attr.string(doc = "The ninja version", default = DEFAULT_NINJA_VERSION),
})

pkgconfig_toolchain_version = tag_class(attrs = {
"version": attr.string(doc = "The pkgconfig version", default = DEFAULT_PKGCONFIG_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,
)

versions = {
"cmake": _DEFAULT_CMAKE_VERSION,
"ninja": _DEFAULT_NINJA_VERSION,
}
cmake_registered = False
make_registered = False
meson_registered = False
ninja_registered = False
pkgconfig_registered = False
toolchain_names = []
toolchain_target = []
toolchain_types = []

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)
cmake_toolchains_src(toolchain.version)

toolchain_names.append("cmake_{}_from_src".format(toolchain.version))
toolchain_types.append("@rules_foreign_cc//toolchains:cmake_toolchain")
toolchain_target.append("@cmake_{}_src//:built_cmake".format(toolchain.version))
cmake_registered = True

for toolchain in mod.tags.make:
make_toolchain(toolchain.version)
toolchain_names.append("make_{}_from_src".format(toolchain.version))
toolchain_types.append("@rules_foreign_cc//toolchains:make_toolchain")
toolchain_target.append("@gnumake_{}_src//:built_make".format(toolchain.version))
make_registered = True

for toolchain in mod.tags.meson:
meson_toolchain(toolchain.version)
toolchain_names.append("meson_{}_from_src".format(toolchain.version))
toolchain_types.append("@rules_foreign_cc//toolchains:meson_toolchain")
toolchain_target.append("@meson_{}_src//:built_meson".format(toolchain.version))
meson_registered = True

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

toolchain_names.append("ninja_{}_from_src".format(toolchain.version))
toolchain_types.append("@rules_foreign_cc//toolchains:ninja_toolchain")
toolchain_target.append("@ninja_{}_src//:built_ninja".format(toolchain.version))
ninja_registered = True

for toolchain in mod.tags.pkgconfig:
pkgconfig_toolchain(toolchain.version)
toolchain_names.append("pkgconfig_{}_from_src".format(toolchain.version))
toolchain_types.append("@rules_foreign_cc//toolchains:pkgconfig_toolchain")
toolchain_target.append("@pkgconfig_{}_src//:built_pkgconfig".format(toolchain.version))
pkgconfig_registered = True

if not cmake_registered:
cmake_toolchains(DEFAULT_CMAKE_VERSION)
cmake_toolchains_src(DEFAULT_CMAKE_VERSION)

toolchain_names.append("cmake_{}_from_src".format(DEFAULT_CMAKE_VERSION))
toolchain_types.append("@rules_foreign_cc//toolchains:cmake_toolchain")
toolchain_target.append("@cmake_{}_src//:built_cmake".format(DEFAULT_CMAKE_VERSION))

if not make_registered:
make_toolchain(DEFAULT_MAKE_VERSION)
toolchain_names.append("make_{}_from_src".format(DEFAULT_MAKE_VERSION))
toolchain_types.append("@rules_foreign_cc//toolchains:make_toolchain")
toolchain_target.append("@gnumake_{}_src//:built_make".format(DEFAULT_MAKE_VERSION))
if not meson_registered:
meson_toolchain(DEFAULT_MESON_VERSION)
toolchain_names.append("meson_{}_from_src".format(DEFAULT_MESON_VERSION))
toolchain_types.append("@rules_foreign_cc//toolchains:meson_toolchain")
toolchain_target.append("@meson_{}_src//:built_meson".format(DEFAULT_MESON_VERSION))
if not ninja_registered:
ninja_toolchains(DEFAULT_NINJA_VERSION)
ninja_toolchains_src(DEFAULT_NINJA_VERSION)
toolchain_names.append("ninja_{}_from_src".format(DEFAULT_NINJA_VERSION))
toolchain_types.append("@rules_foreign_cc//toolchains:ninja_toolchain")
toolchain_target.append("@ninja_{}_src//:built_ninja".format(DEFAULT_NINJA_VERSION))
if not pkgconfig_registered:
pkgconfig_toolchain(DEFAULT_PKGCONFIG_VERSION)
toolchain_names.append("pkgconfig_{}_from_src".format(DEFAULT_PKGCONFIG_VERSION))
toolchain_types.append("@rules_foreign_cc//toolchains:pkgconfig_toolchain")
toolchain_target.append("@pkgconfig_{}_src//:built_pkgconfig".format(DEFAULT_PKGCONFIG_VERSION))

register_framework_toolchains(register_toolchains = False)

prebuilt_toolchains(
cmake_version = versions["cmake"],
ninja_version = versions["ninja"],
register_toolchains = False,
)
hub_repo(name = "toolchain_hub", toolchain_names = toolchain_names, toolchain_target = toolchain_target, toolchain_types = toolchain_types)

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,
},
)
4 changes: 2 additions & 2 deletions foreign_cc/meson.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ def meson_with_requirements(name, requirements, **kwargs):

meson_tool(
name = "meson_tool_for_{}".format(name),
main = "@meson_src//:meson.py",
data = ["@meson_src//:runtime"],
main = "@meson_1.1.1_src//:meson.py",
data = ["@meson_1.1.1_src//:runtime"],
requirements = requirements,
tags = tags + ["manual"],
)
Expand Down
Empty file.
33 changes: 33 additions & 0 deletions foreign_cc/private/bzlmod/toolchain_hub.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def _hub_repo_impl(rctx):
if len(rctx.attr.toolchain_names) != len(rctx.attr.toolchain_target):
fail("toolchain_names and toolchain_target must have the same length.")
if len(rctx.attr.toolchain_names) != len(rctx.attr.toolchain_types):
fail("toolchain_names and toolchain_types must have the same length.")

toolchains = ["toolchain(name = '{}', toolchain = '{}', toolchain_type = '{}')".format(name, target, type) for (name, target, type) in zip(rctx.attr.toolchain_names, rctx.attr.toolchain_target, rctx.attr.toolchain_types)]

rctx.file("BUILD", "\n".join(toolchains), executable = False)

hub_repo = repository_rule(
doc = """\
This private rule creates a repo with a BUILD file that containers all the toolchain
rules that have been requested by the user so that the MODULE.bazel file can simply
register `:all` and get all the toolchains registered in a single call.
""",
implementation = _hub_repo_impl,
attrs = {
"toolchain_names": attr.string_list(
doc = "The list of toolchains to include in the hub repo.",
default = [],
),
"toolchain_target": attr.string_list(
doc = "The list of toolchain targets to include in the hub repo.",
default = [],
),
"toolchain_types": attr.string_list(
doc = "The list of toolchain targets to include in the hub repo.",
default = [],
),
# "_rules_foreign_cc_workspace: attr.label(default = Label("//:does_not_matter_what_this_name_is")),
},
)
28 changes: 15 additions & 13 deletions foreign_cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ 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.29.1"
DEFAULT_MAKE_VERSION = "4.4.1"
DEFAULT_MESON_VERSION = "1.5.1"
DEFAULT_NINJA_VERSION = "1.12.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.12.1",
meson_version = "1.5.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,
register_built_pkgconfig_toolchain = True):
"""Call this function from the WORKSPACE file to initialize rules_foreign_cc \
dependencies and let neccesary code generation happen \
Expand Down Expand Up @@ -50,8 +55,6 @@ def rules_foreign_cc_dependencies(
register_built_tools: If true, toolchains that build the tools from source are registered
register_toolchains: If true, registers the toolchains via native.register_toolchains. Used by bzlmod
register_built_pkgconfig_toolchain: If true, the built pkgconfig toolchain will be registered. On Windows it may be preferrable to set this to False, as
this requires the --enable_runfiles bazel option. Also note that building pkgconfig from source under bazel results in paths that are more
than 256 characters long, which will not work on Windows unless the following options are added to the .bazelrc and symlinks are enabled in Windows.
Expand All @@ -61,13 +64,13 @@ def rules_foreign_cc_dependencies(
startup --output_user_root=C:/b -> This is required to keep paths as short as possible
"""

register_framework_toolchains(register_toolchains = register_toolchains)
register_framework_toolchains(register_toolchains = True)

if register_toolchains:
native.register_toolchains(*native_tools_toolchains)
native.register_toolchains(*native_tools_toolchains)

if register_default_tools:
prebuilt_toolchains(cmake_version, ninja_version, register_toolchains)
prebuilt_toolchains(cmake_version, ninja_version)
native.register_toolchains("@prebuilt_cmake_toolchains//:all", "@prebuilt_ninja_toolchains//:all")

if register_built_tools:
built_toolchains(
Expand All @@ -76,7 +79,6 @@ def rules_foreign_cc_dependencies(
ninja_version = ninja_version,
meson_version = meson_version,
pkgconfig_version = pkgconfig_version,
register_toolchains = register_toolchains,
register_built_pkgconfig_toolchain = register_built_pkgconfig_toolchain,
)

Expand Down
30 changes: 0 additions & 30 deletions toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,6 @@ current_autoconf_toolchain(
name = "current_autoconf_toolchain",
)

toolchain(
name = "built_cmake_toolchain",
toolchain = "//toolchains/private:built_cmake",
toolchain_type = ":cmake_toolchain",
)

toolchain(
name = "built_ninja_toolchain",
toolchain = "//toolchains/private:built_ninja",
toolchain_type = ":ninja_toolchain",
)

toolchain(
name = "built_make_toolchain",
toolchain = "//toolchains/private:built_make",
toolchain_type = ":make_toolchain",
)

toolchain(
name = "built_meson_toolchain",
toolchain = "//toolchains/private:built_meson",
toolchain_type = ":meson_toolchain",
)

toolchain(
name = "built_pkgconfig_toolchain",
toolchain = "//toolchains/private:built_pkgconfig",
toolchain_type = ":pkgconfig_toolchain",
)

# Preinstalled cmake will always be the default, if toolchain with more exact constraints
# is not defined before; registered from workspace_definitions.bzl#rules_foreign_cc_dependencies
toolchain(
Expand Down
Loading

0 comments on commit 55ea21b

Please sign in to comment.