forked from SLACKHA/pyJac-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_helper.py
459 lines (344 loc) · 13.5 KB
/
setup_helper.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
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
"""
Setuptools helper to deal with a default / user specified siteconf for OpenCL
execution.
Borrowed heavily from `pyopencl`_'s aksetup_helper.py, from which the siteconf
idea is based upon.
. _pyopencl https://documen.tician.de/pyopencl/
"""
import sys
def configure_frontend():
from optparse import OptionParser
schema = get_config_schema()
if schema.have_config():
print("************************************************************")
print("*** I have detected that you have already run configure.")
print("*** I'm taking the configured values as defaults for this")
print("*** configure run. If you don't want this, delete the file")
print("*** %s." % schema.get_conf_file())
print("************************************************************")
description = "generate a configuration file for this software package"
parser = OptionParser(description=description)
parser.add_option(
"--python-exe", dest="python_exe", default=sys.executable,
help="Which Python interpreter to use", metavar="PATH")
parser.add_option("--prefix", default=None,
help="Ignored")
parser.add_option("--enable-shared", help="Ignored", action="store_false")
parser.add_option("--disable-static", help="Ignored", action="store_false")
parser.add_option("--update-user",
help="Update user config file ({})".format(
schema.user_conf_file),
action="store_true")
parser.add_option("--update-global",
help="Update global config file ({})".format(
schema.global_conf_file),
action="store_true")
schema.add_to_configparser(parser, schema.read_config())
options, args = parser.parse_args()
config = schema.get_from_configparser(options)
schema.write_config(config)
if options.update_user:
schema.update_user_config(config)
if options.update_global:
schema.update_global_config(config)
# {{{ configure guts
def default_or(a, b):
if a is None:
return b
else:
return a
def expand_str(s, options):
import re
def my_repl(match):
sym = match.group(1)
try:
repl = options[sym]
except KeyError:
from os import environ
repl = environ[sym]
return expand_str(repl, options)
return re.subn(r"\$\{([a-zA-Z0-9_]+)\}", my_repl, s)[0]
def expand_value(v, options):
if isinstance(v, str):
return expand_str(v, options)
elif isinstance(v, list):
result = []
for i in v:
try:
exp_i = expand_value(i, options)
except:
pass
else:
result.append(exp_i)
return result
else:
return v
def expand_options(options):
return dict(
(k, expand_value(v, options)) for k, v in options.items())
class ConfigSchema:
def __init__(self, options, conf_file="siteconf.py", conf_dir="."):
self.optdict = dict((opt.name, opt) for opt in options)
self.options = options
self.conf_dir = conf_dir
self.conf_file = conf_file
from os.path import expanduser
self.user_conf_file = expanduser("~/.aksetup-defaults.py")
if not sys.platform.lower().startswith("win"):
self.global_conf_file = "/etc/aksetup-defaults.py"
else:
self.global_conf_file = None
def get_conf_file(self):
import os
return os.path.join(self.conf_dir, self.conf_file)
def set_conf_dir(self, conf_dir):
self.conf_dir = conf_dir
def get_default_config(self):
return dict((opt.name, opt.default) for opt in self.options)
def read_config_from_pyfile(self, filename):
result = {}
filevars = {}
infile = open(filename, "r")
try:
contents = infile.read()
finally:
infile.close()
exec(compile(contents, filename, "exec"), filevars)
for key, value in filevars.items():
if key in self.optdict:
result[key] = value
return result
def update_conf_file(self, filename, config):
result = {}
filevars = {}
try:
exec(compile(open(filename, "r").read(), filename, "exec"), filevars)
except IOError:
pass
if "__builtins__" in filevars:
del filevars["__builtins__"]
for key, value in config.items():
if value is not None:
filevars[key] = value
keys = filevars.keys()
keys.sort()
outf = open(filename, "w")
for key in keys:
outf.write("%s = %s\n" % (key, repr(filevars[key])))
outf.close()
return result
def update_user_config(self, config):
self.update_conf_file(self.user_conf_file, config)
def update_global_config(self, config):
if self.global_conf_file is not None:
self.update_conf_file(self.global_conf_file, config)
def get_default_config_with_files(self):
result = self.get_default_config()
import os
confignames = []
if self.global_conf_file is not None:
confignames.append(self.global_conf_file)
confignames.append(self.user_conf_file)
for fn in confignames:
if os.access(fn, os.R_OK):
result.update(self.read_config_from_pyfile(fn))
return result
def have_global_config(self):
import os
result = os.access(self.user_conf_file, os.R_OK)
if self.global_conf_file is not None:
result = result or os.access(self.global_conf_file, os.R_OK)
return result
def have_config(self):
import os
return os.access(self.get_conf_file(), os.R_OK)
def update_from_python_snippet(self, config, py_snippet, filename):
filevars = {}
exec(compile(py_snippet, filename, "exec"), filevars)
for key, value in filevars.items():
if key in self.optdict:
config[key] = value
elif key == "__builtins__":
pass
else:
raise KeyError("invalid config key in %s: %s" % (
filename, key))
def update_config_from_and_modify_command_line(self, config, argv):
cfg_prefix = "--conf:"
i = 0
while i < len(argv):
arg = argv[i]
if arg.startswith(cfg_prefix):
del argv[i]
self.update_from_python_snippet(
config, arg[len(cfg_prefix):], "<command line>")
else:
i += 1
return config
def read_config(self):
import os
cfile = self.get_conf_file()
result = self.get_default_config_with_files()
if os.access(cfile, os.R_OK):
with open(cfile, "r") as inf:
py_snippet = inf.read()
self.update_from_python_snippet(result, py_snippet, cfile)
return result
def add_to_configparser(self, parser, def_config=None):
if def_config is None:
def_config = self.get_default_config_with_files()
for opt in self.options:
default = default_or(def_config.get(opt.name), opt.default)
opt.add_to_configparser(parser, default)
def get_from_configparser(self, options):
result = {}
for opt in self.options:
result[opt.name] = opt.take_from_configparser(options)
return result
def write_config(self, config):
outf = open(self.get_conf_file(), "w")
for opt in self.options:
value = config[opt.name]
if value is not None:
outf.write("%s = %s\n" % (opt.name, repr(config[opt.name])))
outf.close()
def make_substitutions(self, config):
return dict((opt.name, opt.value_to_str(config[opt.name]))
for opt in self.options)
class Option(object):
def __init__(self, name, default=None, help=None):
self.name = name
self.default = default
self.help = help
def as_option(self):
return self.name.lower().replace("_", "-")
def metavar(self):
last_underscore = self.name.rfind("_")
return self.name[last_underscore+1:]
def get_help(self, default):
result = self.help
if self.default:
result += " (default: %s)" % self.value_to_str(
default_or(default, self.default))
return result
def value_to_str(self, default):
return default
def add_to_configparser(self, parser, default=None):
default = default_or(default, self.default)
default_str = self.value_to_str(default)
parser.add_option(
"--" + self.as_option(), dest=self.name,
default=default_str,
metavar=self.metavar(), help=self.get_help(default))
def take_from_configparser(self, options):
return getattr(options, self.name)
class StringListOption(Option):
def value_to_str(self, default):
if default is None:
return None
return ",".join([str(el).replace(",", r"\,") for el in default])
def get_help(self, default):
return Option.get_help(self, default) + " (several ok)"
def take_from_configparser(self, options):
opt = getattr(options, self.name)
if opt is None:
return None
else:
if opt:
import re
sep = re.compile(r"(?<!\\),")
result = sep.split(opt)
result = [i.replace(r"\,", ",") for i in result]
return result
else:
return []
class IncludeDir(StringListOption):
def __init__(self, lib_name, default=None, human_name=None, help=None):
StringListOption.__init__(
self, "%s_INC_DIR" % lib_name, default,
help=help or ("Include directories for %s" % (
human_name or humanize(lib_name))))
class LibraryDir(StringListOption):
def __init__(self, lib_name, default=None, human_name=None, help=None):
StringListOption.__init__(
self, "%s_LIB_DIR" % lib_name, default,
help=help or ("Library directories for %s" % (
human_name or humanize(lib_name))))
class Libraries(StringListOption):
def __init__(self, lib_name, default=None, human_name=None, help=None):
StringListOption.__init__(
self, "%s_LIBNAME" % lib_name, default,
help=help or ("Library names for %s (without lib or .so)" % (
human_name or humanize(lib_name))))
# {{{ tools
def flatten(list):
"""For an iterable of sub-iterables, generate each member of each
sub-iterable in turn, i.e. a flattened version of that super-iterable.
Example: Turn [[a,b,c],[d,e,f]] into [a,b,c,d,e,f].
"""
for sublist in list:
for j in sublist:
yield j
def humanize(sym_str):
words = sym_str.lower().replace("_", " ").split(" ")
return " ".join([word.capitalize() for word in words])
# }}}
def get_config_schema():
default_cxxflags = ['-std=gnu++11']
default_ccflags = []
default_clflags = []
if 'darwin' in sys.platform:
import platform
osx_ver, _, _ = platform.mac_ver()
osx_ver = '.'.join(osx_ver.split('.')[:2])
sysroot_paths = [
"/Applications/Xcode.app/Contents/Developer/Platforms/"
"MacOSX.platform/Developer/SDKs/MacOSX%s.sdk" % osx_ver,
"/Developer/SDKs/MacOSX%s.sdk" % osx_ver
]
default_libs = []
default_cxxflags = default_cxxflags + [
'-stdlib=libc++', '-mmacosx-version-min=10.7',
'-arch', 'i386', '-arch', 'x86_64'
]
from os.path import isdir
for srp in sysroot_paths:
if isdir(srp):
default_cxxflags.extend(['-isysroot', srp])
break
default_ldflags = default_cxxflags[:] + ["-Wl,-framework,OpenCL"]
else:
default_libs = ["OpenCL"]
if "linux" in sys.platform:
# Requested in
# https://github.com/inducer/pyopencl/issues/132#issuecomment-314713573
# to make life with Altera FPGAs less painful by default.
default_ldflags = ["-Wl,--no-as-needed"]
else:
default_ldflags = []
return ConfigSchema([
Option("CL_VERSION", '1.2',
"Dotted CL version (e.g. 1.2) which you'd like to use. "
"By default, we use OpenCL 1.2"),
IncludeDir("CL", []),
LibraryDir("CL", []),
Libraries("CL", default_libs),
IncludeDir("ADEPT", []),
LibraryDir("ADEPT", []),
Libraries("ADEPT", ['adept']),
StringListOption("CC_FLAGS", default_ccflags,
help="Any extra C compiler options to include"),
StringListOption("CL_FLAGS", default_clflags,
help="Any extra OpenCL compiler options to include"),
StringListOption("CXXFLAGS", default_cxxflags,
help="Any extra C++ compiler options to include"),
StringListOption("LDFLAGS", default_ldflags,
help="Any extra linker options to include"),
])
# {{{ siteconf handling
def get_config(schema=None, warn_about_no_config=True):
if schema is None:
schema = get_config_schema()
config = expand_options(schema.read_config())
schema.update_config_from_and_modify_command_line(config, sys.argv)
return config