Skip to content

Commit

Permalink
Streamline Python wheel workflow, and add tests and a CMake setup for…
Browse files Browse the repository at this point in the history
… bindings (#1517)

- Remove the libdeflate patch, not actually necessary
- Add a CMake build setup, controlled ``OPENEXR_BUILD_PYTHON``, off by default
- Update the doc strings
- Add ``#define PY_SSIZE_T_CLEAN``, apparently required in Python 3.10
  for ``Py_BuildValue("s#")``. PreviewImage attributes crash
  otherwise.
- Add a more extensive unit test
- Disable building PyPy wheels, since the bindings don't work properly
  (the ``channels()`` method fails in ``test_unittests.py`` when built
  against PyPy)

Signed-off-by: Cary Phillips <[email protected]>
  • Loading branch information
cary-ilm committed Aug 15, 2023
1 parent aed455f commit 318536c
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 82 deletions.
96 changes: 31 additions & 65 deletions .github/workflows/python-wheels.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) Contributors to the OpenEXR Project.
#

name: Python wheels

on:
push:
branches: [ $default-branch, main, python ]
branches-ignore:
- RB-2.*
tags-ignore:
- v1.*
- v2.*
paths:
- '**'
- '!**.md'
- '!website/**'
- 'website/src/**'
- '!bazel/**'
pull_request:
branches-ignore:
- RB-2.*
tags-ignore:
- v1.*
- v2.*
paths:
- '**'
- '!**.md'
- '!website/**'
- 'website/src/**'
- '!bazel/**'

jobs:
build_wheels:
Expand All @@ -12,30 +38,13 @@ jobs:
matrix:
os: [ubuntu-22.04, windows-latest, macOS-latest]
env:
# Skip 32-bit wheels builds
CIBW_SKIP: "*-win32 *_i686"
# Skip 32-bit wheels builds.
# Also skip the PyPy builds, since they fail the unittests
CIBW_SKIP: "*-win32 *_i686 pp*"
CIBW_BEFORE_BUILD: >
echo "Installing Zlib..." &&
cd zlib.build &&
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../openexr.install -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_PREFIX_PATH=../openexr.install -DCMAKE_INSTALL_LIBDIR=lib ../zlib &&
cmake --build ./ --config Release --clean-first &&
cmake --install ./ --config Release &&
cd .. &&
echo "Installing libDeflate..." &&
cd libdeflate.build &&
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../openexr.install -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_PREFIX_PATH=../openexr.install -DCMAKE_INSTALL_LIBDIR=lib -DLIBDEFLATE_BUILD_SHARED_LIB=OFF -DLIBDEFLATE_USE_SHARED_LIB=OFF -DBUILD_SHARED_LIBS=OFF ../libdeflate &&
cmake --build ./ --config Release --clean-first &&
cmake --install ./ --config Release &&
cd .. &&
echo "Installing Imath-3.1.9..." &&
cd imath.build &&
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../openexr.install -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_PREFIX_PATH=../openexr.install -DCMAKE_INSTALL_LIBDIR=lib -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=OFF ../imath &&
cmake --build ./ --config Release --clean-first &&
cmake --install ./ --config Release &&
cd .. &&
echo "Installing OpenEXR..." &&
cd openexr.build &&
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../openexr.install -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_PREFIX_PATH=../openexr.install -DCMAKE_INSTALL_LIBDIR=lib -DBUILD_TESTING=OFF -DOPENEXR_INSTALL_EXAMPLES=OFF -DBUILD_SHARED_LIBS=OFF -DOPENEXR_FORCE_INTERNAL_DEFLATE=ON ../ &&
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../openexr.install -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_PREFIX_PATH=../openexr.install -DCMAKE_INSTALL_LIBDIR=lib -DBUILD_TESTING=OFF -DOPENEXR_INSTALL_EXAMPLES=OFF -DOPENEXR_BUILD_TOOLS=OFF -DBUILD_SHARED_LIBS=OFF -DOPENEXR_FORCE_INTERNAL_DEFLATE=ON -DOPENEXR_FORCE_INTERNAL_IMATH=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON ../ &&
cmake --build ./ --config Release --clean-first &&
cmake --install ./ --config Release &&
cd ..
Expand All @@ -61,52 +70,9 @@ jobs:
- name: Create folders
run: |
mkdir -p ${{github.workspace}}/zlib.build
mkdir -p ${{github.workspace}}/libdeflate.build
mkdir -p ${{github.workspace}}/imath.build
mkdir -p ${{github.workspace}}/openexr.build
mkdir -p ${{github.workspace}}/openexr.install
- name: download Zlib source code
uses: suisei-cn/[email protected]
with:
url: https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz
target: ${{github.workspace}}/

- name: Extract Zlib
run: |
tar -xvzf zlib-1.2.13.tar.gz -C ${{github.workspace}}/
mv zlib-1.2.13 zlib
rm zlib-1.2.13.tar.gz
- name: download libDeflate source code
uses: suisei-cn/[email protected]
with:
url: https://github.com/ebiggers/libdeflate/archive/refs/tags/v1.18.tar.gz
target: ${{github.workspace}}/

