Skip to content
Draft
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
1 change: 1 addition & 0 deletions apple/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ bzl_library(
name = "apple_binary",
srcs = ["apple_binary.bzl"],
deps = [
"//apple/internal:apple_toolchains",
"//apple/internal:linking_support",
"//apple/internal:rule_attrs",
"//apple/internal:rule_factory",
Expand Down
8 changes: 8 additions & 0 deletions apple/apple_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ load(
"@build_bazel_apple_support//lib:apple_support.bzl",
"apple_support",
)
load(
"//apple/internal:apple_toolchains.bzl",
"AppleXPlatToolsToolchainInfo",
"apple_toolchain_utils",
)
load(
"//apple/internal:linking_support.bzl",
"linking_support",
Expand Down Expand Up @@ -67,6 +72,7 @@ visionOS binaries require a visionOS SDK provided by Xcode 15.1 beta or later.
Resolved Xcode is version {xcode_version}.
""".format(xcode_version = str(xcode_version_config.xcode_version())))

apple_xplat_toolchain_info = ctx.attr._xplat_toolchain[AppleXPlatToolsToolchainInfo]
binary_type = ctx.attr.binary_type
bundle_loader = ctx.attr.bundle_loader
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
Expand Down Expand Up @@ -97,6 +103,7 @@ Resolved Xcode is version {xcode_version}.
link_result = linking_support.register_binary_linking_action(
ctx,
cc_toolchains = cc_toolchain_forwarder,
build_settings = apple_xplat_toolchain_info.build_settings,
bundle_loader = bundle_loader,
exported_symbols_lists = ctx.files.exported_symbols_lists,
extra_linkopts = extra_linkopts,
Expand Down Expand Up @@ -152,6 +159,7 @@ implementation of `apple_binary` in Bazel core so that it can be removed.
implementation = _apple_binary_impl,
attrs = [
apple_support.platform_constraint_attrs(),
apple_toolchain_utils.shared_attrs(),
rule_attrs.binary_linking_attrs(
deps_cfg = transition_support.apple_platform_split_transition,
is_test_supporting_rule = False,
Expand Down
8 changes: 8 additions & 0 deletions apple/build_settings/build_settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ physical devices) or `xcrun simctl list devices` (for simulators).
""",
default = "",
),
"dsym_variant_flag": struct(
doc = """
Controls the output format for dSYM debug symbols. Valid values are:
- "flat": Outputs individual dSYM files as additional outputs from the target.
- "bundle": Outputs dSYM bundles as directories.
""",
default = "flat",
),
}

# List of all registered build settings without command line flags at
Expand Down
12 changes: 12 additions & 0 deletions apple/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ bzl_library(
"//apple:__subpackages__",
],
deps = [
":fragment_support",
":outputs",
":platform_support",
"@bazel_skylib//lib:paths",
"@rules_cc//cc/common",
Expand Down Expand Up @@ -261,6 +263,14 @@ bzl_library(
],
)

bzl_library(
name = "fragment_support",
srcs = ["fragment_support.bzl"],
visibility = [
"//apple:__subpackages__",
],
)

bzl_library(
name = "framework_import_support",
srcs = ["framework_import_support.bzl"],
Expand Down Expand Up @@ -345,6 +355,7 @@ bzl_library(
":compilation_support",
":entitlements_support",
":intermediates",
":outputs",
":providers",
":rule_support",
"@bazel_skylib//lib:collections",
Expand Down Expand Up @@ -432,6 +443,7 @@ bzl_library(
],
deps = [
":experimental",
":fragment_support",
":intermediates",
"@bazel_skylib//lib:paths",
],
Expand Down
4 changes: 0 additions & 4 deletions apple/internal/apple_framework_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,8 @@ def _apple_dynamic_framework_import_impl(ctx):
providers.append(cc_info)

# Create AppleDynamicFramework provider.
framework_groups = _grouped_framework_files(framework_imports)
framework_dirs_set = depset(framework_groups.keys())
providers.append(new_appledynamicframeworkinfo(
cc_info = cc_info,
framework_dirs = framework_dirs_set,
framework_files = depset(framework_imports),
))

if "apple._import_framework_via_swiftinterface" in features and framework.swift_interface_imports:
Expand Down
68 changes: 33 additions & 35 deletions apple/internal/compilation_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ load(
"@rules_cc//cc/common:cc_common.bzl",
"cc_common",
)
load(
"//apple/internal:fragment_support.bzl",
"fragment_support",
)
load(
"//apple/internal:outputs.bzl",
"outputs",
)
load(
"//apple/internal:platform_support.bzl",
"platform_support",
Expand Down Expand Up @@ -163,10 +171,12 @@ def _register_binary_strip_action(
"-bundle" in extra_link_args or link_bundle or "-kext" in extra_link_args):
strip_safe = True

# TODO(b/331163513): Use intermediates.file() instead of declare_shareable_artifact().
stripped_binary = ctx.actions.declare_shareable_artifact(
paths.join(ctx.label.package, name),
apple_platform_info.target_build_config.bin_dir,
stripped_binary = outputs.main_binary(
actions = ctx.actions,
apple_platform_info = apple_platform_info,
cpp_fragment = ctx.fragments.cpp,
label = ctx.label,
unstripped = False,
)
args = ctx.actions.args()
args.add("strip")
Expand Down Expand Up @@ -240,9 +250,6 @@ def _classify_libraries(libraries_to_link):
}
return always_link_libraries.keys(), as_needed_libraries.keys()

def _emit_builtin_objc_strip_action(ctx):
return ctx.fragments.cpp.objc_should_strip_binary

def _register_configuration_specific_link_actions(
*,
additional_outputs,
Expand All @@ -268,23 +275,19 @@ def _register_configuration_specific_link_actions(
ctx = common_variables.ctx
feature_configuration = _build_feature_configuration(common_variables)

# When compilation_mode=opt and objc_enable_binary_stripping are specified, the unstripped
# binary containing debug symbols is generated by the linker, which also needs the debug
# symbols for dead-code removal. The binary is also used to generate dSYM bundle if
# --apple_generate_dsym is specified. A symbol strip action is later registered to strip
# the symbol table from the unstripped binary.
if _emit_builtin_objc_strip_action(ctx):
# TODO(b/331163513): Use intermediates.file() instead of declare_shareable_artifact().
binary = ctx.actions.declare_shareable_artifact(
paths.join(ctx.label.package, name + "_unstripped"),
apple_platform_info.target_build_config.bin_dir,
)
# TODO: Remove hasattr check when we drop Bazel 8
if hasattr(ctx.fragments.objc, "builtin_objc_strip_action"):
unstripped = ctx.fragments.objc.builtin_objc_strip_action
else:
# TODO(b/331163513): Use intermediates.file() instead of declare_shareable_artifact().
binary = ctx.actions.declare_shareable_artifact(
paths.join(ctx.label.package, name),
apple_platform_info.target_build_config.bin_dir,
)
unstripped = False

binary = outputs.main_binary(
actions = ctx.actions,
apple_platform_info = apple_platform_info,
cpp_fragment = ctx.fragments.cpp,
label = ctx.label,
unstripped = unstripped,
)

# TODO: Delete feature check and else branch when we drop Bazel 8 support (see f4a3fa40)
if cc_common.is_enabled(
Expand Down Expand Up @@ -374,7 +377,12 @@ def _register_configuration_specific_link_actions_with_cpp_variables(
variables_extension = user_variable_extensions,
)

if _emit_builtin_objc_strip_action(ctx):
# TODO: Remove hasattr check when we drop Bazel 8
if hasattr(ctx.fragments.objc, "builtin_objc_strip_action") and \
fragment_support.is_objc_strip_action_enabled(
cpp_fragment = ctx.fragments.cpp,
) and \
ctx.fragments.objc.builtin_objc_strip_action:
return _register_binary_strip_action(
ctx = ctx,
apple_platform_info = apple_platform_info,
Expand Down Expand Up @@ -554,17 +562,7 @@ def _register_configuration_specific_link_actions_with_objc_variables(
main_output = binary,
)

if _emit_builtin_objc_strip_action(ctx):
return _register_binary_strip_action(
apple_platform_info = apple_platform_info,
binary = binary,
ctx = ctx,
extra_link_args = extra_link_args,
feature_configuration = feature_configuration,
name = name,
)
else:
return binary
return binary

compilation_support = struct(
# TODO(b/331163513): Move apple_common.compliation_support.build_common_variables here, too.
Expand Down
16 changes: 16 additions & 0 deletions apple/internal/features_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Support macros to assist in detecting build features."""

load("@bazel_skylib//lib:new_sets.bzl", "sets")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")

def _compute_enabled_features(*, requested_features, unsupported_features):
"""Returns a list of features for the given build.
Expand All @@ -33,6 +34,21 @@ def _compute_enabled_features(*, requested_features, unsupported_features):
)
return sets.to_list(enabled_features_set)

def _make_cc_configured_features_init(ctx):
"""Captures the rule ctx for a deferred `cc_common.configure_features(...)` call.

Args:
ctx: The rule context, expected to be captured directly in the rule context and NOT within a
partial or helper method.

Returns:
A lambda that has the captured instance of the rule context, which will always set that rule
context as the `ctx` argument of `cc_common.configure_features(...)` and will forward any
arguments it is given to `cc_common.configure_features(...)`.
"""
return lambda *args, **kwargs: cc_common.configure_features(ctx = ctx, *args, **kwargs)

features_support = struct(
compute_enabled_features = _compute_enabled_features,
make_cc_configured_features_init = _make_cc_configured_features_init,
)
31 changes: 31 additions & 0 deletions apple/internal/fragment_support.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2025 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Wrapper to centralize configuration of fragment-based logic."""

visibility([
"//apple/...",
"//test/...",
])

def _is_objc_strip_action_enabled(*, cpp_fragment):
"""Returns wheither the Objective-C strip action is active."""
return (
cpp_fragment.objc_enable_binary_stripping() and
cpp_fragment.compilation_mode() == "opt"
)

fragment_support = struct(
is_objc_strip_action_enabled = _is_objc_strip_action_enabled,
)
Loading
Loading