-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathsetup.py
More file actions
589 lines (515 loc) · 19.5 KB
/
Copy pathsetup.py
File metadata and controls
589 lines (515 loc) · 19.5 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#
# setup script for fitsio, using setuptools
#
# c.f.
# https://packaging.python.org/guides/distributing-packages-using-setuptools/
from __future__ import print_function
from setuptools import setup, Extension
from setuptools.command.build_ext import (
build_ext,
new_compiler,
customize_compiler,
)
import warnings
import tempfile
import tarfile
import shlex
import sys
import os
import subprocess
from subprocess import PIPE
import glob
import shutil
if "FITSIO_FAIL_ON_BAD_PATCHES" in os.environ:
if os.environ["FITSIO_FAIL_ON_BAD_PATCHES"].lower() in ["false", "0"]:
FITSIO_FAIL_ON_BAD_PATCHES = False
else:
FITSIO_FAIL_ON_BAD_PATCHES = True
else:
FITSIO_FAIL_ON_BAD_PATCHES = True
if "--use-system-fitsio" in sys.argv:
del sys.argv[sys.argv.index("--use-system-fitsio")]
USE_SYSTEM_FITSIO = True
else:
USE_SYSTEM_FITSIO = False or "FITSIO_USE_SYSTEM_FITSIO" in os.environ
if "--system-fitsio-includedir" in sys.argv or any(
a.startswith("--system-fitsio-includedir=") for a in sys.argv
):
if "--system-fitsio-includedir" in sys.argv:
ind = sys.argv.index("--system-fitsio-includedir")
SYSTEM_FITSIO_INCLUDEDIR = sys.argv[ind + 1]
del sys.argv[ind + 1]
del sys.argv[ind]
else:
for ind in range(len(sys.argv)):
if sys.argv[ind].startswith("--system-fitsio-includedir="):
break
SYSTEM_FITSIO_INCLUDEDIR = sys.argv[ind].split("=", 1)[1]
del sys.argv[ind]
else:
SYSTEM_FITSIO_INCLUDEDIR = os.environ.get(
"FITSIO_SYSTEM_FITSIO_INCLUDEDIR",
None,
)
if "--system-fitsio-libdir" in sys.argv or any(
a.startswith("--system-fitsio-libdir=") for a in sys.argv
):
if "--system-fitsio-libdir" in sys.argv:
ind = sys.argv.index("--system-fitsio-libdir")
SYSTEM_FITSIO_LIBDIR = sys.argv[ind + 1]
del sys.argv[ind + 1]
del sys.argv[ind]
else:
for ind in range(len(sys.argv)):
if sys.argv[ind].startswith("--system-fitsio-libdir="):
break
SYSTEM_FITSIO_LIBDIR = sys.argv[ind].split("=", 1)[1]
del sys.argv[ind]
else:
SYSTEM_FITSIO_LIBDIR = os.environ.get(
"FITSIO_SYSTEM_FITSIO_LIBDIR",
None,
)
if "--system-fitsio-has-curl" in sys.argv:
del sys.argv[sys.argv.index("--system-fitsio-has-curl")]
SYSTEM_FITSIO_HAS_CURL = True
else:
SYSTEM_FITSIO_HAS_CURL = (
False or "FITSIO_SYSTEM_FITSIO_HAS_CURL" in os.environ
)
if "--system-fitsio-has-bzip2" in sys.argv:
del sys.argv[sys.argv.index("--system-fitsio-has-bzip2")]
SYSTEM_FITSIO_HAS_BZIP2 = True
else:
SYSTEM_FITSIO_HAS_BZIP2 = (
False or "FITSIO_SYSTEM_FITSIO_HAS_BZIP2" in os.environ
)
def _print_msg(text):
print("\n" + "=" * 79 + f"\n{text}\n" + "=" * 79, flush=True)
def _copy_update(dir1, dir2):
f1 = os.listdir(dir1)
for f in f1:
path1 = os.path.join(dir1, f)
path2 = os.path.join(dir2, f)
if os.path.isdir(path1):
if not os.path.exists(path2):
os.makedirs(path2)
_copy_update(path1, path2)
else:
if not os.path.exists(path2):
shutil.copy(path1, path2)
else:
stat1 = os.stat(path1)
stat2 = os.stat(path2)
if stat1.st_mtime > stat2.st_mtime:
shutil.copy(path1, path2)
class build_ext_subclass(build_ext):
cfitsio_version = '4.6.4'
cfitsio_dir = 'cfitsio-%s' % cfitsio_version
def finalize_options(self):
build_ext.finalize_options(self)
self.cfitsio_build_dir = os.path.abspath(
os.path.join(self.build_temp, self.cfitsio_dir)
)
self.cfitsio_patch_dir = os.path.abspath(
os.path.join(self.build_temp, 'patches')
)
self.cfitsio_cmake_build_dir = os.path.join(
self.cfitsio_build_dir, "build"
)
self.cfitsio_cmake_prefix_dir = os.path.join(
self.cfitsio_build_dir, "prefix"
)
if USE_SYSTEM_FITSIO:
if SYSTEM_FITSIO_INCLUDEDIR is not None:
for pth in SYSTEM_FITSIO_INCLUDEDIR.split(os.pathsep):
_print_msg(f"Adding include directory '{pth}'")
self.include_dirs.insert(0, pth)
if SYSTEM_FITSIO_LIBDIR is not None:
for pth in SYSTEM_FITSIO_LIBDIR.split(os.pathsep):
_print_msg(f"Adding lib directory '{pth}'")
self.library_dirs.insert(0, pth)
else:
if os.name == "nt":
self.include_dirs.insert(
0, os.path.join(self.cfitsio_cmake_prefix_dir, "include")
)
self.library_dirs.insert(
0, os.path.join(self.cfitsio_cmake_prefix_dir, "lib")
)
else:
self.include_dirs.insert(0, self.cfitsio_build_dir)
def run(self):
# For extensions that require 'numpy' in their include dirs,
# replace 'numpy' with the actual paths
import numpy
np_include = numpy.get_include()
for extension in self.extensions:
if 'numpy' in extension.include_dirs:
idx = extension.include_dirs.index('numpy')
extension.include_dirs.insert(idx, np_include)
extension.include_dirs.remove('numpy')
build_ext.run(self)
def build_extensions(self):
if not USE_SYSTEM_FITSIO:
# Use the compiler for building python to build cfitsio
# for maximized compatibility.
if os.name != "nt":
self.build_cfitsio_unix()
else:
self.build_cfitsio_win()
# on windows we build a full static lib so we have to tell
# the compiler to link against it
self.compiler.add_library('cfitsio')
else:
self.compiler.add_library('cfitsio')
# Check if system cfitsio was compiled with bzip2 and/or curl
if SYSTEM_FITSIO_HAS_BZIP2 or self.check_system_cfitsio_objects(
'bzip2'
):
_print_msg("linking Python extension to bzip2")
if os.name == "nt":
self.compiler.add_library('libbz2')
else:
self.compiler.add_library('bz2')
self.compiler.define_macro('FITSIO_HAS_BZIP2_SUPPORT')
else:
_print_msg("bzip2 support is disabled")
if SYSTEM_FITSIO_HAS_CURL or self.check_system_cfitsio_objects(
'curl_'
):
_print_msg("linking Python extension to curl")
if os.name == "nt":
self.compiler.add_library('libcurl')
else:
self.compiler.add_library('curl')
self.compiler.define_macro('FITSIO_HAS_CURL_SUPPORT')
else:
_print_msg("curl support is disabled")
self.compiler.define_macro('FITSIO_USING_SYSTEM_FITSIO')
self.compiler.add_library('z')
# fitsio requires libm as well, but do not need to link it
# explicitly on windows
if os.name != "nt":
self.compiler.add_library('m')
# call the original build_extensions
build_ext.build_extensions(self)
def build_cfitsio_win(self):
self.extract_cfitsio()
# we patch the source in the build dir to avoid mucking with the repo
self.patch_cfitsio()
os.makedirs(self.cfitsio_cmake_build_dir, exist_ok=True)
os.makedirs(self.cfitsio_cmake_prefix_dir, exist_ok=True)
env = {}
env.update(os.environ)
# make a new instance to get name without changing current instance
tmp_cc = new_compiler()
customize_compiler(tmp_cc)
tmp_cc.initialize(self.plat_name)
env["CC"] = tmp_cc.cc
_print_msg("setting windows compiler to " + env["CC"])
cmake_cmd = [
"cmake",
]
if "CMAKE_ARGS" in os.environ:
cmake_cmd += shlex.split(os.environ["CMAKE_ARGS"], posix=False)
cmake_cmd += [
"-G",
"NMake Makefiles",
f"-DCMAKE_INSTALL_PREFIX={self.cfitsio_cmake_prefix_dir}",
"-DCMAKE_BUILD_TYPE=Release",
"-DBUILD_SHARED_LIBS=Off",
"..",
]
_print_msg("windows cmake command: " + repr(cmake_cmd))
subprocess.run(
cmake_cmd,
check=True,
cwd=self.cfitsio_cmake_build_dir,
env=env,
)
subprocess.run(
["nmake"],
check=True,
cwd=self.cfitsio_cmake_build_dir,
)
subprocess.run(
["nmake", "install"],
check=True,
cwd=self.cfitsio_cmake_build_dir,
)
# figure out if we have curl support
r = subprocess.run(
[
"dumpbin",
"/symbols",
os.path.join(
self.cfitsio_cmake_prefix_dir, "lib", "cfitsio.lib"
),
],
check=True,
capture_output=True,
text=True,
)
found_curl = False
for line in (r.stdout + r.stderr).splitlines():
if "External" in line.split() and "_curl_" in line:
_print_msg("found curl symbol: " + line.strip())
found_curl = True
if found_curl:
_print_msg(
"found curl in symbols\nlinking Python extension to curl"
)
self.compiler.add_library('libcurl')
self.compiler.define_macro('FITSIO_HAS_CURL_SUPPORT')
else:
_print_msg(
"did not find curl in symbols\ncurl support is disabled"
)
def build_cfitsio_unix(self):
CCold = self.compiler.compiler
if 'ccache' in CCold:
CC = []
for val in CCold:
if val == 'ccache':
print("removing ccache from the compiler options")
continue
CC.append(val)
else:
CC = None
self.configure_cfitsio_unix(
CC=CC,
ARCHIVE=self.compiler.archiver,
RANLIB=self.compiler.ranlib,
)
# If configure detected bzlib.h, we have to link to libbz2
with open(os.path.join(self.cfitsio_build_dir, 'Makefile')) as fp:
_makefile = fp.read()
_have_bzip2 = False
_have_curl = False
for line in _makefile.splitlines():
for _part in line.split("="):
for _eqpart in _part.split():
if "-lbz2" in _eqpart:
_have_bzip2 = True
if "-lcurl" in _eqpart:
_have_curl = True
if _have_bzip2:
_print_msg(
"found -lbz2 in Makefile\n"
"linking Python extension to bzip2"
)
self.compiler.add_library('bz2')
self.compiler.define_macro('FITSIO_HAS_BZIP2_SUPPORT')
else:
_print_msg(
"did not find -lbz2 in Makefile\nbzip2 support is disabled"
)
if _have_curl:
_print_msg(
"found -lcurl in Makefile\n"
"linking Python extension to curl"
)
self.compiler.add_library('curl')
self.compiler.define_macro('FITSIO_HAS_CURL_SUPPORT')
else:
_print_msg(
"did not find -lcurl in Makefile\ncurl support is disabled"
)
self.compile_cfitsio_unix()
# link against the .a library in cfitsio;
# It should have been a 'static' library of relocatable objects
# (-fPIC), since we use the python compiler flags
link_objects = glob.glob(os.path.join(self.cfitsio_build_dir, '*.o'))
self.compiler.set_link_objects(link_objects)
# Ultimate hack: append the .a files to the dependency list
# so they will be properly rebuild if cfitsio source is updated.
for ext in self.extensions:
ext.depends += link_objects
def patch_cfitsio(self):
_print_msg("patching cfitsio")
if not os.path.exists(self.cfitsio_patch_dir):
os.makedirs(self.cfitsio_patch_dir)
_copy_update('patches', self.cfitsio_patch_dir)
try:
subprocess.check_call(["patch", "-v"])
except subprocess.CalledProcessError as e:
warnings.warn(
"`patch` command not found! "
"Some bugs in cfitsio may not be fixed! "
"See the patches we carry at "
"https://github.com/esheldon/fitsio/tree/master/patches."
)
if FITSIO_FAIL_ON_BAD_PATCHES:
raise e
else:
return
patches = glob.glob(os.path.join(self.cfitsio_patch_dir, '*.patch'))
for patch in patches:
try:
print("trying patch:", patch, flush=True)
subprocess.run(
[
"patch",
"-N",
"--dry-run",
"-p1",
"--batch",
"--reject-file=/dev/null",
"--input",
patch,
],
check=True,
cwd=self.cfitsio_build_dir,
)
except subprocess.CalledProcessError as e:
warnings.warn(
"Failed to apply patch: " + os.path.basename(patch)
)
if FITSIO_FAIL_ON_BAD_PATCHES:
raise e
else:
print("applying patch:", patch, flush=True)
subprocess.run(
[
"patch",
"-p1",
"--batch",
"--reject-file=/dev/null",
"--input",
patch,
],
check=True,
cwd=self.cfitsio_build_dir,
)
def extract_cfitsio(self):
if not os.path.exists(self.cfitsio_build_dir):
os.makedirs(self.cfitsio_build_dir)
if sys.version_info.major >= 3 and sys.version_info.minor >= 12:
tar_kwargs = {"filter": "fully_trusted"}
else:
tar_kwargs = {}
with tempfile.TemporaryDirectory() as tmpdir:
if os.path.exists(self.cfitsio_dir) and os.path.isdir(
self.cfitsio_dir
):
_print_msg(
"using cfitsio source code from "
f"{self.cfitsio_dir} for debugging"
)
_copy_update(
self.cfitsio_dir,
self.cfitsio_build_dir,
)
else:
with tarfile.open(self.cfitsio_dir + ".tar.gz", "r:gz") as tar:
tar.extractall(path=tmpdir, **tar_kwargs)
_copy_update(
os.path.join(tmpdir, self.cfitsio_dir),
self.cfitsio_build_dir,
)
with tempfile.TemporaryDirectory() as tmpdir:
with tarfile.open("zlib.tar.gz", "r:gz") as tar:
tar.extractall(path=tmpdir, **tar_kwargs)
_copy_update(
os.path.join(tmpdir, "zlib"), self.cfitsio_build_dir
)
def configure_cfitsio_unix(self, CC=None, ARCHIVE=None, RANLIB=None):
self.extract_cfitsio()
# we patch the source in the build dir to avoid mucking with the repo
self.patch_cfitsio()
makefile = os.path.join(self.cfitsio_build_dir, 'Makefile')
if os.path.exists(makefile):
# Makefile already there
_print_msg("found Makefile so not running configure!")
return
else:
_print_msg("configuring cfitsio")
# the latest cfitsio build system links its example
# programs (e.g., `cookbook`) against the shared library.
# when we use `-fvisibility=hidden` in the CFLAGS (
# needed to hide the cfitsio symbols in the python `.so``),
# the linking against the shared library fails.
# so we disable shared libraries with (`--disable-shared``)
# and add `-fPIC` to the flags to ensure the python `.so`
# works properly later
args = [
'--without-fortran',
'--disable-shared',
'--enable-reentrant',
# we bundle zlib directly so skip check
'--without-zlib-check',
]
# we use -fPIC and -fvisibility=hidden to ensure we build a linkable
# static library with the symbols hidden
# the -ffp-contract=off flag ensures we do not use FMA instructions on
# ARM systems so that lossy floating point compression is reproducible
our_cflags = "-fPIC -fvisibility=hidden -ffp-contract=off"
if "FITSIO_BZIP2_DIR" in os.environ:
if not os.environ["FITSIO_BZIP2_DIR"]:
args += ["--with-bzip2"]
else:
args += ['--with-bzip2="%s"' % os.environ["FITSIO_BZIP2_DIR"]]
else:
# let autoconf detect if we have bzip2
args += ['--with-bzip2']
env = {}
env.update(os.environ)
if CC is not None:
env["CC"] = ' '.join(CC[:1])
env["CFLAGS"] = ' '.join(CC[1:]) + our_cflags
else:
if "CFLAGS" in os.environ:
env["CFLAGS"] = os.environ["CFLAGS"] + " " + our_cflags
else:
env["CFLAGS"] = our_cflags
if ARCHIVE:
env["ARCHIVE"] = ' '.join(ARCHIVE)
if RANLIB:
env["RANLIB"] = ' '.join(RANLIB)
res = subprocess.run(
["sh", "./configure"] + args,
cwd=self.cfitsio_build_dir,
env=env,
)
if res.returncode != 0:
with open(
os.path.join(self.cfitsio_build_dir, "config.log")
) as fp:
logfile = fp.read()
raise ValueError(
"could not configure cfitsio %s: config.log:\n\n%s"
% (
self.cfitsio_version,
logfile,
)
)
def compile_cfitsio_unix(self):
_print_msg("building cfitsio")
res = subprocess.run(
"make",
cwd=self.cfitsio_build_dir,
)
if res.returncode != 0:
raise ValueError(
"could not compile cfitsio %s" % self.cfitsio_version
)
def check_system_cfitsio_objects(self, obj_name):
for lib_dir in self.library_dirs:
if os.path.isfile('%s/libcfitsio.a' % (lib_dir)):
res = subprocess.run(
["nm", "-g", "%s/libcfitsio.a" % lib_dir],
stdout=PIPE,
stderr=PIPE,
)
for line in res.stdout.decode("utf-8").splitlines():
if obj_name in line:
return True
return False
return False
sources = ["fitsio/fitsio_pywrap.c"]
ext = Extension("fitsio._fitsio_wrap", sources, include_dirs=['numpy'])
setup(
ext_modules=[ext],
cmdclass={"build_ext": build_ext_subclass},
)