Skip to content

Commit f1ae333

Browse files
Add cibuildwheel example (#30)
* add cibuildwheel example * Potential fix for code scanning alert no. 8: Workflow does not contain permissions Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * wip * wip * wip * wip * wip * fix * wip * use CONAN_PY_BUILD_PROFILE_AUTODETECT * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * simplify * remove unused * wip * check with cmake require * minor changes * add version * wip * fix --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent cce7420 commit f1ae333

15 files changed

Lines changed: 308 additions & 1 deletion

File tree

.github/workflows/cibw-example.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: Example – cibuildwheel + conan-py-build
2+
3+
permissions:
4+
contents: read
5+
6+
on:
7+
workflow_dispatch:
8+
push:
9+
branches: [main]
10+
pull_request:
11+
branches: [main]
12+
13+
jobs:
14+
# Build conan-py-build wheel and upload as artifact for build-wheels jobs.
15+
backend-wheel:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
persist-credentials: false
21+
- uses: actions/setup-python@v5
22+
with:
23+
python-version: "3.12"
24+
- run: pip install build && python -m build --wheel --outdir backend-dist .
25+
- uses: actions/upload-artifact@v4
26+
with:
27+
name: conan-py-build-wheel
28+
path: backend-dist/*.whl
29+
30+
build-wheels:
31+
needs: backend-wheel
32+
name: Build wheels on ${{ matrix.os }}
33+
runs-on: ${{ matrix.os }}
34+
strategy:
35+
fail-fast: false
36+
matrix:
37+
include:
38+
- os: ubuntu-latest
39+
arch: x86_64
40+
profile: linux-x86_64
41+
- os: ubuntu-24.04-arm
42+
arch: aarch64
43+
profile: linux-aarch64
44+
- os: macos-14
45+
arch: arm64
46+
profile: macos-arm64
47+
- os: macos-14
48+
arch: x86_64
49+
profile: macos-x86_64.jinja
50+
- os: windows-latest
51+
arch: AMD64
52+
profile: windows-x86_64
53+
54+
env:
55+
CIBW_ARCHS_LINUX: ${{ matrix.arch }}
56+
CIBW_ARCHS_MACOS: ${{ matrix.arch }}
57+
CIBW_ARCHS_WINDOWS: ${{ matrix.arch }}
58+
CIBW_CONFIG_SETTINGS: "host-profile=./profiles/${{ matrix.profile }}"
59+
CIBW_BEFORE_BUILD: "pip install backend-dist/*.whl"
60+
CIBW_BUILD_VERBOSITY: 3
61+
steps:
62+
- uses: actions/checkout@v4
63+
with:
64+
persist-credentials: false
65+
66+
- uses: actions/download-artifact@v4
67+
with:
68+
name: conan-py-build-wheel
69+
70+
# CIBW_BEFORE_BUILD installs the backend from backend-dist/*.whl
71+
- name: Place backend wheel inside example project
72+
shell: bash
73+
run: |
74+
mkdir -p examples/cibw-example/backend-dist
75+
mv *.whl examples/cibw-example/backend-dist/
76+
ls -la examples/cibw-example/backend-dist/
77+
78+
# On Linux, cibuildwheel runs in Docker and only gets files from the repo copy
79+
# (e.g. git archive), so backend-dist is not there. Mount it so before_build can pip install.
80+
- name: Set container engine to mount backend-dist (Linux only)
81+
if: runner.os == 'Linux'
82+
shell: bash
83+
run: |
84+
echo "CIBW_CONTAINER_ENGINE=docker; create_args: -v ${{ github.workspace }}/examples/cibw-example/backend-dist:/project/backend-dist" >> $GITHUB_ENV
85+
86+
# Export cpython-portable to Conan cache so it's available for the build.
87+
- name: Export cpython-portable
88+
shell: bash
89+
run: |
90+
pip install conan
91+
conan profile detect --force
92+
for ver in 3.9.20 3.10.19 3.11.14 3.12.12 3.13.11; do
93+
conan export recipes/cpython --version=$ver
94+
done
95+
96+
- name: Set CIBW_BEFORE_BUILD to absolute path (macOS and Windows)
97+
if: runner.os != 'Linux'
98+
shell: bash
99+
run: |
100+
whl=$(ls "${{ github.workspace }}/examples/cibw-example/backend-dist/"*.whl 2>/dev/null | head -1)
101+
echo "CIBW_BEFORE_BUILD=pip install $whl" >> $GITHUB_ENV
102+
103+
- name: Build wheels
104+
uses: pypa/cibuildwheel@v3.4.0
105+
with:
106+
package-dir: examples/cibw-example
107+
output-dir: wheelhouse
108+
109+
- uses: actions/upload-artifact@v4
110+
with:
111+
name: cibw-wheels-${{ matrix.os }}-${{ matrix.arch }}
112+
path: wheelhouse/*.whl

examples/cibw-example/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
backend-dist/
2+
wheelhouse/
3+
*.whl
4+
conan-py-build.profile
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(myadder_cibw LANGUAGES CXX)
3+
4+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
5+
set(PYBIND11_FINDPYTHON ON)
6+
find_package(pybind11 CONFIG REQUIRED)
7+
find_package(fmt REQUIRED)
8+
9+
pybind11_add_module(_core src/myadder.cpp)
10+
target_link_libraries(_core PRIVATE pybind11::module fmt::fmt)
11+
12+
if(APPLE)
13+
set_target_properties(_core PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
14+
endif()
15+
16+
install(TARGETS _core DESTINATION myadder_pybind11)

examples/cibw-example/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example: cibuildwheel + conan-py-build
2+
3+
This example is based on **[basic-pybind11](../basic-pybind11/)** (pybind11 extension). It adds **cibuildwheel** to build wheels on multiple platforms.

examples/cibw-example/conanfile.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from conan import ConanFile
2+
from conan.tools.cmake import CMake, cmake_layout
3+
4+
5+
class MyadderCibwConan(ConanFile):
6+
name = "myadder-cibw"
7+
version = "0.1.0"
8+
settings = "os", "compiler", "build_type", "arch"
9+
generators = "CMakeToolchain", "CMakeDeps"
10+
11+
def layout(self):
12+
cmake_layout(self)
13+
14+
def requirements(self):
15+
self.requires("pybind11/3.0.1")
16+
self.requires("fmt/12.1.0")
17+
18+
def build(self):
19+
cmake = CMake(self)
20+
cmake.configure()
21+
cmake.build()
22+
23+
def package(self):
24+
cmake = CMake(self)
25+
cmake.install()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include(default)
2+
[settings]
3+
arch=armv8
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include(default)
2+
[settings]
3+
arch=x86_64
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include(default)
2+
[settings]
3+
arch=armv8
4+
os.version=11.0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% set py_ver = os.environ["CONAN_CPYTHON_VERSION"] %}
2+
{% set py_tag = "cp" + "".join(py_ver.split(".")[:2]) %}
3+
{% set cpython_ref = "cpython-portable/" + py_ver %}
4+
include(default)
5+
6+
[settings]
7+
arch=x86_64
8+
os.version=11.0
9+
10+
[tool_requires]
11+
cmake/[>3.29]
12+
&:{{ cpython_ref }}
13+
14+
[buildenv]
15+
WHEEL_PYVER={{ py_tag }}
16+
WHEEL_ABI={{ py_tag }}
17+
WHEEL_ARCH=macosx_11_0_x86_64
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include(default)
2+
[settings]
3+
arch=x86_64

0 commit comments

Comments
 (0)