From 863eab96c53ac5c8b5281fc9d889d0ac94cb289b Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Thu, 22 Feb 2024 20:58:57 +0100 Subject: [PATCH] Add --sizeopt flag to `@nanobind` target Controls the passing of size optimization flags for each target compiler. Unfortunately, since this needs a `select()` based on compiler and sizeopt yes/no, we now have to explicitly match the C++ compiler from the configured toolchain. Also, this results in quite a few config settings in the `@nanobind` target, which could be adjusted in visibility when this is more polished. The current attempt seems to build the nanobind example properly. --- MODULE.bazel | 1 + helpers.bzl | 8 ++++++++ nanobind.BUILD | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 helpers.bzl diff --git a/MODULE.bazel b/MODULE.bazel index b9080a3..e4ed562 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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. diff --git a/helpers.bzl b/helpers.bzl new file mode 100644 index 0000000..6104e2d --- /dev/null +++ b/helpers.bzl @@ -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": [], + }) diff --git a/nanobind.BUILD b/nanobind.BUILD index c90c4fb..9577d20 100644 --- a/nanobind.BUILD +++ b/nanobind.BUILD @@ -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"]), @@ -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. @@ -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(