-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_extensions.py
More file actions
66 lines (56 loc) · 2.32 KB
/
build_extensions.py
File metadata and controls
66 lines (56 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
"""
Utility to build HELP3O.FOR as a Python extension.
Compatible with numpy.f2py and meson.
"""
import os
import subprocess
import sys
import platform
from pathlib import Path
def build_help3o():
"""Compile HELP3O.FOR using numpy.f2py."""
fortran_source = Path(__file__).parent / "HELP3O.FOR"
if not fortran_source.exists():
raise FileNotFoundError(f"HELP3O.FOR not found: {fortran_source}")
print(f"Compiling {fortran_source}...")
print(f"Python version: {sys.version}")
print(f"Platform: {platform.system()}")
# Build with f2py
cmd = [
sys.executable, '-m', 'numpy.f2py',
'-c', '-m', 'HELP3O',
str(fortran_source),
]
env = os.environ.copy()
# Add static linking flags for macOS to avoid runtime GCC dependencies
if platform.system() == 'Darwin':
# Static link GCC libraries so users don't need to install GCC
static_flags = '-static-libgfortran -static-libquadmath -static-libgcc'
cmd.extend(['--f90flags=' + static_flags])
cmd.extend(['--f77flags=' + static_flags])
print(f"macOS detected: adding static linking flags")
elif platform.system() == 'Windows':
# Try to statically link the MinGW runtime to avoid shipping DLLs.
# Some MinGW toolchains do not support -static-libssp, so rely on the default lib.
static_link_flags = "-static -static-libgcc -static-libgfortran -static-libstdc++ -static-libquadmath"
env["NPY_DISTUTILS_APPEND_FLAGS"] = "1"
env["LDFLAGS"] = (env.get("LDFLAGS", "").strip() + " " + static_link_flags).strip()
env["OPT"] = (env.get("OPT", "").strip() + " -static").strip()
print("Windows detected: enabling static MinGW linking via LDFLAGS/OPT.")
print(f"Command: {' '.join(cmd)}")
try:
subprocess.run(cmd, check=True, env=env)
print("Compilation succeeded.")
# Locate the generated .so/.pyd file
import glob
binaries = glob.glob("HELP3O.*.so") + glob.glob("HELP3O.*.pyd")
if binaries:
print(f"Binary created: {binaries[0]}")
else:
print("Warning: binary file not found.")
except subprocess.CalledProcessError as e:
print(f"Compilation failed: {e}")
sys.exit(1)
if __name__ == "__main__":
build_help3o()