Skip to content
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
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ use_repo(provisioning_profile_repository, "local_provisioning_profiles")

apple_cc_configure = use_extension("@build_bazel_apple_support//crosstool:setup.bzl", "apple_cc_configure_extension")
use_repo(apple_cc_configure, "local_config_apple_cc")

register_toolchains("//apple:dtrace_toolchain")
20 changes: 20 additions & 0 deletions apple/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//apple/internal/aspects:resource_aspect_hint.bzl", "apple_resource_hint", "apple_resource_hint_action")
load(":cc_toolchain_forwarder.bzl", "cc_toolchain_forwarder")
load(":dtrace.bzl", "dtrace_toolchain")

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

Expand Down Expand Up @@ -319,3 +320,22 @@ apple_resource_hint(
name = "suppress_resources",
action = apple_resource_hint_action.suppress,
)

toolchain_type(
name = "dtrace_toolchain_type",
)

dtrace_toolchain(
name = "dtrace_toolchain_impl",
# If no label specified the system dtrace binary is used.
dtrace = None,
)

toolchain(
name = "dtrace_toolchain",
exec_compatible_with = [
"@platforms//os:macos",
],
toolchain = ":dtrace_toolchain_impl",
toolchain_type = ":dtrace_toolchain_type",
)
37 changes: 25 additions & 12 deletions apple/dtrace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ load(
"bundle_paths",
)

def _dtrace_toolchain_impl(ctx):
"""Implementation for dtrace_toolchain."""
return [
platform_common.ToolchainInfo(
dtrace_executable = ctx.executable.dtrace or "/usr/sbin/dtrace",
),
]

dtrace_toolchain = rule(
implementation = _dtrace_toolchain_impl,
attrs = {
"dtrace": attr.label(
doc = "dtrace binary to use. If not set /usr/sbin/dtrace is used.",
mandatory = False,
executable = True,
cfg = "exec",
),
},
doc = "Defines a toolchain for dtrace_compile rules.",
)

def _dtrace_compile_impl(ctx):
"""Implementation for dtrace_compile."""
apple_fragment = ctx.fragments.apple
Expand All @@ -41,9 +62,9 @@ def _dtrace_compile_impl(ctx):
output_hdrs = []
include_dir = None

dtrace = "/usr/sbin/dtrace"
if ctx.executable.dtrace:
dtrace = ctx.executable.dtrace
# Get dtrace executable from toolchain
dtrace_toolchain = ctx.toolchains["//apple:dtrace_toolchain_type"]
dtrace = dtrace_toolchain.dtrace_executable

for src in ctx.files.srcs:
owner_relative_path = bundle_paths.owner_relative_path(src)
Expand Down Expand Up @@ -83,21 +104,13 @@ def _dtrace_compile_impl(ctx):
dtrace_compile = rule(
implementation = _dtrace_compile_impl,
attrs = dicts.add(apple_support.action_required_attrs(), {
"dtrace": attr.label(
doc = "dtrace binary to use.",
mandatory = False,
executable = True,
cfg = "exec",
),
"srcs": attr.label_list(
allow_files = [".d"],
allow_empty = False,
doc = "dtrace(.d) source files to be compiled.",
),
}),
exec_compatible_with = [
"@platforms//os:macos",
],
toolchains = ["//apple:dtrace_toolchain_type"],
fragments = ["apple"],
doc = """
Compiles
Expand Down
2 changes: 2 additions & 0 deletions apple/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ def apple_rules_dependencies(ignore_version_differences = False, include_bzlmod_
ignore_version_differences = ignore_version_differences,
)

native.register_toolchains("//apple:dtrace_toolchain")

_maybe(
http_archive,
name = "xctestrunner",
Expand Down