@@ -291,17 +291,30 @@ def run(self):
291291# Extensions
292292################################################################################
293293
294- py_limited_api = sys .version_info >= (3 , 12 )
295- if py_limited_api :
296- setup_opts = {
297- "bdist_wheel" : {"py_limited_api" : "cp312" },
298- }
299- else :
300- setup_opts = {}
294+ ################################################################################
295+ # Extensions
296+ ################################################################################
297+
298+ # Enable limited ABI build
299+ # nanobind supports limited ABI for Python 3.12 and later.
300+ # https://blog.trailofbits.com/2022/11/15/python-wheels-abi-abi3audit/
301+ # 1. The Py_LIMITED_API macro is defined in the extension
302+ # 2. py_limited_api in Extension tags the extension as abi3
303+ # 3. bdist_wheel options tag the wheel as abi3
304+ NO_GIL = hasattr (sys , "_is_gil_enabled" ) and not sys ._is_gil_enabled ()
305+ PY_312_OR_NEWER = sys .version_info >= (3 , 12 )
306+ USE_LIMITED_API = not NO_GIL and PY_312_OR_NEWER and platform .system () != "FreeBSD"
307+
308+ macros = []
309+ if USE_LIMITED_API :
310+ macros .append (("Py_LIMITED_API" , "0x030C0000" ))
301311
302312ext_modules = [
303313 setuptools .Extension (
304- name = "onnxoptimizer.onnx_opt_cpp2py_export" , sources = [], py_limited_api = py_limited_api
314+ name = "onnxoptimizer.onnx_opt_cpp2py_export" ,
315+ sources = [],
316+ py_limited_api = USE_LIMITED_API ,
317+ define_macros = macros ,
305318 )
306319]
307320
@@ -316,6 +329,14 @@ def run(self):
316329# Test
317330################################################################################
318331
332+ bdist_wheel_options = {}
333+ if USE_LIMITED_API :
334+ bdist_wheel_options ["py_limited_api" ] = "cp312"
335+
336+ setup_opts = {}
337+ if bdist_wheel_options :
338+ setup_opts ["bdist_wheel" ] = bdist_wheel_options
339+
319340setuptools .setup (
320341 version = version_info .version ,
321342 ext_modules = ext_modules ,
0 commit comments