Skip to content

Commit

Permalink
Add .abi3.so copy file, extension name helper
Browse files Browse the repository at this point in the history
Also add more required config settings for stable ABI + unix (producing
a .abi3.so extension).
  • Loading branch information
nicholasjng committed Mar 5, 2024
1 parent 58f76d8 commit 5bc9ecf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
36 changes: 36 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,42 @@ config_setting(
flag_values = {":py-limited-api": "unset"},
)

selects.config_setting_group(
name = "unix",
match_any = [
"@platforms//os:linux",
"@platforms//os:macos",
],
)

# Config setting indicating that stable ABI extension build was requested.
selects.config_setting_group(
name = "stable-abi",
match_any = [
":cp312",
":cp313",
],
)

# A stable ABI build on Linux or Mac.
# This requires a different extension name (.abi3.so instead of just .so).
selects.config_setting_group(
name = "stable-abi-unix",
match_all = [
":stable-abi",
":unix",
],
)

# An unlimited Python ABI build on Linux or Mac. Produces a regular .so file.
selects.config_setting_group(
name = "unstable-abi-unix",
match_all = [
":pyunlimitedapi",
":unix",
],
)

# Is the currently configured C++ compiler not MSVC?
selects.config_setting_group(
name = "nonmsvc",
Expand Down
13 changes: 8 additions & 5 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ which can then be included e.g. as a `data` input in a ``native.py_library``.
"""

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@nanobind_bazel//:helpers.bzl", "extension_name")

NANOBIND_COPTS = select({
Label("@rules_cc//cc/compiler:msvc-cl"): [],
Expand Down Expand Up @@ -40,7 +41,6 @@ def nanobind_extension(
features = [],
deps = [],
**kwargs):
# TODO: This might need a different suffix depending on SABI yes/no.
native.cc_binary(
name = name + ".so",
srcs = srcs,
Expand All @@ -52,6 +52,12 @@ def nanobind_extension(
**kwargs
)

copy_file(
name = name + "_copy_so_to_abi3_so",
src = name + ".so",
out = name + ".abi3.so",
)

copy_file(
name = name + "_copy_so_to_pyd",
src = name + ".so",
Expand All @@ -60,10 +66,7 @@ def nanobind_extension(

native.alias(
name = name,
actual = select({
"@platforms//os:windows": name + ".pyd",
"//conditions:default": name + ".so",
}),
actual = extension_name(name),
)

def nanobind_library(
Expand Down
11 changes: 10 additions & 1 deletion helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ def sizedefs():
"@nanobind_bazel//:without_sizeopts": [],
})

# define the Python version hex if stable ABI builds are requested.
# Define the Python version hex if stable ABI builds are requested.
def pyversionhex():
return select({
"@nanobind_bazel//:cp312": ["Py_LIMITED_API=0x030C0000"],
"@nanobind_bazel//:cp313": ["Py_LIMITED_API=0x030D0000"],
"@nanobind_bazel//:pyunlimitedapi": [],
})

# Get the name for a built nanobind extension based on target platform
# and stable ABI build yes/no.
def extension_name(name):
return select({
"@platforms//os:windows": name + ".pyd",
"@nanobind_bazel//:stable-abi-unix": name + ".abi3.so",
"@nanobind_bazel//:unstable-abi-unix": name + ".so",
})

0 comments on commit 5bc9ecf

Please sign in to comment.