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)".
This commit is contained in:
Nicholas Junge 2023-10-25 13:12:18 +02:00 committed by GitHub
parent e45585a4b8
commit 698d1dc8c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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"],
)