Skip to content
5 changes: 3 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

name: Build and Test, (Release)
name: Build and Test, Release

on:
push:
Expand All @@ -24,6 +24,7 @@ jobs:
CIBW_BEFORE_BUILD_LINUX: pip install protobuf
CIBW_BEFORE_BUILD_WINDOWS: python -m pip install protobuf
CIBW_BEFORE_BUILD_MACOS: pip install protobuf
CIBW_ENABLE: "cpython-freethreading"
CIBW_TEST_REQUIRES_LINUX: pytest pytest-xdist ruff mypy onnxruntime onnxscript
CIBW_TEST_REQUIRES_MACOS: pytest pytest-xdist
CIBW_TEST_REQUIRES_WINDOWS: pytest pytest-xdist
Expand All @@ -40,7 +41,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-24.04, windows-2022, macos-15]
python: ["cp312", "cp311", "cp310"] # because of abi3 we don't need to build separate whls for python 3.13, 3.14
python: ["cp313t", "cp312", "cp311", "cp310"] # because of abi3 we don't need to build separate whls for python 3.13, 3.14
steps:
- uses: actions/checkout@v6.0.1
with:
Expand Down
37 changes: 29 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,30 @@ def run(self):
# Extensions
################################################################################

py_limited_api = sys.version_info >= (3, 12)
if py_limited_api:
setup_opts = {
"bdist_wheel": {"py_limited_api": "cp312"},
}
else:
setup_opts = {}
################################################################################
# Extensions
################################################################################

# Enable limited ABI build
# nanobind supports limited ABI for Python 3.12 and later.
# https://blog.trailofbits.com/2022/11/15/python-wheels-abi-abi3audit/
# 1. The Py_LIMITED_API macro is defined in the extension
# 2. py_limited_api in Extension tags the extension as abi3
# 3. bdist_wheel options tag the wheel as abi3
NO_GIL = hasattr(sys, "_is_gil_enabled") and not sys._is_gil_enabled()
PY_312_OR_NEWER = sys.version_info >= (3, 12)
USE_LIMITED_API = not NO_GIL and PY_312_OR_NEWER and platform.system() != "FreeBSD"

macros = []
if USE_LIMITED_API:
macros.append(("Py_LIMITED_API", "0x030C0000"))

ext_modules = [
setuptools.Extension(
name="onnxoptimizer.onnx_opt_cpp2py_export", sources=[], py_limited_api=py_limited_api
name="onnxoptimizer.onnx_opt_cpp2py_export",
sources=[],
py_limited_api=USE_LIMITED_API,
define_macros=macros,
)
]

Expand All @@ -316,6 +329,14 @@ def run(self):
# Test
################################################################################

bdist_wheel_options = {}
if USE_LIMITED_API:
bdist_wheel_options["py_limited_api"] = "cp312"

setup_opts = {}
if bdist_wheel_options:
setup_opts["bdist_wheel"] = bdist_wheel_options

setuptools.setup(
version=version_info.version,
ext_modules=ext_modules,
Expand Down
Loading