Skip to content

Commit f76d0ef

Browse files
committed
add build files
1 parent cb7c2ef commit f76d0ef

File tree

2 files changed

+142
-60
lines changed

2 files changed

+142
-60
lines changed

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ classifiers = [
3636
Homepage = "https://github.com/diffpy/diffpy.pdffit2/"
3737
Issues = "https://github.com/diffpy/diffpy.pdffit2/issues/"
3838

39+
[tools.setuptools]
40+
package-dir = {"" = "src"}
41+
3942
[tool.setuptools-git-versioning]
4043
enabled = true
4144
template = "{tag}"

setup.py

100644100755
+139-60
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,150 @@
11
#!/usr/bin/env python
22

3-
# Extensions script for diffpy.pdffit2
3+
# Installation script for diffpy.pdffit2
44

5-
import glob
6-
import sys
7-
8-
from setuptools import Extension, setup
9-
10-
# Define extension arguments here
11-
ext_kws = {
12-
"libraries": [],
13-
"extra_compile_args": [],
14-
"extra_link_args": [],
15-
"include_dirs": [],
16-
}
5+
"""PDFfit2 - real space structure refinement engine
176
7+
Packages: diffpy.pdffit2
8+
Scripts: pdffit2
9+
"""
1810

19-
# Figure out the tagged name of boost_python library.
20-
def get_boost_libraries():
21-
"""Check for installed boost_python shared library.
22-
23-
Returns list of required boost_python shared libraries that are installed
24-
on the system. If required libraries are not found, an Exception will be
25-
thrown.
11+
import os
12+
import re
13+
import sys
14+
import warnings
15+
16+
from setuptools import Extension, find_packages, setup
17+
18+
MYDIR = os.path.dirname(os.path.abspath(__file__))
19+
versioncfgfile = os.path.join(MYDIR, "src/diffpy/pdffit2/version.cfg")
20+
gitarchivecfgfile = os.path.join(MYDIR, ".gitarchive.cfg")
21+
22+
23+
# Helper functions -----------------------------------------------------------
24+
25+
26+
def get_compiler_type():
27+
"""find compiler used for building extensions."""
28+
cc_arg = [a for a in sys.argv if a.startswith("--compiler=")]
29+
if cc_arg:
30+
compiler_type = cc_arg[-1].split("=", 1)[1]
31+
else:
32+
from distutils.ccompiler import new_compiler
33+
34+
compiler_type = new_compiler().compiler_type
35+
return compiler_type
36+
37+
38+
def get_gsl_config():
39+
"""Return dictionary with paths to GSL library."""
40+
gslcfgpaths = [os.path.join(p, "gsl-config") for p in ([MYDIR] + os.environ["PATH"].split(os.pathsep))]
41+
gslcfgpaths = [p for p in gslcfgpaths if os.path.isfile(p)]
42+
rv = {"include_dirs": [], "library_dirs": []}
43+
if not gslcfgpaths:
44+
wmsg = "Cannot find gsl-config in {!r} nor in system PATH."
45+
warnings.warn(wmsg.format(MYDIR))
46+
return rv
47+
gslcfg = gslcfgpaths[0]
48+
with open(gslcfg) as fp:
49+
txt = fp.read()
50+
mprefix = re.search("(?m)^prefix=(.+)", txt)
51+
minclude = re.search(r"(?m)^[^#]*\s-I(\S+)", txt)
52+
mlibpath = re.search(r"(?m)^[^#]*\s-L(\S+)", txt)
53+
if not mprefix:
54+
emsg = "Cannot find 'prefix=' line in {}."
55+
raise RuntimeError(emsg.format(gslcfg))
56+
p = mprefix.group(1)
57+
inc = minclude.group(1) if minclude else (p + "/include")
58+
lib = mlibpath.group(1) if mlibpath else (p + "/lib")
59+
rv["include_dirs"] += [inc]
60+
rv["library_dirs"] += [lib]
61+
return rv
62+
63+
64+
def get_gsl_config_win():
65+
"""Return dictionary with paths to GSL library, windwows version.
66+
This version is installed with conda.
2667
"""
27-
baselib = "boost_python"
28-
major, minor = (str(x) for x in sys.version_info[:2])
29-
pytags = [major + minor, major, ""]
30-
mttags = ["", "-mt"]
31-
boostlibtags = [(pt + mt) for mt in mttags for pt in pytags] + [""]
32-
from ctypes.util import find_library
33-
34-
for tag in boostlibtags:
35-
lib = baselib + tag
36-
found = find_library(lib)
37-
if found:
38-
break
39-
40-
# Show warning when library was not detected.
41-
if not found:
42-
import platform
43-
import warnings
44-
45-
ldevname = "LIBRARY_PATH"
46-
if platform.system() == "Darwin":
47-
ldevname = "DYLD_FALLBACK_LIBRARY_PATH"
48-
wmsg = ("Cannot detect name suffix for the %r library. " "Consider setting %s.") % (baselib, ldevname)
49-
warnings.warn(wmsg)
50-
51-
libs = [lib]
52-
return libs
53-
54-
55-
def create_extensions():
56-
"Initialize Extension objects for the setup function."
57-
blibs = [n for n in get_boost_libraries() if n not in ext_kws["libraries"]]
58-
ext_kws["libraries"] += blibs
59-
ext = Extension("diffpy.pdffit2.pdffit2_ext", glob.glob("src/extensions/*.cpp"), **ext_kws)
60-
return [ext]
61-
62-
63-
# Extensions not included in pyproject.toml
64-
setup_args = dict(
65-
ext_modules=[],
68+
conda_prefix = os.environ["CONDA_PREFIX"]
69+
inc = os.path.join(conda_prefix, "Library", "include")
70+
lib = os.path.join(conda_prefix, "Library", "lib")
71+
rv = {"include_dirs": [], "library_dirs": []}
72+
rv["include_dirs"] += [inc]
73+
rv["library_dirs"] += [lib]
74+
return rv
75+
76+
77+
# ----------------------------------------------------------------------------
78+
79+
# compile and link options
80+
define_macros = []
81+
os_name = os.name
82+
if os_name == "nt":
83+
gcfg = get_gsl_config_win()
84+
else:
85+
gcfg = get_gsl_config()
86+
include_dirs = [MYDIR] + gcfg["include_dirs"]
87+
library_dirs = []
88+
libraries = []
89+
extra_objects = []
90+
extra_compile_args = []
91+
extra_link_args = []
92+
93+
compiler_type = get_compiler_type()
94+
if compiler_type in ("unix", "cygwin", "mingw32"):
95+
extra_compile_args = ["-std=c++11", "-Wall", "-Wno-write-strings", "-O3", "-funroll-loops", "-ffast-math"]
96+
extra_objects += ((p + "/libgsl.a") for p in gcfg["library_dirs"])
97+
elif compiler_type == "msvc":
98+
define_macros += [("_USE_MATH_DEFINES", None)]
99+
extra_compile_args = ["/EHs"]
100+
libraries += ["gsl"]
101+
library_dirs += gcfg["library_dirs"]
102+
# add optimization flags for other compilers if needed
103+
104+
105+
# define extension here
106+
pdffit2module = Extension(
107+
"diffpy.pdffit2.pdffit2",
108+
[
109+
"src/extensions/pdffit2module/bindings.cc",
110+
"src/extensions/pdffit2module/misc.cc",
111+
"src/extensions/pdffit2module/pdffit2module.cc",
112+
"src/extensions/pdffit2module/pyexceptions.cc",
113+
"src/extensions/libpdffit2/Atom.cc",
114+
"src/extensions/libpdffit2/LocalPeriodicTable.cc",
115+
"src/extensions/libpdffit2/OutputStreams.cc",
116+
"src/extensions/libpdffit2/PeriodicTable.cc",
117+
"src/extensions/libpdffit2/PointsInSphere.cc",
118+
"src/extensions/libpdffit2/StringUtils.cc",
119+
"src/extensions/libpdffit2/fit.cc",
120+
"src/extensions/libpdffit2/gaussj.cc",
121+
"src/extensions/libpdffit2/metric.cc",
122+
"src/extensions/libpdffit2/nrutil.cc",
123+
"src/extensions/libpdffit2/output.cc",
124+
"src/extensions/libpdffit2/parser.cc",
125+
"src/extensions/libpdffit2/pdf.cc",
126+
"src/extensions/libpdffit2/pdffit.cc",
127+
"src/extensions/libpdffit2/pdflsmin.cc",
128+
"src/extensions/libpdffit2/scatlen.cc",
129+
"src/extensions/libpdffit2/stru.cc",
130+
],
131+
include_dirs=include_dirs,
132+
libraries=libraries,
133+
library_dirs=library_dirs,
134+
define_macros=define_macros,
135+
extra_compile_args=extra_compile_args,
136+
extra_link_args=extra_link_args,
137+
extra_objects=extra_objects,
66138
)
67139

140+
setup_args = dict(
141+
packages=find_packages(where="src"),
142+
package_dir={"": "src"},
143+
ext_modules=[pdffit2module],
144+
# scripts=[]
145+
)
68146

69147
if __name__ == "__main__":
70-
setup_args["ext_modules"] = create_extensions()
71148
setup(**setup_args)
149+
150+
# End of file

0 commit comments

Comments
 (0)