|
| 1 | +"""Custom setup.py to build C++ components with CMake before packaging.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
1 | 8 | from setuptools import setup |
| 9 | +from setuptools.command.build import build |
| 10 | + |
| 11 | + |
| 12 | +def is_binary(filepath: str | Path) -> bool: |
| 13 | + """Check if a file is a binary executable.""" |
| 14 | + path = str(filepath) |
| 15 | + if path.startswith("."): |
| 16 | + return False |
| 17 | + try: |
| 18 | + with open(path, "rb") as f: |
| 19 | + chunk = f.read(1024) |
| 20 | + if b"\0" in chunk: |
| 21 | + return True |
| 22 | + except Exception as err: |
| 23 | + print(f"Error checking if file is binary: {err}", file=sys.stderr) |
| 24 | + return False |
| 25 | + return False |
| 26 | + |
| 27 | + |
| 28 | +class CustomBuild(build): |
| 29 | + """Custom build command that runs cmake and make.""" |
| 30 | + |
| 31 | + def run(self): |
| 32 | + """Run cmake and make before the normal build.""" |
| 33 | + bin_dir = Path("epigeec/bin") |
| 34 | + lib_dir = Path("epigeec/lib") |
| 35 | + |
| 36 | + # Check if binaries exist |
| 37 | + has_binaries = False |
| 38 | + if bin_dir.exists(): |
| 39 | + binaries = [b for b in bin_dir.glob("*") if is_binary(b)] |
| 40 | + has_binaries = len(binaries) > 0 |
| 41 | + |
| 42 | + if lib_dir.exists() and not has_binaries: |
| 43 | + libs = list(lib_dir.glob("*.so*")) |
| 44 | + has_binaries = len(libs) > 0 |
| 45 | + |
| 46 | + if not has_binaries: |
| 47 | + print("=" * 60, file=sys.stderr) |
| 48 | + print("Building C++ components with CMake...", file=sys.stderr) |
| 49 | + print("=" * 60, file=sys.stderr) |
| 50 | + |
| 51 | + try: |
| 52 | + # Create build directory |
| 53 | + build_dir = Path("build_cmake") |
| 54 | + build_dir.mkdir(exist_ok=True) |
| 55 | + |
| 56 | + # Run cmake (configure from build directory) |
| 57 | + result = subprocess.run( |
| 58 | + ["cmake", ".."], |
| 59 | + cwd=build_dir, |
| 60 | + check=True, |
| 61 | + capture_output=True, |
| 62 | + text=True, |
| 63 | + ) |
| 64 | + print(result.stdout, file=sys.stderr) |
| 65 | + |
| 66 | + # Run make |
| 67 | + nproc = os.cpu_count() or 4 |
| 68 | + result = subprocess.run( |
| 69 | + ["cmake", "--build", ".", "-j", str(nproc)], |
| 70 | + cwd=build_dir, |
| 71 | + check=True, |
| 72 | + capture_output=True, |
| 73 | + text=True, |
| 74 | + ) |
| 75 | + print(result.stdout, file=sys.stderr) |
| 76 | + |
| 77 | + print("=" * 60, file=sys.stderr) |
| 78 | + print("C++ build complete!", file=sys.stderr) |
| 79 | + |
| 80 | + # Verify build succeeded |
| 81 | + if bin_dir.exists(): |
| 82 | + binaries = [b for b in bin_dir.glob("*") if is_binary(b)] |
| 83 | + print(f"Built {len(binaries)} binaries:", file=sys.stderr) |
| 84 | + for b in binaries: |
| 85 | + print(f" - {b.name}", file=sys.stderr) |
| 86 | + |
| 87 | + print("=" * 60, file=sys.stderr) |
| 88 | + |
| 89 | + except FileNotFoundError as e: |
| 90 | + print( |
| 91 | + f""" |
| 92 | +ERROR: Required build tool not found: {e} |
| 93 | +
|
| 94 | +Please install the required dependencies: |
| 95 | + - CMake |
| 96 | + - C++ compiler (gcc/g++) |
| 97 | + - HDF5 development libraries |
| 98 | + - Boost development libraries |
| 99 | +
|
| 100 | +On Ubuntu/Debian: |
| 101 | + sudo apt-get install cmake build-essential libhdf5-dev libboost-dev |
| 102 | +
|
| 103 | +On RHEL/Fedora: |
| 104 | + sudo yum install cmake gcc-c++ hdf5-devel boost-devel |
| 105 | +
|
| 106 | +On macOS: |
| 107 | + brew install cmake hdf5 boost libomp |
| 108 | + """, |
| 109 | + file=sys.stderr, |
| 110 | + ) |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | + except subprocess.CalledProcessError as e: |
| 114 | + print( |
| 115 | + f""" |
| 116 | +ERROR: Build failed with exit code {e.returncode} |
| 117 | +
|
| 118 | +STDOUT: |
| 119 | +{e.stdout} |
| 120 | +
|
| 121 | +STDERR: |
| 122 | +{e.stderr} |
| 123 | +
|
| 124 | +Please check that all dependencies are installed correctly. |
| 125 | + """, |
| 126 | + file=sys.stderr, |
| 127 | + ) |
| 128 | + sys.exit(1) |
| 129 | + else: |
| 130 | + print("=" * 60, file=sys.stderr) |
| 131 | + print("C++ binaries already exist, skipping build.", file=sys.stderr) |
| 132 | + if bin_dir.exists(): |
| 133 | + binaries = [b for b in bin_dir.glob("*") if is_binary(b)] |
| 134 | + for b in binaries: |
| 135 | + print(f" - {b.name}", file=sys.stderr) |
| 136 | + print("=" * 60, file=sys.stderr) |
| 137 | + |
| 138 | + # Continue with normal build |
| 139 | + super().run() |
| 140 | + |
2 | 141 |
|
3 | | -# This tells setuptools we have platform-specific binary content |
4 | 142 | setup( |
5 | | - has_ext_modules=lambda: True, |
| 143 | + cmdclass={"build": CustomBuild}, |
| 144 | + has_ext_modules=lambda: True, # Makes wheel tag platform-specific |
6 | 145 | ) |
0 commit comments