-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.py
executable file
·384 lines (302 loc) · 11 KB
/
configure.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
#!/usr/bin/env python3
import collections
import os
import platform
import subprocess as sp
import sys
from functools import partial
from itertools import chain
import click
import pystache as mustache
PROJECT_PATH = os.getcwd()
GEN_FILES = {
'Tuprules.tup.in': 'Tuprules.tup',
}
DYLIB_SUFFIXES = {
'darwin': 'dylib',
'linux': 'so',
'win': 'dll',
}
STLIB_SUFFIXES = {
'darwin': 'a',
'linux': 'a',
'win': 'lib',
}
OBJ_SUFFIXES = {
'darwin': 'o',
'linux': 'o',
'win': 'obj',
}
IS_WINDOWS = sys.platform.startswith('win')
IS_LINUX = sys.platform.startswith('linux')
IS_OSX = sys.platform.startswith('darwin')
def get_platform_file_suffix(suffix_map):
for k, v in suffix_map.items():
if sys.platform.startswith(k):
return v
raise UnsupportedPlatformError
EXECUTABLE_NAME = '{}-{}'.format(
os.path.basename(PROJECT_PATH).lower(),
platform.machine().lower()
)
CompilerSpec = collections.namedtuple(
'CompilerSpec',
(
'cc_rule',
'link_rule',
'dylib_link_rule',
'stlib_link_rule',
'cflags',
'ldflags',
'inc_dir_flag',
'lib_dir_flag',
'lib_flag',
)
)
class UnsupportedPlatformError(RuntimeError):
def __init__(self):
super(UnsupportedPlatformError, self).__init__(
'unsupported platform "{}"'.format(sys.platform)
)
class Config():
def __init__(self, **kwargs):
self.executable = kwargs.get('executable', EXECUTABLE_NAME)
self.project_path = kwargs.get('project_path', PROJECT_PATH)
self.inc_paths = kwargs.get('inc_paths', {})
self.lib_paths = kwargs.get('lib_paths', {})
self.debug = kwargs.get('debug', True)
self.compiler_spec = None
self.objects = []
self.deps = []
self.cflags = {}
self.ldflags = {}
def as_dict(self):
def expand_flag(fmtstr, arg):
return fmtstr.format(arg)
config = {
'project_path': self.project_path,
'build_mode': 'debug' if self.debug else 'release',
'cc_rule': self.compiler_spec.cc_rule,
'link_rule': self.compiler_spec.link_rule,
'dylib_link_rule': self.compiler_spec.dylib_link_rule,
'dylib_suffix': get_platform_file_suffix(DYLIB_SUFFIXES),
'stlib_link_rule': self.compiler_spec.stlib_link_rule,
'cflags': self.compiler_spec.cflags,
'ldflags': self.compiler_spec.ldflags,
'inc_dir_flag': partial(expand_flag, self.compiler_spec.inc_dir_flag),
'lib_dir_flag': partial(expand_flag, self.compiler_spec.lib_dir_flag),
'lib_flag': partial(expand_flag, self.compiler_spec.lib_flag),
'stlib_suffix': get_platform_file_suffix(STLIB_SUFFIXES),
'obj_suffix': get_platform_file_suffix(OBJ_SUFFIXES),
'executable': self.executable,
}
config.update({
'{}_cflags'.format(key): ' '.join(sorted(value)) for key, value in self.cflags.items()
})
config.update({
'{}_ldflags'.format(key): ' '.join(sorted(value)) for key, value in self.ldflags.items()
})
return config
def find_executable(executable, path=None):
"""
Find if 'executable' can be run. Looks for it in 'path'
(string that lists directories separated by 'os.pathsep';
defaults to os.environ['PATH']). Checks for all executable
extensions. Returns full path or None if no command is found.
"""
if path is None:
path = os.environ['PATH']
paths = path.split(os.pathsep)
extlist = ['']
if os.name == 'os2':
(_, ext) = os.path.splitext(executable)
# executable files on OS/2 can have an arbitrary extension, but
# .exe is automatically appended if no dot is present in the name
if not ext:
executable = executable + ".exe"
elif sys.platform == 'win32':
pathext = os.environ['PATHEXT'].lower().split(os.pathsep)
(_, ext) = os.path.splitext(executable)
if ext.lower() not in pathext:
extlist = pathext
for ext in extlist:
execname = executable + ext
if os.path.isfile(execname):
return execname
else:
for p in paths:
f = os.path.join(p, execname)
if os.path.isfile(f):
return f
return None
def check_gcc(debug):
path = find_executable('gcc')
if path:
cflags = '-Wall -Werror -Wextra -std=c99 -pedantic'
ldflags = ''
if debug:
cflags = cflags + ' -g -DDEBUG'
else:
cflags = cflags + ' -O3'
return CompilerSpec(
cc_rule='{path} $(CFLAGS) -fPIC -c %f -o %o'.format(path=path),
link_rule='{path} %f $(LDFLAGS) -o %o'.format(path=path),
dylib_link_rule='{path} $(LDFLAGS) -shared %f -o %o'.format(path=path),
stlib_link_rule='{path} -Wl,-r -no-pie %f -o %o -nostdlib'.format(path=path),
cflags=cflags,
ldflags=ldflags,
inc_dir_flag='-I{}',
lib_dir_flag='-L{}',
lib_flag='-l{}'
)
return None
def check_msvc(debug):
cl_path = find_executable('cl.exe')
link_path = find_executable('link.exe')
lib_path = find_executable('lib.exe')
if cl_path and link_path and lib_path:
if debug:
cflags = ' /Od /DDEBUG /DEBUG /Zi /FS'
ldflags = '/DEBUG:FULL'
else:
cflags = ''
ldflags = ''
return CompilerSpec(
cc_rule=f'{cl_path} /TC /nologo /Fo:%o $(CFLAGS) /c %f',
link_rule=f'{link_path} /NOLOGO /OUT:%o $(LDFLAGS) /SUBSYSTEM:WINDOWS %f',
dylib_link_rule=f'{link_path} /NOLOGO /OUT:%o /IMPLIB:%O.lib /DLL $(LDFLAGS) /SUBSYSTEM:WINDOWS %f',
stlib_link_rule=f'{lib_path} /NOLOGO /OUT:%o $(LDFLAGS) /SUBSYSTEM:WINDOWS %f',
cflags=cflags,
ldflags=ldflags,
inc_dir_flag='/I {}',
lib_dir_flag='/LIBPATH:{}',
lib_flag='{}.lib'
)
return None
COMPILERS = {
'GCC': check_gcc,
'MSVC': check_msvc,
}
def find_compiler(config):
for name, find_func in COMPILERS.items():
spec = find_func(config.debug)
if spec is not None:
config.compiler_spec = spec
return name
return False
def find_pkg_config(config, pkg_name, store_name=None):
pkgconfig = find_executable('pkg-config')
if pkgconfig is None:
return False
store_name = store_name or pkg_name
try:
ldflags = sp.check_output([pkgconfig, pkg_name, '--libs']).strip().decode('utf8')
ldflags_set = config.ldflags.setdefault(store_name, set())
ldflags_set.add(ldflags)
cflags = sp.check_output([pkgconfig, pkg_name, '--cflags']).strip().decode('utf8')
cflags_set = config.cflags.setdefault(store_name, set())
cflags_set.add(cflags)
return True
except sp.CalledProcessError:
return False
def find_lib(config, lib_name, store_name=None):
lib_filename = '{}.lib'.format(lib_name)
if store_name is None:
store_name = lib_name
for path in config.lib_paths.get(store_name, []):
for filename in os.listdir(path):
abs_filename = os.path.join(path, filename)
if filename == lib_filename and os.path.isfile(abs_filename):
ldflags = config.ldflags.setdefault(store_name, set())
ldflags.add(config.compiler_spec.lib_dir_flag.format(path))
ldflags.add(config.compiler_spec.lib_flag.format(lib_name))
return True
return False
def find_header(config, header_name, store_name=None):
if store_name is None:
store_name = os.path.splitext(os.path.basename())[0]
for path in config.inc_paths.get(store_name, []):
for filename in os.listdir(path):
abs_filename = os.path.join(path, filename)
if filename == header_name and os.path.isfile(abs_filename):
cflags = config.cflags.setdefault(store_name, set())
cflags.add(config.compiler_spec.inc_dir_flag.format(path))
return True
return False
def find_sdl2(config):
if IS_WINDOWS:
libs = ['SDL2', 'SDL2main']
headers = ['SDL.h']
return all(chain(
(find_lib(config, lib, 'sdl') for lib in libs),
(find_header(config, header, 'sdl') for header in headers)
))
return find_pkg_config(config, 'sdl2', 'sdl')
def find_python3(config):
if IS_WINDOWS:
libs = ['python36']
headers = ['Python.h']
return all(chain(
(find_lib(config, lib, 'python') for lib in libs),
(find_header(config, header, 'python') for header in headers)
))
return find_pkg_config(config, 'python-3.6', 'python')
def generate_files(config):
context = config.as_dict()
for template_filename, out_filename in GEN_FILES.items():
with open(template_filename, 'r') as tmpl_fp, open(out_filename, 'w') as out_fp:
tmpl = tmpl_fp.read()
makefile = mustache.render(tmpl, context)
out_fp.write(makefile)
FINDERS = (
('C compiler', 'cc', find_compiler),
('SDL2', 'sdl', find_sdl2),
('Python3', 'python', find_python3),
)
def lib_options(lib_specs):
def wrapper(func):
for user_name, lib_name, _ in lib_specs:
func = click.option(
'--{}-libpath'.format(lib_name),
type=click.Path(exists=True, file_okay=False, dir_okay=True),
multiple=True,
help='{} library search path'.format(user_name)
)(func)
func = click.option(
'--{}-incpath'.format(lib_name),
type=click.Path(exists=True, file_okay=False, dir_okay=True),
multiple=True,
help='{} headers search path'.format(user_name)
)(func)
return func
return wrapper
@click.command()
@click.option('--executable', type=str, default=EXECUTABLE_NAME,
help='output executable (default: "{}")'.format(EXECUTABLE_NAME))
@lib_options(FINDERS)
def configure(executable=EXECUTABLE_NAME, **kwargs):
"""Configures the project and creates the building configuration."""
def get_paths(suffix):
return {
key.replace(suffix, ''): path for key, path in kwargs.items()
if key.endswith(suffix)
}
inc_paths = get_paths('_incpath')
lib_paths = get_paths('_libpath')
config = Config(inc_paths=inc_paths, lib_paths=lib_paths)
config.executable = executable
for name, _, find_func in FINDERS:
print('Checking for {}...'.format(name), end=' ')
result = find_func(config)
if result:
if isinstance(result, bool):
print('found')
else:
print(result)
else:
print('not found')
print('Abort')
exit(1)
generate_files(config)
if __name__ == '__main__':
configure()