-
Notifications
You must be signed in to change notification settings - Fork 63
/
configure.py
executable file
·2100 lines (1703 loc) · 76.9 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import collections
import copy
import json
import sys
import os
import os.path
import platform
import re
import shlex
import shutil
import subprocess
import traceback
import logging
import time
import errno
import optparse # pylint: disable=deprecated-module
# An error caused by and to be fixed by the user, e.g. invalid command line argument
class UserError(Exception):
pass
# An error caused by bugs in this script or when reading/parsing build data files
# Those are not expected to be fixed by the user of this script
class InternalError(Exception):
pass
def flatten(l):
return sum(l, [])
def normalize_source_path(source):
"""
cmake needs this, and nothing else minds
"""
return os.path.normpath(source).replace('\\', '/')
class LexResult(object):
pass
class LexerError(InternalError):
def __init__(self, msg, lexfile, line):
super(LexerError, self).__init__(msg)
self.msg = msg
self.lexfile = lexfile
self.line = line
def __str__(self):
return '%s at %s:%d' % (self.msg, self.lexfile, self.line)
def parse_lex_dict(as_list):
if len(as_list) % 3 != 0:
raise InternalError("Lex dictionary has invalid format (input not divisible by 3): %s" % as_list)
result = {}
for key, sep, value in [as_list[3*i:3*i+3] for i in range(0, len(as_list)//3)]:
if sep != '->':
raise InternalError("Lex dictionary has invalid format")
result[key] = value
return result
def lex_me_harder(infofile, allowed_groups, allowed_maps, name_val_pairs):
"""
Generic lexer function for info.txt and src/build-data files
"""
out = LexResult()
# Format as a nameable Python variable
def py_var(group):
return group.replace(':', '_')
lexer = shlex.shlex(open(infofile), infofile, posix=True)
lexer.wordchars += ':.<>/,-!?+*' # handle various funky chars in info.txt
groups = allowed_groups + allowed_maps
for group in groups:
out.__dict__[py_var(group)] = []
for (key, val) in name_val_pairs.items():
out.__dict__[key] = val
def lexed_tokens(): # Convert to an iterator
while True:
token = lexer.get_token()
if token != lexer.eof:
yield token
else:
return
for token in lexed_tokens():
match = re.match('<(.*)>', token)
# Check for a grouping
if match is not None:
group = match.group(1)
if group not in groups:
raise LexerError('Unknown group "%s"' % (group),
infofile, lexer.lineno)
end_marker = '</' + group + '>'
token = lexer.get_token()
while token != end_marker:
out.__dict__[py_var(group)].append(token)
token = lexer.get_token()
if token is None:
raise LexerError('Group "%s" not terminated' % (group),
infofile, lexer.lineno)
elif token in name_val_pairs.keys():
if isinstance(out.__dict__[token], list):
out.__dict__[token].append(lexer.get_token())
else:
out.__dict__[token] = lexer.get_token()
else: # No match -> error
raise LexerError('Bad token "%s"' % (token), infofile, lexer.lineno)
for group in allowed_maps:
out.__dict__[group] = parse_lex_dict(out.__dict__[group])
return out
class InfoObject(object):
def __init__(self, infofile):
"""
Constructor sets members `infofile`, `lives_in`, `parent_module` and `basename`
"""
self.infofile = infofile
(dirname, basename) = os.path.split(infofile)
self.lives_in = dirname
if basename == 'info.txt':
(obj_dir, self.basename) = os.path.split(dirname)
if os.access(os.path.join(obj_dir, 'info.txt'), os.R_OK):
self.parent_module = os.path.basename(obj_dir)
else:
self.parent_module = None
else:
self.basename = basename.replace('.txt', '')
class ModuleInfo(InfoObject):
"""
Represents the information about a particular module
"""
def __init__(self, infofile):
super(ModuleInfo, self).__init__(infofile)
lex = lex_me_harder(
infofile,
['header:internal', 'header:public', 'header:external', 'requires',
'os_features', 'arch', 'cc', 'libs', 'frameworks', 'comment', 'warning'
],
['defines'],
{
'load_on': 'auto',
'need_isa': '',
'third_party': 'no'
})
def check_header_duplicates(header_list_public, header_list_internal):
pub_header = set(header_list_public)
int_header = set(header_list_internal)
if not pub_header.isdisjoint(int_header):
logging.error("Module %s header contains same header in public and internal sections" % self.infofile)
check_header_duplicates(lex.header_public, lex.header_internal)
all_source_files = []
all_header_files = []
for fspath in os.listdir(self.lives_in):
if fspath.endswith('.cpp'):
all_source_files.append(fspath)
elif fspath.endswith('.h'):
all_header_files.append(fspath)
self.source = all_source_files
# If not entry for the headers, all are assumed public
if lex.header_internal == [] and lex.header_public == []:
self.header_public = list(all_header_files)
self.header_internal = []
else:
self.header_public = lex.header_public
self.header_internal = lex.header_internal
self.header_external = lex.header_external
# Coerce to more useful types
def convert_lib_list(l):
if len(l) % 3 != 0:
raise InternalError("Bad <libs> in module %s" % (self.basename))
result = {}
for sep in l[1::3]:
if sep != '->':
raise InternalError("Bad <libs> in module %s" % (self.basename))
for (targetlist, vallist) in zip(l[::3], l[2::3]):
vals = vallist.split(',')
for target in targetlist.split(','):
result[target] = result.setdefault(target, []) + vals
return result
# Convert remaining lex result to members
self.arch = lex.arch
self.cc = lex.cc
self.comment = ' '.join(lex.comment) if lex.comment else None
self._defines = lex.defines
self._validate_defines_content(self._defines)
self.frameworks = convert_lib_list(lex.frameworks)
self.libs = convert_lib_list(lex.libs)
self.load_on = lex.load_on
self.need_isa = lex.need_isa.split(',') if lex.need_isa else []
self.third_party = True if lex.third_party == 'yes' else False
self.os_features = lex.os_features
self.requires = lex.requires
self.warning = ' '.join(lex.warning) if lex.warning else None
# Modify members
self.source = [normalize_source_path(os.path.join(self.lives_in, s)) for s in self.source]
self.header_internal = [os.path.join(self.lives_in, s) for s in self.header_internal]
self.header_public = [os.path.join(self.lives_in, s) for s in self.header_public]
self.header_external = [os.path.join(self.lives_in, s) for s in self.header_external]
# Filesystem read access check
for src in self.source + self.header_internal + self.header_public + self.header_external:
if not os.access(src, os.R_OK):
logging.error("Missing file %s in %s" % (src, infofile))
# Check for duplicates
def intersect_check(type_a, list_a, type_b, list_b):
intersection = set.intersection(set(list_a), set(list_b))
if intersection:
logging.error('Headers %s marked both %s and %s' % (' '.join(intersection), type_a, type_b))
intersect_check('public', self.header_public, 'internal', self.header_internal)
intersect_check('public', self.header_public, 'external', self.header_external)
intersect_check('external', self.header_external, 'internal', self.header_internal)
@staticmethod
def _validate_defines_content(defines):
for key, value in defines.items():
if not re.match('^[0-9A-Za-z_]{3,30}$', key):
raise InternalError('Module defines key has invalid format: "%s"' % key)
if not re.match('^[0-9]{8}$', value):
raise InternalError('Module defines value has invalid format: "%s"' % value)
def cross_check(self, arch_info, cc_info, all_os_features):
for feat in set(flatten([o.split(',') for o in self.os_features])):
if feat not in all_os_features:
logging.error("Module %s uses an OS feature (%s) which no OS supports", self.infofile, feat)
for supp_cc in self.cc:
if supp_cc not in cc_info:
colon_idx = supp_cc.find(':')
# a versioned compiler dependency
if colon_idx > 0 and supp_cc[0:colon_idx] in cc_info:
pass
else:
raise InternalError('Module %s mentions unknown compiler %s' % (self.infofile, supp_cc))
for supp_arch in self.arch:
if supp_arch not in arch_info:
raise InternalError('Module %s mentions unknown arch %s' % (self.infofile, supp_arch))
def sources(self):
return self.source
def public_headers(self):
return self.header_public
def internal_headers(self):
return self.header_internal
def external_headers(self):
return self.header_external
def defines(self):
return [(key + ' ' + value) for key, value in self._defines.items()]
def compatible_cpu(self, archinfo, options):
arch_name = archinfo.basename
cpu_name = options.cpu
for isa in self.need_isa:
if isa in options.disable_intrinsics:
return False # explicitly disabled
if isa not in archinfo.isa_extensions:
return False
if self.arch != []:
if arch_name not in self.arch and cpu_name not in self.arch:
return False
return True
def compatible_os(self, os_data, options):
if not self.os_features:
return True
def has_all(needed, provided):
for n in needed:
if n not in provided:
return False
return True
provided_features = os_data.enabled_features(options)
for feature_set in self.os_features:
if has_all(feature_set.split(','), provided_features):
return True
return False
def compatible_compiler(self, ccinfo, cc_min_version, arch):
# Check if this compiler supports the flags we need
def supported_isa_flags(ccinfo, arch):
for isa in self.need_isa:
if ccinfo.isa_flags_for(isa, arch) is None:
return False
return True
# Check if module gives explicit compiler dependencies
def supported_compiler(ccinfo, cc_min_version):
if self.cc == []:
# no compiler restriction
return True
if ccinfo.basename in self.cc:
# compiler is supported, independent of version
return True
# Maybe a versioned compiler dep
for cc in self.cc:
try:
name, version = cc.split(":")
if name == ccinfo.basename:
min_cc_version = [int(v) for v in version.split('.')]
cur_cc_version = [int(v) for v in cc_min_version.split('.')]
# With lists of ints, this does what we want
return cur_cc_version >= min_cc_version
except ValueError:
# No version part specified
pass
return False # compiler not listed
return supported_isa_flags(ccinfo, arch) and supported_compiler(ccinfo, cc_min_version)
def dependencies(self, osinfo):
# base is an implicit dep for all submodules
deps = ['base']
if self.parent_module != None:
deps.append(self.parent_module)
for req in self.requires:
if req.find('?') != -1:
(cond, dep) = req.split('?')
if osinfo is None or cond in osinfo.target_features:
deps.append(dep)
else:
deps.append(req)
return deps
def dependencies_exist(self, modules):
"""
Ensure that all dependencies of this module actually exist, warning
about any that do not
"""
missing = [s for s in self.dependencies(None) if s not in modules]
if missing:
logging.error("Module '%s', dep of '%s', does not exist" % (
missing, self.basename))
def load_info_files(search_dir, descr, filename_matcher, class_t):
info = {}
def filename_matches(filename):
if isinstance(filename_matcher, str):
return filename == filename_matcher
else:
return filename_matcher.match(filename) is not None
for (dirpath, _, filenames) in os.walk(search_dir):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if filename_matches(filename):
info_obj = class_t(filepath)
info[info_obj.basename] = info_obj
if info:
infotxt_basenames = ' '.join(sorted([key for key in info]))
logging.debug('Loaded %d %s files: %s' % (len(info), descr, infotxt_basenames))
else:
logging.warning('Failed to load any %s files' % (descr))
return info
def load_build_data_info_files(source_paths, descr, subdir, class_t):
matcher = re.compile(r'[_a-z0-9]+\.txt$')
return load_info_files(os.path.join(source_paths.build_data_dir, subdir), descr, matcher, class_t)
def portable_symlink(file_path, target_dir, method):
"""
Copy or link the file, depending on what the platform offers
"""
if not os.access(file_path, os.R_OK):
logging.warning('Missing file %s' % (file_path))
return
if method == 'symlink':
rel_file_path = os.path.relpath(file_path, start=target_dir)
os.symlink(rel_file_path, os.path.join(target_dir, os.path.basename(file_path)))
elif method == 'hardlink':
os.link(file_path, os.path.join(target_dir, os.path.basename(file_path)))
elif method == 'copy':
shutil.copy(file_path, target_dir)
else:
raise UserError('Unknown link method %s' % (method))
def choose_link_method(options):
"""
Choose the link method based on system availability and user request
"""
req = options.link_method
def useable_methods():
# Symbolic link support on Windows was introduced in Windows 6.0 (Vista) and Python 3.2
# Furthermore the SeCreateSymbolicLinkPrivilege is required in order to successfully create symlinks
# So only try to use symlinks on Windows if explicitly requested
if req == 'symlink' and options.os == 'windows':
yield 'symlink'
# otherwise keep old conservative behavior
if 'symlink' in os.__dict__ and options.os != 'windows':
yield 'symlink'
if 'link' in os.__dict__:
yield 'hardlink'
yield 'copy'
for method in useable_methods():
if req is None or req == method:
logging.info('Using %s to link files into build dir ' \
'(use --link-method to change)' % (method))
return method
logging.warning('Could not use link method "%s", will copy instead' % (req))
return 'copy'
def system_cpu_info():
cpu_info = []
if platform.machine() != '':
cpu_info.append(platform.machine())
if platform.processor() != '':
cpu_info.append(platform.processor())
if 'uname' in os.__dict__:
cpu_info.append(os.uname()[4])
return cpu_info
def canon_processor(archinfo, proc):
proc = fixup_proc_name(proc)
# First, try to search for an exact match
for ainfo in archinfo.values():
if ainfo.basename == proc or proc in ainfo.aliases:
return ainfo.basename
return None
def guess_processor(archinfo):
for info_part in system_cpu_info():
if info_part:
match = canon_processor(archinfo, info_part)
if match != None:
logging.debug("Matched '%s' to processor '%s'" % (info_part, match))
return match, info_part
else:
logging.debug("Failed to deduce CPU from '%s'" % info_part)
raise UserError('Could not determine target CPU; set with --cpu')
def have_program(program):
"""
Test for the existence of a program
"""
def exe_test(path, program):
exe_file = os.path.join(path, program)
if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
logging.debug('Found program %s in %s' % (program, path))
return True
else:
return False
exe_suffixes = ['', '.exe']
for path in os.environ['PATH'].split(os.pathsep):
for suffix in exe_suffixes:
if exe_test(path, program + suffix):
return True
logging.debug('Program %s not found' % (program))
return False
def set_defaults_for_unset_options(options, info_arch, info_cc): # pylint: disable=too-many-branches
if options.os is None:
system_from_python = platform.system().lower()
if re.match('^cygwin_.*', system_from_python):
logging.debug("Converting '%s' to 'cygwin'", system_from_python)
options.os = 'cygwin'
else:
options.os = system_from_python
if is_windows(options):
options.os = 'mingw'
logging.info('Guessing target OS is %s (use --os to set)' % (options.os))
def deduce_compiler_type_from_cc_bin(cc_bin):
if cc_bin.find('clang') != -1 or cc_bin in ['emcc', 'em++']:
return 'clang'
if cc_bin.find('-g++') != -1:
return 'gcc'
return None
if options.compiler is None and options.compiler_binary != None:
options.compiler = deduce_compiler_type_from_cc_bin(options.compiler_binary)
if options.compiler is None:
if options.os == 'windows':
if have_program('g++') and not have_program('cl'):
options.compiler = 'gcc'
else:
options.compiler = 'msvc'
elif options.os in ['darwin', 'freebsd', 'openbsd', 'ios']:
# Prefer Clang on these systems
if have_program('clang++'):
options.compiler = 'clang'
else:
options.compiler = 'gcc'
if options.os == 'openbsd':
# The assembler shipping with OpenBSD 5.9 does not support avx2
del info_cc['gcc'].isa_flags['avx2']
else:
options.compiler = 'gcc'
if options.compiler is None:
logging.error('Could not guess which compiler to use, use --cc or CXX to set')
else:
logging.info('Guessing to use compiler %s (use --cc or CXX to set)' % (options.compiler))
if options.cpu is None:
(arch, cpu) = guess_processor(info_arch)
options.arch = arch
options.cpu = cpu
if is_windows(options):
options.cpu = "x86_32"
logging.info('Guessing target processor is a %s (use --cpu to set)' % (options.cpu))
if is_windows(options) or options.os == 'darwin':
options.build_botan = True
options.build_zlib = True
options.build_bzip2 = True
class EncryptPadConfigureLogHandler(logging.StreamHandler, object):
def emit(self, record):
# Do the default stuff first
super(EncryptPadConfigureLogHandler, self).emit(record)
# Exit script if and ERROR or worse occurred
if record.levelno >= logging.ERROR:
sys.exit(1)
def setup_logging(options):
log_level = logging.INFO
lh = EncryptPadConfigureLogHandler(sys.stdout)
lh.setFormatter(logging.Formatter('%(levelname) 7s: %(message)s'))
logging.getLogger().addHandler(lh)
logging.getLogger().setLevel(log_level)
# Workaround for Windows systems where antivirus is enabled GH #353
def robust_rmtree(path, max_retries=5):
for _ in range(max_retries):
try:
shutil.rmtree(path)
return
except OSError:
time.sleep(0.1)
# Final attempt, pass any exceptions up to caller.
shutil.rmtree(path)
# Workaround for Windows systems where antivirus is enabled GH #353
def robust_makedirs(directory, max_retries=5):
for _ in range(max_retries):
try:
os.makedirs(directory)
return
except OSError as e:
if e.errno == errno.EEXIST:
raise
else:
time.sleep(0.1)
# Final attempt, pass any exceptions up to caller.
os.makedirs(directory)
def read_textfile(filepath):
"""
Read a whole file into memory as a string
"""
if filepath is None:
return ''
with open(filepath) as f:
return ''.join(f.readlines())
def process_template(template_file, variables):
# pylint: disable=too-many-branches,too-many-statements
"""
Perform template substitution
The template language supports (un-nested) conditionals.
"""
class SimpleTemplate(object):
def __init__(self, vals):
self.vals = vals
self.value_pattern = re.compile(r'%{([a-z][a-z_0-9\|]+)}')
self.cond_pattern = re.compile('%{(if|unless) ([a-z][a-z_0-9]+)}')
self.for_pattern = re.compile('(.*)%{for ([a-z][a-z_0-9]+)}')
self.join_pattern = re.compile('(.*)%{join ([a-z][a-z_0-9]+)}')
def substitute(self, template):
# pylint: disable=too-many-locals
def insert_value(match):
v = match.group(1)
if v in self.vals:
return str(self.vals.get(v))
if v.endswith('|upper'):
v = v.replace('|upper', '')
if v in self.vals:
return str(self.vals.get(v)).upper()
raise KeyError(v)
lines = template.splitlines()
output = ""
idx = 0
while idx < len(lines):
cond_match = self.cond_pattern.match(lines[idx])
join_match = self.join_pattern.match(lines[idx])
for_match = self.for_pattern.match(lines[idx])
if cond_match:
cond_type = cond_match.group(1)
cond_var = cond_match.group(2)
include_cond = False
if cond_type == 'if' and cond_var in self.vals and self.vals.get(cond_var):
include_cond = True
elif cond_type == 'unless' and (cond_var not in self.vals or (not self.vals.get(cond_var))):
include_cond = True
idx += 1
while idx < len(lines):
if lines[idx] == '%{endif}':
break
if include_cond:
output += lines[idx] + "\n"
idx += 1
elif join_match:
join_var = join_match.group(2)
join_str = ' '
join_line = '%%{join %s}' % (join_var)
output += lines[idx].replace(join_line, join_str.join(self.vals[join_var])) + "\n"
elif for_match:
for_prefix = for_match.group(1)
output += for_prefix
for_var = for_match.group(2)
if for_var not in self.vals:
raise InternalError("Unknown for loop iteration variable '%s'" % (for_var))
var = self.vals[for_var]
if not isinstance(var, list):
raise InternalError("For loop iteration variable '%s' is not a list" % (for_var))
idx += 1
for_body = ""
while idx < len(lines):
if lines[idx] == '%{endfor}':
break
for_body += lines[idx] + "\n"
idx += 1
for v in var:
if isinstance(v, dict):
for_val = for_body
for ik, iv in v.items():
for_val = for_val.replace('%{' + ik + '}', iv)
output += for_val + "\n"
else:
output += for_body.replace('%{i}', v).replace('%{i|upper}', v.upper())
output += "\n"
else:
output += lines[idx] + "\n"
idx += 1
return self.value_pattern.sub(insert_value, output) + '\n'
try:
return SimpleTemplate(variables).substitute(read_textfile(template_file))
except KeyError as e:
logging.error('Unbound var %s in template %s' % (e, template_file))
except Exception as e: # pylint: disable=broad-except
logging.error('Exception %s during template processing file %s' % (e, template_file))
def fixup_proc_name(proc):
proc = proc.lower().replace(' ', '')
for junk in ['(tm)', '(r)']:
proc = proc.replace(junk, '')
return proc
class ArchInfo(InfoObject):
def __init__(self, infofile):
super(ArchInfo, self).__init__(infofile)
lex = lex_me_harder(
infofile,
['aliases', 'isa_extensions'],
[],
{
'endian': None,
'family': None,
'wordsize': 32
})
self.aliases = lex.aliases
self.endian = lex.endian
self.family = lex.family
self.isa_extensions = lex.isa_extensions
self.wordsize = int(lex.wordsize)
if self.wordsize not in [32, 64]:
logging.error('Unexpected wordsize %d for arch %s', self.wordsize, infofile)
alphanumeric = re.compile('^[a-z0-9]+$')
for isa in self.isa_extensions:
if alphanumeric.match(isa) is None:
logging.error('Invalid name for ISA extension "%s"', isa)
def supported_isa_extensions(self, cc, options):
isas = []
for isa in self.isa_extensions:
if isa not in options.disable_intrinsics:
if cc.isa_flags_for(isa, self.basename) is not None:
isas.append(isa)
return sorted(isas)
class OsInfo(InfoObject): # pylint: disable=too-many-instance-attributes
def __init__(self, infofile):
super(OsInfo, self).__init__(infofile)
lex = lex_me_harder(
infofile,
['aliases', 'target_features'],
[],
{
'program_suffix': '',
'obj_suffix': 'o',
'soname_suffix': '',
'soname_pattern_patch': '',
'soname_pattern_abi': '',
'soname_pattern_base': '',
'static_suffix': 'a',
'ar_command': 'ar',
'ar_options': '',
'ar_output_to': '',
'install_root': '/usr/local',
'header_dir': 'include',
'bin_dir': 'bin',
'lib_dir': 'lib',
'doc_dir': 'share/doc',
'man_dir': 'share/man',
'use_stack_protector': 'true',
'so_post_link_command': '',
'cli_exe_name': 'encryptcli',
'test_exe_name': 'encryptpad_test',
'lib_prefix': 'lib',
'library_name': 'botan{suffix}-{major}',
})
if lex.ar_command == 'ar' and lex.ar_options == '':
lex.ar_options = 'crs'
if lex.soname_pattern_base:
self.soname_pattern_base = lex.soname_pattern_base
if lex.soname_pattern_patch == '' and lex.soname_pattern_abi == '':
self.soname_pattern_patch = lex.soname_pattern_base
self.soname_pattern_abi = lex.soname_pattern_base
elif lex.soname_pattern_abi != '' and lex.soname_pattern_abi != '':
self.soname_pattern_patch = lex.soname_pattern_patch
self.soname_pattern_abi = lex.soname_pattern_abi
else:
# base set, only one of patch/abi set
raise InternalError("Invalid soname_patterns in %s" % (self.infofile))
else:
if lex.soname_suffix:
self.soname_pattern_base = "libbotan{lib_suffix}-{version_major}.%s" % (lex.soname_suffix)
self.soname_pattern_abi = self.soname_pattern_base + ".{abi_rev}"
self.soname_pattern_patch = self.soname_pattern_abi + ".{version_minor}.{version_patch}"
else:
# Could not calculate soname_pattern_*
# This happens for OSs without shared library support (e.g. nacl, mingw, includeos, cygwin)
self.soname_pattern_base = None
self.soname_pattern_abi = None
self.soname_pattern_patch = None
self._aliases = lex.aliases
self.ar_command = lex.ar_command
self.ar_options = lex.ar_options
self.bin_dir = lex.bin_dir
self.cli_exe_name = lex.cli_exe_name
self.test_exe_name = lex.test_exe_name
self.doc_dir = lex.doc_dir
self.header_dir = lex.header_dir
self.install_root = lex.install_root
self.lib_dir = lex.lib_dir
self.lib_prefix = lex.lib_prefix
self.library_name = lex.library_name
self.man_dir = lex.man_dir
self.obj_suffix = lex.obj_suffix
self.program_suffix = lex.program_suffix
self.so_post_link_command = lex.so_post_link_command
self.static_suffix = lex.static_suffix
self.target_features = lex.target_features
self.use_stack_protector = (lex.use_stack_protector == "true")
def matches_name(self, nm):
if nm in self._aliases:
return True
for alias in self._aliases:
if re.match(alias, nm):
return True
return False
def building_shared_supported(self):
return self.soname_pattern_base != None
def enabled_features(self, options):
feats = []
for feat in self.target_features:
if feat not in options.without_os_features:
feats.append(feat)
for feat in options.with_os_features:
if feat not in self.target_features:
feats.append(feat)
return sorted(feats)
class CompilerInfo(InfoObject): # pylint: disable=too-many-instance-attributes
def __init__(self, infofile):
super(CompilerInfo, self).__init__(infofile)
lex = lex_me_harder(
infofile,
[],
['cpu_flags', 'cpu_flags_no_debug', 'so_link_commands', 'binary_link_commands',
'mach_abi_linking', 'isa_flags', 'sanitizers'],
{
'binary_name': None,
'linker_name': None,
'macro_name': None,
'output_to_object': '-o ',
'output_to_exe': '-o ',
'add_include_dir_option': '-I',
'add_lib_dir_option': '-L',
'add_sysroot_option': '',
'add_lib_option': '-l',
'add_framework_option': '-framework ',
'preproc_flags': '-E',
'compile_flags': '-c',
'debug_info_flags': '-g',
'optimization_flags': '',
'size_optimization_flags': '',
'sanitizer_optimization_flags': '',
'coverage_flags': '',
'stack_protector_flags': '',
'shared_flags': '',
'lang_flags': '',
'warning_flags': '',
'maintainer_warning_flags': '',
'visibility_build_flags': '',
'visibility_attribute': '',
'ar_command': '',
'ar_options': '',
'ar_output_to': '',
'static_linking': '',
'large_files': '',
})
self.add_framework_option = lex.add_framework_option
self.add_include_dir_option = lex.add_include_dir_option
self.add_lib_dir_option = lex.add_lib_dir_option
self.add_lib_option = lex.add_lib_option
self.add_sysroot_option = lex.add_sysroot_option
self.ar_command = lex.ar_command
self.ar_options = lex.ar_options
self.ar_output_to = lex.ar_output_to
self.binary_link_commands = lex.binary_link_commands
self.binary_name = lex.binary_name
self.cpu_flags = lex.cpu_flags
self.cpu_flags_no_debug = lex.cpu_flags_no_debug
self.compile_flags = lex.compile_flags
self.coverage_flags = lex.coverage_flags
self.debug_info_flags = lex.debug_info_flags
self.isa_flags = lex.isa_flags
self.lang_flags = lex.lang_flags
self.linker_name = lex.linker_name
self.mach_abi_linking = lex.mach_abi_linking
self.macro_name = lex.macro_name
self.maintainer_warning_flags = lex.maintainer_warning_flags
self.optimization_flags = lex.optimization_flags
self.output_to_exe = lex.output_to_exe
self.output_to_object = lex.output_to_object
self.preproc_flags = lex.preproc_flags
self.sanitizers = lex.sanitizers
self.sanitizer_types = []
self.sanitizer_optimization_flags = lex.sanitizer_optimization_flags
self.shared_flags = lex.shared_flags
self.size_optimization_flags = lex.size_optimization_flags
self.so_link_commands = lex.so_link_commands
self.stack_protector_flags = lex.stack_protector_flags
self.visibility_attribute = lex.visibility_attribute
self.visibility_build_flags = lex.visibility_build_flags
self.warning_flags = lex.warning_flags
self.static_linking = lex.static_linking
self.large_files = lex.large_files
def isa_flags_for(self, isa, arch):
if isa in self.isa_flags:
return self.isa_flags[isa]
arch_isa = '%s:%s' % (arch, isa)
if arch_isa in self.isa_flags:
return self.isa_flags[arch_isa]
return None
def get_isa_specific_flags(self, isas, arch, options):
flags = set()
def simd32_impl():
for simd_isa in ['sse2', 'altivec', 'neon']:
if simd_isa in arch.isa_extensions and \
simd_isa not in options.disable_intrinsics and \
self.isa_flags_for(simd_isa, arch.basename):
return simd_isa
return None
for isa in isas:
if isa == 'simd':
isa = simd32_impl()
if isa is None:
continue
flagset = self.isa_flags_for(isa, arch.basename)
if flagset is None:
raise UserError('Compiler %s does not support %s' % (self.basename, isa))
flags.add(flagset)
return " ".join(sorted(flags))
def gen_shared_flags(self, options):
"""
Return the shared library build flags, if any
"""
def flag_builder():
return ''
return ' '.join(list(flag_builder()))