Skip to content

Commit 455a505

Browse files
committed
Move some linkopts to features
bazelbuild/rules_apple#2214
1 parent 96d7cff commit 455a505

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

crosstool/cc_toolchain_config.bzl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,38 @@ def _impl(ctx):
993993
],
994994
)
995995

996+
link_dylib_feature = feature(
997+
name = "link_dylib",
998+
flag_sets = [
999+
flag_set(
1000+
actions = _DYNAMIC_LINK_ACTIONS,
1001+
flag_groups = [
1002+
flag_group(
1003+
flags = [
1004+
"-dynamiclib",
1005+
],
1006+
),
1007+
],
1008+
),
1009+
],
1010+
)
1011+
1012+
link_bundle_feature = feature(
1013+
name = "link_bundle",
1014+
flag_sets = [
1015+
flag_set(
1016+
actions = _DYNAMIC_LINK_ACTIONS,
1017+
flag_groups = [
1018+
flag_group(
1019+
flags = [
1020+
"-bundle",
1021+
],
1022+
),
1023+
],
1024+
),
1025+
],
1026+
)
1027+
9961028
no_deduplicate_feature = feature(
9971029
name = "no_deduplicate",
9981030
enabled = True,
@@ -2511,6 +2543,8 @@ def _impl(ctx):
25112543
ubsan_feature,
25122544
default_sanitizer_flags_feature,
25132545
treat_warnings_as_errors_feature,
2546+
link_dylib_feature,
2547+
link_bundle_feature,
25142548
]
25152549

25162550
if (ctx.attr.cpu == "darwin_x86_64" or

test/linking_tests.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,19 @@ def linking_test_suite(name):
6464
mnemonic = "ObjcLink",
6565
target_under_test = "//test/test_data:macos_binary",
6666
)
67+
68+
default_test(
69+
name = "{}_dylib_test".format(name),
70+
tags = [name],
71+
expected_argv = [
72+
"-Xlinker",
73+
"-objc_abi_version",
74+
"-Xlinker",
75+
"2",
76+
"-ObjC",
77+
"-dynamiclib",
78+
],
79+
not_expected_argv = [],
80+
mnemonic = "ObjcLink",
81+
target_under_test = "//test/test_data:macos_dylib",
82+
)

test/starlark_apple_binary.bzl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22

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

5+
_supports_extra_requested_features = hasattr(apple_common.platform_type, "visionos")
6+
57
def _starlark_apple_binary_impl(ctx):
8+
extra_requested_features = []
9+
bazel_6_linkopts = []
10+
if ctx.attr.binary_type == "dylib":
11+
extra_requested_features.append("link_dylib")
12+
bazel_6_linkopts = ["-dynamiclib"]
13+
elif ctx.attr.binary_type == "loadable_bundle":
14+
extra_requested_features.append("link_bundle")
15+
bazel_6_linkopts = ["-bundle"]
16+
17+
kwargs = {}
18+
if _supports_extra_requested_features:
19+
kwargs["extra_requested_features"] = extra_requested_features
20+
else:
21+
kwargs["extra_linkopts"] = bazel_6_linkopts
22+
623
link_result = apple_common.link_multi_arch_binary(
724
ctx = ctx,
825
stamp = ctx.attr.stamp,
26+
**kwargs
927
)
1028
processed_binary = ctx.actions.declare_file(
1129
"{}_lipobin".format(ctx.label.name),
@@ -65,7 +83,10 @@ starlark_apple_binary = rule(
6583
default = Label("@bazel_tools//tools/objc:xcrunwrapper"),
6684
executable = True,
6785
),
68-
"binary_type": attr.string(default = "executable"),
86+
"binary_type": attr.string(
87+
default = "executable",
88+
values = ["dylib", "executable", "loadable_bundle"],
89+
),
6990
"bundle_loader": attr.label(),
7091
"deps": attr.label_list(
7192
cfg = apple_platform_split_transition,

test/test_data/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ cc_library(
5252
tags = TARGETS_UNDER_TEST_TAGS,
5353
)
5454

55+
starlark_apple_binary(
56+
name = "macos_dylib",
57+
binary_type = "dylib",
58+
minimum_os_version = "13.0",
59+
platform_type = "macos",
60+
tags = TARGETS_UNDER_TEST_TAGS,
61+
deps = [":cc_lib"],
62+
)
63+
5564
objc_library(
5665
name = "objc_lib",
5766
srcs = ["objc_lib.m"],

0 commit comments

Comments
 (0)