diff --git a/README.md b/README.md index a370eee..49c79f5 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ To build using pip/setuptools: * create a venv at the same level as the openpivcore directory * activate the venv -* run pip against the name of the directory containing the openpiv code e.g. `pip install openpiv-c-qt/` +* run pip against the name of the directory containing the openpiv code e.g. `pip install ` + * a clean step should happen as part of the build, this can also be run manually with `python setup.py clean` To build using cmake: diff --git a/pyproject.toml b/pyproject.toml index fe001e6..b121096 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,11 +7,3 @@ requires = [ "pybind11" ] build-backend = "setuptools.build_meta" - -[tool.cibuildwheel] -# test-command = "pytest {project}/tests" -# test-extras = ["test"] -# test-skip = ["*universal2:arm64"] - -# Setuptools bug causes collision between pypy and cpython artifacts -#before-build = "rm -rf {project}/build {project}/*.egg-info" diff --git a/setup.py b/setup.py index 412a794..863decb 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,13 @@ from glob import glob import os import re +import shutil import subprocess import sys from distutils import sysconfig as ds from setuptools import setup +from setuptools import Command from setuptools import Extension as _extension from setuptools.command.build_ext import build_ext as _build_ext @@ -17,6 +19,7 @@ "win-arm64": "ARM64", } +package_root = os.path.dirname(os.path.realpath(__file__)) # A CMakeExtension needs a sourcedir instead of a file list. # The name must be the _single_ output extension from the CMake build. @@ -28,7 +31,14 @@ def __init__(self, name, sourcedir=""): class CMakeBuild(_build_ext): + def __init__(self, args): + super().__init__(args) + self.clean_cmd = CleanCommand(args) + def build_extension(self, ext): + # force cleanup before we build + self.clean_cmd.run() + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) # required for auto-detection & inclusion of auxiliary "native" libs @@ -73,7 +83,6 @@ def build_extension(self, ext): pass else: - # Single config generators are handled "normally" single_config = any(x in cmake_generator for x in {"NMake", "Ninja"}) @@ -113,8 +122,31 @@ def build_extension(self, ext): os.makedirs(build_temp) subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_temp) - subprocess.check_call(["cmake", "--build", ".", "--clean-first"] + build_args, cwd=build_temp) + subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_temp) + + +class CleanCommand(Command): + """Custom clean command to tidy up the project root.""" + CLEAN_FILES = './build ./dist ./*.pyc ./*.tgz ./*.egg-info'.split(' ') + + user_options = [] + + def initialize_options(self): + pass + + def finalize_options(self): + pass + def run(self): + for path_spec in self.CLEAN_FILES: + # Make paths absolute and relative to this path + abs_paths = glob(os.path.normpath(os.path.join(package_root, path_spec))) + for path in [str(p) for p in abs_paths]: + if not path.startswith(package_root): + # Die if path in CLEAN_FILES is absolute + outside this directory + raise ValueError(f"{path} is not a valid path") + print('removing %s' % os.path.relpath(path)) + shutil.rmtree(path) # The information here can also be placed in setup.cfg - better separation of # logic and declaration, and simpler if you include description/version in a file. @@ -126,7 +158,10 @@ def build_extension(self, ext): description="Python bindings for libopenpivcore", long_description="", ext_modules=[ CMakeExtension("pyopenpivcore") ], - cmdclass={"build_ext": CMakeBuild}, + cmdclass={ + "build_ext": CMakeBuild, + "clean": CleanCommand + }, zip_safe=False, extras_require={"test": ["pytest>=6.0"]}, python_requires=">=3.6",