Skip to content
Closed
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
16 changes: 16 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ archive_override(
urls = ["https://github.com/bazel-contrib/toolchains_llvm/archive/ffe407ca2423f4d171b7a0f3897c23bbb7e40310.tar.gz"],
)

#################################
## Tools that ship with the OS ##
#################################

find_macos_codesign = use_extension("//bazel/toolchains/codesign:configure.bzl", "find_macos_codesign", dev_dependency = True)
use_repo(find_macos_codesign, "macos_codesign")

find_macos_pkgbuild = use_extension("//bazel/toolchains/pkgbuild:configure.bzl", "find_macos_pkgbuild", dev_dependency = True)
use_repo(find_macos_pkgbuild, "macos_pkgbuild")

register_toolchains(
"@macos_codesign//:all",
"@macos_pkgbuild//:all",
dev_dependency = True,
)

#########################
## Prebuilt binaries ##
#########################
Expand Down
26 changes: 26 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions bazel/toolchains/codesign/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""toolchain to wrap the codesign binary.

Type: @codesign//:codesign_toolchain_type

Toolchains:
- @codesign//:codesign_toolchain: provides the tool
- @codesign//:codesign_missing_toolchain: provides a fallback toolchain for
exec platforms where codesign might not be available.
"""
12 changes: 12 additions & 0 deletions bazel/toolchains/codesign/configure.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Repository rule to autoconfigure a toolchain using the system codesign."""

load("//bazel/toolchains/common:defs.bzl", "make_repo_builder")

# This must match the name used by register_toolchains in consuming MODULE.bazel files.
NAME = "macos_codesign"

build_repo_for_toolchain = make_repo_builder(name = NAME)

find_macos_codesign = module_extension(
implementation = lambda ctx: build_repo_for_toolchain(name = NAME),
)
12 changes: 12 additions & 0 deletions bazel/toolchains/common/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# //bazel/toolchains.

package(default_visibility = ["//visibility:private"])

filegroup(
name = "common",
srcs = [
"toolchain_BUILD.tpl",
"toolchain_defs.bzl.tpl",
],
visibility = ["//bazel/toolchains:__subpackages__"],
)
56 changes: 56 additions & 0 deletions bazel/toolchains/common/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Utilitites for creating toolchains to wrap system provided tools."""

def write_toolchain_repo(rctx, tool_name, tool_path, tool_version = "<unknown>"):
if not tool_path:
tool_path = ""
rctx.template(
"BUILD",

This comment was marked as outdated.

Label("@@//bazel/toolchains/common:toolchain_BUILD.tpl"),
substitutions = {
"{GENERATOR}": "@@//bazel/toolchains/{tool_name}/{tool_name}_configure.bzl%find_system_{tool_name}".format(
tool_name = tool_name,
),
"{TOOL_NAME}": tool_name,
"{TOOL_PATH}": str(tool_path),
"{TOOL_VERSION}": tool_version,
},
executable = False,
)
rctx.template(
"defs.bzl",
Label("@@//bazel/toolchains/common:toolchain_defs.bzl.tpl"),
substitutions = {
"{GENERATOR}": "@@//bazel/toolchains/{tool_name}/{tool_name}_configure.bzl%find_system_{tool_name}".format(
tool_name = tool_name,
),
"{TOOL_NAME}": tool_name,
},
executable = False,
)

def _default_repo_builder_impl(rctx):
tool_name = rctx.original_name
tool_path = rctx.which(tool_name)
if rctx.attr.verbose:
if tool_path:
print("Found %s at '%s'" % (tool_name, tool_path)) # buildifier: disable=print
else:
print("No system %s found." % tool_name) # buildifier: disable=print
Comment thread
aiuto marked this conversation as resolved.
write_toolchain_repo(
rctx = rctx,
tool_name = tool_name,
tool_path = tool_path,
)

def make_repo_builder(name, impl = _default_repo_builder_impl):
return repository_rule(
implementation = impl,
doc = """Create a repository that defines an {name} toolchain based on the system {name}.""".format(name = name),
local = True,
environ = ["PATH"],
attrs = {
"verbose": attr.bool(
doc = "If true, print status messages.",
),
},
)
49 changes: 49 additions & 0 deletions bazel/toolchains/common/toolchain_BUILD.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This content is generated by {GENERATOR}
load(":defs.bzl", "is_{TOOL_NAME}_available", "{TOOL_NAME}_toolchain")

toolchain_type(
name = "{TOOL_NAME}_toolchain_type",
visibility = ["//visibility:public"],
)

{TOOL_NAME}_toolchain(
name = "{TOOL_NAME}_auto",
path = "{TOOL_PATH}",
version = "{TOOL_VERSION}",
)

toolchain(
name = "{TOOL_NAME}_toolchain",
toolchain = ":{TOOL_NAME}_auto",
toolchain_type = ":{TOOL_NAME}_toolchain_type",
Comment thread
aiuto marked this conversation as resolved.
)

# {TOOL_NAME}_missing_toolchain provides a fallback toolchain so that toolchain
# resolution can succeed even on platforms that do not have that tool.
# If this toolchain is selected, the constraint ":have_{TOOL_NAME}" will not be satistifed,
# so that can be used with with exec_compatible_with clauses.
Comment on lines +23 to +24

This comment was marked as outdated.

{TOOL_NAME}_toolchain(
name = "no_{TOOL_NAME}",
)

toolchain(
name = "zzz_{TOOL_NAME}_missing_toolchain", # keep name lexicographically last
toolchain = ":no_{TOOL_NAME}",
toolchain_type = ":{TOOL_NAME}_toolchain_type",
)

# Expose the availability of an actual otool as a feature flag, so we can
# create a config_setting from it.
is_{TOOL_NAME}_available(
name = "is_{TOOL_NAME}_available",
)

# Expose the availability of the toolchain as a config_setting, so we can
# select() on it.
config_setting(
name = "have_{TOOL_NAME}",
flag_values = {
":is_{TOOL_NAME}_available": "1",
},
visibility = ["//visibility:public"],
)
51 changes: 51 additions & 0 deletions bazel/toolchains/common/toolchain_defs.bzl.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""toolchain to provide the {TOOL_NAME} binary."""

