Skip to content

Commit

Permalink
[toolchain] Make Os and Oz mutually exclusive.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sasdf committed Jan 17, 2025
1 parent d3e7c5a commit c2d502c
Showing 1 changed file with 75 additions and 42 deletions.
117 changes: 75 additions & 42 deletions toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(
Expand Down Expand Up @@ -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"],
Expand Down

0 comments on commit c2d502c

Please sign in to comment.