Skip to content

Commit

Permalink
Merge pull request #3 from nicholasjng/add-sizeopt-flag
Browse files Browse the repository at this point in the history
Add --sizeopt flag to `@nanobind` target
  • Loading branch information
nicholasjng authored Mar 4, 2024
2 parents 411abf3 + 863eab9 commit e2dc8c8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module(
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "bazel_skylib", version = "1.5.0")

# Parses the configured nanobind version from this file, and creates a `http_archive` for it.
# Currently, this points to stable tags only.
Expand Down
8 changes: 8 additions & 0 deletions helpers.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Helper flags for nanobind build options."""

def sizeopts():
return select({
"@nanobind//:msvc_and_minsize": ["/Os"],
"@nanobind//:nonmsvc_and_minsize": ["-Os"],
"@nanobind//:without_sizeopts": [],
})
55 changes: 52 additions & 3 deletions nanobind.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,62 @@ Size optimizations used: -Os, LTO
Linker optimizations used: LTCG (MSVC on Windows), linker response file (macOS only).
"""

load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@nanobind_bazel//:helpers.bzl", "sizeopts")

licenses(["notice"])

# TODO: Change this when cleaning up exports later.
package(default_visibility = ["//visibility:public"])

bool_flag(
name = "minsize",
build_setting_default = False,
)

config_setting(
name = "with_sizeopts",
flag_values = {":minsize": "True"},
)

config_setting(
name = "without_sizeopts",
flag_values = {":minsize": "False"},
)

config_setting(
name = "msvc",
flag_values = {"@bazel_tools//tools/cpp:compiler": "msvc-cl"},
)

# Is the currently configured C++ compiler not MSVC?
selects.config_setting_group(
name = "nonmsvc",
match_any = [
"@rules_cc//cc/compiler:gcc",
"@rules_cc//cc/compiler:clang",
"@rules_cc//cc/compiler:clang-cl",
"@rules_cc//cc/compiler:mingw-gcc",
],
)

selects.config_setting_group(
name = "msvc_and_minsize",
match_all = [
":msvc",
":with_sizeopts",
],
)

selects.config_setting_group(
name = "nonmsvc_and_minsize",
match_all = [
":nonmsvc",
":with_sizeopts",
],
)

cc_library(
name = "nanobind",
srcs = glob(["src/*.cpp"]),
Expand All @@ -24,7 +71,6 @@ cc_library(
copts = select({
":msvc": [
"/EHsc", # exceptions
"/Os", # size optimizations
"/GL", # LTO / whole program optimization
],
# clang and gcc, across all platforms.
Expand All @@ -33,14 +79,17 @@ cc_library(
"-flto",
"-Os",
],
}),
}) + sizeopts(),
includes = [
"ext/robin_map/include",
"include",
],
linkopts = select({
":msvc": ["/LTCG"], # MSVC.
"@platforms//os:macos": ["-Wl,@$(location :cmake/darwin-ld-cpython.sym)"], # Apple.
"@platforms//os:macos": [
"-Wl,@$(location :cmake/darwin-ld-cpython.sym)", # Apple.
"-Wl,-dead_strip",
],
"//conditions:default": [],
}),
textual_hdrs = glob(
Expand Down

0 comments on commit e2dc8c8

Please sign in to comment.