Skip to content

Commit

Permalink
Only apply linker strip optimizations in release builds
Browse files Browse the repository at this point in the history
Otherwise, debugging is impeded.
  • Loading branch information
nicholasjng committed Apr 3, 2024
1 parent 5214b5f commit 2d17be4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
20 changes: 20 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ config_setting(
flag_values = {":py-limited-api": "unset"},
)

config_setting(
name = "MacReleaseBuild",
constraint_values = [
"@platforms//os:macos",
],
values = {
"compilation_mode": "opt",
},
)

config_setting(
name = "LinuxReleaseBuild",
constraint_values = [
"@platforms//os:linux",
],
values = {
"compilation_mode": "opt",
},
)

selects.config_setting_group(
name = "unix",
match_any = [
Expand Down
8 changes: 8 additions & 0 deletions helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ def sizeopts():
"@nanobind_bazel//:without_sizeopts": [],
})

def stripopts():
"""Linker options to strip external and debug symbols from nanobind release builds."""
return select({
"@nanobind_bazel//:MacReleaseBuild": ["-Wl,-x", "-Wl,-S"],
"@nanobind_bazel//:LinuxReleaseBuild": ["-Wl,-s"],
"//conditions:default": [],
})

def sizedefs():
return select({
"@nanobind_bazel//:with_sizeopts": ["NB_COMPACT_ASSERTIONS"],
Expand Down
11 changes: 3 additions & 8 deletions nanobind.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Size optimizations used: -Os, LTO.
Linker optimizations used: LTO (clang, gcc) / LTCG (MSVC), linker response file (macOS only).
"""

load("@nanobind_bazel//:helpers.bzl", "py_limited_api", "sizedefs", "sizeopts")
load("@nanobind_bazel//:helpers.bzl", "py_limited_api", "sizedefs", "sizeopts", "stripopts")

licenses(["notice"])

Expand Down Expand Up @@ -37,19 +37,14 @@ cc_library(
features = ["-pic"], # use a compiler flag instead.
includes = ["include"],
linkopts = select({
"@platforms//os:linux": [
"-Wl,-s",
"-Wl,--gc-sections",
],
"@platforms//os:linux": ["-Wl,--gc-sections"],
"@platforms//os:macos": [
# chained fixups on Apple platforms.
"-Wl,@$(location :cmake/darwin-ld-cpython.sym)",
"-Wl,-dead_strip",
"-Wl,-x",
"-Wl,-S",
],
"//conditions:default": [],
}),
}) + stripopts(),
local_defines = sizedefs(), # sizeopts apply to nanobind only.
textual_hdrs = glob(
[
Expand Down

0 comments on commit 2d17be4

Please sign in to comment.