1- from setuptools import setup
1+ import os
2+ import sys
3+ import shutil
4+ import subprocess
5+ from setuptools import setup , Extension
26from setuptools .command .build_ext import build_ext
37
48
9+ INCLUDE_DIRS = [
10+ "epanet-src/include" ,
11+ "epanet-src/" ,
12+ "epanet-msx-src/include" ,
13+ "epanet-msx-src/" ,
14+ "python-extension" ,
15+ "epanet_plus/include" ,
16+ "epanet-src/util" ,
17+ ]
18+
19+ SOURCES = [
20+ "python-extension/pyepanet.c" ,
21+ "python-extension/pyepanet2.c" ,
22+ "python-extension/pyepanetmsx.c" ,
23+ "python-extension/pyepanet_plus.c" ,
24+ "python-extension/ext.c" ,
25+ "epanet_plus/epanet_plus.c" ,
26+ "epanet-src/epanet2.c" ,
27+ "epanet-src/hydcoeffs.c" ,
28+ "epanet-src/inpfile.c" ,
29+ "epanet-src/quality.c" ,
30+ "epanet-src/rules.c" ,
31+ "epanet-src/epanet.c" ,
32+ "epanet-src/hydraul.c" ,
33+ "epanet-src/input1.c" ,
34+ "epanet-src/mempool.c" ,
35+ "epanet-src/qualreact.c" ,
36+ "epanet-src/smatrix.c" ,
37+ "epanet-src/genmmd.c" ,
38+ "epanet-src/hydsolver.c" ,
39+ "epanet-src/input2.c" ,
40+ "epanet-src/output.c" ,
41+ "epanet-src/qualroute.c" ,
42+ "epanet-src/hash.c" ,
43+ "epanet-src/hydstatus.c" ,
44+ "epanet-src/input3.c" ,
45+ "epanet-src/project.c" ,
46+ "epanet-src/report.c" ,
47+ "epanet-msx-src/hash.c" ,
48+ "epanet-src/flowbalance.c" ,
49+ "epanet-src/leakage.c" ,
50+ "epanet-src/validate.c" ,
51+ "epanet-src/util/filemanager.c" ,
52+ "epanet-src/util/errormanager.c" ,
53+ "epanet-src/util/cstr_helper.c" ,
54+ "epanet-msx-src/msxcompiler.c" ,
55+ "epanet-msx-src/msxfuncs.c" ,
56+ "epanet-msx-src/msxqual.c" ,
57+ "epanet-msx-src/msxutils.c" ,
58+ "epanet-msx-src/smatrix.c" ,
59+ "epanet-msx-src/mathexpr.c" ,
60+ "epanet-msx-src/msxdispersion.c" ,
61+ "epanet-msx-src/msxinp.c" ,
62+ "epanet-msx-src/msxrpt.c" ,
63+ "epanet-msx-src/newton.c" ,
64+ "epanet-msx-src/mempool.c" ,
65+ "epanet-msx-src/msxerr.c" ,
66+ "epanet-msx-src/msxout.c" ,
67+ "epanet-msx-src/msxtank.c" ,
68+ "epanet-msx-src/rk5.c" ,
69+ "epanet-msx-src/msxchem.c" ,
70+ "epanet-msx-src/msxfile.c" ,
71+ "epanet-msx-src/msxproj.c" ,
72+ "epanet-msx-src/msxtoolkit.c" ,
73+ "epanet-msx-src/ros2.c" ,
74+ ]
75+
76+ def get_libomp_prefix () -> str | None :
77+ prefix = os .environ .get ("LIBOMP_PREFIX" )
78+ if prefix :
79+ return prefix
80+
81+ brew = shutil .which ("brew" )
82+ if brew :
83+ try :
84+ return subprocess .check_output (
85+ [brew , "--prefix" , "libomp" ],
86+ text = True ,
87+ ).strip ()
88+ except subprocess .CalledProcessError :
89+ pass
90+
91+ return None
92+
93+
94+ def get_openmp_config () -> dict [str , list [str ]]:
95+ if sys .platform == "darwin" :
96+ prefix = get_libomp_prefix ()
97+ if not prefix :
98+ raise RuntimeError (
99+ "OpenMP runtime `libomp` not found on macOS. "
100+ "Install it with `brew install libomp` and set "
101+ "`LIBOMP_PREFIX=$(brew --prefix libomp)`."
102+ )
103+
104+ return {
105+ "include_dirs" : [f"{ prefix } /include" ],
106+ "extra_compile_args" : ["-Xpreprocessor" , "-fopenmp" ],
107+ "extra_link_args" : [
108+ f"-L{ prefix } /lib" ,
109+ f"-Wl,-rpath,{ prefix } /lib" ,
110+ "-lomp" ,
111+ ],
112+ }
113+
114+ if sys .platform .startswith ("linux" ):
115+ return {
116+ "include_dirs" : [],
117+ "extra_compile_args" : ["-fopenmp" ],
118+ "extra_link_args" : ["-fopenmp" ],
119+ }
120+
121+ if sys .platform == "win32" :
122+ return {
123+ "include_dirs" : [],
124+ "extra_compile_args" : ["/openmp" ],
125+ "extra_link_args" : [],
126+ }
127+
128+ return {
129+ "include_dirs" : [],
130+ "extra_compile_args" : [],
131+ "extra_link_args" : [],
132+ }
133+
134+
135+ omp = get_openmp_config ()
136+
137+
138+ extension = Extension (
139+ name = "epanet" ,
140+ include_dirs = INCLUDE_DIRS + omp ["include_dirs" ],
141+ sources = SOURCES ,
142+ extra_compile_args = omp ["extra_compile_args" ],
143+ extra_link_args = omp ["extra_link_args" ],
144+ )
145+
146+
5147class build_ext_with_numpy (build_ext ):
6148 def build_extensions (self ):
7149 import numpy
@@ -13,4 +155,4 @@ def build_extensions(self):
13155 super ().build_extensions ()
14156
15157
16- setup (cmdclass = {"build_ext" : build_ext_with_numpy })
158+ setup (ext_modules = [ extension ], cmdclass = {"build_ext" : build_ext_with_numpy })
0 commit comments