forked from scikit-beam/scikit-beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupext.py
112 lines (84 loc) · 2.49 KB
/
setupext.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
#!/usr/bin/env python
# Copyright (c) Brookhaven National Lab 2O14
# All rights reserved
# BSD License
# See LICENSE for full text
"""
setupext.py is for c routines
"""
import os
import sys
import six
from six.moves import configparser
import numpy as np
import copy
from distutils.core import setup, Extension
options = {'build_ctrans': False}
ext_default = {'include_dirs': [np.get_include()],
'library_dirs': [],
'libraries': [],
'define_macros': []}
setup_files = ['setup.cfg.%s' % sys.platform, 'setup.cfg']
def detectCPUs():
# Linux, Unix and MacOS:
if hasattr(os, "sysconf"):
if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
# Linux & Unix:
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else:
# OSX:
return int(os.popen2("sysctl -n hw.ncpu")[1].read())
# Windows:
if os.environ.has_key("NUMBER_OF_PROCESSORS"):
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
if ncpus > 0:
return ncpus
return 1 # Default
def parseExtensionSetup(name, config, default):
default = copy.deepcopy(default)
try:
default['include_dirs'] = config.get(name, "include_dirs").split(os.pathsep)
except:
pass
try:
default['library_dirs'] = config.get(name, "library_dirs").split(os.pathsep)
except:
pass
try:
default['libraries'] = config.get(name, "libraries").split(",")
except:
pass
return default
setupfile = None
for f in setup_files:
if os.path.exists(f):
setupfile = f
break
if setupfile is not None:
config = configparser.SafeConfigParser()
config.read(setupfile)
print(config)
try:
options['build_ctrans'] = config.getboolean("ctrans", "build")
except:
pass
ctrans = parseExtensionSetup('ctrans', config, ext_default)
threads = False
try:
threads = config.getboolean("ctrans", "usethreads")
except:
pass
nthreads = detectCPUs() * 2
try:
nthreads = config.getint("ctrans", "max_threads")
except:
pass
if threads:
ctrans['define_macros'].append(('USE_THREADS', None))
ctrans['define_macros'].append(('NTHREADS', nthreads))
ext_modules = []
if options['build_ctrans']:
ext_modules.append(Extension('ctrans', ['src/ctrans.c'],
**ctrans))