-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.py
More file actions
133 lines (105 loc) · 3.64 KB
/
setup.py
File metadata and controls
133 lines (105 loc) · 3.64 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Setup script to build and install the QligFEP package with Fortran compilation.
It integrates the Fortran compilation step into the setuptools build process.
"""
import platform
import shutil
import subprocess
import sys
from pathlib import Path
from setuptools import setup
from setuptools.command.build_py import build_py as _build_py
def find_gfortran():
"""Find gfortran compiler in environment or system PATH."""
gfortran = shutil.which("gfortran")
if gfortran:
return gfortran
if platform.system() == "Darwin":
homebrew_paths = [
"/opt/homebrew/bin/gfortran",
"/usr/local/bin/gfortran",
]
for path in homebrew_paths:
if Path(path).exists():
return path
return None
def compile_qprep():
"""Compile qprep binary using make."""
src_dir = Path(__file__).parent / "src"
q6_dir = src_dir / "q6"
if not q6_dir.exists():
print(f"WARNING: Q6 source directory not found: {q6_dir}", file=sys.stderr)
return False
gfortran = find_gfortran()
if not gfortran:
system = platform.system()
comp_flag = "osx" if system == "Darwin" else "gcc"
print(
f"\nWARNING: gfortran compiler not found!\n"
f"The qprep binary could not be compiled automatically.\n"
f"Please install gfortran and manually compile:\n"
f" cd {q6_dir}\n"
f" make qprep COMP={comp_flag}\n",
file=sys.stderr
)
return False
print(f"\nFound gfortran: {gfortran}")
print(f"Compiling qprep in {q6_dir}...")
system = platform.system()
comp_flag = "osx" if system == "Darwin" else "gcc"
print(f"Using compiler configuration: COMP={comp_flag}\n")
try:
subprocess.run(
["make", "clean"],
cwd=q6_dir,
check=False,
capture_output=True
)
result = subprocess.run(
["make", "qprep", f"COMP={comp_flag}"],
cwd=q6_dir,
check=True,
capture_output=True,
text=True
)
print(result.stdout)
bin_dir = q6_dir / "bin" / "q6"
bin_dir.mkdir(parents=True, exist_ok=True)
qprep_binary = q6_dir / "qprep"
if qprep_binary.exists():
shutil.move(str(qprep_binary), str(bin_dir / "qprep"))
print(f"Successfully compiled qprep -> {bin_dir / 'qprep'}\n")
obj_dir = q6_dir / "obj"
obj_dir.mkdir(exist_ok=True)
for pattern in ["*.o", "*.mod"]:
for obj_file in q6_dir.glob(pattern):
shutil.move(str(obj_file), str(obj_dir / obj_file.name))
return True
else:
print("ERROR: qprep binary not found after compilation", file=sys.stderr)
return False
except subprocess.CalledProcessError as e:
print(
f"\nERROR: Compilation failed!\n"
f"Command: {' '.join(e.cmd)}\n"
f"Output: {e.stdout}\n"
f"Error: {e.stderr}",
file=sys.stderr
)
return False
class BuildWithFortran(_build_py):
"""Custom build_py command that compiles Fortran code before building Python package."""
def run(self):
"""Execute the build, including Fortran compilation."""
print("\n" + "="*70)
print("Building QligFEP - Compiling Fortran qprep binary")
print("="*70)
compile_qprep()
print("="*70)
print("Continuing with Python package build")
print("="*70 + "\n")
super().run()
setup(
cmdclass={
'build_py': BuildWithFortran,
}
)