From b219e18b91b4279a582abb9195a1fefc5d8838c0 Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Fri, 27 Oct 2023 13:49:43 +0200 Subject: [PATCH] [bindings] Add LTO builds on Windows+MSVC (#1687) * Add LTO builds on Windows+MSVC Gates the MSVC switches behind an `@bazel_skylib:selects` statement. This is a first experiment from best guesses and studying the MSVC docs. * Fix misleading inline comment --- bindings/python/nanobind.BUILD | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bindings/python/nanobind.BUILD b/bindings/python/nanobind.BUILD index c7edfb2b..c6fa1c6d 100644 --- a/bindings/python/nanobind.BUILD +++ b/bindings/python/nanobind.BUILD @@ -2,11 +2,21 @@ licenses(["notice"]) package(default_visibility = ["//visibility:public"]) +load("@bazel_skylib//lib:selects.bzl", "selects") + config_setting( name = "msvc_compiler", flag_values = {"@bazel_tools//tools/cpp:compiler": "msvc-cl"}, ) +selects.config_setting_group( + name = "winplusmsvc", + match_all = [ + "@platforms//os:windows", + ":msvc_compiler", + ], +) + cc_library( name = "nanobind", srcs = glob([ @@ -20,6 +30,7 @@ cc_library( ":msvc_compiler": [ "/EHsc", # exceptions "/Os", # size optimizations + "/GL", # LTO / whole program optimization ], # these should work on both clang and gcc. "//conditions:default": [ @@ -33,7 +44,8 @@ cc_library( "include", ], linkopts = select({ - "@platforms//os:macos": ["-Wl,@$(location :cmake/darwin-ld-cpython.sym)"], + ":winplusmsvc": ["/LTGC"], # Windows + MSVC. + "@platforms//os:macos": ["-Wl,@$(location :cmake/darwin-ld-cpython.sym)"], # Apple. "//conditions:default": [], }), textual_hdrs = glob(