- name: Extract libDeflate
run: |
tar -xvzf v1.18.tar.gz -C ${{github.workspace}}/
mv libdeflate-1.18 libdeflate
rm v1.18.tar.gz
- name: Patch libDeflate
run: |
patch -u libdeflate/CMakeLists.txt -i ${{github.workspace}}/src/wrappers/python/libdeflate.patch
- name: download Imath source code
uses: suisei-cn/[email protected]
with:
url: https://github.com/AcademySoftwareFoundation/Imath/archive/refs/tags/v3.1.9.tar.gz
target: ${{github.workspace}}/

- name: Extract Imath
run: |
tar -xvzf v3.1.9.tar.gz -C ${{github.workspace}}/
mv Imath-3.1.9 imath
rm v3.1.9.tar.gz
- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse

Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,7 @@ if (NOT OPENEXR_IS_SUBPROJECT)
add_subdirectory(website/src)
endif()

option(OPENEXR_BUILD_PYTHON "Set ON to build python bindings")
if (OPENEXR_BUILD_PYTHON AND NOT OPENEXR_IS_SUBPROJECT)
add_subdirectory(src/wrappers/python)
endif()
29 changes: 29 additions & 0 deletions src/wrappers/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright Contributors to the OpenEXR Project.

if(NOT "${CMAKE_PROJECT_NAME}" STREQUAL "OpenEXR")
cmake_minimum_required(VERSION 3.12)
project(PyOpenEXR)
find_package(OpenEXR)
endif()

get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()

add_library (PyOpenEXR SHARED OpenEXR.cpp)

set (Python_ADDITIONAL_VERSIONS 3)
find_package (PythonLibs REQUIRED)
find_package (PythonInterp REQUIRED)

include_directories ("${PYTHON_INCLUDE_DIRS}")

set_target_properties (PyOpenEXR PROPERTIES PREFIX "")
set_target_properties (PyOpenEXR PROPERTIES OUTPUT_NAME "OpenEXR")
set_target_properties (PyOpenEXR PROPERTIES SUFFIX ".so")

target_link_libraries (PyOpenEXR "${PYTHON_LIBRARIES}" OpenEXR::OpenEXR)

3 changes: 2 additions & 1 deletion src/wrappers/python/OpenEXR.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) DreamWorks Animation LLC and Contributors of the OpenEXR Project
// Copyright (c) Contributors to the OpenEXR Project.
//

#define PY_SSIZE_T_CLEAN // required for Py_BuildValue("s#") for Python 3.10
#include <Python.h>

#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
Expand Down
26 changes: 10 additions & 16 deletions src/wrappers/python/setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) Contributors to the OpenEXR Project.

from setuptools import setup, Extension
import os
import platform
import re


DESC = """Python bindings for ILM's OpenEXR image file format.
DESC = """Python bindings for the OpenEXR image file format.
This is a script to autobuild the wheels using github actions. Please, do not
use it manually
If you detect any problem, please feel free to report the issue on the GitHub
page:
https://github.com/sanguinariojoe/pip-openexr/issues
https://github.com/AcademySoftwareFoundation/openexr/issues
"""


Expand All @@ -29,19 +26,16 @@
version = f"{version_major}.{version_minor}.{version_patch}"

libs=[]
libs_static=['z',
f'OpenEXR-{version_major}_{version_minor}',
libs_static=[f'OpenEXR-{version_major}_{version_minor}',
f'IlmThread-{version_major}_{version_minor}',
f'Iex-{version_major}_{version_minor}',
f'Imath-3_1',
f'OpenEXRCore-{version_major}_{version_minor}',
f'deflate']
f'Imath-{version_major}_{version_minor}',
f'OpenEXRCore-{version_major}_{version_minor}'
]
definitions = [('PYOPENEXR_VERSION_MAJOR', f'{version_major}'),
('PYOPENEXR_VERSION_MINOR', f'{version_minor}'),
('PYOPENEXR_VERSION_PATCH', f'{version_patch}'),]
if platform.system() == "Windows":
libs_static[0] = 'zlibstatic'
libs_static[-1] = 'deflatestatic'
definitions = [('PYOPENEXR_VERSION', f'\\"{version}\\"')]
extra_compile_args = []
if platform.system() == 'Darwin':
Expand All @@ -65,10 +59,10 @@


setup(name='OpenEXR',
author = 'James Bowman',
author_email = 'jamesb@excamera.com',
url = 'https://github.com/sanguinariojoe/pip-openexr',
description = "Python bindings for ILM's OpenEXR image file format",
author = 'Contributors to the OpenEXR Project',
author_email = 'info@openexr.com',
url = 'https://github.com/AcademySoftwareFoundation/openexr',
description = "Python bindings for the OpenEXR image file format",
long_description = DESC,
version=version,
ext_modules=[
Expand Down
Loading

0 comments on commit 318536c

Please sign in to comment.