-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigure.py
486 lines (406 loc) · 16.6 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
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
#!/usr/bin/env python3
# encoding: utf-8
import sysconfig
from waflib.Tools.ccroot import USELIB_VARS
from Common import *
# Parse the use flags file and command line options
def configure_use(cfg):
use = get_use(cfg)
for use_flag in use:
configure_single_use(cfg, use, use_flag)
#endfor
#enddef
# Parse a single use flag and the corresponding command line options
def configure_single_use(cfg, use, use_flag):
type = str()
if not 'type' in use[use_flag]:
cfg.fatal(use_flag + ': Use flag type is required!')
else:
type = use[use_flag]['type']
#endif
if not 'platforms' in use[use_flag]:
cfg.fatal(use_flag + ': Platforms are required!')
#endif
if not 'common' in use[use_flag]:
cfg.fatal(use_flag + ': A common block is required!')
#endif
if type != 'flags' and not 'optional' in use[use_flag]['common']:
cfg.fatal(use_flag \
+ ': An "optional" directive is required in a common' \
+ ' block, where the type is not "flags"!')
#endif
if type != 'flags' and not 'code' in use[use_flag]['common']:
cfg.fatal(use_flag \
+ ': A "code" directive is required in a common' \
+ ' block, where the type is not "flags"!')
#endif
if not cfg.options.target_platform in use[use_flag]['platforms']:
return
#endif
optional = False
optional_platform = cfg.env.cur_toolset
if not optional_platform in use[use_flag] \
or not 'optional' in use[use_flag][optional_platform]:
optional_platform = 'common'
#endif
if type != 'flags' and use[use_flag][optional_platform]['optional'] == True:
optional = True
if cfg.options.__dict__['without_' + use_flag] == True:
cfg.msg('Checking for library <' + use_flag + '>', 'Disabled, skipping...', color='YELLOW')
return
#endif
#endif
source = str()
flags = dict()
flags.setdefault('common', dict())
flags.setdefault(cfg.env.cur_toolset, dict())
flags.setdefault(cfg.env.cur_platform, dict())
for toolset in flags:
flags[toolset].setdefault('defines', [])
flags[toolset].setdefault('includes', [])
flags[toolset].setdefault('cc_flags', [])
flags[toolset].setdefault('cxx_flags', [])
flags[toolset].setdefault('ld_flags', [])
flags[toolset].setdefault('lib_paths', [])
flags[toolset].setdefault('libs', [])
flags[toolset].setdefault('use', [])
flags[toolset].setdefault('forced_includes', [])
#endfor
if type != 'flags':
file = os.path.normcase(
os.path.normpath(
os.path.join('UseFlags', use[use_flag]['common']['code'])))
with open(file, encoding='utf-8') as source_file:
source = source_file.read();
#endwith
#endif
for toolset in [cfg.env.cur_platform, cfg.env.cur_toolset, 'common']:
if not toolset in use[use_flag]:
continue
#endif
toolset_is_platform = False
if toolset == cfg.env.cur_platform:
toolset_is_platform = True
#endif
if toolset == 'common':
skip_common = False
if cfg.env.cur_platform in use[use_flag]:
cur_platform = cfg.env.cur_platform
if isinstance(use[use_flag][cfg.env.cur_platform], str):
cur_platform = use[use_flag][cfg.env.cur_platform]
#endif
if 'no_common' in use[use_flag][cur_platform]:
skip_common = use[use_flag][cur_platform]['no_common']
#endif
#endif
if cfg.env.cur_toolset in use[use_flag]:
cur_toolset = cfg.env.cur_toolset
if isinstance(use[use_flag][cfg.env.cur_toolset], str):
cur_toolset = use[use_flag][cfg.env.cur_toolset]
#endif
if 'no_common' in use[use_flag][cur_toolset]:
skip_common = use[use_flag][cur_toolset]['no_common']
#endif
#endif
if skip_common:
continue
#endif
#endif
if isinstance(use[use_flag][toolset], str):
toolset = use[use_flag][toolset]
# Toolset might be a toolset of another platform, so do this again.
flags.setdefault(toolset, dict())
flags[toolset].setdefault('defines', [])
flags[toolset].setdefault('includes', [])
flags[toolset].setdefault('cc_flags', [])
flags[toolset].setdefault('cxx_flags', [])
flags[toolset].setdefault('ld_flags', [])
flags[toolset].setdefault('lib_paths', [])
flags[toolset].setdefault('libs', [])
flags[toolset].setdefault('use', [])
flags[toolset].setdefault('forced_includes', [])
#endif
def read_option(write_option, variation_option, obj):
if variation_option in obj:
flags[toolset][write_option] += obj[variation_option]
#endif
#enddef
if type != 'flags' \
and cfg.options.__dict__[use_flag + '_includes'] != None:
flags[toolset]['includes'] = cfg.options.__dict__[use_flag + '_includes']
elif 'includes' in use[use_flag][toolset]:
flags[toolset]['includes'] = use[use_flag][toolset]['includes']
#endif
current_toolset = get_toolset(cfg)
cur_conf = cfg.env.cur_conf
def make_flags_absolute(relative_path):
return os.path.normcase(
os.path.normpath(
os.path.join(cfg.path.abspath(), relative_path)))
#enddef
flags[toolset]['includes'] = list(
map(make_flags_absolute, flags[toolset]['includes']))
if 'defines' in use[use_flag][toolset]:
read_option('defines', 'base', use[use_flag][toolset]['defines'])
read_option('defines', cur_conf, use[use_flag][toolset]['defines'])
for option in ['stlib', 'shlib']:
read_option('defines', option, use[use_flag][toolset]['defines'])
read_option(
'defines',
option + '_' + cur_conf,
use[use_flag][toolset]['defines'])
#endfor
#endif
for option in ['cc_flags', 'cxx_flags', 'ld_flags']:
read_option(option, option, use[use_flag][toolset])
read_option(option, option + '_' + cur_conf, use[use_flag][toolset])
#endfor
read_option('use', 'use', use[use_flag][toolset])
# This should probably be made more readable somehow,
# but idk how to check for the options without horrible hacks
if type == 'lib':
# we either have a dynamic or static lib
default_shared = True
if 'shared' in use[use_flag][toolset]:
default_shared = use[use_flag][toolset]['shared']
#endif
if cfg.options.__dict__['with_' + use_flag] == 'dynamic':
default_shared = True
elif cfg.options.__dict__['with_' + use_flag] == 'static':
default_shared = False
#endif
if default_shared:
cfg.options.__dict__['with_' + use_flag] = 'dynamic'
if cfg.options.__dict__[use_flag + '_libpath'] != None:
flags[toolset]['lib_paths'] = cfg.options.__dict__[use_flag + '_libpath']
# No command line option passed
elif 'shlib_path' in use[use_flag][toolset]:
flags[toolset]['lib_paths'] = use[use_flag][toolset]['shlib_path']
#endif
if cfg.options.__dict__[use_flag + '_lib'] != None:
flags[toolset]['libs'] = [cfg.options.__dict__[use_flag + '_lib']]
# No command line option passed
elif 'shlib_link' in use[use_flag][toolset]:
flags[toolset]['libs'] = use[use_flag][toolset]['shlib_link']
#endif
else:
cfg.options.__dict__['with_' + use_flag] = 'static'
if cfg.options.__dict__[use_flag + '_stlibpath'] != None:
flags[toolset]['lib_paths'] = cfg.options.__dict__[use_flag + '_stlibpath']
# No command line option passed
elif 'stlib_path' in use[use_flag][toolset]:
flags[toolset]['lib_paths'] = use[use_flag][toolset]['stlib_path']
#endif
if cfg.options.__dict__[use_flag + '_lib'] != None:
flags[toolset]['libs'] = [cfg.options.__dict__[use_flag + '_lib']]
# No command line option passed
elif 'stlib_link' in use[use_flag][toolset]:
flags[toolset]['libs'] = use[use_flag][toolset]['stlib_link']
#endif
#endif
# Make library paths absolute
for i in range(len(flags[toolset]['lib_paths'])):
flags[toolset]['lib_paths'][i] = os.path.normcase(
os.path.normpath(
os.path.join(
cfg.path.abspath(),
flags[toolset]['lib_paths'][i])))
#endfor
#endif
if not toolset in [cfg.env.cur_platform, cfg.env.cur_toolset, 'common']:
if toolset_is_platform:
flags[cfg.env.cur_platform] = flags[toolset]
else:
flags[cfg.env.cur_toolset] = flags[toolset]
#endif
#endif
#endfor
if not source and type != 'flags':
cfg.fatal('Source of test file for <' + use_flag + '> is empty.')
#endif
# These flags are additive
defines = flags[cfg.env.cur_toolset]['defines'] \
+ flags[cfg.env.cur_platform]['defines'] \
+ flags['common']['defines']
includes = flags[cfg.env.cur_toolset]['includes'] \
+ flags[cfg.env.cur_platform]['includes'] \
+ flags['common']['includes']
cc_flags = flags[cfg.env.cur_toolset]['cc_flags'] \
+ flags[cfg.env.cur_platform]['cc_flags'] \
+ flags['common']['cc_flags']
cxx_flags = flags[cfg.env.cur_toolset]['cxx_flags'] \
+ flags[cfg.env.cur_platform]['cxx_flags'] \
+ flags['common']['cxx_flags']
use = flags[cfg.env.cur_toolset]['use'] \
+ flags[cfg.env.cur_platform]['use'] \
+ flags['common']['use']
# Linking related flags however are not
ld_flags = []
if flags[cfg.env.cur_toolset]['ld_flags'] != []:
ld_flags = flags[cfg.env.cur_toolset]['ld_flags']
elif flags[cfg.env.cur_platform]['ld_flags'] != []:
ld_flags = flags[cfg.env.cur_platform]['ld_flags']
else:
ld_flags = flags['common']['ld_flags']
#endif
lib_paths = []
if flags[cfg.env.cur_toolset]['lib_paths'] != []:
lib_paths = flags[cfg.env.cur_toolset]['lib_paths']
elif flags[cfg.env.cur_platform]['lib_paths'] != []:
lib_paths = flags[cfg.env.cur_platform]['lib_paths']
else:
lib_paths = flags['common']['lib_paths']
#endif
libs = []
if flags[cfg.env.cur_toolset]['libs'] != []:
libs = flags[cfg.env.cur_toolset]['libs']
elif flags[cfg.env.cur_platform]['libs'] != []:
libs = flags[cfg.env.cur_platform]['libs']
else:
libs = flags['common']['libs']
#endif
# remove duplicates from the list
for toolset in flags:
for sublist in flags[toolset]:
flags[toolset][sublist] = list(dict.fromkeys(flags[toolset][sublist]))
#endfor
#endfor
if type == 'lib':
if cfg.options.__dict__['with_' + use_flag] == 'dynamic':
cfg.check_cxx(
fragment=source,
use=use + [use_flag] + ['EXE'],
uselib_store=use_flag,
cxxflags=cxx_flags,
cflags=cc_flags,
ldflags=ld_flags,
libpath=lib_paths,
lib=libs,
defines=defines,
system_includes=includes,
msg='Checking for dynamic library <' + use_flag + '>',
mandatory=not optional)
elif cfg.options.__dict__['with_' + use_flag] == 'static':
cfg.check_cxx(
fragment=source,
use=use + [use_flag] + ['EXE'],
uselib_store=use_flag,
cxxflags=cxx_flags,
cflags=cc_flags,
ldflags=ld_flags,
stlibpath=lib_paths,
stlib=libs,
defines=defines,
system_includes=includes,
msg='Checking for static library <' + use_flag + '>',
mandatory=not optional)
#endif
elif type == 'headers': # header only lib
cfg.check_cxx(
fragment=source,
use=use + [use_flag] + ['EXE'],
uselib_store=use_flag,
defines=defines,
cxxflags=cxx_flags,
cflags=cc_flags,
ldflags=ld_flags,
system_includes=includes,
msg='Checking for header only library <' + use_flag + '>',
mandatory=not optional)
elif type == 'flags':
cfg.msg('Adding extra flags for <' + use_flag + '>', '✔')
cfg.env['CFLAGS_' + use_flag] = cc_flags
cfg.env['CXXFLAGS_' + use_flag] = cxx_flags
cfg.env['LDFLAGS_' + use_flag] = ld_flags
cfg.env['SYSINCLUDES_' + use_flag] = includes
#endif
#enddef
# Standard waf configuration function, called when configure is passed
# Here we load, parse and cache the toolset passed to waf
def configure(cfg: ConfigurationContext):
if sysconfig.get_platform() == 'mingw':
Logs.enable_colors(2)
#endif
# Cache configuration flags so they can't be overriden at build (1)
cfg.env.cur_toolset = cfg.options.toolset
config = get_config(cfg)
toolset = get_toolset(cfg)
# Cache configuration flags so they can't be overriden at build (2)
config_found = False
for configuration in config['configurations']:
if cfg.options.config == configuration:
config_found = True
#endif
#endfor
if config_found == False:
cfg.fatal('Invalid configuration used!')
#endif
# Save the configuration for error checking during build
cfg.env.cur_conf = cfg.options.config
# Cache configuration flags so they can't be overriden at build (3)
platform_found = False
for platform in config['target_platforms']:
if cfg.options.target_platform == platform:
platform_found = True
#endif
#endfor
if platform_found == False:
cfg.fatal('Invalid platform used!')
#endif
cfg.env.cur_platform = cfg.options.target_platform
ignore_paths = False
if 'ignore_paths' in toolset:
ignore_paths = toolset['ignore_paths']
#endif
## Set the compiler paths so waf can find them
if not ignore_paths:
cfg.env.CC = toolset['cc_path']
cfg.env.CXX = toolset['cxx_path']
cfg.env.AR = toolset['ar_path']
#endif
cfg.load(toolset['cc'])
cfg.load(toolset['cxx'])
cfg.load('clang_compilation_database')
cfg.env.COMPILER_CC = cfg.env['CC']
cfg.env.COMPILER_CXX = cfg.env['CXX']
if toolset['cc'] == 'msvc' or toolset['cxx'] == 'msvc':
cfg.load('msvc_pdb')
#endif
cfg.env.FORCEINCLUDES = []
def read_optional_flag(
target: str,
option: str,
obj: JSONType = toolset) -> None:
if option in obj:
cfg.env.append_value(target, obj[option])
#endif
#enddef
# Parse compiler flags, defines and system includes
for env_flag, toolset_flag in \
[ \
('CFLAGS', 'cc_flags'), \
('CXXFLAGS', 'cxx_flags'), \
('ARFLAGS', 'stlib_flags'), \
('STLIBPATH', 'stlib_path'), \
('LINKFLAGS_cshlib', 'shlib_flags'), \
('LINKFLAGS_cxxshlib', 'shlib_flags'), \
('LIBPATH', 'shlib_path'), \
('LINKFLAGS_EXE', 'exe_flags'), \
('LDFLAGS', 'ld_flags')
]:
cfg.env[env_flag] = toolset[toolset_flag]
read_optional_flag(env_flag, toolset_flag + '_' + cfg.options.config)
#endfor
if cfg.options.development:
cfg.env.CFLAGS += toolset['dev_cc_flags']
cfg.env.CXXFLAGS += toolset['dev_cxx_flags']
#endif
cfg.env.DEFINES += toolset['defines']['base']
read_optional_flag('DEFINES', cfg.options.config, toolset['defines'])
for path in toolset['system_includes']:
flag = [normalized_join2(cfg.path.abspath(), path)]
cfg.env.SYSINCLUDES += flag
#endfor
# Configure use flags
configure_use(cfg)
#enddef