From 5bc9ecff7578e13e5904860e961c4014fffabc4f Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Tue, 5 Mar 2024 22:00:21 +0100 Subject: [PATCH] Add .abi3.so copy file, extension name helper Also add more required config settings for stable ABI + unix (producing a .abi3.so extension). --- BUILD | 36 ++++++++++++++++++++++++++++++++++++ build_defs.bzl | 13 ++++++++----- helpers.bzl | 11 ++++++++++- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/BUILD b/BUILD index 9853201..483b000 100644 --- a/BUILD +++ b/BUILD @@ -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", diff --git a/build_defs.bzl b/build_defs.bzl index 744d93b..81cd0f4 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -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"): [], @@ -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, @@ -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", @@ -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( diff --git a/helpers.bzl b/helpers.bzl index 9da8303..ec5aaae 100644 --- a/helpers.bzl +++ b/helpers.bzl @@ -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", + })