Skip to content

Commit

Permalink
Move some linkopts to features
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Aug 28, 2023
1 parent 96d7cff commit 455a505
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
34 changes: 34 additions & 0 deletions crosstool/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,38 @@ def _impl(ctx):
],
)

link_dylib_feature = feature(
name = "link_dylib",
flag_sets = [
flag_set(
actions = _DYNAMIC_LINK_ACTIONS,
flag_groups = [
flag_group(
flags = [
"-dynamiclib",
],
),
],
),
],
)

link_bundle_feature = feature(
name = "link_bundle",
flag_sets = [
flag_set(
actions = _DYNAMIC_LINK_ACTIONS,
flag_groups = [
flag_group(
flags = [
"-bundle",
],
),
],
),
],
)

no_deduplicate_feature = feature(
name = "no_deduplicate",
enabled = True,
Expand Down Expand Up @@ -2511,6 +2543,8 @@ def _impl(ctx):
ubsan_feature,
default_sanitizer_flags_feature,
treat_warnings_as_errors_feature,
link_dylib_feature,
link_bundle_feature,
]

if (ctx.attr.cpu == "darwin_x86_64" or
Expand Down
16 changes: 16 additions & 0 deletions test/linking_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ def linking_test_suite(name):
mnemonic = "ObjcLink",
target_under_test = "//test/test_data:macos_binary",
)

default_test(
name = "{}_dylib_test".format(name),
tags = [name],
expected_argv = [
"-Xlinker",
"-objc_abi_version",
"-Xlinker",
"2",
"-ObjC",
"-dynamiclib",
],
not_expected_argv = [],
mnemonic = "ObjcLink",
target_under_test = "//test/test_data:macos_dylib",
)
23 changes: 22 additions & 1 deletion test/starlark_apple_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@

load("//test:transitions.bzl", "apple_platform_split_transition")

_supports_extra_requested_features = hasattr(apple_common.platform_type, "visionos")

def _starlark_apple_binary_impl(ctx):
extra_requested_features = []
bazel_6_linkopts = []
if ctx.attr.binary_type == "dylib":
extra_requested_features.append("link_dylib")
bazel_6_linkopts = ["-dynamiclib"]
elif ctx.attr.binary_type == "loadable_bundle":
extra_requested_features.append("link_bundle")
bazel_6_linkopts = ["-bundle"]

kwargs = {}
if _supports_extra_requested_features:
kwargs["extra_requested_features"] = extra_requested_features
else:
kwargs["extra_linkopts"] = bazel_6_linkopts

link_result = apple_common.link_multi_arch_binary(
ctx = ctx,
stamp = ctx.attr.stamp,
**kwargs
)
processed_binary = ctx.actions.declare_file(
"{}_lipobin".format(ctx.label.name),
Expand Down Expand Up @@ -65,7 +83,10 @@ starlark_apple_binary = rule(
default = Label("@bazel_tools//tools/objc:xcrunwrapper"),
executable = True,
),
"binary_type": attr.string(default = "executable"),
"binary_type": attr.string(
default = "executable",
values = ["dylib", "executable", "loadable_bundle"],
),
"bundle_loader": attr.label(),
"deps": attr.label_list(
cfg = apple_platform_split_transition,
Expand Down
9 changes: 9 additions & 0 deletions test/test_data/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ cc_library(
tags = TARGETS_UNDER_TEST_TAGS,
)

starlark_apple_binary(
name = "macos_dylib",
binary_type = "dylib",
minimum_os_version = "13.0",
platform_type = "macos",
tags = TARGETS_UNDER_TEST_TAGS,
deps = [":cc_lib"],
)

objc_library(
name = "objc_lib",
srcs = ["objc_lib.m"],
Expand Down

0 comments on commit 455a505

Please sign in to comment.