load("@@//bazel/toolchains:toolchain_info.bzl", "ToolInfo")

def _{TOOL_NAME}_toolchain_impl(ctx):
if ctx.attr.label and ctx.attr.path:
fail("{TOOL_NAME}_toolchain must not specify both label and path.")
valid = bool(ctx.attr.label) or bool(ctx.attr.path)
toolchain_info = platform_common.ToolchainInfo(
{TOOL_NAME} = ToolInfo(
name = str(ctx.label),
valid = valid,
label = ctx.attr.label,
path = ctx.attr.path,
version = ctx.attr.version,
),
)
return [toolchain_info]

{TOOL_NAME}_toolchain = rule(
implementation = _{TOOL_NAME}_toolchain_impl,
attrs = {
"label": attr.label(
doc = "A valid label of a target to build or a prebuilt binary. Mutually exclusive with path.",
cfg = "exec",
executable = True,
allow_files = True,
),
"path": attr.string(
doc = "The path to the executable. Mutually exclusive with label.",
),
"version": attr.string(
doc = "The version string of the executable. This should be manually set.",
),
},
)

# Expose the presence of {TOOL_NAME} in the resolved toolchain as a flag.
# The signal is to look at the default toolchain of the {TOOL_NAME}_toolchan_type
# and see that it is valid (that is, it has a path).
def _is_{TOOL_NAME}_available_impl(ctx):
toolchain = ctx.toolchains["@{TOOL_NAME}//:{TOOL_NAME}_toolchain_type"].{TOOL_NAME}
return [config_common.FeatureFlagInfo(
value = ("1" if toolchain.valid else "0"),
)]

is_{TOOL_NAME}_available = rule(
implementation = _is_{TOOL_NAME}_available_impl,
attrs = {},
toolchains = ["@{TOOL_NAME}//:{TOOL_NAME}_toolchain_type"],
)
8 changes: 8 additions & 0 deletions bazel/toolchains/pkgbuild/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Toolchain to wrap the pkgbuild binary.

Type: @macos_pkgbuild//:tool_toolchain_type

Toolchains:
- @macos_pkgbuild//:macos_pkgbuild_toolchain: provides the tool
- @macos_pkgbuild//:macos_pkgbuild_missing_toolchain: provides a fallback toolchain for exec platforms where pkgbuild might not be available.
"""
12 changes: 12 additions & 0 deletions bazel/toolchains/pkgbuild/configure.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Repository rule to autoconfigure a toolchain using the system pkgbuild."""

load("//bazel/toolchains/common:defs.bzl", "make_repo_builder")

# This must match the name used by register_toolchains in MODULE.bazel.
NAME = "macos_pkgbuild"

build_repo_for_toolchain = make_repo_builder(name = NAME)

find_macos_pkgbuild = module_extension(
implementation = lambda ctx: build_repo_for_toolchain(name = NAME),
)
Loading