-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsetup.py
More file actions
330 lines (276 loc) · 9.98 KB
/
setup.py
File metadata and controls
330 lines (276 loc) · 9.98 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import os
import platform
import re
import subprocess
from setuptools import Extension, setup
from wheel.bdist_wheel import bdist_wheel as native_bdist_wheel
try:
from pip import main as pip_main
except Exception:
from pip._internal import main as pip_main
try:
from Cython.Build.Distutils import build_ext as native_build_ext
except ImportError:
print("Suitable Cython unavailable, installing...")
pip_main(["install", "cython"])
from Cython.Build.Distutils import build_ext as native_build_ext
try:
import numpy as np
except ImportError:
print("Suitable numpy unavailable, installing...")
pip_main(["install", "numpy"])
import numpy as np
include_dirs = [np.get_include()]
with open("README.rst") as readme_file:
readme = readme_file.read()
with open("HISTORY.rst") as history_file:
history = history_file.read()
requirements = [
"numpy>=1.16.0",
"pint>=0.7.0",
"unyt",
"xarray>=0.8.0",
"sympl>=0.5.0",
"cython>=0.25",
"scipy>=0.18.1",
]
test_requirements = [
"pytest>=2.9.2",
"mock>=2.0.0",
]
# Find first gcc directory
def find_homebrew_gcc():
# Check both standard Homebrew locations
paths = ["/opt/homebrew/Cellar/gcc*", "/usr/local/Cellar/gcc*"]
candidates = []
for path in paths:
candidates.extend(glob.glob(path))
if candidates:
# Prefer the newest version if multiple are found (glob order might vary, but usually sorted)
# Assuming glob returns absolute paths.
candidates.sort(reverse=True)
print(f"Found Homebrew GCC candidates: {candidates}")
return candidates[0]
print("Warning: Could not find Homebrew GCC in standard locations.")
return None
# Platform specific settings
def guess_compiler_name(env_name):
search_string = ""
if env_name == "FC":
search_string = r"gfortran-\d+$"
if env_name == "CC":
search_string = r"gcc-\d+$"
gcc_dir = find_homebrew_gcc()
if gcc_dir:
# Search deeply in the found gcc directory for bin/
for root, dirs, files in os.walk(gcc_dir):
if not root.endswith("/bin"): # Optimization: Only look in bin directories
continue
for line in files:
if re.match(search_string, line):
print(f"Guessing {env_name} = {line} from {root}")
os.environ[env_name] = os.path.join(root, line)
return # Stop after finding one
operating_system = platform.system()
libraries = ["m", "gfortran"]
default_link_args = []
default_compile_args = []
compiled_base_dir = "climt/_lib"
if operating_system == "Linux":
libraries = ["m", "gfortran", "rt"]
default_link_args = ["-lgfortran", "-lm"]
if operating_system == "Windows":
compiled_base_dir = "climt\\_lib"
dir_path = os.getcwd()
compiled_path = os.path.join(dir_path, compiled_base_dir)
lib_path_list = [os.path.join(compiled_path, operating_system + "/")]
inc_path = os.path.join(compiled_path, "include")
include_dirs.append(inc_path)
# Compile libraries
# Attempt to guess compilers on Darwin if not set
if operating_system == "Darwin":
if "FC" not in os.environ:
guess_compiler_name("FC")
if "CC" not in os.environ:
guess_compiler_name("CC")
if "FC" not in os.environ:
if operating_system == "Darwin":
os.environ["FC"] = "gfortran"
os.environ["F77"] = "gfortran"
else:
os.environ["FC"] = "gfortran"
os.environ["F77"] = "gfortran"
if "CC" not in os.environ:
if operating_system == "Darwin":
os.environ["CC"] = "gcc"
else:
os.environ["CC"] = "gcc"
if "CLIMT_OPT_FLAGS" not in os.environ:
os.environ["CLIMT_OPT_FLAGS"] = "-O3"
if operating_system == "Windows":
os.environ["CC"] = "gcc.exe"
os.environ["FC"] = "gfortran.exe"
os.environ["AR"] = "gcc-ar.exe"
libraries = []
default_link_args = ["-l:libgfortran.a", "-l:libquadmath.a", "-l:libm.a"]
default_compile_args = ["-DMS_WIN64"]
os.environ["FFLAGS"] = "-fPIC -fno-range-check " + os.environ["CLIMT_OPT_FLAGS"]
os.environ["CFLAGS"] = "-fPIC " + os.environ["CLIMT_OPT_FLAGS"]
if operating_system == "Darwin":
gcc_dir = find_homebrew_gcc()
if gcc_dir:
# Also need to find libgfortran.a to link against it?
# The original code did this.
for root, dirs, files in os.walk(gcc_dir):
if "lib" not in root:
continue # Optimization
for line in files:
if re.match(r"libgfortran\.a", line):
# Avoid i386 libs on 64bit systems
if "i386" not in root:
if root not in lib_path_list:
lib_path_list.append(root)
if "MACOSX_DEPLOYMENT_TARGET" not in os.environ:
os.environ["FFLAGS"] += " -mmacosx-version-min=10.9"
os.environ["CFLAGS"] += " -mmacosx-version-min=10.9"
default_link_args = []
os.environ["LDSHARED"] = os.environ["CC"] + " -bundle -undefined dynamic_lookup"
print("Compilers: ", os.environ.get("CC"), os.environ.get("FC"))
print("LDSHARED: ", os.environ.get("LDSHARED"))
print("FFLAGS: ", os.environ.get("FFLAGS"))
print("Lib Paths: ", lib_path_list)
# Create a custom build class to build libraries, and patch cython extensions
def build_libraries():
if os.environ.get("READTHEDOCS") == "True":
return
curr_dir = os.getcwd()
os.chdir(compiled_path)
os.environ["PWD"] = compiled_path
cmd = ["make", "CLIMT_ARCH=" + operating_system]
print(f"Building libraries in {compiled_path}")
print(f"Command: {cmd}")
# Use subprocess.run to capture output if needed, or just let it stream to stdout/stderr
# call() streams to stdout/stderr by default.
ret = subprocess.call(cmd)
os.chdir(curr_dir)
os.environ["PWD"] = curr_dir
if ret != 0:
raise RuntimeError(f"Library build failed with exit code {ret}")
# Custom build class
class climt_build_ext(native_build_ext):
def run(self):
build_libraries()
native_build_ext.run(self)
# Custom bdist_wheel class
class climt_bdist_wheel(native_bdist_wheel):
def run(self):
self.run_command("build")
native_bdist_wheel.run(self)
# Define extensions to be built
if os.environ.get("READTHEDOCS") == "True":
ext_modules = []
else:
# Use the lib_path_list populated earlier
ext_modules = [
Extension(
"climt._components._berger_solar_insolation",
["climt/_components/_berger_solar_insolation.pyx"],
),
Extension(
"climt._components.simple_physics._simple_physics",
sources=["climt/_components/simple_physics/_simple_physics.pyx"],
libraries=libraries,
include_dirs=include_dirs,
extra_compile_args=default_compile_args,
library_dirs=lib_path_list,
extra_link_args=[os.path.join(lib_path_list[0], "libsimple_physics.a")]
+ default_link_args,
),
Extension(
"climt._components.emanuel._emanuel_convection",
sources=["climt/_components/emanuel/_emanuel_convection.pyx"],
libraries=libraries,
include_dirs=include_dirs,
extra_compile_args=default_compile_args,
library_dirs=lib_path_list,
extra_link_args=[os.path.join(lib_path_list[0], "libemanuel.a")]
+ default_link_args,
),
Extension(
"climt._components.rrtmg.lw._rrtmg_lw",
sources=["climt/_components/rrtmg/lw/_rrtmg_lw.pyx"],
libraries=libraries,
include_dirs=include_dirs,
extra_compile_args=default_compile_args + ["-fopenmp"],
library_dirs=lib_path_list,
extra_link_args=[
os.path.join(lib_path_list[0], "librrtmg_lw.a"),
"-fopenmp",
]
+ default_link_args,
),
Extension(
"climt._components.rrtmg.sw._rrtmg_sw",
sources=["climt/_components/rrtmg/sw/_rrtmg_sw.pyx"],
libraries=libraries,
include_dirs=include_dirs,
extra_compile_args=default_compile_args + ["-fopenmp"],
library_dirs=lib_path_list,
extra_link_args=[
os.path.join(lib_path_list[0], "librrtmg_sw.a"),
"-fopenmp",
]
+ default_link_args,
),
Extension(
"climt._components.dcmip._dcmip",
sources=["climt/_components/dcmip/_dcmip.pyx"],
libraries=libraries,
include_dirs=include_dirs,
extra_compile_args=default_compile_args,
library_dirs=lib_path_list,
extra_link_args=[os.path.join(lib_path_list[0], "libdcmip.a")]
+ default_link_args,
),
]
setup(
name="climt",
version="0.18.5",
description="CliMT is a Toolkit for building Earth system models in Python.",
long_description=readme + "\n\n" + history,
author="Rodrigo Caballero",
author_email="rodrigo.caballero@misu.su.se",
url="https://github.com/CliMT/climt",
packages=[
"climt",
],
package_dir={"climt": "climt"},
include_package_data=True,
install_requires=requirements,
cmdclass={
"build_ext": climt_build_ext,
"bdist_wheel": climt_bdist_wheel,
},
ext_modules=ext_modules,
include_dirs=include_dirs,
license="BSD license",
zip_safe=False,
keywords="climt",
classifiers=[
"Development Status :: 2 - Pre-Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Natural Language :: English",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
],
test_suite="tests",
tests_require=test_requirements,
)