forked from diffpy/diffpy.pdffit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·159 lines (128 loc) · 4.76 KB
/
setup.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
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
#!/usr/bin/env python
# Extensions script for diffpy.pdffit2
"""PDFfit2 - real space structure refinement engine
Packages: diffpy.pdffit2
Scripts: pdffit2
"""
import glob
import os
import re
import shutil
import sys
import warnings
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
# Use this version when git data are not available, like in git zip archive.
# Update when tagging a new release.
FALLBACK_VERSION = "1.4.3"
MYDIR = os.path.dirname(os.path.abspath(__file__))
# Helper functions -----------------------------------------------------------
def get_compiler_type():
"""find compiler used for building extensions."""
cc_arg = [a for a in sys.argv if a.startswith("--compiler=")]
if cc_arg:
compiler_type = cc_arg[-1].split("=", 1)[1]
else:
from distutils.ccompiler import new_compiler
compiler_type = new_compiler().compiler_type
return compiler_type
def get_gsl_config():
"""Return dictionary with paths to GSL library."""
gslcfgpaths = [os.path.join(p, "gsl-config") for p in ([MYDIR] + os.environ["PATH"].split(os.pathsep))]
gslcfgpaths = [p for p in gslcfgpaths if os.path.isfile(p)]
rv = {"include_dirs": [], "library_dirs": []}
if not gslcfgpaths:
wmsg = "Cannot find gsl-config in {!r} nor in system PATH."
warnings.warn(wmsg.format(MYDIR))
return rv
gslcfg = gslcfgpaths[0]
with open(gslcfg) as fp:
txt = fp.read()
mprefix = re.search("(?m)^prefix=(.+)", txt)
minclude = re.search(r"(?m)^[^#]*\s-I(\S+)", txt)
mlibpath = re.search(r"(?m)^[^#]*\s-L(\S+)", txt)
if not mprefix:
emsg = "Cannot find 'prefix=' line in {}."
raise RuntimeError(emsg.format(gslcfg))
p = mprefix.group(1)
inc = minclude.group(1) if minclude else (p + "/include")
lib = mlibpath.group(1) if mlibpath else (p + "/lib")
rv["include_dirs"] += [inc]
rv["library_dirs"] += [lib]
return rv
def get_gsl_config_win():
"""Return dictionary with paths to GSL library on Windows."""
gsl_path = os.environ.get("GSL_PATH")
if gsl_path:
inc = os.path.join(gsl_path, "include")
lib = os.path.join(gsl_path, "lib")
else:
conda_prefix = os.environ.get("CONDA_PREFIX")
if conda_prefix:
inc = os.path.join(conda_prefix, "Library", "include")
lib = os.path.join(conda_prefix, "Library", "lib")
else:
raise EnvironmentError(
"Neither GSL_PATH nor CONDA_PREFIX environment variables are set. "
"Please ensure GSL is installed and GSL_PATH is correctly set."
)
return {"include_dirs": [inc], "library_dirs": [lib]}
class CustomBuildExt(build_ext):
def run(self):
super().run()
gsl_path = os.environ.get("GSL_PATH") or os.path.join(os.environ.get("CONDA_PREFIX", ""), "Library")
bin_path = os.path.join(gsl_path, "bin")
dest_path = os.path.join(self.build_lib, "diffpy", "pdffit2")
os.makedirs(dest_path, exist_ok=True)
for dll_file in glob.glob(os.path.join(bin_path, "gsl*.dll")):
shutil.copy(dll_file, dest_path)
# ----------------------------------------------------------------------------
# compile and link options
define_macros = []
os_name = os.name
if os_name == "nt":
gcfg = get_gsl_config_win()
else:
gcfg = get_gsl_config()
include_dirs = [MYDIR] + gcfg["include_dirs"]
library_dirs = []
if sys.platform == "darwin":
libraries = []
else:
libraries = ["gsl"]
extra_objects = []
extra_compile_args = []
extra_link_args = []
compiler_type = get_compiler_type()
if compiler_type in ("unix", "cygwin", "mingw32"):
extra_compile_args = ["-std=c++11", "-Wall", "-Wno-write-strings", "-O3", "-funroll-loops", "-ffast-math"]
extra_objects += [
os.path.join(p, "libgsl.a") for p in gcfg["library_dirs"] if os.path.isfile(os.path.join(p, "libgsl.a"))
]
elif compiler_type == "msvc":
define_macros += [("_USE_MATH_DEFINES", None)]
extra_compile_args = ["/EHs"]
library_dirs += gcfg["library_dirs"]
# add optimization flags for other compilers if needed
# define extension arguments here
ext_kws = {
"include_dirs": include_dirs,
"libraries": libraries,
"library_dirs": library_dirs,
"define_macros": define_macros,
"extra_compile_args": extra_compile_args,
"extra_link_args": extra_link_args,
"extra_objects": extra_objects,
}
# define extension here
def create_extensions():
ext = Extension("diffpy.pdffit2.pdffit2", glob.glob("src/extensions/**/*.cc"), **ext_kws)
return [ext]
setup_args = dict(
ext_modules=[],
cmdclass={"build_ext": CustomBuildExt},
)
if __name__ == "__main__":
setup_args["ext_modules"] = create_extensions()
setup(**setup_args)
# End of file