From a481e2eab94a9b463188cd6baadfb3d40e4a5b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Achard?= Date: Sat, 8 Jul 2023 10:19:00 +0200 Subject: [PATCH] Fix symlinks and make doc optional --- .github/workflows/wheel_workflow.yml | 6 +- CMakeLists.txt | 1 + setup.py | 97 +++++++++++++--------------- src/OpenColorIO/CMakeLists.txt | 11 +++- tests/data/__init__.py | 0 5 files changed, 56 insertions(+), 59 deletions(-) delete mode 100644 tests/data/__init__.py diff --git a/.github/workflows/wheel_workflow.yml b/.github/workflows/wheel_workflow.yml index 187ecb330b..702243b789 100644 --- a/.github/workflows/wheel_workflow.yml +++ b/.github/workflows/wheel_workflow.yml @@ -128,7 +128,7 @@ jobs: platforms: all - name: Build wheels - uses: pypa/cibuildwheel@v2.12.0 + uses: pypa/cibuildwheel@v2.13.1 env: CIBW_BUILD: ${{ matrix.python }} CIBW_ARCHS: ${{ matrix.arch }} @@ -194,7 +194,7 @@ jobs: python-version: '3.8' - name: Build wheels - uses: pypa/cibuildwheel@v2.12.0 + uses: pypa/cibuildwheel@v2.13.1 env: CIBW_BUILD: ${{ matrix.python }} CIBW_ARCHS: ${{ matrix.arch }} @@ -245,7 +245,7 @@ jobs: python-version: '3.8' - name: Build wheels - uses: pypa/cibuildwheel@v2.12.0 + uses: pypa/cibuildwheel@v2.13.1 env: CIBW_BUILD: ${{ matrix.python }} CIBW_ARCHS: ${{ matrix.arch }} diff --git a/CMakeLists.txt b/CMakeLists.txt index cdc283c2d4..e3fee2be6d 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,6 +161,7 @@ endif() # Other preferences option(OCIO_VERBOSE "Display more information when searching or installing dependencies" OFF) +option(OCIO_USE_SOVERSION "Enable versioning in OCIO shared library name" ON) ############################################################################### # Warnings / debugging settings diff --git a/setup.py b/setup.py index e584c0a1a6..1d67a70f24 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,6 @@ import os import re -import shutil import subprocess import sys import tempfile @@ -16,60 +15,49 @@ # Extract the project version from CMake generated ABI header. -# NOTE: When droping support for Python2 we can use -# tempfile.TemporaryDirectory() context manager instead of try...finally. def get_version(): VERSION_REGEX = re.compile( r"^\s*#\s*define\s+OCIO_VERSION_FULL_STR\s+\"(.*)\"\s*$", re.MULTILINE) here = os.path.abspath(os.path.dirname(__file__)) - dirpath = tempfile.mkdtemp() - - try: - stdout = subprocess.check_output(["cmake", here], cwd=dirpath) - path = os.path.join(dirpath, "include", "OpenColorIO", "OpenColorABI.h") - with open(path) as f: - match = VERSION_REGEX.search(f.read()) - return match.group(1) - except Exception as e: - raise RuntimeError( - "Unable to find OpenColorIO version: {}".format(str(e)) - ) - finally: - shutil.rmtree(dirpath) - - -# Remove symlinks as Python wheels do not support them at the moment. -# Rename the remaining dylib to the expected install name (major.minor). -def patch_symlink(folder): - if sys.platform.startswith("darwin"): - VERSION_REGEX = re.compile( - r"^libOpenColorIO.(?P\d+).(?P\d+).\d+.dylib$") - NAME_PATTERN = "libOpenColorIO.{major}.{minor}.dylib" - elif sys.platform.startswith("linux"): - VERSION_REGEX = re.compile( - r"^libOpenColorIO.so.(?P\d+).(?P\d+).\d+$") - NAME_PATTERN = "libOpenColorIO.so.{major}.{minor}" - else: - return - - # First remove all symlinks in the folder - for f in os.listdir(folder): - filepath = os.path.join(folder, f) - if os.path.islink(filepath): - os.remove(filepath) - - # Then match the remaining OCIO dynamic lib and rename it - for f in os.listdir(folder): - filepath = os.path.join(folder, f) - match = VERSION_REGEX.search(f) - if match: - res = match.groupdict() - new_filename = NAME_PATTERN.format( - major=res["major"], minor=res["minor"]) - new_filepath = os.path.join(folder, new_filename) - os.rename(filepath, new_filepath) - break + + with tempfile.TemporaryDirectory() as tmpdir: + try: + subprocess.check_call(["cmake", here], cwd=tmpdir) + path = os.path.join(tmpdir, "include", "OpenColorIO", "OpenColorABI.h") + with open(path) as f: + match = VERSION_REGEX.search(f.read()) + return match.group(1) + except Exception as e: + raise RuntimeError( + "Unable to find OpenColorIO version: {}".format(str(e)) + ) + + +# Call CMake find_package from a dummy script and return whether the package +# has been found or not. +def cmake_find_package(package_name): + with tempfile.TemporaryDirectory() as tmpdir: + try: + cmakelist_path = os.path.join(tmpdir, "CMakeLists.txt") + with open(cmakelist_path, "w") as f: + f.write(""" +cmake_minimum_required(VERSION 3.13) +project(test LANGUAGES CXX) + +find_package({} REQUIRED) +""".format(package_name) + ) + + subprocess.check_call( + ["cmake", tmpdir], + cwd=tmpdir, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL + ) + return True + except Exception as e: + return False # Convert distutils Windows platform specifiers to CMake -A arguments @@ -112,7 +100,7 @@ def build_extension(self, ext): # Not used on MSVC, but no harm "-DCMAKE_BUILD_TYPE={}".format(cfg), "-DBUILD_SHARED_LIBS=ON", - "-DOCIO_BUILD_DOCS=ON", + "-DOCIO_USE_SOVERSION=OFF", "-DOCIO_BUILD_APPS=ON", "-DOCIO_BUILD_TESTS=OFF", "-DOCIO_BUILD_GPU_TESTS=OFF", @@ -175,6 +163,11 @@ def build_extension(self, ext): if sys.platform.startswith("darwin"): cmake_args += ["-DCMAKE_INSTALL_RPATH={}".format("@loader_path;@loader_path/..")] + # Documentation is used for Python docstrings but we allow to build + # the wheel without docs to remove a hard dependency on doxygen. + if cmake_find_package("Doxygen"): + cmake_args += ["-DOCIO_BUILD_DOCS=ON"] + # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level # across all generators. if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ: @@ -194,8 +187,6 @@ def build_extension(self, ext): ["cmake", "--build", "."] + build_args, cwd=self.build_temp ) - patch_symlink(extdir) - # For historical reason, we use PyOpenColorIO as the import name setup( diff --git a/src/OpenColorIO/CMakeLists.txt b/src/OpenColorIO/CMakeLists.txt index 08252e860b..0f478d2836 100755 --- a/src/OpenColorIO/CMakeLists.txt +++ b/src/OpenColorIO/CMakeLists.txt @@ -390,15 +390,20 @@ elseif(APPLE) if (_OCIO_LINK_FLAGS_LIST_) list(JOIN _OCIO_LINK_FLAGS_LIST_ ";" _OCIO_LINK_FLAGS_LIST_) set(CUSTOM_LINK_FLAGS "${CUSTOM_LINK_FLAGS};${_OCIO_LINK_FLAGS_LIST_}") - endif() + endif() +endif() + +if (OCIO_USE_SOVERSION) + set_target_properties(OpenColorIO PROPERTIES + VERSION ${OpenColorIO_VERSION} + SOVERSION ${SOVERSION} + ) endif() set_target_properties(OpenColorIO PROPERTIES OUTPUT_NAME ${PROJECT_NAME}${OCIO_LIBNAME_SUFFIX} COMPILE_OPTIONS "${PLATFORM_COMPILE_OPTIONS}" LINK_OPTIONS "${CUSTOM_LINK_FLAGS}" - VERSION ${OpenColorIO_VERSION} - SOVERSION ${SOVERSION} PUBLIC_HEADER "${INSTALL_HEADERS}" ) diff --git a/tests/data/__init__.py b/tests/data/__init__.py deleted file mode 100644 index e69de29bb2..0000000000