forked from 21cmfast/21cmFAST
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_cffi.py
executable file
·79 lines (65 loc) · 2.1 KB
/
build_cffi.py
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
"""Build the C code with CFFI."""
import os
from cffi import FFI
ffi = FFI()
LOCATION = os.path.dirname(os.path.abspath(__file__))
CLOC = os.path.join(LOCATION, "src", "py21cmfast", "src")
include_dirs = [CLOC]
# =================================================================
# Set compilation arguments dependent on environment... a bit buggy
# =================================================================
if "DEBUG" in os.environ:
extra_compile_args = ["-fopenmp", "-w", "-g", "-O0"]
else:
extra_compile_args = ["-fopenmp", "-Ofast", "-w"]
# Set the C-code logging level.
# If DEBUG is set, we default to the highest level, but if not,
# we set it to the level just above no logging at all.
log_level = os.environ.get("LOG_LEVEL", 4 if "DEBUG" in os.environ else 1)
available_levels = [
"NONE",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"SUPER_DEBUG",
"ULTRA_DEBUG",
]
if isinstance(log_level, str) and log_level.upper() in available_levels:
log_level = available_levels.index(log_level.upper())
try:
log_level = int(log_level)
except ValueError:
# note: for py35 support, can't use f strings.
raise ValueError(
"LOG_LEVEL must be specified as a positive integer, or one "
"of {}".format(available_levels)
)
library_dirs = []
for k, v in os.environ.items():
if "inc" in k.lower():
include_dirs += [v]
elif "lib" in k.lower():
library_dirs += [v]
# =================================================================
# This is the overall C code.
ffi.set_source(
"py21cmfast.c_21cmfast", # Name/Location of shared library module
"""
#define LOG_LEVEL {log_level}
#include "GenerateICs.c"
""".format(
log_level=log_level
),
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=["m", "gsl", "gslcblas", "fftw3f_omp", "fftw3f"],
extra_compile_args=extra_compile_args,
)
# This is the Header file
with open(os.path.join(CLOC, "21cmFAST.h")) as f:
ffi.cdef(f.read())
with open(os.path.join(CLOC, "Globals.h")) as f:
ffi.cdef(f.read())
if __name__ == "__main__":
ffi.compile()