Skip to content

Commit 7fcbcc7

Browse files
committed
Use cibuildwheels to build wheels instead of custom code
1 parent 5bf9470 commit 7fcbcc7

File tree

6 files changed

+74
-60
lines changed

6 files changed

+74
-60
lines changed

.github/workflows/build-wheels.yml

+40-52
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,64 @@ on:
1010
jobs:
1111
build-wheels:
1212
runs-on: ${{ matrix.os }}
13-
name: ${{ matrix.name }}
13+
name: ${{ matrix.os }}
1414
strategy:
1515
matrix:
16-
include:
17-
- name: x86_64 macOS
18-
os: macos-11
19-
platform-name: macosx-10.9-x86_64
20-
- name: M1 macOS
21-
os: macos-11
22-
platform-name: macosx-11.0-arm64
23-
# TODO: add a Windows builder
16+
# TODO: add windows builder
17+
os: [ubuntu-20.04, macos-11]
18+
2419
steps:
2520
- uses: actions/checkout@v3
2621
with:
2722
fetch-depth: 0
23+
2824
- name: Set up Python
2925
uses: actions/setup-python@v4
3026
with:
3127
python-version: "3.10"
32-
- name: build wheel
33-
env:
34-
MACOS_DEPLOYMENT_TARGET: "11.0"
35-
run: |
36-
python -m pip install wheel cmake torch
37-
python setup.py bdist_wheel --plat-name ${{ matrix.platform-name }}
38-
cd sphericart-torch
39-
python setup.py bdist_wheel --plat-name ${{ matrix.platform-name }}
4028

