Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[bazel] Enable dead code stripping on macOS #537

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ build:fastdbg -c fastbuild
build:fastdbg --cxxopt='-gline-tables-only' --host_cxxopt='-gline-tables-only'
build:fastdbg --linkopt='-gline-tables-only' --host_linkopt='-gline-tables-only'
build:fastdbg --strip=never
build:fastdbg --//:dead_strip=False

# Additional Rust flags (see https://doc.rust-lang.org/rustc/codegen-options/index.html)
# Need to disable debug-assertions for fastdbg, should be off automatically for opt
# Need to disable debug-assertions for fastbuild, should be off automatically for opt
build --@rules_rust//:extra_rustc_flags=-C,panic=abort,-C,debug-assertions=n

build:fastdbg --@rules_rust//:extra_rustc_flags=-C,panic=unwind,-C,debug-assertions=y,-C,debuginfo=1
Expand Down
40 changes: 40 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//:capnpc-ts/package_json.bzl", capnpc_ts_bin = "bin")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@bazel_skylib//lib:selects.bzl", "selects")

cc_capnp_library(
name = "icudata-embed",
Expand All @@ -29,3 +31,41 @@ capnpc_ts_bin.capnpc_ts_binary(
name = "capnpc_ts",
visibility = ["//visibility:public"],
)

# bazel enables the --ffunction-sections, --gc-sections flags used to remove dead code by default
# on Linux opt builds. Enable the equivalent macOS flag -Wl,-dead_strip here to work around bazel
# idiosyncrasies.
# Note that the flag is defined for all non-debug builds of kj_test() and wd_cc_binary() on mac as
# it surprisingly appears to be faster, perhaps because the linker generates and writes binaries
# with much fewer symbols. This also helps reduce local and CI disk usage. If needed, dead code
# stripping can be limited to optimized builds.
config_setting(
name = "fast_build",
values = {"compilation_mode": "fastbuild"},
)

config_setting(
name = "opt_build",
values = {"compilation_mode": "opt"},
)

bool_flag(
name = "dead_strip",
build_setting_default = True,
)

config_setting(
name = "set_dead_strip",
flag_values = {"dead_strip": "True"},
)

# Workaround for bazel not supporting negated conditions (https://github.com/bazelbuild/bazel-skylib/issues/272)
selects.config_setting_group(
name = "not_dbg_build",
match_any = [":fast_build", ":opt_build"],
)

selects.config_setting_group(
name = "use_dead_strip",
match_all = ["@platforms//os:macos", ":set_dead_strip", ":not_dbg_build"],
)
4 changes: 4 additions & 0 deletions build/kj_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ def kj_test(
"@platforms//os:windows": [],
"//conditions:default": ["@workerd//src/workerd/util:symbolizer"],
}) + deps,
linkopts = select({
"@//:use_dead_strip": ["-Wl,-dead_strip"],
"//conditions:default": [""],
}),
)
5 changes: 5 additions & 0 deletions build/wd_cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

def wd_cc_binary(
name,
linkopts = [],
visibility = None,
**kwargs):
"""Wrapper for cc_binary that sets common attributes
"""
native.cc_binary(
name = name,
linkopts = linkopts + select({
"@//:use_dead_strip": ["-Wl,-dead_strip"],
"//conditions:default": [""],
}),
visibility = visibility,
**kwargs
)
2 changes: 1 addition & 1 deletion src/workerd/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("//:build/kj_test.bzl", "kj_test")
load("//:build/wd_cc_library.bzl", "wd_cc_library")
load("//:build/wd_cc_binary.bzl", "wd_cc_binary")
load("//:build/wd_cc_capnp_library.bzl", "wd_cc_capnp_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "int_flag")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@bazel_skylib//lib:selects.bzl", "selects")

config_setting(
Expand Down