forked from diffpy/diffpy.pdffit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·132 lines (106 loc) · 3.78 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
#!/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 sys
import warnings
from setuptools import Extension, setup
# 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, windwows version.
This version is installed with conda.
"""
conda_prefix = os.environ["CONDA_PREFIX"]
inc = os.path.join(conda_prefix, "Library", "include")
lib = os.path.join(conda_prefix, "Library", "lib")
rv = {"include_dirs": [], "library_dirs": []}
rv["include_dirs"] += [inc]
rv["library_dirs"] += [lib]
return rv
# ----------------------------------------------------------------------------
# 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 = []
libraries = []
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 += ((p + "/libgsl.a") for p in gcfg["library_dirs"])
elif compiler_type == "msvc":
define_macros += [("_USE_MATH_DEFINES", None)]
extra_compile_args = ["/EHs"]
libraries += ["gsl"]
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=[],
)
if __name__ == "__main__":
setup_args["ext_modules"] = create_extensions()
setup(**setup_args)
# End of file