Skip to content

Commit

Permalink
Enable link time -Os in opt builds (#348)
Browse files Browse the repository at this point in the history
`ld` starting Xcode 15 (`ld_prime`) now supports this option to perform
more general code de-deduplication passes which it was not doing prior.

It is not documented anywhere but Xcode 15+ has this enabled by default
in the default Release scheme (which has`Optimization Level =
Fastest/Smallest -Os` by default).

I'm adding this as a separate feature so that it can be disabled but
there could be an argument whether to enable this by default !

Fun note: passing any character after `-O` performs the same as `-Os`...
  • Loading branch information
cerisier authored Nov 11, 2024
1 parent 8ee7a2d commit 27df79d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crosstool/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,27 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
],
)

link_osize_opt_feature = feature(
name = "link_osize_opt",
enabled = True,
flag_sets = [
flag_set(
actions = _DYNAMIC_LINK_ACTIONS,
flag_groups = [
flag_group(
flags = [
"-Xlinker",
"-Os",
],
),
],
with_features = [
with_feature_set(features = ["opt"]),
],
),
],
)

output_execpath_flags_feature = feature(
name = "output_execpath_flags",
flag_sets = [
Expand Down Expand Up @@ -2708,6 +2729,7 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
user_link_flags_feature,
default_link_flags_feature,
no_deduplicate_feature,
link_osize_opt_feature,
dead_strip_feature,
apply_implicit_frameworks_feature,
link_cocoa_feature,
Expand Down
37 changes: 37 additions & 0 deletions test/linking_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ dsym_test = make_action_command_line_test_rule(
},
)

opt_osize_enabled_test = make_action_command_line_test_rule(
config_settings = {
"//command_line_option:compilation_mode": "opt",
"//command_line_option:features": [
"link_osize_opt",
],
},
)

disable_opt_osize_test = make_action_command_line_test_rule(
config_settings = {
"//command_line_option:compilation_mode": "opt",
"//command_line_option:features": [
"-link_osize_opt",
],
},
)

def linking_test_suite(name):
"""Tests for linking behavior.
Expand Down Expand Up @@ -123,6 +141,25 @@ def linking_test_suite(name):
target_under_test = "//test/test_data:macos_binary",
)

opt_osize_enabled_test(
name = "{}_opt_osize_enabled_test".format(name),
tags = [name],
expected_argv = [
"-Xlinker",
"-Os",
],
mnemonic = "ObjcLink",
target_under_test = "//test/test_data:macos_binary",
)

disable_opt_osize_test(
name = "{}_disable_opt_osize_test".format(name),
tags = [name],
not_expected_argv = ["-Os"],
mnemonic = "ObjcLink",
target_under_test = "//test/test_data:macos_binary",
)

disable_objc_test(
name = "{}_disable_objc_apple_link_test".format(name),
tags = [name],
Expand Down

0 comments on commit 27df79d

Please sign in to comment.