Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add strip optimizations to linker flags #22

Merged
merged 3 commits into from
Apr 3, 2024
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
39 changes: 39 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,45 @@ 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",
},
)

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

selects.config_setting_group(
name = "releaseBuild",
match_any = [
":LinuxReleaseBuild",
":MacReleaseBuild",
":WindowsReleaseBuild",
],
)

selects.config_setting_group(
name = "unix",
match_any = [
Expand Down
4 changes: 2 additions & 2 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ which can then be included e.g. as a `data` input in a ``native.py_library``.
"""

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@nanobind_bazel//:helpers.bzl", "extension_name", "sizeopts")
load("@nanobind_bazel//:helpers.bzl", "extension_name", "nb_sizeopts")

NANOBIND_COPTS = select({
"@platforms//os:macos": [
Expand All @@ -27,7 +27,7 @@ NANOBIND_COPTS = select({
"-fdata-sections",
],
"//conditions:default": [],
}) + sizeopts()
}) + nb_sizeopts()

NANOBIND_DEPS = [Label("@nanobind//:nanobind")]

Expand Down
16 changes: 12 additions & 4 deletions helpers.bzl
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
"""Helper flags for nanobind build options."""

def sizeopts():
def nb_sizeopts():
return select({
"@nanobind_bazel//:msvc_and_minsize": ["/Os"],
"@nanobind_bazel//:nonmsvc_and_minsize": ["-Os"],
"@nanobind_bazel//:without_sizeopts": [],
})

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

def maybe_compact_asserts():
return select({
"@nanobind_bazel//:releaseBuild": ["NB_COMPACT_ASSERTIONS"],
"//conditions:default": [],
})

# Define the Python version hex if stable ABI builds are requested.
Expand Down
22 changes: 14 additions & 8 deletions nanobind.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
A cross-platform nanobind Bazel build.
Supports size and linker optimizations across all three major operating systems.
Size optimizations used: -Os, LTO.
Linker optimizations used: LTO (clang, gcc) / LTCG (MSVC), linker response file (macOS only).
Linker optimizations used: Debug stripping (release mode), linker response file (macOS only).
"""

load("@nanobind_bazel//:helpers.bzl", "py_limited_api", "sizedefs", "sizeopts")
load(
"@nanobind_bazel//:helpers.bzl",
"maybe_compact_asserts",
"nb_sizeopts",
"nb_stripopts",
"py_limited_api",
)

licenses(["notice"])

Expand All @@ -26,26 +32,26 @@ cc_library(
],
"@platforms//os:linux": [
"-fPIC",
"-fvisibility=hidden",
"-ffunction-sections",
"-fdata-sections",
"-fno-strict-aliasing",
],
"//conditions:default": [],
}) + sizeopts(),
}) + nb_sizeopts(),
defines = py_limited_api(),
features = ["-pic"], # use a compiler flag instead.
includes = ["include"],
linkopts = select({
"@platforms//os:linux": [
"-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",
],
"//conditions:default": [],
}),
local_defines = sizedefs(), # sizeopts apply to nanobind only.
}) + nb_stripopts(),
local_defines = maybe_compact_asserts(),
textual_hdrs = glob(
[
"include/**/*.h",
Expand Down