|
| 1 | +"""Build script.""" |
| 2 | + |
| 3 | +import shutil |
| 4 | +import sys |
| 5 | +from distutils import log as distutils_log |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any, Dict |
| 8 | + |
| 9 | +import skbuild |
| 10 | +import skbuild.constants |
| 11 | + |
| 12 | +__all__ = ("build",) |
| 13 | + |
| 14 | + |
| 15 | +def build(setup_kwargs: Dict[str, Any]) -> None: |
| 16 | + """Build C-extensions.""" |
| 17 | + cmake_args = [ |
| 18 | + '-DINSTALL_DOC:BOOL=OFF', |
| 19 | + '-DRUN_GCOV:BOOL=OFF', |
| 20 | + '-DLIB_SUFFIX=' |
| 21 | + ] + ( |
| 22 | + ['-DREADLINE_ROOT=/usr/local/opt/portable-readline', |
| 23 | + '-DREADLINE_INCLUDE_DIR=/usr/local/opt/portable-readline/include', |
| 24 | + '-DREADLINE_LIBRARY=/usr/local/opt/libedit/lib/libedit.dylib', |
| 25 | + '-DICU_ROOT=/usr/local/opt/icu4c'] if sys.platform.startswith("darwin") else [] |
| 26 | + ) |
| 27 | + skbuild.setup(**setup_kwargs, script_args=["build_ext"]) |
| 28 | + # skbuild.setup(**setup_kwargs, script_args=cmake_args) |
| 29 | + |
| 30 | + src_dir = Path(skbuild.constants.CMAKE_INSTALL_DIR()) / "opentrep" |
| 31 | + dest_dir = Path("opentrep") |
| 32 | + |
| 33 | + # Delete C-extensions copied in previous runs, just in case. |
| 34 | + remove_files(dest_dir, "**/*.pyd") |
| 35 | + remove_files(dest_dir, "**/*.so") |
| 36 | + |
| 37 | + # Copy built C-extensions back to the project. |
| 38 | + copy_files(src_dir, dest_dir, "**/*.pyd") |
| 39 | + copy_files(src_dir, dest_dir, "**/*.so") |
| 40 | + |
| 41 | + |
| 42 | +def remove_files(target_dir: Path, pattern: str) -> None: |
| 43 | + """Delete files matched with a glob pattern in a directory tree.""" |
| 44 | + for path in target_dir.glob(pattern): |
| 45 | + if path.is_dir(): |
| 46 | + shutil.rmtree(path) |
| 47 | + else: |
| 48 | + path.unlink() |
| 49 | + distutils_log.info(f"removed {path}") |
| 50 | + |
| 51 | + |
| 52 | +def copy_files(src_dir: Path, dest_dir: Path, pattern: str) -> None: |
| 53 | + """Copy files matched with a glob pattern in a directory tree to another.""" |
| 54 | + for src in src_dir.glob(pattern): |
| 55 | + dest = dest_dir / src.relative_to(src_dir) |
| 56 | + if src.is_dir(): |
| 57 | + # NOTE: inefficient if subdirectories also match to the pattern. |
| 58 | + copy_files(src, dest, "*") |
| 59 | + else: |
| 60 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 61 | + shutil.copy2(src, dest) |
| 62 | + distutils_log.info(f"copied {src} to {dest}") |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + build({}) |
| 67 | + |
0 commit comments