From 698d1dc8c321d0797487e7988825f3f43c1d86b0 Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Wed, 25 Oct 2023 13:12:18 +0200 Subject: [PATCH] Reapply size optimizations for clang & MSVC, LTO for Mac+Linux (#1685) * Reapply size optimization for clang, equivalent options for MSVC Working towards cross-platform optimal nanobind building configurations. * Add LTO back to non-Windows builds The Windows case (the option name is "/GL") is more complicated, since there, the compiler options also need to be passed to the linker if LTO is enabled. Since we are gating the linker options on platform at the moment instead of compiler, we need to implement a Bazel boolean flag for the case "Platform == MacOS && Compiler == AnyOf(gcc, clang)". --- bindings/python/nanobind.BUILD | 42 ++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/bindings/python/nanobind.BUILD b/bindings/python/nanobind.BUILD index 28520195..c7edfb2b 100644 --- a/bindings/python/nanobind.BUILD +++ b/bindings/python/nanobind.BUILD @@ -2,18 +2,40 @@ licenses(["notice"]) package(default_visibility = ["//visibility:public"]) -filegroup( - name = "symboltable", - srcs = ["cmake/darwin-ld-cpython.sym"], +config_setting( + name = "msvc_compiler", + flag_values = {"@bazel_tools//tools/cpp:compiler": "msvc-cl"}, ) cc_library( name = "nanobind", srcs = glob([ - "src/*.cpp" + "src/*.cpp", ]), - copts = ["-fexceptions"], - includes = ["include", "ext/robin_map/include"], + additional_linker_inputs = select({ + "@platforms//os:macos": [":cmake/darwin-ld-cpython.sym"], + "//conditions:default": [], + }), + copts = select({ + ":msvc_compiler": [ + "/EHsc", # exceptions + "/Os", # size optimizations + ], + # these should work on both clang and gcc. + "//conditions:default": [ + "-fexceptions", + "-flto", + "-Os", + ], + }), + includes = [ + "ext/robin_map/include", + "include", + ], + linkopts = select({ + "@platforms//os:macos": ["-Wl,@$(location :cmake/darwin-ld-cpython.sym)"], + "//conditions:default": [], + }), textual_hdrs = glob( [ "include/**/*.h", @@ -21,13 +43,5 @@ cc_library( "ext/robin_map/include/tsl/*.h", ], ), - linkopts = select({ - "@platforms//os:macos": ["-Wl,@$(location :cmake/darwin-ld-cpython.sym)"], - "//conditions:default": [], - }), - additional_linker_inputs = select({ - "@platforms//os:macos": [":cmake/darwin-ld-cpython.sym"], - "//conditions:default": [], - }), deps = ["@python_headers"], )