Skip to content

Commit

Permalink
Initial bzlmod support
Browse files Browse the repository at this point in the history
  • Loading branch information
jsharpe committed Dec 17, 2021
1 parent f76d928 commit ca7c54f
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 285 deletions.
6 changes: 6 additions & 0 deletions foreign_cc/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

def _init(module_ctx):
rules_foreign_cc_dependencies(register_toolchains = False, register_preinstalled_tools = False)

ext = module_extension(implementation = _init)
5 changes: 3 additions & 2 deletions foreign_cc/private/framework/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ framework_toolchain_repository = repository_rule(
)

# buildifier: disable=unnamed-macro
def register_framework_toolchains():
def register_framework_toolchains(register_toolchains = True):
"""Define and register the foreign_cc framework toolchains"""
toolchains = []

Expand All @@ -103,4 +103,5 @@ def register_framework_toolchains():

toolchains.append("@{}//:toolchain".format(toolchain_name))

native.register_toolchains(*toolchains)
if (register_toolchains):
native.register_toolchains(*toolchains)
13 changes: 9 additions & 4 deletions foreign_cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def rules_foreign_cc_dependencies(
make_version = "4.3",
ninja_version = "1.10.2",
register_preinstalled_tools = True,
register_built_tools = True):
register_built_tools = True,
register_toolchains = True):
"""Call this function from the WORKSPACE file to initialize rules_foreign_cc \
dependencies and let neccesary code generation happen \
(Code generation is needed to support different variants of the C++ Starlark API.).
Expand Down Expand Up @@ -41,20 +42,24 @@ def rules_foreign_cc_dependencies(
installed on the exec host
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_framework_toolchains()
register_framework_toolchains(register_toolchains = register_toolchains)

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

if register_default_tools:
prebuilt_toolchains(cmake_version, ninja_version)
prebuilt_toolchains(cmake_version, ninja_version, register_toolchains)

if register_built_tools:
built_toolchains(
cmake_version = cmake_version,
make_version = make_version,
ninja_version = ninja_version,
register_toolchains = register_toolchains,
)

if register_preinstalled_tools:
Expand Down
39 changes: 21 additions & 18 deletions toolchains/built_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ _CMAKE_SRCS = {
}

# buildifier: disable=unnamed-macro
def built_toolchains(cmake_version, make_version, ninja_version):
def built_toolchains(cmake_version, make_version, ninja_version, register_toolchains):
"""Register toolchains for built tools that will be built from source"""
_cmake_toolchain(cmake_version)
_make_toolchain(make_version)
_ninja_toolchain(ninja_version)

def _cmake_toolchain(version):
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_cmake_toolchain",
)
_cmake_toolchain(cmake_version, register_toolchains)
_make_toolchain(make_version, register_toolchains)
_ninja_toolchain(ninja_version, register_toolchains)

def _cmake_toolchain(version, register_toolchains):
if (register_toolchains):
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_cmake_toolchain",
)

if _CMAKE_SRCS.get(version):
cmake_meta = _CMAKE_SRCS[version]
Expand Down Expand Up @@ -403,16 +404,17 @@ def _cmake_toolchain(version):

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

def _make_toolchain(version):
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_make_toolchain",
)
def _make_toolchain(version, register_toolchains):
if (register_toolchains):
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_make_toolchain",
)
if version == "4.3":
maybe(
http_archive,
name = "gnumake_src",
build_file_content = _ALL_CONTENT,
patches = ["@rules_foreign_cc//toolchains:make-reproducible-bootstrap.patch"],
patches = [str(Label("//toolchains:make-reproducible-bootstrap.patch"))],
sha256 = "e05fdde47c5f7ca45cb697e973894ff4f5d79e13b750ed57d7b66d8defc78e19",
strip_prefix = "make-4.3",
urls = [
Expand All @@ -424,10 +426,11 @@ def _make_toolchain(version):

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

def _ninja_toolchain(version):
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_ninja_toolchain",
)
def _ninja_toolchain(version, register_toolchains):
if (register_toolchains):
native.register_toolchains(
"@rules_foreign_cc//toolchains:built_ninja_toolchain",
)
if version == "1.10.2":
maybe(
http_archive,
Expand Down
Loading

0 comments on commit ca7c54f

Please sign in to comment.