Skip to content

Commit

Permalink
Make static linking for nanobind_extension optional (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjng authored Mar 10, 2024
1 parent 97e3db2 commit 5b82019
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
11 changes: 6 additions & 5 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ load("@nanobind_bazel//:helpers.bzl", "extension_name")

NANOBIND_COPTS = select({
Label("@rules_cc//cc/compiler:msvc-cl"): [],
"//conditions:default": ["-fexceptions", "-fvisibility=hidden"],
"//conditions:default": ["-fexceptions", "-fno-strict-aliasing"],
})

NANOBIND_FEATURES = [
Expand All @@ -31,8 +31,10 @@ NANOBIND_DEPS = [
# A C++ Python extension library built with nanobind.
# Given a name $NAME, defines the following targets:
# 1. $NAME.so, a shared object library for use on Linux/Mac.
# 2. $NAME.pyd, a copy of $NAME.so for use on Windows.
# 3. $NAME, an alias pointing to the appropriate library
# 2. $NAME.abi3.so, a copy of $NAME.so for Linux/Mac,
# indicating that it is compatible with the Python stable ABI.
# 3. $NAME.pyd, a copy of $NAME.so for use on Windows.
# 4. $NAME, an alias pointing to the appropriate library
# depending on the target platform.
def nanobind_extension(
name,
Expand All @@ -47,8 +49,7 @@ def nanobind_extension(
copts = copts + NANOBIND_COPTS,
features = features + NANOBIND_FEATURES,
deps = deps + NANOBIND_DEPS,
linkshared = True,
linkstatic = True,
linkshared = True, # Python extensions need to be shared libs.
**kwargs
)

Expand Down
2 changes: 1 addition & 1 deletion helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def sizedefs():
})

# Define the Python version hex if stable ABI builds are requested.
def pyversionhex():
def py_limited_api():
return select({
"@nanobind_bazel//:cp312": ["Py_LIMITED_API=0x030C0000"],
"@nanobind_bazel//:cp313": ["Py_LIMITED_API=0x030D0000"],
Expand Down
1 change: 1 addition & 0 deletions internal_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def _internal_configure_extension_impl(module_ctx):
name = "robin_map",
build_file = "//:robin_map.BUILD",
strip_prefix = "robin-map-1.2.1",
integrity = "sha256-K1TSwd4vc76lxR1dy9ZIE6CMrxv93P3u5Aq3TpWZ6OM=",
urls = ["https://github.com/Tessil/robin-map/archive/refs/tags/v1.2.1.tar.gz"],
)

Expand Down
38 changes: 23 additions & 15 deletions nanobind.BUILD
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
"""
A cross-platform nanobind Bazel BUILD.
A cross-platform nanobind Bazel build.
Supports size and linker optimizations across all three major operating systems.
Size optimizations used: -Os, LTO
Linker optimizations used: LTCG (MSVC on Windows), linker response file (macOS only).
Size optimizations used: -Os, LTO.
Linker optimizations used: LTO (clang, gcc) / LTCG (MSVC), linker response file (macOS only).
"""

load("@nanobind_bazel//:helpers.bzl", "pyversionhex", "sizedefs", "sizeopts")
load("@nanobind_bazel//:helpers.bzl", "py_limited_api", "sizedefs", "sizeopts")

licenses(["notice"])

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

_NB_DEPS = [
"@robin_map",
"@rules_python//python/cc:current_py_cc_headers",
] + select({
# we need to link in the Python libs only on Windows to signal to the linker that it
# needs to go searching for these symbols at runtime.
# TODO: This seems Windows-specific, so change to `@platforms//os:windows`?
"@rules_cc//cc/compiler:msvc-cl": ["@rules_python//python/cc:current_py_cc_libs"],
"//conditions:default": [],
})

cc_library(
name = "nanobind",
srcs = glob(["src/*.cpp"]),
Expand All @@ -28,19 +38,20 @@ cc_library(
"//conditions:default": [
"-fexceptions",
"-flto",
"-fno-strict-aliasing",
],
}) + sizeopts(),
defines = pyversionhex(),
includes = [
"ext/robin_map/include",
"include",
],
defines = py_limited_api(),
includes = ["include"],
linkopts = select({
"@rules_cc//cc/compiler:msvc-cl": ["/LTCG"], # MSVC.
"@platforms//os:linux": [
"-Wl,--gc-sections",
],
"@platforms//os:macos": [
"-Wl,@$(location :cmake/darwin-ld-cpython.sym)", # Apple.
"-Wl,-dead_strip",
],
"@rules_cc//cc/compiler:msvc-cl": ["/LTCG"], # MSVC.
"//conditions:default": [],
}),
local_defines = sizedefs(), # sizeopts apply to nanobind only.
Expand All @@ -50,8 +61,5 @@ cc_library(
"src/*.h",
],
),
deps = [
"@robin_map",
"@rules_python//python/cc:current_py_cc_headers",
],
deps = _NB_DEPS,
)

0 comments on commit 5b82019

Please sign in to comment.