41-
# build sphericart-torch wheels on CI, but do no include them in the
42-
# uploaded wheels (see justification below in the Linux builder)
43-
# mv dist/*.whl ../dist/
44-
- uses: actions/upload-artifact@v3
45-
with:
46-
name: wheels
47-
path: dist/*.whl
48-
- name: upload wheel to GitHub release
49-
if: startsWith(github.ref, 'refs/tags/')
50-
uses: softprops/action-gh-release@v1
51-
with:
52-
files: dist/*.whl
53-
env:
54-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
- name: Install cibuildwheel
30+
run: python -m pip install cibuildwheel build
5531

56-
build-manylinux-wheels:
57-
runs-on: ubuntu-20.04
58-
# TODO: add other arch for linux?
59-
name: x86_64 manylinux
60-
steps:
61-
- uses: actions/checkout@v3
62-
with:
63-
fetch-depth: 0
64-
- name: build wheel in docker
32+
- name: Build sphericart wheels
6533
run: |
66-
docker run --rm -v $(pwd):/code quay.io/pypa/manylinux2014_x86_64 bash -c "cd /code && /opt/python/cp38-cp38/bin/python -m pip wheel --no-deps -w dist . --verbose"
67-
docker run --rm -v $(pwd):/code quay.io/pypa/manylinux2014_x86_64 bash -c "cd /code/sphericart-torch && /opt/python/cp38-cp38/bin/python -m pip wheel --no-deps -w dist . --verbose --extra-index-url=https://download.pytorch.org/whl/cpu"
68-
- name: run auditwheel in docker
34+
# ensure we build the wheel from the sdist, not the checkout
35+
python -m build --sdist . --outdir dist
36+
python -m cibuildwheel dist/*.tar.gz --output-dir dist
37+
env:
38+
# build wheels on CPython 3.10
39+
CIBW_BUILD: cp310-*
40+
# skip musl and 32-bit builds
41+
CIBW_SKIP: "*-musllinux* *-win32 *-manylinux_i686"
42+
# on macOS, build both Intel & Apple Silicon versions
43+
CIBW_ARCHS_MACOS: x86_64 arm64
44+
CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
45+
46+
- name: Build sphericart-torch wheels
6947
run: |
70-
docker run --rm -v $(pwd):/code quay.io/pypa/manylinux2014_x86_64 bash -c "auditwheel repair /code/dist/*.whl -w /code/dist"
71-
docker run --rm -v $(pwd):/code quay.io/pypa/manylinux2014_x86_64 bash -c "auditwheel repair --exclude libtorch.so --exclude libtorch_cpu.so --exclude libc10.so /code/sphericart-torch/dist/*.whl -w /code/dist"
72-
- name: remove wheel with wrong tag
73-
run: sudo rm dist/*linux_x86_64.whl
74-
- name: remove sphericart-torch wheels
75-
# these wheels are only built for one Python version and one PyTorch
76-
# version, so we should not upload them since they won't be usable by
77-
# most users
78-
run: sudo rm dist/sphericart_torch*.whl
48+
# ensure we build the wheel from the sdist, not the checkout
49+
python -m build --sdist sphericart-torch --outdir sphericart-torch/dist
50+
python -m cibuildwheel sphericart-torch/dist/*.tar.gz --output-dir sphericart-torch/dist
51+
env:
52+
CIBW_BUILD: cp310-*
53+
CIBW_SKIP: "*-musllinux* *-win32 *-manylinux_i686"
54+
# we can not build wheels for macos-arm64, since the host is always
55+
# x86_64, and we assume we can link against the host version of libtorch
56+
CIBW_ARCHS_MACOS: x86_64
57+
CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
58+
# Use the CPU only version of torch when building/running the code
59+
CIBW_ENVIRONMENT: PIP_EXTRA_INDEX_URL=https://download.pytorch.org/whl/cpu
60+
# do not complain for missing libtorch.so in sphericart-torch wheel
61+
CIBW_REPAIR_WHEEL_COMMAND_MACOS: |
62+
delocate-wheel --ignore-missing-dependencies --require-archs {delocate_archs} -w {dest_dir} -v {wheel}
63+
CIBW_REPAIR_WHEEL_COMMAND_LINUX: |
64+
auditwheel repair --exclude libtorch.so --exclude libtorch_cpu.so --exclude libc10.so -w {dest_dir} {wheel}
65+
7966
- uses: actions/upload-artifact@v3
8067
with:
8168
name: wheels
8269
path: dist/*.whl
70+
8371
- name: upload wheel to GitHub release
8472
if: startsWith(github.ref, 'refs/tags/')
8573
uses: softprops/action-gh-release@v1

MANIFEST.in

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
recursive-include sphericart *
2+
3+
include pyproject.toml
4+
include README.md
5+
include LICENSE

setup.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ def run(self):
3838
cmake_options = [
3939
f"-DCMAKE_INSTALL_PREFIX={install_dir}",
4040
"-DBUILD_SHARED_LIBS=ON",
41-
"-DSPHERICART_BUILD_EXAMPLES=OFF",
4241
"-DSPHERICART_BUILD_TESTS=OFF",
4342
]
4443

44+
if sys.platform.startswith("darwin"):
45+
cmake_options.append("-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=11.0")
46+
47+
# ARCHFLAGS is used by cibuildwheel to pass the requested arch to the
48+
# compilers
49+
ARCHFLAGS = os.environ.get("ARCHFLAGS")
50+
if ARCHFLAGS is not None:
51+
cmake_options.append(f"-DCMAKE_C_FLAGS={ARCHFLAGS}")
52+
cmake_options.append(f"-DCMAKE_CXX_FLAGS={ARCHFLAGS}")
53+
4554
subprocess.run(
4655
["cmake", source_dir, *cmake_options],
4756
cwd=build_dir,

sphericart-torch/MANIFEST.in

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ recursive-include sphericart *
66

77
recursive-include src *
88
recursive-include include *
9+
10+
include pyproject.toml
11+
include README.md

sphericart-torch/setup.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import subprocess
33
import sys
44

5-
import cmake
65
from setuptools import Extension, setup
76
from setuptools.command.bdist_egg import bdist_egg
87
from setuptools.command.build_ext import build_ext
@@ -27,14 +26,23 @@ def run(self):
2726
f"-DPYTHON_EXECUTABLE={sys.executable}",
2827
]
2928

30-
CMAKE_EXE = os.path.join(cmake.CMAKE_BIN_DIR, "cmake")
29+
if sys.platform.startswith("darwin"):
30+
cmake_options.append("-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=11.0")
31+
32+
# ARCHFLAGS is used by cibuildwheel to pass the requested arch to the
33+
# compilers
34+
ARCHFLAGS = os.environ.get("ARCHFLAGS")
35+
if ARCHFLAGS is not None:
36+
cmake_options.append(f"-DCMAKE_C_FLAGS={ARCHFLAGS}")
37+
cmake_options.append(f"-DCMAKE_CXX_FLAGS={ARCHFLAGS}")
38+
3139
subprocess.run(
32-
[CMAKE_EXE, source_dir, *cmake_options],
40+
["cmake", source_dir, *cmake_options],
3341
cwd=build_dir,
3442
check=True,
3543
)
3644
subprocess.run(
37-
[CMAKE_EXE, "--build", build_dir, "--target", "install"],
45+
["cmake", "--build", build_dir, "--target", "install"],
3846
check=True,
3947
)
4048

sphericart/CMakeLists.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ add_library(sphericart
3737
)
3838

3939
set_target_properties(sphericart PROPERTIES
40-
VERSION "0.1.0"
41-
SOVERSION "0.1"
40+
VERSION ${SPHERICART_VERSION}
41+
SOVERSION ${SPHERICART_VERSION_MAJOR}.${SPHERICART_VERSION_MINOR}
4242
POSITION_INDEPENDENT_CODE ON
4343
)
4444

@@ -85,7 +85,9 @@ else()
8585
endif()
8686

8787
check_cxx_compiler_flag("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
88-
if(COMPILER_SUPPORTS_MARCH_NATIVE)
88+
# for some reason COMPILER_SUPPORTS_MARCH_NATIVE is true with Apple clang,
89+
# but then fails with `the clang compiler does not support '-march=native'`
90+
if(COMPILER_SUPPORTS_MARCH_NATIVE AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
8991
message(STATUS "march=native is enabled")
9092
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
9193
endif()

0 commit comments

Comments
 (0)