forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
1712 lines (1390 loc) · 58.3 KB
/
shared.py
File metadata and controls
1712 lines (1390 loc) · 58.3 KB
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
# Copyright 2011 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
from __future__ import print_function
from subprocess import PIPE
import atexit
import binascii
import base64
import difflib
import json
import logging
import math
import os
import re
import shutil
import subprocess
import time
import sys
import tempfile
if sys.version_info < (3, 5):
print('error: emscripten requires python 3.5 or above', file=sys.stderr)
sys.exit(1)
from .toolchain_profiler import ToolchainProfiler
from .tempfiles import try_delete
from . import cache, tempfiles, colored_logger
from . import diagnostics
__rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
WINDOWS = sys.platform.startswith('win')
MACOS = sys.platform == 'darwin'
LINUX = sys.platform.startswith('linux')
DEBUG = int(os.environ.get('EMCC_DEBUG', '0'))
EXPECTED_NODE_VERSION = (4, 1, 1)
EXPECTED_BINARYEN_VERSION = 95
EXPECTED_LLVM_VERSION = "12.0"
SIMD_INTEL_FEATURE_TOWER = ['-msse', '-msse2', '-msse3', '-mssse3', '-msse4.1', '-msse4.2', '-mavx']
SIMD_NEON_FLAGS = ['-mfpu=neon']
# can add %(asctime)s to see timestamps
logging.basicConfig(format='%(name)s:%(levelname)s: %(message)s',
level=logging.DEBUG if DEBUG else logging.INFO)
colored_logger.enable()
logger = logging.getLogger('shared')
if sys.version_info < (2, 7, 12):
logger.debug('python versions older than 2.7.12 are known to run into outdated SSL certificate related issues, https://github.com/emscripten-core/emscripten/issues/6275')
# warning about absolute-paths is disabled by default, and not enabled by -Wall
diagnostics.add_warning('absolute-paths', enabled=False, part_of_all=False)
diagnostics.add_warning('separate-asm')
diagnostics.add_warning('almost-asm')
diagnostics.add_warning('invalid-input')
# Don't show legacy settings warnings by default
diagnostics.add_warning('legacy-settings', enabled=False, part_of_all=False)
# Catch-all for other emcc warnings
diagnostics.add_warning('linkflags')
diagnostics.add_warning('emcc')
diagnostics.add_warning('undefined', error=True)
diagnostics.add_warning('deprecated')
diagnostics.add_warning('version-check')
diagnostics.add_warning('export-main')
diagnostics.add_warning('unused-command-line-argument', shared=True)
def exit_with_error(msg, *args):
diagnostics.error(msg, *args)
# On Windows python suffers from a particularly nasty bug if python is spawning
# new processes while python itself is spawned from some other non-console
# process.
# Use a custom replacement for Popen on Windows to avoid the "WindowsError:
# [Error 6] The handle is invalid" errors when emcc is driven through cmake or
# mingw32-make.
# See http://bugs.python.org/issue3905
class WindowsPopen(object):
def __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False,
shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
# (stdin, stdout, stderr) store what the caller originally wanted to be done with the streams.
# (stdin_, stdout_, stderr_) will store the fixed set of streams that workaround the bug.
self.stdin_ = stdin
self.stdout_ = stdout
self.stderr_ = stderr
# If the caller wants one of these PIPEd, we must PIPE them all to avoid the 'handle is invalid' bug.
if self.stdin_ == PIPE or self.stdout_ == PIPE or self.stderr_ == PIPE:
if self.stdin_ is None:
self.stdin_ = PIPE
if self.stdout_ is None:
self.stdout_ = PIPE
if self.stderr_ is None:
self.stderr_ = PIPE
try:
# Call the process with fixed streams.
self.process = subprocess.Popen(args, bufsize, executable, self.stdin_, self.stdout_, self.stderr_, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
self.pid = self.process.pid
except Exception as e:
logger.error('\nsubprocess.Popen(args=%s) failed! Exception %s\n' % (shlex_join(args), str(e)))
raise
def communicate(self, input=None):
output = self.process.communicate(input)
self.returncode = self.process.returncode
# If caller never wanted to PIPE stdout or stderr, route the output back to screen to avoid swallowing output.
if self.stdout is None and self.stdout_ == PIPE and len(output[0].strip()):
print(output[0], file=sys.stdout)
if self.stderr is None and self.stderr_ == PIPE and len(output[1].strip()):
print(output[1], file=sys.stderr)
# Return a mock object to the caller. This works as long as all emscripten code immediately .communicate()s the result, and doesn't
# leave the process object around for longer/more exotic uses.
if self.stdout is None and self.stderr is None:
return (None, None)
if self.stdout is None:
return (None, output[1])
if self.stderr is None:
return (output[0], None)
return (output[0], output[1])
def poll(self):
return self.process.poll()
def kill(self):
return self.process.kill()
def path_from_root(*pathelems):
return os.path.join(__rootpath__, *pathelems)
def root_is_writable():
return os.access(__rootpath__, os.W_OK)
# Switch to shlex.quote once we can depend on python 3
def shlex_quote(arg):
if ' ' in arg and (not (arg.startswith('"') and arg.endswith('"'))) and (not (arg.startswith("'") and arg.endswith("'"))):
return '"' + arg.replace('"', '\\"') + '"'
return arg
# Switch to shlex.join once we can depend on python 3.8:
# https://docs.python.org/3/library/shlex.html#shlex.join
def shlex_join(cmd):
return ' '.join(shlex_quote(x) for x in cmd)
# This is a workaround for https://bugs.python.org/issue9400
class Py2CalledProcessError(subprocess.CalledProcessError):
def __init__(self, returncode, cmd, output=None, stderr=None):
super(Exception, self).__init__(returncode, cmd, output, stderr)
self.returncode = returncode
self.cmd = cmd
self.output = output
self.stderr = stderr
# https://docs.python.org/3/library/subprocess.html#subprocess.CompletedProcess
class Py2CompletedProcess:
def __init__(self, args, returncode, stdout, stderr):
self.args = args
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def __repr__(self):
_repr = ['args=%s' % repr(self.args), 'returncode=%s' % self.returncode]
if self.stdout is not None:
_repr.append('stdout=' + repr(self.stdout))
if self.stderr is not None:
_repr.append('stderr=' + repr(self.stderr))
return 'CompletedProcess(%s)' % ', '.join(_repr)
def check_returncode(self):
if self.returncode != 0:
raise Py2CalledProcessError(returncode=self.returncode, cmd=self.args, output=self.stdout, stderr=self.stderr)
def run_process(cmd, check=True, input=None, *args, **kw):
"""Runs a subpocess returning the exit code.
By default this function will raise an exception on failure. Therefor this should only be
used if you want to handle such failures. For most subprocesses, failures are not recoverable
and should be fatal. In those cases the `check_call` wrapper should be preferred.
"""
kw.setdefault('universal_newlines', True)
debug_text = '%sexecuted %s' % ('successfully ' if check else '', shlex_join(cmd))
if hasattr(subprocess, "run"):
ret = subprocess.run(cmd, check=check, input=input, *args, **kw)
logger.debug(debug_text)
return ret
# Python 2 compatibility: Introduce Python 3 subprocess.run-like behavior
if input is not None:
kw['stdin'] = subprocess.PIPE
proc = Popen(cmd, *args, **kw)
stdout, stderr = proc.communicate(input)
result = Py2CompletedProcess(cmd, proc.returncode, stdout, stderr)
if check:
result.check_returncode()
logger.debug(debug_text)
return result
def check_call(cmd, *args, **kw):
"""Like `run_process` above but treat failures as fatal and exit_with_error."""
try:
return run_process(cmd, *args, **kw)
except subprocess.CalledProcessError as e:
exit_with_error("'%s' failed (%d)", shlex_join(cmd), e.returncode)
except OSError as e:
exit_with_error("'%s' failed: %s", shlex_join(cmd), str(e))
def run_js_tool(filename, jsargs=[], *args, **kw):
"""Execute a javascript tool.
This is used by emcc to run parts of the build process that are written
implemented in javascript.
"""
command = NODE_JS + [filename] + jsargs
print_compiler_stage(command)
return check_call(command, *args, **kw).stdout
# Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'.
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
if os.path.isabs(program):
if os.path.isfile(program):
return program
if WINDOWS:
for suffix in ['.exe', '.cmd', '.bat']:
if is_exe(program + suffix):
return program + suffix
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
if WINDOWS:
for suffix in ('.exe', '.cmd', '.bat'):
if is_exe(exe_file + suffix):
return exe_file + suffix
return None
# Only used by tests and by ctor_evaller.py. Once fastcomp is removed
# this can most likely be moved into the tests/jsrun.py.
def timeout_run(proc, timeout=None, full_output=False, check=True):
start = time.time()
if timeout is not None:
while time.time() - start < timeout and proc.poll() is None:
time.sleep(0.1)
if proc.poll() is None:
proc.kill() # XXX bug: killing emscripten.py does not kill it's child process!
raise Exception("Timed out")
stdout, stderr = proc.communicate()
out = ['' if o is None else o for o in (stdout, stderr)]
if check and proc.returncode != 0:
raise subprocess.CalledProcessError(proc.returncode, '', stdout, stderr)
if TRACK_PROCESS_SPAWNS:
logging.info('Process ' + str(proc.pid) + ' finished after ' + str(time.time() - start) + ' seconds. Exit code: ' + str(proc.returncode))
return '\n'.join(out) if full_output else out[0]
def generate_config(path, first_time=False):
# Note: repr is used to ensure the paths are escaped correctly on Windows.
# The full string is replaced so that the template stays valid Python.
config_file = open(path_from_root('tools', 'settings_template.py')).read().splitlines()
config_file = config_file[3:] # remove the initial comment
config_file = '\n'.join(config_file)
# autodetect some default paths
config_file = config_file.replace('\'{{{ EMSCRIPTEN_ROOT }}}\'', repr(EMSCRIPTEN_ROOT))
llvm_root = os.path.dirname(which('llvm-dis') or '/usr/bin/llvm-dis')
config_file = config_file.replace('\'{{{ LLVM_ROOT }}}\'', repr(llvm_root))
node = which('nodejs') or which('node') or 'node'
config_file = config_file.replace('\'{{{ NODE }}}\'', repr(node))
abspath = os.path.abspath(os.path.expanduser(path))
# write
with open(abspath, 'w') as f:
f.write(config_file)
if first_time:
print('''
==============================================================================
Welcome to Emscripten!
This is the first time any of the Emscripten tools has been run.
A settings file has been copied to %s, at absolute path: %s
It contains our best guesses for the important paths, which are:
LLVM_ROOT = %s
NODE_JS = %s
EMSCRIPTEN_ROOT = %s
Please edit the file if any of those are incorrect.
This command will now exit. When you are done editing those paths, re-run it.
==============================================================================
''' % (path, abspath, llvm_root, node, EMSCRIPTEN_ROOT), file=sys.stderr)
def parse_config_file():
"""Parse the emscripten config file using python's exec.
Also also EM_<KEY> environment variables to override specific config keys.
"""
config = {}
config_text = open(CONFIG_FILE, 'r').read() if CONFIG_FILE else EM_CONFIG
try:
exec(config_text, config)
except Exception as e:
exit_with_error('Error in evaluating %s (at %s): %s, text: %s', EM_CONFIG, CONFIG_FILE, str(e), config_text)
CONFIG_KEYS = (
'NODE_JS',
'BINARYEN_ROOT',
'POPEN_WORKAROUND',
'SPIDERMONKEY_ENGINE',
'V8_ENGINE',
'LLVM_ROOT',
'LLVM_ADD_VERSION',
'CLANG_ADD_VERSION',
'CLOSURE_COMPILER',
'JAVA',
'JS_ENGINE',
'JS_ENGINES',
'WASMER',
'WASMTIME',
'WASM_ENGINES',
'FROZEN_CACHE',
'CACHE',
'PORTS',
)
# Only propagate certain settings from the config file.
for key in CONFIG_KEYS:
env_var = 'EM_' + key
env_value = os.environ.get(env_var)
if env_value is not None:
globals()[key] = env_value
elif key in config:
globals()[key] = config[key]
# Certain keys are mandatory
for key in ('LLVM_ROOT', 'NODE_JS', 'BINARYEN_ROOT'):
if key not in config:
exit_with_error('%s is not defined in %s', key, config_file_location())
if not globals()[key]:
exit_with_error('%s is set to empty value in %s', key, config_file_location())
if not NODE_JS:
exit_with_error('NODE_JS is not defined in %s', config_file_location())
def listify(x):
if type(x) is not list:
return [x]
return x
def fix_js_engine(old, new):
global JS_ENGINES
if old is None:
return
JS_ENGINES = [new if x == old else x for x in JS_ENGINES]
return new
def normalize_config_settings():
global CACHE, PORTS, JAVA, CLOSURE_COMPILER
global NODE_JS, V8_ENGINE, JS_ENGINE, JS_ENGINES, SPIDERMONKEY_ENGINE, WASM_ENGINES
# EM_CONFIG stuff
if not JS_ENGINES:
JS_ENGINES = [NODE_JS]
if not JS_ENGINE:
JS_ENGINE = JS_ENGINES[0]
# Engine tweaks
if SPIDERMONKEY_ENGINE:
new_spidermonkey = SPIDERMONKEY_ENGINE
if '-w' not in str(new_spidermonkey):
new_spidermonkey += ['-w']
SPIDERMONKEY_ENGINE = fix_js_engine(SPIDERMONKEY_ENGINE, new_spidermonkey)
NODE_JS = fix_js_engine(NODE_JS, listify(NODE_JS))
V8_ENGINE = fix_js_engine(V8_ENGINE, listify(V8_ENGINE))
JS_ENGINE = fix_js_engine(JS_ENGINE, listify(JS_ENGINE))
JS_ENGINES = [listify(engine) for engine in JS_ENGINES]
WASM_ENGINES = [listify(engine) for engine in WASM_ENGINES]
if not CACHE:
if root_is_writable():
CACHE = path_from_root('cache')
else:
# Use the legacy method of putting the cache in the user's home directory
# if the emscripten root is not writable.
# This is useful mostly for read-only installation and perhaps could
# be removed in the future since such installations should probably be
# setting a specific cache location.
logger.debug('Using home-directory for emscripten cache due to read-only root')
CACHE = os.path.expanduser(os.path.join('~', '.emscripten_cache'))
if not PORTS:
PORTS = os.path.join(CACHE, 'ports')
if CLOSURE_COMPILER is None:
if WINDOWS:
CLOSURE_COMPILER = [path_from_root('node_modules', '.bin', 'google-closure-compiler.cmd')]
else:
# Work around an issue that Closure compiler can take up a lot of memory and crash in an error
# "FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory"
CLOSURE_COMPILER = NODE_JS + ['--max_old_space_size=8192', path_from_root('node_modules', '.bin', 'google-closure-compiler')]
if JAVA is None:
logger.debug('JAVA not defined in ' + config_file_location() + ', using "java"')
JAVA = 'java'
# Returns the location of the emscripten config file.
def config_file_location():
# Handle the case where there is no config file at all (i.e. If EM_CONFIG is passed as python code
# direclty on the command line).
if not CONFIG_FILE:
return '<inline config>'
return CONFIG_FILE
def get_clang_version():
if not hasattr(get_clang_version, 'found_version'):
if not os.path.exists(CLANG_CC):
exit_with_error('clang executable not found at `%s`' % CLANG_CC)
proc = check_call([CLANG_CC, '--version'], stdout=PIPE)
m = re.search(r'[Vv]ersion\s+(\d+\.\d+)', proc.stdout)
get_clang_version.found_version = m and m.group(1)
return get_clang_version.found_version
def check_llvm_version():
actual = get_clang_version()
if EXPECTED_LLVM_VERSION in actual:
return True
diagnostics.warning('version-check', 'LLVM version appears incorrect (seeing "%s", expected "%s")', actual, EXPECTED_LLVM_VERSION)
return False
def get_llc_targets():
if not os.path.exists(LLVM_COMPILER):
exit_with_error('llc executable not found at `%s`' % LLVM_COMPILER)
try:
llc_version_info = run_process([LLVM_COMPILER, '--version'], stdout=PIPE).stdout
except subprocess.CalledProcessError:
exit_with_error('error running `llc --version`. Check your llvm installation (%s)' % LLVM_COMPILER)
if 'Registered Targets:' not in llc_version_info:
exit_with_error('error parsing output of `llc --version`. Check your llvm installation (%s)' % LLVM_COMPILER)
pre, targets = llc_version_info.split('Registered Targets:')
return targets
def check_llvm():
targets = get_llc_targets()
if 'wasm32' not in targets and 'WebAssembly 32-bit' not in targets:
logger.critical('LLVM has not been built with the WebAssembly backend, llc reports:')
print('===========================================================================', file=sys.stderr)
print(targets, file=sys.stderr)
print('===========================================================================', file=sys.stderr)
return False
return True
def get_node_directory():
return os.path.dirname(NODE_JS[0] if type(NODE_JS) is list else NODE_JS)
# When we run some tools from npm (closure, html-minifier-terser), those
# expect that the tools have node.js accessible in PATH. Place our node
# there when invoking those tools.
def env_with_node_in_path():
env = os.environ.copy()
env['PATH'] = get_node_directory() + os.pathsep + env['PATH']
return env
def check_node_version():
try:
actual = run_process(NODE_JS + ['--version'], stdout=PIPE).stdout.strip()
version = tuple(map(int, actual.replace('v', '').replace('-pre', '').split('.')))
except Exception as e:
diagnostics.warning('version-check', 'cannot check node version: %s', e)
return False
if version < EXPECTED_NODE_VERSION:
diagnostics.warning('version-check', 'node version appears too old (seeing "%s", expected "%s")', actual, 'v' + ('.'.join(map(str, EXPECTED_NODE_VERSION))))
return False
return True
def set_version_globals():
global EMSCRIPTEN_VERSION, EMSCRIPTEN_VERSION_MAJOR, EMSCRIPTEN_VERSION_MINOR, EMSCRIPTEN_VERSION_TINY
filename = path_from_root('emscripten-version.txt')
with open(filename) as f:
EMSCRIPTEN_VERSION = f.read().strip().replace('"', '')
parts = [int(x) for x in EMSCRIPTEN_VERSION.split('.')]
EMSCRIPTEN_VERSION_MAJOR, EMSCRIPTEN_VERSION_MINOR, EMSCRIPTEN_VERSION_TINY = parts
def generate_sanity():
sanity_file_content = EMSCRIPTEN_VERSION + '|' + LLVM_ROOT + '|' + get_clang_version()
if CONFIG_FILE:
config = open(CONFIG_FILE).read()
else:
config = EM_CONFIG
# Convert to unsigned for python2 and python3 compat
checksum = binascii.crc32(config.encode()) & 0xffffffff
sanity_file_content += '|%#x\n' % checksum
return sanity_file_content
def perform_sanify_checks():
logger.info('(Emscripten: Running sanity checks)')
with ToolchainProfiler.profile_block('sanity compiler_engine'):
try:
run_process(NODE_JS + ['-e', 'console.log("hello")'], stdout=PIPE)
except Exception as e:
exit_with_error('The configured node executable (%s) does not seem to work, check the paths in %s (%s)', NODE_JS, config_file_location, str(e))
with ToolchainProfiler.profile_block('sanity LLVM'):
for cmd in [CLANG_CC, LLVM_AR, LLVM_AS, LLVM_NM]:
if not os.path.exists(cmd) and not os.path.exists(cmd + '.exe'): # .exe extension required for Windows
exit_with_error('Cannot find %s, check the paths in %s', cmd, EM_CONFIG)
def check_sanity(force=False):
"""Check that basic stuff we need (a JS engine to compile, Node.js, and Clang
and LLVM) exists.
The test runner always does this check (through |force|). emcc does this less
frequently, only when ${EM_CONFIG}_sanity does not exist or is older than
EM_CONFIG (so, we re-check sanity when the settings are changed). We also
re-check sanity and clear the cache when the version changes.
"""
if not force and os.environ.get('EMCC_SKIP_SANITY_CHECK') == '1':
return
# We set EMCC_SKIP_SANITY_CHECK so that any subprocesses that we launch will
# not re-run the tests.
os.environ['EMCC_SKIP_SANITY_CHECK'] = '1'
with ToolchainProfiler.profile_block('sanity'):
check_llvm_version()
if not CONFIG_FILE:
return # config stored directly in EM_CONFIG => skip sanity checks
expected = generate_sanity()
sanity_file = Cache.get_path('sanity.txt', root=True)
if os.path.exists(sanity_file):
sanity_data = open(sanity_file).read()
if sanity_data != expected:
logger.debug('old sanity: %s' % sanity_data)
logger.debug('new sanity: %s' % expected)
if FROZEN_CACHE:
logger.info('(Emscripten: config changed, cache may need to be cleared, but FROZEN_CACHE is set)')
else:
logger.info('(Emscripten: config changed, clearing cache)')
Cache.erase()
# the check actually failed, so definitely write out the sanity file, to
# avoid others later seeing failures too
force = False
elif not force:
return # all is well
# some warning, mostly not fatal checks - do them even if EM_IGNORE_SANITY is on
check_node_version()
llvm_ok = check_llvm()
if os.environ.get('EM_IGNORE_SANITY'):
logger.info('EM_IGNORE_SANITY set, ignoring sanity checks')
return
if not llvm_ok:
exit_with_error('failing sanity checks due to previous llvm failure')
perform_sanify_checks()
if not force:
# Only create/update this file if the sanity check succeeded, i.e., we got here
Cache.ensure()
with open(sanity_file, 'w') as f:
f.write(expected)
# Some distributions ship with multiple llvm versions so they add
# the version to the binaries, cope with that
def build_llvm_tool_path(tool):
if LLVM_ADD_VERSION:
return os.path.join(LLVM_ROOT, tool + "-" + LLVM_ADD_VERSION)
else:
return os.path.join(LLVM_ROOT, tool)
# Some distributions ship with multiple clang versions so they add
# the version to the binaries, cope with that
def build_clang_tool_path(tool):
if CLANG_ADD_VERSION:
return os.path.join(LLVM_ROOT, tool + "-" + CLANG_ADD_VERSION)
else:
return os.path.join(LLVM_ROOT, tool)
def exe_suffix(cmd):
return cmd + '.exe' if WINDOWS else cmd
def bat_suffix(cmd):
return cmd + '.bat' if WINDOWS else cmd
def replace_suffix(filename, new_suffix):
assert new_suffix[0] == '.'
return os.path.splitext(filename)[0] + new_suffix
# In MINIMAL_RUNTIME mode, keep suffixes of generated files simple
# ('.mem' instead of '.js.mem'; .'symbols' instead of '.js.symbols' etc)
# Retain the original naming scheme in traditional runtime.
def replace_or_append_suffix(filename, new_suffix):
assert new_suffix[0] == '.'
return replace_suffix(filename, new_suffix) if Settings.MINIMAL_RUNTIME else filename + new_suffix
def safe_ensure_dirs(dirname):
try:
os.makedirs(dirname)
except OSError:
# Python 2 compatibility: makedirs does not support exist_ok parameter
# Ignore error for already existing dirname as exist_ok does
if not os.path.isdir(dirname):
raise
# Temp dir. Create a random one, unless EMCC_DEBUG is set, in which case use the canonical
# temp directory (TEMP_DIR/emscripten_temp).
def get_emscripten_temp_dir():
"""Returns a path to EMSCRIPTEN_TEMP_DIR, creating one if it didn't exist."""
global configuration, EMSCRIPTEN_TEMP_DIR
if not EMSCRIPTEN_TEMP_DIR:
EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=configuration.TEMP_DIR)
def prepare_to_clean_temp(d):
def clean_temp():
try_delete(d)
atexit.register(clean_temp)
prepare_to_clean_temp(EMSCRIPTEN_TEMP_DIR) # this global var might change later
return EMSCRIPTEN_TEMP_DIR
def get_canonical_temp_dir(temp_dir):
return os.path.join(temp_dir, 'emscripten_temp')
class Configuration(object):
def __init__(self):
self.EMSCRIPTEN_TEMP_DIR = None
self.TEMP_DIR = os.environ.get("EMCC_TEMP_DIR", tempfile.gettempdir())
if not os.path.isdir(self.TEMP_DIR):
exit_with_error("The temporary directory `" + self.TEMP_DIR + "` does not exist! Please make sure that the path is correct.")
self.CANONICAL_TEMP_DIR = get_canonical_temp_dir(self.TEMP_DIR)
if DEBUG:
self.EMSCRIPTEN_TEMP_DIR = self.CANONICAL_TEMP_DIR
try:
safe_ensure_dirs(self.EMSCRIPTEN_TEMP_DIR)
except Exception as e:
exit_with_error(str(e) + 'Could not create canonical temp dir. Check definition of TEMP_DIR in ' + config_file_location())
def get_temp_files(self):
return tempfiles.TempFiles(
tmp=self.TEMP_DIR if not DEBUG else get_emscripten_temp_dir(),
save_debug_files=os.environ.get('EMCC_DEBUG_SAVE'))
def apply_configuration():
global configuration, EMSCRIPTEN_TEMP_DIR, CANONICAL_TEMP_DIR, TEMP_DIR
configuration = Configuration()
EMSCRIPTEN_TEMP_DIR = configuration.EMSCRIPTEN_TEMP_DIR
CANONICAL_TEMP_DIR = configuration.CANONICAL_TEMP_DIR
TEMP_DIR = configuration.TEMP_DIR
def get_llvm_target():
return LLVM_TARGET
def emsdk_ldflags(user_args):
if os.environ.get('EMMAKEN_NO_SDK'):
return []
library_paths = [
path_from_root('system', 'local', 'lib'),
path_from_root('system', 'lib'),
Cache.dirname
]
ldflags = ['-L' + l for l in library_paths]
if '-nostdlib' in user_args:
return ldflags
# TODO(sbc): Add system libraries here rather than conditionally including
# them via .symbols files.
libraries = []
ldflags += ['-l' + l for l in libraries]
return ldflags
def emsdk_cflags(user_args, cxx):
# Disable system C and C++ include directories, and add our own (using
# -isystem so they are last, like system dirs, which allows projects to
# override them)
c_opts = ['-Xclang', '-nostdsysteminc']
c_include_paths = [
path_from_root('system', 'include', 'compat'),
path_from_root('system', 'include'),
path_from_root('system', 'include', 'libc'),
path_from_root('system', 'lib', 'libc', 'musl', 'arch', 'emscripten'),
path_from_root('system', 'local', 'include'),
path_from_root('system', 'include', 'SSE'),
path_from_root('system', 'include', 'neon'),
path_from_root('system', 'lib', 'compiler-rt', 'include'),
path_from_root('system', 'lib', 'libunwind', 'include'),
Cache.get_path('include')
]
cxx_include_paths = [
path_from_root('system', 'include', 'libcxx'),
path_from_root('system', 'lib', 'libcxxabi', 'include'),
]
def include_directive(paths):
result = []
for path in paths:
result += ['-Xclang', '-isystem' + path]
return result
def array_contains_any_of(hay, needles):
for n in needles:
if n in hay:
return True
if array_contains_any_of(user_args, SIMD_INTEL_FEATURE_TOWER) or array_contains_any_of(user_args, SIMD_NEON_FLAGS):
if '-msimd128' not in user_args:
exit_with_error('Passing any of ' + ', '.join(SIMD_INTEL_FEATURE_TOWER + SIMD_NEON_FLAGS) + ' flags also requires passing -msimd128!')
c_opts += ['-D__SSE__=1']
if array_contains_any_of(user_args, SIMD_INTEL_FEATURE_TOWER[1:]):
c_opts += ['-D__SSE2__=1']
if array_contains_any_of(user_args, SIMD_INTEL_FEATURE_TOWER[2:]):
c_opts += ['-D__SSE3__=1']
if array_contains_any_of(user_args, SIMD_INTEL_FEATURE_TOWER[3:]):
c_opts += ['-D__SSSE3__=1']
if array_contains_any_of(user_args, SIMD_INTEL_FEATURE_TOWER[4:]):
c_opts += ['-D__SSE4_1__=1']
if array_contains_any_of(user_args, SIMD_INTEL_FEATURE_TOWER[5:]):
c_opts += ['-D__SSE4_2__=1']
if array_contains_any_of(user_args, SIMD_INTEL_FEATURE_TOWER[6:]):
c_opts += ['-D__AVX__=1']
if array_contains_any_of(user_args, SIMD_NEON_FLAGS):
c_opts += ['-D__ARM_NEON__=1']
# libcxx include paths must be defined before libc's include paths otherwise libcxx will not build
if cxx:
c_opts += include_directive(cxx_include_paths)
return c_opts + include_directive(c_include_paths)
def get_asmflags(user_args):
return ['-target', get_llvm_target()]
def get_cflags(user_args, cxx):
# Set the LIBCPP ABI version to at least 2 so that we get nicely aligned string
# data and other nice fixes.
c_opts = [# '-fno-threadsafe-statics', # disabled due to issue 1289
'-target', get_llvm_target(),
'-D__EMSCRIPTEN_major__=' + str(EMSCRIPTEN_VERSION_MAJOR),
'-D__EMSCRIPTEN_minor__=' + str(EMSCRIPTEN_VERSION_MINOR),
'-D__EMSCRIPTEN_tiny__=' + str(EMSCRIPTEN_VERSION_TINY),
'-D_LIBCPP_ABI_VERSION=2']
# For compatability with the fastcomp compiler that defined these
c_opts += ['-Dunix',
'-D__unix',
'-D__unix__']
# Changes to default clang behavior
# Implicit functions can cause horribly confusing function pointer type errors, see #2175
# If your codebase really needs them - very unrecommended! - you can disable the error with
# -Wno-error=implicit-function-declaration
# or disable even a warning about it with
# -Wno-implicit-function-declaration
c_opts += ['-Werror=implicit-function-declaration']
if os.environ.get('EMMAKEN_NO_SDK') or '-nostdinc' in user_args:
return c_opts
return c_opts + emsdk_cflags(user_args, cxx)
# Settings. A global singleton. Not pretty, but nicer than passing |, settings| everywhere
class SettingsManager(object):
class __impl(object):
attrs = {}
internal_settings = set()
def __init__(self):
self.reset()
@classmethod
def reset(cls):
cls.attrs = {}
# Load the JS defaults into python.
settings = open(path_from_root('src', 'settings.js')).read().replace('//', '#')
settings = re.sub(r'var ([\w\d]+)', r'attrs["\1"]', settings)
# Variable TARGET_NOT_SUPPORTED is referenced by value settings.js (also beyond declaring it),
# so must pass it there explicitly.
exec(settings, {'attrs': cls.attrs})
settings = open(path_from_root('src', 'settings_internal.js')).read().replace('//', '#')
settings = re.sub(r'var ([\w\d]+)', r'attrs["\1"]', settings)
internal_attrs = {}
exec(settings, {'attrs': internal_attrs})
cls.attrs.update(internal_attrs)
if 'EMCC_STRICT' in os.environ:
cls.attrs['STRICT'] = int(os.environ.get('EMCC_STRICT'))
# Special handling for LEGACY_SETTINGS. See src/setting.js for more
# details
cls.legacy_settings = {}
cls.alt_names = {}
for legacy in cls.attrs['LEGACY_SETTINGS']:
if len(legacy) == 2:
name, new_name = legacy
cls.legacy_settings[name] = (None, 'setting renamed to ' + new_name)
cls.alt_names[name] = new_name
cls.alt_names[new_name] = name
default_value = cls.attrs[new_name]
else:
name, fixed_values, err = legacy
cls.legacy_settings[name] = (fixed_values, err)
default_value = fixed_values[0]
assert name not in cls.attrs, 'legacy setting (%s) cannot also be a regular setting' % name
if not cls.attrs['STRICT']:
cls.attrs[name] = default_value
cls.internal_settings = set(internal_attrs.keys())
# Transforms the Settings information into emcc-compatible args (-s X=Y, etc.). Basically
# the reverse of load_settings, except for -Ox which is relevant there but not here
@classmethod
def serialize(cls):
ret = []
for key, value in cls.attrs.items():
if key == key.upper(): # this is a hack. all of our settings are ALL_CAPS, python internals are not
jsoned = json.dumps(value, sort_keys=True)
ret += ['-s', key + '=' + jsoned]
return ret
@classmethod
def to_dict(cls):
return cls.attrs.copy()
@classmethod
def copy(cls, values):
cls.attrs = values
@classmethod
def apply_opt_level(cls, opt_level, shrink_level=0, noisy=False):
if opt_level >= 1:
cls.attrs['ASSERTIONS'] = 0
if shrink_level >= 2:
cls.attrs['EVAL_CTORS'] = 1
def keys(self):
return self.attrs.keys()
def __getattr__(self, attr):
if attr in self.attrs:
return self.attrs[attr]
else:
raise AttributeError("Settings object has no attribute '%s'" % attr)
def __setattr__(self, attr, value):
if attr == 'STRICT' and value:
for a in self.legacy_settings:
self.attrs.pop(a, None)
if attr in self.legacy_settings:
# TODO(sbc): Rather then special case this we should have STRICT turn on the
# legacy-settings warning below
if self.attrs['STRICT']:
exit_with_error('legacy setting used in strict mode: %s', attr)
fixed_values, error_message = self.legacy_settings[attr]
if fixed_values and value not in fixed_values:
exit_with_error('Invalid command line option -s ' + attr + '=' + str(value) + ': ' + error_message)
diagnostics.warning('legacy-settings', 'use of legacy setting: %s (%s)', attr, error_message)
if attr in self.alt_names:
alt_name = self.alt_names[attr]
self.attrs[alt_name] = value
if attr not in self.attrs:
msg = "Attempt to set a non-existent setting: '%s'\n" % attr
suggestions = difflib.get_close_matches(attr, list(self.attrs.keys()))
suggestions = [s for s in suggestions if s not in self.legacy_settings]
suggestions = ', '.join(suggestions)
if suggestions:
msg += ' - did you mean one of %s?\n' % suggestions
msg += " - perhaps a typo in emcc's -s X=Y notation?\n"
msg += ' - (see src/settings.js for valid values)'
exit_with_error(msg)
self.attrs[attr] = value
@classmethod
def get(cls, key):
return cls.attrs.get(key)
@classmethod
def __getitem__(cls, key):
return cls.attrs[key]
@classmethod
def target_environment_may_be(self, environment):
return self.attrs['ENVIRONMENT'] == '' or environment in self.attrs['ENVIRONMENT'].split(',')
__instance = None
@staticmethod
def instance():
if SettingsManager.__instance is None:
SettingsManager.__instance = SettingsManager.__impl()
return SettingsManager.__instance
def __getattr__(self, attr):
return getattr(self.instance(), attr)
def __setattr__(self, attr, value):
return setattr(self.instance(), attr, value)
def get(self, key):
return self.instance().get(key)
def __getitem__(self, key):