From 27df79d278aac09e3d973b98af2553f0a569cba3 Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Mon, 11 Nov 2024 16:02:32 +0100 Subject: [PATCH] Enable link time -Os in opt builds (#348) `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`... --- crosstool/cc_toolchain_config.bzl | 22 ++++++++++++++++++ test/linking_tests.bzl | 37 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/crosstool/cc_toolchain_config.bzl b/crosstool/cc_toolchain_config.bzl index 23743d1..4eac42d 100644 --- a/crosstool/cc_toolchain_config.bzl +++ b/crosstool/cc_toolchain_config.bzl @@ -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 = [ @@ -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, diff --git a/test/linking_tests.bzl b/test/linking_tests.bzl index 9fd5dc0..d495430 100644 --- a/test/linking_tests.bzl +++ b/test/linking_tests.bzl @@ -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. @@ -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],