|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | # Extensions script for diffpy.pdffit2 |
4 | | -"""PDFfit2 - real space structure refinement engine |
5 | | -
|
6 | | -Packages: diffpy.pdffit2 |
7 | | -Scripts: pdffit2 |
8 | | -""" |
9 | 4 |
|
10 | 5 | import glob |
11 | | -import os |
12 | | -import re |
13 | | -import shutil |
14 | | -import sys |
15 | | -import warnings |
16 | | -from pathlib import Path |
| 6 | +import os # noqa |
| 7 | +import re # noqa |
| 8 | +import sys # noqa |
17 | 9 |
|
18 | 10 | from setuptools import Extension, setup |
19 | | -from setuptools.command.build_ext import build_ext |
20 | | - |
21 | | -# Use this version when git data are not available, like in git zip archive. |
22 | | -# Update when tagging a new release. |
23 | | -FALLBACK_VERSION = "1.4.3" |
24 | | - |
25 | | -MYDIR = str(Path(__file__).parent.resolve()) |
26 | | - |
27 | | -# Helper functions ----------------------------------------------------------- |
28 | | - |
29 | | - |
30 | | -def get_compiler_type(): |
31 | | - """Return the compiler type used during the build.""" |
32 | | - cc_arg = [a for a in sys.argv if a.startswith("--compiler=")] |
33 | | - if cc_arg: |
34 | | - return cc_arg[-1].split("=", 1)[1] |
35 | | - from distutils.ccompiler import new_compiler |
36 | | - |
37 | | - return new_compiler().compiler_type |
38 | | - |
39 | | - |
40 | | -def get_gsl_config(): |
41 | | - """ |
42 | | - Determine the GSL include and library directories by trying in order: |
43 | | - 1) CONDA_PREFIX, |
44 | | - 2) GSL_PATH, |
45 | | - 3) gsl-config (for Unix-like systems). |
46 | | - Raises EnvironmentError if none are found. |
47 | | - """ |
48 | | - rv = {"include_dirs": [], "library_dirs": []} |
49 | | - |
50 | | - # 1. Check using CONDA_PREFIX. |
51 | | - conda_prefix = os.environ.get("CONDA_PREFIX", "") |
52 | | - if conda_prefix: |
53 | | - if os.name == "nt": |
54 | | - inc = Path(conda_prefix) / "Library" / "include" |
55 | | - lib = Path(conda_prefix) / "Library" / "lib" |
56 | | - else: |
57 | | - inc = Path(conda_prefix) / "include" |
58 | | - lib = Path(conda_prefix) / "lib" |
59 | | - if inc.is_dir() and lib.is_dir(): |
60 | | - rv["include_dirs"].append(str(inc)) |
61 | | - rv["library_dirs"].append(str(lib)) |
62 | | - return rv |
63 | | - else: |
64 | | - warnings.warn( |
65 | | - f"CONDA_PREFIX is set to {conda_prefix}, " |
66 | | - "but GSL not found at those paths. Proceeding..." |
67 | | - ) |
68 | 11 |
|
69 | | - # 2. Check using GSL_PATH. |
70 | | - gsl_path = os.environ.get("GSL_PATH", "") |
71 | | - if gsl_path: |
72 | | - inc = Path(gsl_path) / "include" |
73 | | - lib = Path(gsl_path) / "lib" |
74 | | - if inc.is_dir() and lib.is_dir(): |
75 | | - rv["include_dirs"].append(str(inc)) |
76 | | - rv["library_dirs"].append(str(lib)) |
77 | | - return rv |
78 | | - else: |
79 | | - raise EnvironmentError( |
80 | | - f"GSL_PATH={gsl_path} is set, but {inc} or {lib} not found. " |
81 | | - "Please verify your GSL_PATH." |
82 | | - ) |
83 | | - |
84 | | - # 3. Try using the gsl-config executable (only on Unix-like systems). |
85 | | - if os.name != "nt": |
86 | | - path_dirs = os.environ.get("PATH", "").split(os.pathsep) |
87 | | - gslcfg_paths = [Path(p) / "gsl-config" for p in path_dirs if p] |
88 | | - gslcfg_paths = [p for p in gslcfg_paths if p.is_file()] |
89 | | - if gslcfg_paths: |
90 | | - gslcfg = gslcfg_paths[0] |
91 | | - txt = gslcfg.read_text() |
92 | | - prefix_match = re.search(r"(?m)^prefix=(.+)", txt) |
93 | | - include_match = re.search(r"(?m)^[^#]*\s-I(\S+)", txt) |
94 | | - lib_match = re.search(r"(?m)^[^#]*\s-L(\S+)", txt) |
95 | | - if prefix_match: |
96 | | - prefix_path = Path(prefix_match.group(1)) |
97 | | - inc_dir = ( |
98 | | - include_match.group(1) |
99 | | - if include_match |
100 | | - else (prefix_path / "include") |
101 | | - ) |
102 | | - lib_dir = ( |
103 | | - lib_match.group(1) if lib_match else (prefix_path / "lib") |
104 | | - ) |
105 | | - rv["include_dirs"].append(str(inc_dir)) |
106 | | - rv["library_dirs"].append(str(lib_dir)) |
107 | | - return rv |
108 | | - else: |
109 | | - raise RuntimeError(f"Cannot parse 'prefix=' from {gslcfg}.") |
110 | | - else: |
111 | | - warnings.warn( |
112 | | - "No gsl-config found in PATH. GSL may not be installed or not in PATH. " |
113 | | - "Proceeding without GSL configuration." |
114 | | - ) |
115 | | - |
116 | | - # 4. Nothing found: raise error. |
117 | | - raise EnvironmentError( |
118 | | - "Unable to locate GSL:\n" |
119 | | - "1) CONDA_PREFIX not set or no GSL there\n" |
120 | | - "2) GSL_PATH not set or invalid\n" |
121 | | - "3) gsl-config not available\n" |
122 | | - "Please set GSL_PATH or use a conda environment with GSL." |
123 | | - ) |
124 | | - |
125 | | - |
126 | | -class CustomBuildExt(build_ext): |
127 | | - def run(self): |
128 | | - # Retrieve the GSL library directories and append them to each extension. |
129 | | - gsl_cfg = get_gsl_config() |
130 | | - lib_dirs = gsl_cfg.get("library_dirs", []) |
131 | | - for ext in self.extensions: |
132 | | - # Add gsl lib for linking. |
133 | | - ext.library_dirs.extend(lib_dirs) |
134 | | - # Embed RPATH flags, runtime linking without LD_LIBRARY_PATH. |
135 | | - ext.extra_link_args = ext.extra_link_args or [] |
136 | | - for lib in lib_dirs: |
137 | | - ext.extra_link_args.append(f"-Wl,-rpath,{lib}") |
138 | | - super().run() |
139 | | - # Avoid dll error |
140 | | - gsl_path = ( |
141 | | - Path(os.environ.get("GSL_PATH")) |
142 | | - if os.environ.get("GSL_PATH") |
143 | | - else Path(os.environ.get("CONDA_PREFIX", "")) / "Library" |
144 | | - ) |
145 | | - bin_path = gsl_path / "bin" |
146 | | - dest_path = Path(self.build_lib) / "diffpy" / "pdffit2" |
147 | | - dest_path.mkdir(parents=True, exist_ok=True) |
148 | | - for dll_file in bin_path.glob("gsl*.dll"): |
149 | | - shutil.copy(str(dll_file), str(dest_path)) |
| 12 | +# Define extension arguments here |
| 13 | +ext_kws = { |
| 14 | + "libraries": [], |
| 15 | + "extra_compile_args": [], |
| 16 | + "extra_link_args": [], |
| 17 | + "include_dirs": [], |
| 18 | +} |
150 | 19 |
|
151 | 20 |
|
152 | 21 | def create_extensions(): |
153 | | - """Create the list of Extension objects for the build.""" |
154 | | - # lazy evaluation prevents build sdist failure |
155 | | - try: |
156 | | - gcfg = get_gsl_config() |
157 | | - except EnvironmentError: |
158 | | - return [] |
159 | | - |
160 | | - libraries = ["gsl"] |
161 | | - |
162 | | - include_dirs = [MYDIR] + gcfg["include_dirs"] |
163 | | - library_dirs = gcfg["library_dirs"] |
164 | | - define_macros = [] |
165 | | - extra_objects = [] |
166 | | - extra_compile_args = [] |
167 | | - extra_link_args = [] |
168 | | - |
169 | | - compiler_type = get_compiler_type() |
170 | | - if compiler_type in ("unix", "cygwin", "mingw32"): |
171 | | - extra_compile_args = [ |
172 | | - "-std=c++11", |
173 | | - "-Wall", |
174 | | - "-Wno-write-strings", |
175 | | - "-O3", |
176 | | - "-funroll-loops", |
177 | | - "-ffast-math", |
178 | | - ] |
179 | | - elif compiler_type == "msvc": |
180 | | - define_macros += [("_USE_MATH_DEFINES", None)] |
181 | | - extra_compile_args = ["/EHs"] |
182 | | - |
183 | | - # Extension keyword arguments. |
184 | | - ext_kws = { |
185 | | - "include_dirs": include_dirs, |
186 | | - "libraries": libraries, |
187 | | - "library_dirs": library_dirs, |
188 | | - "define_macros": define_macros, |
189 | | - "extra_compile_args": extra_compile_args, |
190 | | - "extra_link_args": extra_link_args, |
191 | | - "extra_objects": extra_objects, |
192 | | - } |
| 22 | + "Initialize Extension objects for the setup function." |
193 | 23 | ext = Extension( |
194 | | - "diffpy.pdffit2.pdffit2", |
195 | | - glob.glob("src/extensions/**/*.cc"), |
196 | | - **ext_kws, |
| 24 | + "diffpy.pdffit2.pdffit2", glob.glob("src/extensions/*.cpp"), **ext_kws |
197 | 25 | ) |
198 | 26 | return [ext] |
199 | 27 |
|
200 | 28 |
|
201 | 29 | # Extensions not included in pyproject.toml |
202 | 30 | setup_args = dict( |
203 | 31 | ext_modules=[], |
204 | | - cmdclass={"build_ext": CustomBuildExt}, |
205 | 32 | ) |
206 | 33 |
|
207 | 34 |
|
|
0 commit comments