Skip to content

Commit 1530700

Browse files
committed
Add cmake project.
Update python binding build env.
1 parent 80952c9 commit 1530700

25 files changed

+1609
-41
lines changed

.github/workflows/wheels.yml

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Build and upload to PyPI
2+
3+
# Build on every branch push, tag push, and pull request change:
4+
on: [push, pull_request]
5+
6+
jobs:
7+
8+
build_wheels:
9+
name: Build wheels on ${{ matrix.os }}
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
matrix:
13+
os: [ubuntu-latest, windows-latest, macos-latest]
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
fetch-tags: true # Optional, use if you use setuptools_scm
20+
21+
- name: Build wheels
22+
uses: pypa/cibuildwheel@v2.16.2
23+
# to supply options, put them in 'env', like:
24+
# env:
25+
# CIBW_SOME_OPTION: value
26+
# Disable building PyPy wheels on all platforms
27+
env:
28+
CIBW_ARCHS_MACOS: "x86_64 universal2 arm64"
29+
CIBW_ARCHS_WINDOWS: "AMD64 x86"
30+
# disable aarm64 build since its too slow to build(docker + qemu)
31+
CIBW_ARCHS_LINUX: "x86_64 i686"
32+
# it looks cibuildwheel fails to add version string to wheel file for python 3.6, so skip it
33+
CIBW_SKIP: pp*
34+
35+
- uses: actions/upload-artifact@v4
36+
with:
37+
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
38+
path: ./wheelhouse/*.whl
39+
40+
# It looks cibuildwheels did not clean build folder(CMake), and it results to Windows arm64 build failure(trying to reuse x86 build of .obj)
41+
# So supply separated build job for Windows ARM64 build
42+
# TODO: clean build folder using CIBW_BEFORE_ALL?
43+
build_win_arm64_wheels:
44+
name: Build ARM64 wheels on Windows.
45+
runs-on: windows-latest
46+
steps:
47+
- uses: actions/checkout@v4
48+
with:
49+
fetch-depth: 0
50+
fetch-tags: true # Optional, use if you use setuptools_scm
51+
52+
- name: Build wheels
53+
uses: pypa/cibuildwheel@v2.16.2
54+
# to supply options, put them in 'env', like:
55+
# env:
56+
# CIBW_SOME_OPTION: value
57+
# Disable building PyPy wheels on all platforms
58+
env:
59+
CIBW_ARCHS_WINDOWS: "ARM64"
60+
CIBW_SKIP: pp*
61+
62+
- uses: actions/upload-artifact@v4
63+
with:
64+
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
65+
path: ./wheelhouse/*.whl
66+
67+
make_sdist:
68+
name: Make SDist
69+
runs-on: ubuntu-latest
70+
steps:
71+
- uses: actions/checkout@v4
72+
with:
73+
fetch-depth: 0 # Optional, use if you use setuptools_scm
74+
fetch-tags: true # Optional, use if you use setuptools_scm
75+
76+
- name: Build SDist
77+
run: pipx run build --sdist
78+
79+
- uses: actions/upload-artifact@v4
80+
with:
81+
name: cibw-sdist
82+
path: dist/*.tar.gz
83+
84+
upload_all:
85+
needs: [build_wheels, build_wheels, make_sdist]
86+
runs-on: ubuntu-latest
87+
environment: release
88+
permissions:
89+
# IMPORTANT: this permission is mandatory for trusted publishing
90+
id-token: write
91+
# upload to PyPI on every tag starting with 'v'
92+
# NOTE: Without github.event_name & githug.ref check, `upload_all` task is still triggered on 'main' branch push.
93+
# (then get 'Branch "main" is not allowed to deploy to release due to environment protection rules.' error)
94+
# So still do event_name and github.ref check.
95+
# TODO: Make it work only using Github `environment` feature.
96+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
97+
# alternatively, to publish when a GitHub Release is created, use the following rule:
98+
# if: github.event_name == 'push' && github.event.action == 'published'
99+
steps:
100+
- uses: actions/download-artifact@v4
101+
with:
102+
pattern: cibw-*
103+
path: dist
104+
merge-multiple: true
105+
106+
- uses: pypa/gh-action-pypi-publish@release/v1
107+
with:
108+
# Use Trusted Publisher feature:
109+
# https://docs.pypi.org/trusted-publishers/
110+
# so no use of PYPI_API_TOKEN
111+
#password: ${{ secrets.PYPI_API_TOKEN }}
112+
#
113+
# Avoid race condition when using multiple CIs
114+
skip-existing: true
115+
verbose: true

CMakeLists.txt

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
set(EXE_TARGET "test_dng_loader")
4+
set(PY_TARGET "tinydng_ext")
5+
project(${EXE_TARGET} CXX)
6+
7+
option(TINYDNG_WITH_PYTHON "Build Python module(For developer)." Off)
8+
option(
9+
TINYDNG_PREFER_LOCAL_PYTHON_INSTALLATION
10+
"Prefer locally-installed Python interpreter than system or conda/brew installed Python. Please specify your Python interpreter with `Python3_EXECUTABLE` cmake option if you enable this option."
11+
OFF)
12+
13+
14+
# cmake modules
15+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
16+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/sanitizers)
17+
find_package(Sanitizers) # Address sanitizer (-DSANITIZE_ADDRESS=ON)
18+
19+
# Build standalone .so(for developer)
20+
if (TINYDNG_WITH_PYTHON)
21+
22+
if(TINYDNG_PREFER_LOCAL_PYTHON_INSTALLATION)
23+
#message(STATUS "Local Python")
24+
set(Python3_FIND_FRAMEWORK NEVER) # Do not search framework python
25+
set(Python3_FIND_STRATEGY LOCATION)
26+
set(Python3_FIND_REGISTRY NEVER) # Windows only
27+
else()
28+
set(Python3_FIND_FRAMEWORK LAST
29+
)# Prefer Brew/Conda to Apple framework python
30+
endif()
31+
32+
find_package(
33+
Python3
34+
COMPONENTS Interpreter Development
35+
REQUIRED)
36+
37+
find_package(pybind11 CONFIG)
38+
39+
endif()
40+
41+
42+
set(CMAKE_CXX_STANDARD 11)
43+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
44+
set(CMAKE_CXX_EXTENSIONS OFF)
45+
46+
add_executable(${EXE_TARGET}
47+
test_loader.cc
48+
)
49+
add_sanitizers(${EXE_TARGET})
50+
51+
if (TINYDNG_WITH_PYTHON)
52+
# pybind11 method:
53+
pybind11_add_module(${PY_TARGET} python/python-bindings.cc)
54+
55+
# repo top directory.
56+
target_include_directories(${PY_TARGET} PRIVATE ${CMAKE_SOURCE_DIR})
57+
58+
# copy .so to python/tinydng
59+
add_custom_command(
60+
TARGET ${PY_TARGET}
61+
POST_BUILD
62+
COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:${PY_TARGET}>"
63+
"${CMAKE_SOURCE_DIR}/python/tinydng/$<TARGET_FILE_NAME:${PY_TARGET}>"
64+
COMMENT "copying tinydng python native module file to python/tinydng/"
65+
VERBATIM)
66+
67+
endif()
68+
69+
# [VisualStudio]
70+
if(WIN32)
71+
# Set ${EXE_TARGET} as a startup project for VS IDE
72+
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${EXE_TARGET})
73+
74+
# For easier debugging in VS IDE(cmake 3.8.0 or later required) Set working
75+
# directory where CMakeLists.txt is placed.
76+
if(CMAKE_VERSION VERSION_GREATER 3.8.0)
77+
set_target_properties(
78+
${EXE_TARGET} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY
79+
"${CMAKE_CURRENT_SOURCE_DIR}")
80+
endif()
81+
endif()

MANIFEST.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include tiny_dng_loader.h
22
include tiny_dng_writer.h
3-
include python-bindings.cc
3+
include python/python-bindings.cc
4+
include python/tinydng/__init__.py
45
include pyproject.toml
56
include setup.py
67
include README.md

bootstrap-cmake-linux-with-python.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
curdir=`pwd`
2+
3+
builddir=${curdir}/build_cmake_with_python
4+
5+
rm -rf ${builddir}
6+
mkdir ${builddir}
7+
8+
# set path to pybind11
9+
# If you install pybind11 through pip, its usually installed to <site-package path>/pybind11.
10+
pybind11_path=`python -c "import site; print (site.getsitepackages()[0])"`
11+
echo ${pybind11_path}
12+
13+
CC=clang CXX=clang++ \
14+
pybind11_DIR=${pybind11_path}/pybind11 \
15+
cmake \
16+
-B${builddir} \
17+
-DCMAKE_VERBOSE_MAKEFILE=1 \
18+
-DTINYDNG_WITH_PYTHON=1 \

bootstrap-cmake-linux.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
curdir=`pwd`
2+
3+
builddir=${curdir}/build_cmake
4+
5+
rm -rf ${builddir}
6+
mkdir ${builddir}
7+
8+
CC=clang CXX=clang++ cmake \
9+
-B${builddir} \
10+
-DSANITIZE_ADDRESS=0 \
11+
-DCMAKE_VERBOSE_MAKEFILE=1

cmake/ClangClCMakeCompileRules.cmake

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# macOS paths usually start with /Users/*. Unfortunately, clang-cl interprets
2+
# paths starting with /U as macro undefines, so we need to put a -- before the
3+
# input file path to force it to be treated as a path. CMake's compilation rules
4+
# should be tweaked accordingly, but until that's done, and to support older
5+
# CMake versions, overriding compilation rules works well enough. This file will
6+
# be included by cmake after the default compilation rules have already been set
7+
# up, so we can just modify them instead of duplicating them entirely.
8+
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")
9+
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT}")

cmake/aarch64-linux-gnu.toolchain

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set(CMAKE_SYSTEM_NAME Linux)
2+
set(CMAKE_SYSTEM_PROCESSOR aarch64)
3+
set(CMAKE_C_COMPILER_TARGET aarch64-linux-gnu)
4+
5+
set(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu/)
6+
7+
# Sync with GitHub Actions config
8+
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
9+
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
10+
11+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
12+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
13+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
14+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

0 commit comments

Comments
 (0)