From c2d502c5589aad41df403072530cf71ce47d7a8a Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Fri, 17 Jan 2025 14:43:40 +0000 Subject: [PATCH] [toolchain] Make Os and Oz mutually exclusive. This change ensures optimization level flags are exclusive, resolving ambiguity caused by the previous implementation where it expects the last flag overwrote the optimization level. Change-Id: I6b372c36ac99eb647f36956934e08fd6cc0e412f Signed-off-by: Yi-Hsuan Deng --- toolchain/BUILD | 117 +++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 42 deletions(-) diff --git a/toolchain/BUILD b/toolchain/BUILD index 9ede21cd85d57..3db31f5353d9d 100644 --- a/toolchain/BUILD +++ b/toolchain/BUILD @@ -87,26 +87,92 @@ cc_action_type( action_name = OT_ACTION_OBJDUMP, ) +# minsize feature to switch between Os and Oz. +cc_feature( + name = "feat_minsize", + feature_name = "minsize", +) + +cc_feature_constraint( + name = "constraint_minsize", + all_of = [":feat_minsize"], +) + +cc_feature_constraint( + name = "constraint_no_minsize", + none_of = [":feat_minsize"], +) + +cc_feature_constraint( + name = "constraint_minsize_lld", + all_of = [ + ":feat_minsize", + ":feat_use_lld", + ], +) + # Built-in Bazel feature enabled for optimized builds. cc_feature( name = "feat_opt", args = [ - ":opt", + ":optsize", + ":minsize", + ":minsize_inline_cc", + ":minsize_inline_ld", + ":default_debug_info", ":lto", ], overrides = "@rules_cc//cc/toolchains/features:opt", ) cc_args( - name = "opt", + name = "default_debug_info", actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], args = [ - "-Os", "-g", "-gdwarf-4", ], ) +cc_args( + name = "optsize", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-Os", + ], + requires_any_of = [":constraint_no_minsize"], +) + +cc_args( + name = "minsize", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-Oz", + ], + requires_any_of = [":constraint_minsize"], +) + +# Inline slightly more which is actually smaller. +cc_args( + name = "minsize_inline_cc", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-mllvm", + "--inline-threshold=10", + ], + requires_any_of = [":constraint_minsize_lld"], +) + +cc_args( + name = "minsize_inline_ld", + actions = ["@rules_cc//cc/toolchains/actions:link_actions"], + args = [ + "-Wl,-mllvm", + "-Wl,--inline-threshold=10", + ], + requires_any_of = [":constraint_minsize_lld"], +) + # Built-in Bazel feature enabled for debug builds. cc_feature( name = "feat_dbg", @@ -126,18 +192,14 @@ cc_args( # Built-in Bazel feature for fast builds. cc_feature( name = "feat_fastbuild", - args = [":fastbuild"], - overrides = "@rules_cc//cc/toolchains/features:fastbuild", -) - -cc_args( - name = "fastbuild", - actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], args = [ - "-Os", - "-g", - "-gdwarf-4", + ":optsize", + ":minsize", + ":minsize_inline_cc", + ":minsize_inline_ld", + ":default_debug_info", ], + overrides = "@rules_cc//cc/toolchains/features:fastbuild", ) cc_args( @@ -306,35 +368,6 @@ cc_args( args = ["-flto"], ) -cc_feature( - name = "feat_minsize", - args = [ - ":minsize_cc", - ":minsize_ld", - ], - feature_name = "minsize", -) - -cc_args( - name = "minsize_cc", - actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], - args = [ - "-Oz", - # Inline slightly more which is actually smaller. - "-mllvm", - "--inline-threshold=10", - ], -) - -cc_args( - name = "minsize_ld", - actions = ["@rules_cc//cc/toolchains/actions:link_actions"], - args = [ - "-Wl,-mllvm", - "-Wl,--inline-threshold=10", - ], -) - cc_args( name = "unique_sections", actions = ["@rules_cc//cc/toolchains/actions:compile_actions"],