1
import shutil, time, os, sys, json, tempfile, copy, shlex, atexit, subprocess, hashlib, cPickle, re
2
from subprocess import Popen, PIPE, STDOUT
3
from tempfile import mkstemp
4
import jsrun, cache, tempfiles
5
from response_file import create_response_file
8
if type(x) is not list: return [x]
12
from tempfiles import try_delete
14
# 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.
15
# 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.
16
# See http://bugs.python.org/issue3905
18
def __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False,
19
shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0):
24
# (stdin, stdout, stderr) store what the caller originally wanted to be done with the streams.
25
# (stdin_, stdout_, stderr_) will store the fixed set of streams that workaround the bug.
30
# If the caller wants one of these PIPEd, we must PIPE them all to avoid the 'handle is invalid' bug.
31
if self.stdin_ == PIPE or self.stdout_ == PIPE or self.stderr_ == PIPE:
32
if self.stdin_ == None:
34
if self.stdout_ == None:
36
if self.stderr_ == None:
39
# emscripten.py supports reading args from a response file instead of cmdline.
40
# Use .rsp to avoid cmdline length limitations on Windows.
41
if len(args) >= 2 and args[1].endswith("emscripten.py"):
42
self.response_filename = create_response_file(args[2:], TEMP_DIR)
43
args = args[0:2] + ['@' + self.response_filename]
46
# Call the process with fixed streams.
47
self.process = subprocess.Popen(args, bufsize, executable, self.stdin_, self.stdout_, self.stderr_, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
49
print >> sys.stderr, '\nsubprocess.Popen(args=%s) failed! Exception %s\n' % (' '.join(args), str(e))
52
def communicate(self, input=None):
53
output = self.process.communicate(input)
54
self.returncode = self.process.returncode
56
# If caller never wanted to PIPE stdout or stderr, route the output back to screen to avoid swallowing output.
57
if self.stdout == None and self.stdout_ == PIPE and len(output[0].strip()) > 0:
58
print >> sys.stdout, output[0]
59
if self.stderr == None and self.stderr_ == PIPE and len(output[1].strip()) > 0:
60
print >> sys.stderr, output[1]
62
# Return a mock object to the caller. This works as long as all emscripten code immediately .communicate()s the result, and doesn't
63
# leave the process object around for longer/more exotic uses.
64
if self.stdout == None and self.stderr == None:
66
if self.stdout == None:
67
return (None, output[1])
68
if self.stderr == None:
69
return (output[0], None)
70
return (output[0], output[1])
73
return self.process.poll()
76
return self.process.kill()
80
# Clean up the temporary response file that was used to spawn this process, so that we don't leave temp files around.
81
tempfiles.try_delete(self.response_filename)
83
pass # Mute all exceptions in dtor, particularly if we didn't use a response file, self.response_filename doesn't exist.
85
# Install our replacement Popen handler if we are running on Windows to avoid python spawn process function.
89
__rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
90
def path_from_root(*pathelems):
91
return os.path.join(__rootpath__, *pathelems)
93
# Emscripten configuration is done through the EM_CONFIG environment variable.
94
# If the string value contained in this environment variable contains newline
95
# separated definitions, then these definitions will be used to configure
96
# Emscripten. Otherwise, the string is understood to be a path to a settings
97
# file that contains the required definitions.
99
EM_CONFIG = os.environ.get('EM_CONFIG')
101
EM_CONFIG = '~/.emscripten'
102
if '\n' in EM_CONFIG:
105
CONFIG_FILE = os.path.expanduser(EM_CONFIG)
106
if not os.path.exists(CONFIG_FILE):
107
config_file = open(path_from_root('tools', 'settings_template_readonly.py')).read().split('\n')
108
config_file = config_file[1:] # remove "this file will be copied..."
109
config_file = '\n'.join(config_file)
110
# autodetect some default paths
111
config_file = config_file.replace('{{{ EMSCRIPTEN_ROOT }}}', __rootpath__)
112
llvm_root = '/usr/bin'
114
llvm_root = os.path.dirname(Popen(['which', 'llvm-dis'], stdout=PIPE).communicate()[0].replace('\n', ''))
117
config_file = config_file.replace('{{{ LLVM_ROOT }}}', llvm_root)
120
node = Popen(['which', 'node'], stdout=PIPE).communicate()[0].replace('\n', '') or \
121
Popen(['which', 'nodejs'], stdout=PIPE).communicate()[0].replace('\n', '') or node
124
config_file = config_file.replace('{{{ NODE }}}', node)
127
python = Popen(['which', 'python2'], stdout=PIPE).communicate()[0].replace('\n', '') or \
128
Popen(['which', 'python'], stdout=PIPE).communicate()[0].replace('\n', '') or python
131
config_file = config_file.replace('{{{ PYTHON }}}', python)
134
open(CONFIG_FILE, 'w').write(config_file)
135
print >> sys.stderr, '''
136
==============================================================================
137
Welcome to Emscripten!
139
This is the first time any of the Emscripten tools has been run.
141
A settings file has been copied to %s, at absolute path: %s
143
It contains our best guesses for the important paths, which are:
150
Please edit the file if any of those are incorrect.
152
This command will now exit. When you are done editing those paths, re-run it.
153
==============================================================================
154
''' % (EM_CONFIG, CONFIG_FILE, llvm_root, python, node, __rootpath__)
157
config_text = open(CONFIG_FILE, 'r').read() if CONFIG_FILE else EM_CONFIG
160
print >> sys.stderr, 'Error in evaluating %s (at %s): %s, text: %s' % (EM_CONFIG, CONFIG_FILE, str(e), config_text)
165
EXPECTED_LLVM_VERSION = (3,2)
167
def check_clang_version():
168
expected = 'clang version ' + '.'.join(map(str, EXPECTED_LLVM_VERSION))
169
actual = Popen([CLANG, '-v'], stderr=PIPE).communicate()[1].split('\n')[0]
170
if expected in actual:
172
print >> sys.stderr, 'warning: LLVM version appears incorrect (seeing "%s", expected "%s")' % (actual, expected)
175
def check_llvm_version():
177
check_clang_version();
179
print >> sys.stderr, 'warning: Could not verify LLVM version: %s' % str(e)
181
EXPECTED_NODE_VERSION = (0,6,8)
183
def check_node_version():
185
node = listify(NODE_JS)
186
actual = Popen(node + ['--version'], stdout=PIPE).communicate()[0].strip()
187
version = tuple(map(int, actual.replace('v', '').split('.')))
188
if version >= EXPECTED_NODE_VERSION:
190
print >> sys.stderr, 'warning: node version appears too old (seeing "%s", expected "%s")' % (actual, 'v' + ('.'.join(map(str, EXPECTED_NODE_VERSION))))
193
print >> sys.stderr, 'warning: cannot check node version:', e
196
# Check that basic stuff we need (a JS engine to compile, Node.js, and Clang and LLVM)
198
# The test runner always does this check (through |force|). emcc does this less frequently,
199
# only when ${EM_CONFIG}_sanity does not exist or is older than EM_CONFIG (so,
200
# we re-check sanity when the settings are changed)
201
# We also re-check sanity and clear the cache when the version changes
203
EMSCRIPTEN_VERSION = '1.4.1'
205
def generate_sanity():
206
return EMSCRIPTEN_VERSION + '|' + get_llvm_target()
208
def check_sanity(force=False):
212
if not force: return # config stored directly in EM_CONFIG => skip sanity checks
214
settings_mtime = os.stat(CONFIG_FILE).st_mtime
215
sanity_file = CONFIG_FILE + '_sanity'
216
if os.path.exists(sanity_file):
218
sanity_mtime = os.stat(sanity_file).st_mtime
219
if sanity_mtime <= settings_mtime:
220
reason = 'settings file has changed'
222
sanity_data = open(sanity_file).read()
223
if sanity_data != generate_sanity():
224
reason = 'system change: %s vs %s' % (generate_sanity(), sanity_data)
226
if not force: return # all is well
228
reason = 'unknown: ' + str(e)
230
print >> sys.stderr, '(Emscripten: %s, clearing cache)' % reason
233
# some warning, not fatal checks - do them even if EM_IGNORE_SANITY is on
237
if os.environ.get('EM_IGNORE_SANITY'):
238
print >> sys.stderr, 'EM_IGNORE_SANITY set, ignoring sanity checks'
241
print >> sys.stderr, '(Emscripten: Running sanity checks)'
243
if not check_engine(COMPILER_ENGINE):
244
print >> sys.stderr, 'FATAL: The JavaScript shell used for compiling (%s) does not seem to work, check the paths in %s' % (COMPILER_ENGINE, EM_CONFIG)
247
if NODE_JS != COMPILER_ENGINE:
248
if not check_engine(NODE_JS):
249
print >> sys.stderr, 'FATAL: Node.js (%s) does not seem to work, check the paths in %s' % (NODE_JS, EM_CONFIG)
252
for cmd in [CLANG, LLVM_LINK, LLVM_AR, LLVM_OPT, LLVM_AS, LLVM_DIS, LLVM_NM]:
253
if not os.path.exists(cmd) and not os.path.exists(cmd + '.exe'): # .exe extension required for Windows
254
print >> sys.stderr, 'FATAL: Cannot find %s, check the paths in %s' % (cmd, EM_CONFIG)
258
subprocess.call([JAVA, '-version'], stdout=PIPE, stderr=PIPE)
260
print >> sys.stderr, 'WARNING: java does not seem to exist, required for closure compiler. -O2 and above will fail. You need to define JAVA in ~/.emscripten'
262
if not os.path.exists(CLOSURE_COMPILER):
263
print >> sys.stderr, 'WARNING: Closure compiler (%s) does not exist, check the paths in %s. -O2 and above will fail' % (CLOSURE_COMPILER, EM_CONFIG)
265
# Sanity check passed!
268
# Only create/update this file if the sanity check succeeded, i.e., we got here
269
f = open(sanity_file, 'w')
270
f.write(generate_sanity())
274
# Any error here is not worth failing on
275
print 'WARNING: sanity check failed to run', e
279
LLVM_ADD_VERSION = os.getenv('LLVM_ADD_VERSION')
280
CLANG_ADD_VERSION = os.getenv('CLANG_ADD_VERSION')
282
# Some distributions ship with multiple llvm versions so they add
283
# the version to the binaries, cope with that
284
def build_llvm_tool_path(tool):
286
return os.path.join(LLVM_ROOT, tool + "-" + LLVM_ADD_VERSION)
288
return os.path.join(LLVM_ROOT, tool)
290
# Some distributions ship with multiple clang versions so they add
291
# the version to the binaries, cope with that
292
def build_clang_tool_path(tool):
293
if CLANG_ADD_VERSION:
294
return os.path.join(LLVM_ROOT, tool + "-" + CLANG_ADD_VERSION)
296
return os.path.join(LLVM_ROOT, tool)
298
CLANG_CC=os.path.expanduser(build_clang_tool_path('clang'))
299
CLANG_CPP=os.path.expanduser(build_clang_tool_path('clang++'))
301
LLVM_LINK=build_llvm_tool_path('llvm-link')
302
LLVM_AR=build_llvm_tool_path('llvm-ar')
303
LLVM_OPT=os.path.expanduser(build_llvm_tool_path('opt'))
304
LLVM_AS=os.path.expanduser(build_llvm_tool_path('llvm-as'))
305
LLVM_DIS=os.path.expanduser(build_llvm_tool_path('llvm-dis'))
306
LLVM_NM=os.path.expanduser(build_llvm_tool_path('llvm-nm'))
307
LLVM_INTERPRETER=os.path.expanduser(build_llvm_tool_path('lli'))
308
LLVM_COMPILER=os.path.expanduser(build_llvm_tool_path('llc'))
310
EMSCRIPTEN = path_from_root('emscripten.py')
311
DEMANGLER = path_from_root('third_party', 'demangler.py')
312
NAMESPACER = path_from_root('tools', 'namespacer.py')
313
EMCC = path_from_root('emcc')
314
EMXX = path_from_root('em++')
315
EMAR = path_from_root('emar')
316
EMRANLIB = path_from_root('emranlib')
317
EMLIBTOOL = path_from_root('emlibtool')
318
EMCONFIG = path_from_root('em-config')
319
EMMAKEN = path_from_root('tools', 'emmaken.py')
320
AUTODEBUGGER = path_from_root('tools', 'autodebugger.py')
321
BINDINGS_GENERATOR = path_from_root('tools', 'bindings_generator.py')
322
EXEC_LLVM = path_from_root('tools', 'exec_llvm.py')
323
FILE_PACKAGER = path_from_root('tools', 'file_packager.py')
325
# Temp dir. Create a random one, unless EMCC_DEBUG is set, in which case use TEMP_DIR/emscripten_temp
328
def __init__(self, environ):
329
self.DEBUG = environ.get('EMCC_DEBUG')
330
if self.DEBUG == "0":
332
self.DEBUG_CACHE = self.DEBUG and "cache" in self.DEBUG
333
self.EMSCRIPTEN_TEMP_DIR = None
336
self.TEMP_DIR = TEMP_DIR
338
print >> sys.stderr, 'TEMP_DIR not defined in ~/.emscripten, using /tmp'
339
self.TEMP_DIR = '/tmp'
341
self.CANONICAL_TEMP_DIR = os.path.join(self.TEMP_DIR, 'emscripten_temp')
345
self.EMSCRIPTEN_TEMP_DIR = self.CANONICAL_TEMP_DIR
346
if not os.path.exists(self.EMSCRIPTEN_TEMP_DIR):
347
os.makedirs(self.EMSCRIPTEN_TEMP_DIR)
349
print >> sys.stderr, e, 'Could not create canonical temp dir. Check definition of TEMP_DIR in ~/.emscripten'
351
def get_temp_files(self):
352
return tempfiles.TempFiles(
353
tmp=self.TEMP_DIR if not self.DEBUG else self.EMSCRIPTEN_TEMP_DIR,
354
save_debug_files=os.environ.get('EMCC_DEBUG_SAVE'))
356
def debug_log(self, msg):
358
print >> sys.stderr, msg
360
configuration = Configuration(environ=os.environ)
361
DEBUG = configuration.DEBUG
362
EMSCRIPTEN_TEMP_DIR = configuration.EMSCRIPTEN_TEMP_DIR
363
DEBUG_CACHE = configuration.DEBUG_CACHE
364
CANONICAL_TEMP_DIR = configuration.CANONICAL_TEMP_DIR
366
if not EMSCRIPTEN_TEMP_DIR:
367
EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=configuration.TEMP_DIR)
369
try_delete(EMSCRIPTEN_TEMP_DIR)
370
atexit.register(clean_temp)
378
JS_ENGINES = [JS_ENGINE]
380
print 'ERROR: %s does not seem to have JS_ENGINES or JS_ENGINE set up' % EM_CONFIG
386
CLOSURE_COMPILER = path_from_root('third_party', 'closure-compiler', 'compiler.jar')
391
if DEBUG: print >> sys.stderr, 'PYTHON not defined in ~/.emscripten, using "python"'
397
if DEBUG: print >> sys.stderr, 'JAVA not defined in ~/.emscripten, using "java"'
400
# Additional compiler options
402
# Target choice. Must be synced with src/settings.js (TARGET_*)
403
def get_llvm_target():
404
return os.environ.get('EMCC_LLVM_TARGET') or 'i386-pc-linux-gnu' # 'le32-unknown-nacl'
405
LLVM_TARGET = get_llvm_target()
408
COMPILER_OPTS # Can be set in EM_CONFIG, optionally
411
# Force a simple, standard target as much as possible: target 32-bit linux, and disable various flags that hint at other platforms
412
COMPILER_OPTS = COMPILER_OPTS + ['-m32', '-U__i386__', '-U__i386', '-Ui386',
413
'-U__SSE__', '-U__SSE_MATH__', '-U__SSE2__', '-U__SSE2_MATH__', '-U__MMX__',
414
'-DEMSCRIPTEN', '-D__EMSCRIPTEN__', '-U__STRICT_ANSI__',
415
'-D__IEEE_LITTLE_ENDIAN', '-fno-math-errno',
416
'-target', LLVM_TARGET]
418
USE_EMSDK = not os.environ.get('EMMAKEN_NO_SDK')
421
# Disable system C and C++ include directories, and add our own (using -idirafter so they are last, like system dirs, which
422
# allows projects to override them)
423
EMSDK_OPTS = ['-nostdinc', '-nostdinc++', '-Xclang', '-nobuiltininc', '-Xclang', '-nostdsysteminc',
424
'-Xclang', '-isystem' + path_from_root('system', 'local', 'include'),
425
'-Xclang', '-isystem' + path_from_root('system', 'include', 'libcxx'),
426
'-Xclang', '-isystem' + path_from_root('system', 'include'),
427
'-Xclang', '-isystem' + path_from_root('system', 'include', 'emscripten'),
428
'-Xclang', '-isystem' + path_from_root('system', 'include', 'bsd'), # posix stuff
429
'-Xclang', '-isystem' + path_from_root('system', 'include', 'libc'),
430
'-Xclang', '-isystem' + path_from_root('system', 'include', 'gfx'),
431
'-Xclang', '-isystem' + path_from_root('system', 'include', 'net'),
432
'-Xclang', '-isystem' + path_from_root('system', 'include', 'SDL'),
434
'-U__APPLE__', '-U__linux__'
436
COMPILER_OPTS += EMSDK_OPTS
440
#print >> sys.stderr, 'SDK opts', ' '.join(EMSDK_OPTS)
441
#print >> sys.stderr, 'Compiler opts', ' '.join(COMPILER_OPTS)
446
if 'gcparam' not in str(SPIDERMONKEY_ENGINE):
447
if type(SPIDERMONKEY_ENGINE) is str:
448
SPIDERMONKEY_ENGINE = [SPIDERMONKEY_ENGINE]
449
SPIDERMONKEY_ENGINE += ['-e', "gcparam('maxBytes', 1024*1024*1024);"] # Our very large files need lots of gc heap
453
WINDOWS = sys.platform.startswith('win')
455
# If we have 'env', we should use that to find python, because |python| may fail while |env python| may work
456
# (For example, if system python is 3.x while we need 2.x, and env gives 2.x if told to do so.)
460
assert 'Python' in Popen(['env', 'python', '-V'], stdout=PIPE, stderr=STDOUT).communicate()[0]
467
def check_engine(engine):
468
# TODO: we call this several times, perhaps cache the results?
471
return True # config stored directly in EM_CONFIG => skip engine check
472
return 'hello, world!' in run_js(path_from_root('tests', 'hello_world.js'), engine)
474
print 'Checking JS engine %s failed. Check %s. Details: %s' % (str(engine), EM_CONFIG, str(e))
477
def run_js(filename, engine=None, *args, **kw):
479
engine = JS_ENGINES[0]
480
return jsrun.run_js(filename, engine, *args, **kw)
483
# By default, LLVM_GCC and CLANG are really the C++ versions. This gets an explicit C version
484
return cxx.replace('clang++', 'clang').replace('g++', 'gcc')
486
def line_splitter(data):
487
"""Silly little tool to split JSON arrays over many lines."""
492
for i in range(len(data)):
494
if data[i] == ' ' and counter > 60:
502
def limit_size(string, MAX=80*20):
503
if len(string) < MAX: return string
504
return string[0:MAX/2] + '\n[..]\n' + string[-MAX/2:]
506
def read_pgo_data(filename):
508
Reads the output of PGO and generates proper information for CORRECT_* == 2 's *_LINES options
513
for line in open(filename, 'r'):
515
if line.rstrip() == '': continue
516
if '%0 failures' in line: continue
517
left, right = line.split(' : ')
518
signature = left.split('|')[1]
520
signs_lines.append(signature)
521
elif 'Overflow' in left:
522
overflows_lines.append(signature)
527
'signs_lines': signs_lines,
528
'overflows_lines': overflows_lines
531
def unique_ordered(values): # return a list of unique values in an input list, without changing order (list(set(.)) would change order randomly)
534
if value in seen: return False
537
return filter(check, values)
539
# Settings. A global singleton. Not pretty, but nicer than passing |, settings| everywhere
546
reset = Settings.reset
548
# Given some emcc-type args (-O3, -s X=Y, etc.), fill Settings with the right settings
550
def load(self, args=[]):
551
# Load the JS defaults into python
552
settings = open(path_from_root('src', 'settings.js')).read().replace('var ', 'Settings.').replace('//', '#')
553
exec settings in globals()
555
# Apply additional settings. First -O, then -s
556
for i in range(len(args)):
557
if args[i].startswith('-O'):
558
level = eval(args[i][2])
559
Settings.apply_opt_level(level)
560
for i in range(len(args)):
562
exec 'Settings.' + args[i+1] in globals() # execute the setting
564
# Transforms the Settings information into emcc-compatible args (-s X=Y, etc.). Basically
565
# the reverse of load_settings, except for -Ox which is relevant there but not here
569
for key, value in Settings.__dict__.iteritems():
570
if key == key.upper(): # this is a hack. all of our settings are ALL_CAPS, python internals are not
571
jsoned = json.dumps(value, sort_keys=True)
572
ret += ['-s', key + '=' + jsoned]
576
def apply_opt_level(self, opt_level, noisy=False):
579
Settings.ASSERTIONS = 0
580
Settings.DISABLE_EXCEPTION_CATCHING = 1
581
Settings.EMIT_GENERATED_FUNCTIONS = 1
585
# Aside from these, -O3 also runs closure compiler and llvm lto
586
Settings.DOUBLE_MODE = 0
587
Settings.PRECISE_I64_MATH = 0
588
if noisy: print >> sys.stderr, 'Warning: Applying some potentially unsafe optimizations! (Use -O2 if this fails.)'
592
Settings.load() # load defaults
601
COMPILER_TEST_OPTS = [] # For use of the test runner
604
def get_building_env(native=False):
605
env = os.environ.copy()
608
env['CXX'] = CLANG_CPP
610
env['CFLAGS'] = '-O2 -fno-math-errno'
612
env['CC'] = EMCC if not WINDOWS else 'python %r' % EMCC
613
env['CXX'] = EMXX if not WINDOWS else 'python %r' % EMXX
614
env['AR'] = EMAR if not WINDOWS else 'python %r' % EMAR
615
env['LD'] = EMCC if not WINDOWS else 'python %r' % EMCC
616
env['LDSHARED'] = EMCC if not WINDOWS else 'python %r' % EMCC
617
env['RANLIB'] = EMRANLIB if not WINDOWS else 'python %r' % EMRANLIB
618
#env['LIBTOOL'] = EMLIBTOOL if not WINDOWS else 'python %r' % EMLIBTOOL
619
env['EMMAKEN_COMPILER'] = Building.COMPILER
620
env['EMSCRIPTEN_TOOLS'] = path_from_root('tools')
621
env['CFLAGS'] = env['EMMAKEN_CFLAGS'] = ' '.join(Building.COMPILER_TEST_OPTS)
622
env['HOST_CC'] = CLANG_CC
623
env['HOST_CXX'] = CLANG_CPP
624
env['HOST_CFLAGS'] = "-W" #if set to nothing, CFLAGS is used, which we don't want
625
env['HOST_CXXFLAGS'] = "-W" #if set to nothing, CXXFLAGS is used, which we don't want
626
env['PKG_CONFIG_LIBDIR'] = path_from_root('system', 'local', 'lib', 'pkgconfig') + os.path.pathsep + path_from_root('system', 'lib', 'pkgconfig')
627
env['PKG_CONFIG_PATH'] = os.environ.get ('EM_PKG_CONFIG_PATH') or ''
631
def handle_CMake_toolchain(args, env):
632
CMakeToolchain = ('''# the name of the target operating system
633
SET(CMAKE_SYSTEM_NAME Linux)
635
# which C and C++ compiler to use
636
SET(CMAKE_C_COMPILER %(winfix)s$EMSCRIPTEN_ROOT/emcc)
637
SET(CMAKE_CXX_COMPILER %(winfix)s$EMSCRIPTEN_ROOT/em++)
638
SET(CMAKE_AR %(winfix)s$EMSCRIPTEN_ROOT/emar)
639
SET(CMAKE_RANLIB %(winfix)s$EMSCRIPTEN_ROOT/emranlib)
640
SET(CMAKE_C_FLAGS $CFLAGS)
641
SET(CMAKE_CXX_FLAGS $CXXFLAGS)
643
# here is the target environment located
644
SET(CMAKE_FIND_ROOT_PATH $EMSCRIPTEN_ROOT/system/include )
646
# adjust the default behaviour of the FIND_XXX() commands:
647
# search headers and libraries in the target environment, search
648
# programs in the host environment
649
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
650
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
651
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
652
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS else 'python ' }) \
653
.replace('$EMSCRIPTEN_ROOT', path_from_root('').replace('\\', '/')) \
654
.replace('$CFLAGS', env['CFLAGS']) \
655
.replace('$CXXFLAGS', env['CFLAGS'])
656
toolchainFile = mkstemp(suffix='.cmaketoolchain.txt', dir=configuration.TEMP_DIR)[1]
657
open(toolchainFile, 'w').write(CMakeToolchain)
658
args.append('-DCMAKE_TOOLCHAIN_FILE=%s' % os.path.abspath(toolchainFile))
662
def configure(args, stdout=None, stderr=None, env=None):
666
env = Building.get_building_env()
667
env['EMMAKEN_JUST_CONFIGURE'] = '1'
668
if 'cmake' in args[0]:
669
args = Building.handle_CMake_toolchain(args, env)
671
process = Popen(args, stdout=stdout, stderr=stderr, env=env)
672
process.communicate()
674
print >> sys.stderr, 'Error: Exception thrown when invoking Popen in configure with args: "%s"!' % ' '.join(args)
676
del env['EMMAKEN_JUST_CONFIGURE']
677
if process.returncode is not 0:
678
raise subprocess.CalledProcessError(cmd=args, returncode=process.returncode)
681
def make(args, stdout=None, stderr=None, env=None):
683
env = Building.get_building_env()
685
print >> sys.stderr, 'Error: Executable to run not specified.'
687
#args += ['VERBOSE=1']
689
process = Popen(args, stdout=stdout, stderr=stderr, env=env)
690
process.communicate()
692
print >> sys.stderr, 'Error: Exception thrown when invoking Popen in make with args: "%s"!' % ' '.join(args)
694
if process.returncode is not 0:
695
raise subprocess.CalledProcessError(cmd=args, returncode=process.returncode)
699
def build_library(name, build_dir, output_dir, generated_libs, configure=['sh', './configure'], configure_args=[], make=['make'], make_args=['-j', '2'], cache=None, cache_name=None, copy_project=False, env_init={}, source_dir=None, native=False):
700
''' Build a library into a .bc file. We build the .bc file once and cache it for all our tests. (We cache in
701
memory since the test directory is destroyed and recreated for each test. Note that we cache separately
702
for different compilers).
703
This cache is just during the test runner. There is a different concept of caching as well, see |Cache|. '''
705
if type(generated_libs) is not list: generated_libs = [generated_libs]
706
if source_dir is None: source_dir = path_from_root('tests', name.replace('_native', ''))
710
project_dir = os.path.join(temp_dir, name)
711
if os.path.exists(project_dir):
712
shutil.rmtree(project_dir)
713
shutil.copytree(source_dir, project_dir) # Useful in debugging sometimes to comment this out, and two lines above
715
project_dir = build_dir
717
old_dir = os.getcwd()
720
os.chdir(project_dir)
721
generated_libs = map(lambda lib: os.path.join(project_dir, lib), generated_libs)
722
#for lib in generated_libs:
724
# os.unlink(lib) # make sure compilation completed successfully
727
env = Building.get_building_env(native)
728
for k, v in env_init.iteritems():
730
if configure: # Useful in debugging sometimes to comment this out (and the lines below up to and including the |link| call)
732
Building.configure(configure + configure_args, stdout=open(os.path.join(project_dir, 'configure_'), 'w'),
733
stderr=open(os.path.join(project_dir, 'configure_err'), 'w'), env=env)
734
except subprocess.CalledProcessError, e:
735
pass # Ignore exit code != 0
736
def open_make_out(i, mode='r'):
737
return open(os.path.join(project_dir, 'make_' + str(i)), mode)
739
def open_make_err(i, mode='r'):
740
return open(os.path.join(project_dir, 'make_err' + str(i)), mode)
742
for i in range(2): # FIXME: Sad workaround for some build systems that need to be run twice to succeed (e.g. poppler)
743
with open_make_out(i, 'w') as make_out:
744
with open_make_err(i, 'w') as make_err:
746
Building.make(make + make_args, stdout=make_out,
747
stderr=make_err, env=env)
748
except subprocess.CalledProcessError, e:
749
pass # Ignore exit code != 0
751
if cache is not None:
752
cache[cache_name] = []
753
for f in generated_libs:
754
basename = os.path.basename(f)
755
cache[cache_name].append((basename, open(f, 'rb').read()))
759
# Due to the ugly hack above our best guess is to output the first run
760
with open_make_err(0) as ferr:
762
sys.stderr.write(line)
763
raise Exception('could not build library ' + name + ' due to exception ' + str(e))
766
return generated_libs
769
def link(files, target):
771
unresolved_symbols = set(['main']) # tracking unresolveds is necessary for .a linking, see below. (and main is always a necessary symbol)
772
resolved_symbols = set()
774
files = map(os.path.abspath, files)
777
has_ar = has_ar or Building.is_ar(f)
779
if not Building.is_ar(f):
780
if Building.is_bitcode(f):
782
new_symbols = Building.llvm_nm(f)
783
resolved_symbols = resolved_symbols.union(new_symbols.defs)
784
unresolved_symbols = unresolved_symbols.union(new_symbols.undefs.difference(resolved_symbols)).difference(new_symbols.defs)
785
actual_files.append(f)
787
# Extract object files from ar archives, and link according to gnu ld semantics
788
# (link in an entire .o from the archive if it supplies symbols still unresolved)
791
temp_dir = os.path.join(EMSCRIPTEN_TEMP_DIR, 'ar_output_' + str(os.getpid()) + '_' + str(len(temp_dirs)))
792
temp_dirs.append(temp_dir)
793
if not os.path.exists(temp_dir):
794
os.makedirs(temp_dir)
796
contents = filter(lambda x: len(x) > 0, Popen([LLVM_AR, 't', f], stdout=PIPE).communicate()[0].split('\n'))
797
#print >> sys.stderr, ' considering archive', f, ':', contents
798
if len(contents) == 0:
799
print >> sys.stderr, 'Warning: Archive %s appears to be empty (recommendation: link an .so instead of .a)' % f
801
for content in contents: # ar will silently fail if the directory for the file does not exist, so make all the necessary directories
802
dirname = os.path.dirname(content)
803
if dirname and not os.path.exists(dirname):
805
Popen([LLVM_AR, 'x', f], stdout=PIPE).communicate() # if absolute paths, files will appear there. otherwise, in this directory
806
contents = map(lambda content: os.path.join(temp_dir, content), contents)
807
contents = filter(os.path.exists, map(os.path.abspath, contents))
808
added_contents = set()
810
#print >> sys.stderr, ' initial undef are now ', unresolved_symbols, '\n'
811
while added: # recursively traverse until we have everything we need
812
#print >> sys.stderr, ' running loop of archive including for', f
814
for content in contents:
815
if content in added_contents: continue
816
new_symbols = Building.llvm_nm(content)
817
# Link in the .o if it provides symbols, *or* this is a singleton archive (which is apparently an exception in gcc ld)
818
#print >> sys.stderr, 'need', content, '?', unresolved_symbols, 'and we can supply', new_symbols.defs
819
#print >> sys.stderr, content, 'DEF', new_symbols.defs, '\n'
820
if new_symbols.defs.intersection(unresolved_symbols) or len(files) == 1:
821
if Building.is_bitcode(content):
822
#print >> sys.stderr, ' adding object', content, '\n'
823
resolved_symbols = resolved_symbols.union(new_symbols.defs)
824
unresolved_symbols = unresolved_symbols.union(new_symbols.undefs.difference(resolved_symbols)).difference(new_symbols.defs)
825
#print >> sys.stderr, ' undef are now ', unresolved_symbols, '\n'
826
actual_files.append(content)
827
added_contents.add(content)
829
#print >> sys.stderr, ' done running loop of archive including for', f
835
actual_files = unique_ordered(actual_files) # tolerate people trying to link a.so a.so etc.
836
if DEBUG: print >>sys.stderr, 'emcc: llvm-linking:', actual_files
838
# check for too-long command line
839
link_cmd = [LLVM_LINK] + actual_files + ['-o', target]
840
# 8k is a bit of an arbitrary limit, but a reasonable one
841
# for max command line size before we use a respose file
843
if WINDOWS and len(' '.join(link_cmd)) > 8192:
844
if DEBUG: print >>sys.stderr, 'using response file for llvm-link'
845
[response_fd, response_file] = mkstemp(suffix='.response', dir=TEMP_DIR)
847
link_cmd = [LLVM_LINK, "@" + response_file]
849
response_fh = os.fdopen(response_fd, 'w')
850
for arg in actual_files:
851
# we can't put things with spaces in the response file
855
response_fh.write(arg + "\n")
857
link_cmd.append("-o")
858
link_cmd.append(target)
860
if len(' '.join(link_cmd)) > 8192:
861
print >>sys.stderr, 'emcc: warning: link command line is very long, even with response file -- use paths with no spaces'
863
output = Popen(link_cmd, stdout=PIPE).communicate()[0]
866
os.unlink(response_file)
868
assert os.path.exists(target) and (output is None or 'Could not open input file' not in output), 'Linking error: ' + output
869
for temp_dir in temp_dirs:
872
# Emscripten optimizations that we run on the .ll file
874
def ll_opts(filename):
875
## Remove target info. This helps LLVM opts, if we run them later
876
#cleaned = filter(lambda line: not line.startswith('target datalayout = ') and not line.startswith('target triple = '),
877
# open(filename + '.o.ll', 'r').readlines())
878
#os.unlink(filename + '.o.ll')
879
#open(filename + '.o.ll.orig', 'w').write(''.join(cleaned))
883
# @param opt Either an integer, in which case it is the optimization level (-O1, -O2, etc.), or a list of raw
884
# optimization passes passed to llvm opt
886
def llvm_opt(filename, opts):
887
if type(opts) is int:
888
opts = Building.pick_llvm_opts(opts)
889
#opts += ['-debug-pass=Arguments']
890
if DEBUG: print >> sys.stderr, 'emcc: LLVM opts:', opts
891
output = Popen([LLVM_OPT, filename] + opts + ['-o=' + filename + '.opt.bc'], stdout=PIPE).communicate()[0]
892
assert os.path.exists(filename + '.opt.bc'), 'Failed to run llvm optimizations: ' + output
893
shutil.move(filename + '.opt.bc', filename)
896
def llvm_opts(filename): # deprecated version, only for test runner. TODO: remove
897
if Building.LLVM_OPTS:
898
shutil.move(filename + '.o', filename + '.o.pre')
899
output = Popen([LLVM_OPT, filename + '.o.pre'] + Building.LLVM_OPT_OPTS + ['-o=' + filename + '.o'], stdout=PIPE).communicate()[0]
900
assert os.path.exists(filename + '.o'), 'Failed to run llvm optimizations: ' + output
903
def llvm_dis(input_filename, output_filename=None):
904
# LLVM binary ==> LLVM assembly
905
if output_filename is None:
906
# use test runner conventions
907
output_filename = input_filename + '.o.ll'
908
input_filename = input_filename + '.o'
909
try_delete(output_filename)
910
output = Popen([LLVM_DIS, input_filename, '-o=' + output_filename], stdout=PIPE).communicate()[0]
911
assert os.path.exists(output_filename), 'Could not create .ll file: ' + output
912
return output_filename
915
def llvm_as(input_filename, output_filename=None):
916
# LLVM assembly ==> LLVM binary
917
if output_filename is None:
918
# use test runner conventions
919
output_filename = input_filename + '.o'
920
input_filename = input_filename + '.o.ll'
921
try_delete(output_filename)
922
output = Popen([LLVM_AS, input_filename, '-o=' + output_filename], stdout=PIPE).communicate()[0]
923
assert os.path.exists(output_filename), 'Could not create bc file: ' + output
924
return output_filename
926
nm_cache = {} # cache results of nm - it can be slow to run
929
def llvm_nm(filename, stdout=PIPE, stderr=None):
930
if filename in Building.nm_cache:
931
#if DEBUG: print >> sys.stderr, 'loading nm results for %s from cache' % filename
932
return Building.nm_cache[filename]
934
# LLVM binary ==> list of symbols
935
output = Popen([LLVM_NM, filename], stdout=stdout, stderr=stderr).communicate()[0]
940
for line in output.split('\n'):
941
if len(line) == 0: continue
942
parts = filter(lambda seg: len(seg) > 0, line.split(' '))
943
if len(parts) == 2: # ignore lines with absolute offsets, these are not bitcode anyhow (e.g. |00000630 t d_source_name|)
944
status, symbol = parts
946
ret.undefs.append(symbol)
948
ret.defs.append(symbol)
950
ret.commons.append(symbol)
951
ret.defs = set(ret.defs)
952
ret.undefs = set(ret.undefs)
953
ret.commons = set(ret.commons)
954
Building.nm_cache[filename] = ret
958
def emcc(filename, args=[], output_filename=None, stdout=None, stderr=None, env=None):
959
if output_filename is None:
960
output_filename = filename + '.o'
961
try_delete(output_filename)
962
Popen([PYTHON, EMCC, filename] + args + ['-o', output_filename], stdout=stdout, stderr=stderr, env=env).communicate()
963
assert os.path.exists(output_filename), 'emcc could not create output file: ' + output_filename
966
def emar(action, output_filename, filenames, stdout=None, stderr=None, env=None):
967
try_delete(output_filename)
968
Popen([PYTHON, EMAR, action, output_filename] + filenames, stdout=stdout, stderr=stderr, env=env).communicate()
970
assert os.path.exists(output_filename), 'emar could not create output file: ' + output_filename
973
def emscripten(filename, append_ext=True, extra_args=[]):
974
# Allow usage of emscripten.py without warning
975
os.environ['EMSCRIPTEN_SUPPRESS_USAGE_WARNING'] = '1'
978
Settings.RELOOPER = Cache.get_path('relooper.js')
979
settings = Settings.serialize()
980
compiler_output = jsrun.timeout_run(Popen([PYTHON, EMSCRIPTEN, filename + ('.o.ll' if append_ext else ''), '-o', filename + '.o.js'] + settings + extra_args, stdout=PIPE), None, 'Compiling')
981
#print compiler_output
983
# Detect compilation crashes and errors
984
if compiler_output is not None and 'Traceback' in compiler_output and 'in test_' in compiler_output: print compiler_output; assert 0
985
assert os.path.exists(filename + '.o.js') and len(open(filename + '.o.js', 'r').read()) > 0, 'Emscripten failed to generate .js: ' + str(compiler_output)
987
return filename + '.o.js'
990
def can_build_standalone():
991
return not Settings.BUILD_AS_SHARED_LIB and not Settings.LINKABLE
994
def can_use_unsafe_opts():
995
return Settings.USE_TYPED_ARRAYS == 2
999
return Settings.INLINING_LIMIT == 0
1002
def get_safe_internalize():
1003
exports = ','.join(map(lambda exp: exp[1:], Settings.EXPORTED_FUNCTIONS))
1004
# internalize carefully, llvm 3.2 will remove even main if not told not to
1005
return ['-internalize', '-internalize-public-api-list=' + exports]
1008
def pick_llvm_opts(optimization_level):
1010
It may be safe to use nonportable optimizations (like -OX) if we remove the platform info from the .ll
1011
(which we do in do_ll_opts) - but even there we have issues (even in TA2) with instruction combining
1012
into i64s. In any case, the handpicked ones here should be safe and portable. They are also tuned for
1013
things that look useful.
1015
An easy way to see LLVM's standard list of passes is
1017
llvm-as < /dev/null | opt -std-compile-opts -disable-output -debug-pass=Arguments
1019
assert 0 <= optimization_level <= 3
1020
unsafe = Building.can_use_unsafe_opts()
1022
if optimization_level > 0:
1024
if not Building.can_inline():
1025
opts.append('-disable-inlining')
1026
if not Building.can_build_standalone():
1027
# -O1 does not have -gobaldce, which removes stuff that is needed for libraries and linkables
1028
optimization_level = min(1, optimization_level)
1029
opts.append('-O%d' % optimization_level)
1030
#print '[unsafe: %s]' % ','.join(opts)
1032
allow_nonportable = False
1033
optimize_size = True
1036
# PassManagerBuilder::populateModulePassManager
1037
if allow_nonportable and use_aa: # ammo.js results indicate this can be nonportable
1038
opts.append('-tbaa')
1039
opts.append('-basicaa') # makes fannkuch slow but primes fast
1041
if Building.can_build_standalone():
1042
opts += Building.get_safe_internalize()
1044
opts.append('-globalopt')
1045
opts.append('-ipsccp')
1046
opts.append('-deadargelim')
1047
if allow_nonportable: opts.append('-instcombine')
1048
opts.append('-simplifycfg')
1050
opts.append('-prune-eh')
1051
if Building.can_inline(): opts.append('-inline')
1052
opts.append('-functionattrs')
1053
if optimization_level > 2:
1054
opts.append('-argpromotion')
1056
# XXX Danger: Can turn a memcpy into something that violates the
1057
# load-store consistency hypothesis. See hashnum() in Lua.
1058
# Note: this opt is of great importance for raytrace...
1059
if allow_nonportable: opts.append('-scalarrepl')
1061
if allow_nonportable: opts.append('-early-cse') # ?
1062
opts.append('-simplify-libcalls')
1063
opts.append('-jump-threading')
1064
if allow_nonportable: opts.append('-correlated-propagation') # ?
1065
opts.append('-simplifycfg')
1066
if allow_nonportable: opts.append('-instcombine')
1068
opts.append('-tailcallelim')
1069
opts.append('-simplifycfg')
1070
opts.append('-reassociate')
1071
opts.append('-loop-rotate')
1072
opts.append('-licm')
1073
opts.append('-loop-unswitch') # XXX should depend on optimize_size
1074
if allow_nonportable: opts.append('-instcombine')
1075
if Settings.QUANTUM_SIZE == 4: opts.append('-indvars') # XXX this infinite-loops raytrace on q1 (loop in |new node_t[count]| has 68 hardcoded ¬ fixed)
1076
if allow_nonportable: opts.append('-loop-idiom') # ?
1077
opts.append('-loop-deletion')
1078
opts.append('-loop-unroll')
1080
##### not in llvm-3.0. but have | #addExtensionsToPM(EP_LoopOptimizerEnd, MPM);| if allow_nonportable: opts.append('-instcombine')
1082
# XXX Danger: Messes up Lua output for unknown reasons
1083
# Note: this opt is of minor importance for raytrace...
1084
if optimization_level > 1 and allow_nonportable: opts.append('-gvn')
1086
opts.append('-memcpyopt') # Danger?
1087
opts.append('-sccp')
1089
if allow_nonportable: opts.append('-instcombine')
1090
opts.append('-jump-threading')
1091
opts.append('-correlated-propagation')
1093
#addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
1095
opts.append('-adce')
1096
opts.append('-simplifycfg')
1097
if allow_nonportable: opts.append('-instcombine')
1099
opts.append('-strip-dead-prototypes')
1101
if Building.can_build_standalone():
1102
opts.append('-globaldce')
1104
if optimization_level > 1: opts.append('-constmerge')
1106
Building.LLVM_OPT_OPTS = opts
1110
def js_optimizer(filename, passes, jcache):
1111
return js_optimizer.run(filename, passes, listify(NODE_JS), jcache)
1114
def closure_compiler(filename):
1115
if not os.path.exists(CLOSURE_COMPILER):
1116
raise Exception('Closure compiler appears to be missing, looked at: ' + str(CLOSURE_COMPILER))
1118
# Something like this (adjust memory as needed):
1119
# java -Xmx1024m -jar CLOSURE_COMPILER --compilation_level ADVANCED_OPTIMIZATIONS --variable_map_output_file src.cpp.o.js.vars --js src.cpp.o.js --js_output_file src.cpp.o.cc.js
1121
'-Xmx' + (os.environ.get('JAVA_HEAP_SIZE') or '1024m'), # if you need a larger Java heap, use this environment variable
1122
'-jar', CLOSURE_COMPILER,
1123
'--compilation_level', 'ADVANCED_OPTIMIZATIONS',
1124
'--formatting', 'PRETTY_PRINT',
1125
'--language_in', 'ECMASCRIPT5',
1126
#'--variable_map_output_file', filename + '.vars',
1127
'--js', filename, '--js_output_file', filename + '.cc.js']
1128
if os.environ.get('EMCC_CLOSURE_ARGS'):
1129
args += shlex.split(os.environ.get('EMCC_CLOSURE_ARGS'))
1130
process = Popen(args, stdout=PIPE, stderr=STDOUT)
1131
cc_output = process.communicate()[0]
1132
if process.returncode != 0 or not os.path.exists(filename + '.cc.js'):
1133
raise Exception('closure compiler error: ' + cc_output + ' (rc: %d)' % process.returncode)
1135
return filename + '.cc.js'
1139
def is_ar(filename):
1141
if Building._is_ar_cache.get(filename):
1142
return Building._is_ar_cache[filename]
1143
b = open(filename, 'r').read(8)
1144
sigcheck = b[0] == '!' and b[1] == '<' and \
1145
b[2] == 'a' and b[3] == 'r' and \
1146
b[4] == 'c' and b[5] == 'h' and \
1147
b[6] == '>' and ord(b[7]) == 10
1148
Building._is_ar_cache[filename] = sigcheck
1150
except Exception, e:
1151
if DEBUG: print >> sys.stderr, 'shared.Building.is_ar failed to test whether file \'%s\' is a llvm archive file! Failed on exception: %s' % (filename, e)
1155
def is_bitcode(filename):
1156
# look for magic signature
1157
b = open(filename, 'r').read(4)
1158
if b[0] == 'B' and b[1] == 'C':
1160
# look for ar signature
1161
elif Building.is_ar(filename):
1163
# on OS X, there is a 20-byte prefix
1164
elif ord(b[0]) == 222 and ord(b[1]) == 192 and ord(b[2]) == 23 and ord(b[3]) == 11:
1165
b = open(filename, 'r').read(24)
1166
return b[20] == 'B' and b[21] == 'C'
1170
# Make sure the relooper exists. If it does not, check out the relooper code and bootstrap it
1172
def ensure_relooper(relooper):
1173
if os.path.exists(relooper): return
1178
print >> sys.stderr, '======================================='
1179
print >> sys.stderr, 'bootstrapping relooper...'
1180
os.chdir(path_from_root('src'))
1182
emcc_debug = os.environ.get('EMCC_DEBUG')
1183
if emcc_debug: del os.environ['EMCC_DEBUG']
1185
def make(opt_level):
1186
raw = relooper + '.raw.js'
1187
Building.emcc(os.path.join('relooper', 'Relooper.cpp'), ['-I' + os.path.join('relooper'), '--post-js',
1188
os.path.join('relooper', 'emscripten', 'glue.js'),
1189
'--memory-init-file', '0',
1190
'-s', 'TOTAL_MEMORY=67108864',
1191
'-s', 'EXPORTED_FUNCTIONS=["_rl_set_output_buffer","_rl_make_output_buffer","_rl_new_block","_rl_delete_block","_rl_block_add_branch_to","_rl_new_relooper","_rl_delete_relooper","_rl_relooper_add_block","_rl_relooper_calculate","_rl_relooper_render", "_rl_set_asm_js_mode"]',
1192
'-s', 'DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=["memcpy", "memset", "malloc", "free", "puts"]',
1193
'-s', 'RELOOPER="' + relooper + '"',
1194
'-O' + str(opt_level), '--closure', '0'], raw)
1195
f = open(relooper, 'w')
1196
f.write("// Relooper, (C) 2012 Alon Zakai, MIT license, https://github.com/kripken/Relooper\n")
1197
f.write("var Relooper = (function() {\n");
1198
f.write(open(raw).read())
1199
f.write('\n return Module.Relooper;\n')
1203
# bootstrap phase 1: generate unrelooped relooper, for which we do not need a relooper (so we cannot recurse infinitely in this function)
1204
print >> sys.stderr, ' bootstrap phase 1'
1206
# bootstrap phase 2: generate relooped relooper, using the unrelooped relooper (we see relooper.js exists so we cannot recurse infinitely in this function)
1207
print >> sys.stderr, ' bootstrap phase 2'
1209
print >> sys.stderr, 'bootstrapping relooper succeeded'
1210
print >> sys.stderr, '======================================='
1214
if emcc_debug: os.environ['EMCC_DEBUG'] = emcc_debug
1216
print >> sys.stderr, 'bootstrapping relooper failed. You may need to manually create relooper.js by compiling it, see src/relooper/emscripten'
1220
def preprocess(infile, outfile):
1222
Preprocess source C/C++ in some special ways that emscripten needs. Returns
1223
a filename (potentially the same one if nothing was changed).
1225
Currently this only does emscripten_jcache_printf(..) rewriting.
1227
src = open(infile).read() # stack warning on jcacheprintf! in docs # add jcache printf test separatrely, for content of printf
1228
if 'emscripten_jcache_printf' not in src: return infile
1230
text = m.groups(0)[0]
1231
assert text.count('(') == 1 and text.count(')') == 1, 'must have simple expressions in emscripten_jcache_printf calls, no parens'
1232
assert text.count('"') == 2, 'must have simple expressions in emscripten_jcache_printf calls, no strings as varargs parameters'
1233
start = text.index('(')
1234
end = text.rindex(')')
1235
args = text[start+1:end].split(',')
1236
args = map(lambda x: x.strip(), args)
1237
if args[0][0] == '"':
1239
args = map(lambda x: str(ord(x)), args[0][1:len(args[0])-1]) + ['0'] + args[1:]
1240
return 'emscripten_jcache_printf_(' + ','.join(args) + ')'
1241
src = re.sub(r'(emscripten_jcache_printf\([^)]+\))', lambda m: fix(m), src)
1242
open(outfile, 'w').write(src)
1245
# compatibility with existing emcc, etc. scripts
1246
Cache = cache.Cache(debug=DEBUG_CACHE)
1247
JCache = cache.JCache(Cache)
1248
chunkify = cache.chunkify
1252
def to_nice_ident(ident): # limited version of the JS function toNiceIdent
1253
return ident.replace('%', '$').replace('@', '_');
1255
# Compression of code and data for smaller downloads
1260
def compressed_name(filename):
1261
return filename + '.compress'
1264
def compress(filename):
1265
execute(Compression.encoder, stdin=open(filename, 'rb'), stdout=open(Compression.compressed_name(filename), 'wb'))
1268
def worth_it(original, compressed):
1269
return compressed < original - 1500 # save at least one TCP packet or so
1271
def execute(cmd, *args, **kw):
1273
return Popen(cmd, *args, **kw).communicate() # let compiler frontend print directly, so colors are saved (PIPE kills that)
1275
if not isinstance(cmd, str):
1277
print >> sys.stderr, 'Invoking Process failed: <<< ' + cmd + ' >>>'
1281
parts = name.split('.')
1287
def unsuffixed(name):
1288
return '.'.join(name.split('.')[:-1])
1290
def unsuffixed_basename(name):
1291
return os.path.basename(unsuffixed(name))