~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tools/shared.py

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
6
 
 
7
def listify(x):
 
8
  if type(x) is not list: return [x]
 
9
  return x
 
10
 
 
11
# Temp file utilities
 
12
from tempfiles import try_delete
 
13
 
 
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
 
17
class WindowsPopen:
 
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):
 
20
    self.stdin = stdin
 
21
    self.stdout = stdout
 
22
    self.stderr = stderr
 
23
    
 
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.
 
26
    self.stdin_ = stdin
 
27
    self.stdout_ = stdout
 
28
    self.stderr_ = stderr
 
29
    
 
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:
 
33
        self.stdin_ = PIPE
 
34
      if self.stdout_ == None:
 
35
        self.stdout_ = PIPE
 
36
      if self.stderr_ == None:
 
37
        self.stderr_ = PIPE
 
38
 
 
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]
 
44
      
 
45
    try:
 
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)
 
48
    except Exception, e:
 
49
      print >> sys.stderr, '\nsubprocess.Popen(args=%s) failed! Exception %s\n' % (' '.join(args), str(e))
 
50
      raise e
 
51
 
 
52
  def communicate(self, input=None):
 
53
    output = self.process.communicate(input)
 
54
    self.returncode = self.process.returncode
 
55
 
 
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]
 
61
 
 
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:
 
65
      return (None, 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])
 
71
 
 
72
  def poll(self):
 
73
    return self.process.poll()
 
74
 
 
75
  def kill(self):
 
76
    return self.process.kill()
 
77
  
 
78
  def __del__(self):
 
79
    try:
 
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)
 
82
    except:
 
83
      pass # Mute all exceptions in dtor, particularly if we didn't use a response file, self.response_filename doesn't exist.
 
84
 
 
85
# Install our replacement Popen handler if we are running on Windows to avoid python spawn process function.
 
86
if os.name == 'nt':
 
87
  Popen = WindowsPopen
 
88
 
 
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)
 
92
 
 
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.
 
98
 
 
99
EM_CONFIG = os.environ.get('EM_CONFIG')
 
100
if not EM_CONFIG:
 
101
  EM_CONFIG = '~/.emscripten'
 
102
if '\n' in EM_CONFIG:
 
103
  CONFIG_FILE = None
 
104
else:
 
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'
 
113
    try:
 
114
      llvm_root = os.path.dirname(Popen(['which', 'llvm-dis'], stdout=PIPE).communicate()[0].replace('\n', ''))
 
115
    except:
 
116
      pass
 
117
    config_file = config_file.replace('{{{ LLVM_ROOT }}}', llvm_root)
 
118
    node = 'node'
 
119
    try:
 
120
      node = Popen(['which', 'node'], stdout=PIPE).communicate()[0].replace('\n', '') or \
 
121
             Popen(['which', 'nodejs'], stdout=PIPE).communicate()[0].replace('\n', '') or node
 
122
    except:
 
123
      pass
 
124
    config_file = config_file.replace('{{{ NODE }}}', node)
 
125
    python = 'python'
 
126
    try:
 
127
      python = Popen(['which', 'python2'], stdout=PIPE).communicate()[0].replace('\n', '') or \
 
128
               Popen(['which', 'python'], stdout=PIPE).communicate()[0].replace('\n', '') or python
 
129
    except:
 
130
      pass
 
131
    config_file = config_file.replace('{{{ PYTHON }}}', python)    
 
132
 
 
133
    # write
 
134
    open(CONFIG_FILE, 'w').write(config_file)
 
135
    print >> sys.stderr, '''
 
136
==============================================================================
 
137
Welcome to Emscripten!
 
138
 
 
139
This is the first time any of the Emscripten tools has been run.
 
140
 
 
141
A settings file has been copied to %s, at absolute path: %s
 
142
 
 
143
It contains our best guesses for the important paths, which are:
 
144
 
 
145
  LLVM_ROOT       = %s
 
146
  PYTHON          = %s
 
147
  NODE_JS         = %s
 
148
  EMSCRIPTEN_ROOT = %s
 
149
 
 
150
Please edit the file if any of those are incorrect.
 
151
 
 
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__)
 
155
    sys.exit(0)
 
156
try:
 
157
  config_text = open(CONFIG_FILE, 'r').read() if CONFIG_FILE else EM_CONFIG
 
158
  exec(config_text)
 
159
except Exception, e:
 
160
  print >> sys.stderr, 'Error in evaluating %s (at %s): %s, text: %s' % (EM_CONFIG, CONFIG_FILE, str(e), config_text)
 
161
  sys.exit(1)
 
162
 
 
163
# Expectations
 
164
 
 
165
EXPECTED_LLVM_VERSION = (3,2)
 
166
 
 
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:
 
171
    return True
 
172
  print >> sys.stderr, 'warning: LLVM version appears incorrect (seeing "%s", expected "%s")' % (actual, expected)
 
173
  return False
 
174
 
 
175
def check_llvm_version():
 
176
  try:
 
177
    check_clang_version();
 
178
  except Exception, e:
 
179
    print >> sys.stderr, 'warning: Could not verify LLVM version: %s' % str(e)
 
180
 
 
181
EXPECTED_NODE_VERSION = (0,6,8)
 
182
 
 
183
def check_node_version():
 
184
  try:
 
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:
 
189
      return True
 
190
    print >> sys.stderr, 'warning: node version appears too old (seeing "%s", expected "%s")' % (actual, 'v' + ('.'.join(map(str, EXPECTED_NODE_VERSION))))
 
191
    return False
 
192
  except Exception, e:
 
193
    print >> sys.stderr, 'warning: cannot check node version:', e
 
194
    return False
 
195
 
 
196
# Check that basic stuff we need (a JS engine to compile, Node.js, and Clang and LLVM)
 
197
# exists.
 
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
 
202
 
 
203
EMSCRIPTEN_VERSION = '1.4.1'
 
204
 
 
205
def generate_sanity():
 
206
  return EMSCRIPTEN_VERSION + '|' + get_llvm_target()
 
207
 
 
208
def check_sanity(force=False):
 
209
  try:
 
210
    reason = None
 
211
    if not CONFIG_FILE:
 
212
      if not force: return # config stored directly in EM_CONFIG => skip sanity checks
 
213
    else:
 
214
      settings_mtime = os.stat(CONFIG_FILE).st_mtime
 
215
      sanity_file = CONFIG_FILE + '_sanity'
 
216
      if os.path.exists(sanity_file):
 
217
        try:
 
218
          sanity_mtime = os.stat(sanity_file).st_mtime
 
219
          if sanity_mtime <= settings_mtime:
 
220
            reason = 'settings file has changed'
 
221
          else:
 
222
            sanity_data = open(sanity_file).read()
 
223
            if sanity_data != generate_sanity():
 
224
              reason = 'system change: %s vs %s' % (generate_sanity(), sanity_data)
 
225
            else:
 
226
              if not force: return # all is well
 
227
        except Exception, e:
 
228
          reason = 'unknown: ' + str(e)
 
229
    if reason:
 
230
      print >> sys.stderr, '(Emscripten: %s, clearing cache)' % reason
 
231
      Cache.erase()
 
232
 
 
233
    # some warning, not fatal checks - do them even if EM_IGNORE_SANITY is on
 
234
    check_llvm_version()
 
235
    check_node_version()
 
236
 
 
237
    if os.environ.get('EM_IGNORE_SANITY'):
 
238
      print >> sys.stderr, 'EM_IGNORE_SANITY set, ignoring sanity checks'
 
239
      return
 
240
 
 
241
    print >> sys.stderr, '(Emscripten: Running sanity checks)'
 
242
 
 
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)
 
245
      sys.exit(1)
 
246
 
 
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)
 
250
        sys.exit(1)
 
251
 
 
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)
 
255
        sys.exit(1)
 
256
 
 
257
    try:
 
258
      subprocess.call([JAVA, '-version'], stdout=PIPE, stderr=PIPE)
 
259
    except:
 
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'
 
261
 
 
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)
 
264
 
 
265
    # Sanity check passed!
 
266
 
 
267
    if not force:
 
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())
 
271
      f.close()
 
272
 
 
273
  except Exception, e:
 
274
    # Any error here is not worth failing on
 
275
    print 'WARNING: sanity check failed to run', e
 
276
 
 
277
# Tools/paths
 
278
 
 
279
LLVM_ADD_VERSION = os.getenv('LLVM_ADD_VERSION')
 
280
CLANG_ADD_VERSION = os.getenv('CLANG_ADD_VERSION')
 
281
 
 
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):
 
285
  if LLVM_ADD_VERSION:
 
286
    return os.path.join(LLVM_ROOT, tool + "-" + LLVM_ADD_VERSION)
 
287
  else:
 
288
    return os.path.join(LLVM_ROOT, tool)
 
289
 
 
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)
 
295
  else:
 
296
    return os.path.join(LLVM_ROOT, tool)
 
297
 
 
298
CLANG_CC=os.path.expanduser(build_clang_tool_path('clang'))
 
299
CLANG_CPP=os.path.expanduser(build_clang_tool_path('clang++'))
 
300
CLANG=CLANG_CPP
 
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'))
 
309
 
 
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')
 
324
 
 
325
# Temp dir. Create a random one, unless EMCC_DEBUG is set, in which case use TEMP_DIR/emscripten_temp
 
326
 
 
327
class Configuration:
 
328
  def __init__(self, environ):
 
329
    self.DEBUG = environ.get('EMCC_DEBUG')
 
330
    if self.DEBUG == "0":
 
331
      self.DEBUG = None
 
332
    self.DEBUG_CACHE = self.DEBUG and "cache" in self.DEBUG
 
333
    self.EMSCRIPTEN_TEMP_DIR = None
 
334
 
 
335
    try:
 
336
      self.TEMP_DIR = TEMP_DIR
 
337
    except NameError:
 
338
      print >> sys.stderr, 'TEMP_DIR not defined in ~/.emscripten, using /tmp'
 
339
      self.TEMP_DIR = '/tmp'
 
340
 
 
341
    self.CANONICAL_TEMP_DIR = os.path.join(self.TEMP_DIR, 'emscripten_temp')
 
342
 
 
343
    if self.DEBUG:
 
344
      try:
 
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)
 
348
      except Exception, e:
 
349
        print >> sys.stderr, e, 'Could not create canonical temp dir. Check definition of TEMP_DIR in ~/.emscripten'
 
350
 
 
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'))
 
355
 
 
356
  def debug_log(self, msg):
 
357
    if self.DEBUG:
 
358
      print >> sys.stderr, msg
 
359
 
 
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
 
365
 
 
366
if not EMSCRIPTEN_TEMP_DIR:
 
367
  EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=configuration.TEMP_DIR)
 
368
  def clean_temp():
 
369
    try_delete(EMSCRIPTEN_TEMP_DIR)
 
370
  atexit.register(clean_temp)
 
371
 
 
372
# EM_CONFIG stuff
 
373
 
 
374
try:
 
375
  JS_ENGINES
 
376
except:
 
377
  try:
 
378
    JS_ENGINES = [JS_ENGINE]
 
379
  except Exception, e:
 
380
    print 'ERROR: %s does not seem to have JS_ENGINES or JS_ENGINE set up' % EM_CONFIG
 
381
    raise
 
382
 
 
383
try:
 
384
  CLOSURE_COMPILER
 
385
except:
 
386
  CLOSURE_COMPILER = path_from_root('third_party', 'closure-compiler', 'compiler.jar')
 
387
 
 
388
try:
 
389
  PYTHON
 
390
except:
 
391
  if DEBUG: print >> sys.stderr, 'PYTHON not defined in ~/.emscripten, using "python"'
 
392
  PYTHON = 'python'
 
393
 
 
394
try:
 
395
  JAVA
 
396
except:
 
397
  if DEBUG: print >> sys.stderr, 'JAVA not defined in ~/.emscripten, using "java"'
 
398
  JAVA = 'java'
 
399
 
 
400
# Additional compiler options
 
401
 
 
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()
 
406
 
 
407
try:
 
408
  COMPILER_OPTS # Can be set in EM_CONFIG, optionally
 
409
except:
 
410
  COMPILER_OPTS = []
 
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]
 
417
 
 
418
USE_EMSDK = not os.environ.get('EMMAKEN_NO_SDK')
 
419
 
 
420
if USE_EMSDK:
 
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'),
 
433
  ] + [
 
434
    '-U__APPLE__', '-U__linux__'
 
435
  ]
 
436
  COMPILER_OPTS += EMSDK_OPTS
 
437
else:
 
438
  EMSDK_OPTS = []
 
439
 
 
440
#print >> sys.stderr, 'SDK opts', ' '.join(EMSDK_OPTS)
 
441
#print >> sys.stderr, 'Compiler opts', ' '.join(COMPILER_OPTS)
 
442
 
 
443
# Engine tweaks
 
444
 
 
445
try:
 
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
 
450
except NameError:
 
451
  pass
 
452
 
 
453
WINDOWS = sys.platform.startswith('win')
 
454
 
 
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.)
 
457
ENV_PREFIX = []
 
458
if not WINDOWS:
 
459
  try:
 
460
    assert 'Python' in Popen(['env', 'python', '-V'], stdout=PIPE, stderr=STDOUT).communicate()[0]
 
461
    ENV_PREFIX = ['env']
 
462
  except:
 
463
    pass
 
464
 
 
465
# Utilities
 
466
 
 
467
def check_engine(engine):
 
468
  # TODO: we call this several times, perhaps cache the results?
 
469
  try:
 
470
    if not CONFIG_FILE:
 
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)
 
473
  except Exception, e:
 
474
    print 'Checking JS engine %s failed. Check %s. Details: %s' % (str(engine), EM_CONFIG, str(e))
 
475
    return False
 
476
 
 
477
def run_js(filename, engine=None, *args, **kw):
 
478
  if engine is None:
 
479
    engine = JS_ENGINES[0]
 
480
  return jsrun.run_js(filename, engine, *args, **kw)
 
481
 
 
482
def to_cc(cxx):
 
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')
 
485
 
 
486
def line_splitter(data):
 
487
  """Silly little tool to split JSON arrays over many lines."""
 
488
 
 
489
  out = ''
 
490
  counter = 0
 
491
 
 
492
  for i in range(len(data)):
 
493
    out += data[i]
 
494
    if data[i] == ' ' and counter > 60:
 
495
      out += '\n'
 
496
      counter = 0
 
497
    else:
 
498
      counter += 1
 
499
 
 
500
  return out
 
501
 
 
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:]
 
505
 
 
506
def read_pgo_data(filename):
 
507
  '''
 
508
    Reads the output of PGO and generates proper information for CORRECT_* == 2 's *_LINES options
 
509
  '''
 
510
  signs_lines = []
 
511
  overflows_lines = []
 
512
 
 
513
  for line in open(filename, 'r'):
 
514
    try:
 
515
      if line.rstrip() == '': continue
 
516
      if '%0 failures' in line: continue
 
517
      left, right = line.split(' : ')
 
518
      signature = left.split('|')[1]
 
519
      if 'Sign' in left:
 
520
        signs_lines.append(signature)
 
521
      elif 'Overflow' in left:
 
522
        overflows_lines.append(signature)
 
523
    except:
 
524
      pass
 
525
 
 
526
  return {
 
527
    'signs_lines': signs_lines,
 
528
    'overflows_lines': overflows_lines
 
529
  }
 
530
 
 
531
def unique_ordered(values): # return a list of unique values in an input list, without changing order (list(set(.)) would change order randomly)
 
532
  seen = set()
 
533
  def check(value):
 
534
    if value in seen: return False
 
535
    seen.add(value)
 
536
    return True
 
537
  return filter(check, values)
 
538
 
 
539
# Settings. A global singleton. Not pretty, but nicer than passing |, settings| everywhere
 
540
 
 
541
class Settings:
 
542
  @classmethod
 
543
  def reset(self):
 
544
    class Settings2:
 
545
      QUANTUM_SIZE = 4
 
546
      reset = Settings.reset
 
547
 
 
548
      # Given some emcc-type args (-O3, -s X=Y, etc.), fill Settings with the right settings
 
549
      @classmethod
 
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()
 
554
 
 
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)):
 
561
          if args[i] == '-s':
 
562
            exec 'Settings.' + args[i+1] in globals() # execute the setting
 
563
 
 
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
 
566
      @classmethod
 
567
      def serialize(self):
 
568
        ret = []
 
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]
 
573
        return ret
 
574
 
 
575
      @classmethod
 
576
      def apply_opt_level(self, opt_level, noisy=False):
 
577
        if opt_level >= 1:
 
578
          Settings.ASM_JS = 1
 
579
          Settings.ASSERTIONS = 0
 
580
          Settings.DISABLE_EXCEPTION_CATCHING = 1
 
581
          Settings.EMIT_GENERATED_FUNCTIONS = 1
 
582
        if opt_level >= 2:
 
583
          Settings.RELOOP = 1
 
584
        if opt_level >= 3:
 
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.)'
 
589
 
 
590
    global Settings
 
591
    Settings = Settings2
 
592
    Settings.load() # load defaults
 
593
 
 
594
Settings.reset()
 
595
 
 
596
# Building
 
597
 
 
598
class Building:
 
599
  COMPILER = CLANG
 
600
  LLVM_OPTS = False
 
601
  COMPILER_TEST_OPTS = [] # For use of the test runner
 
602
 
 
603
  @staticmethod
 
604
  def get_building_env(native=False):
 
605
    env = os.environ.copy()
 
606
    if native:
 
607
      env['CC'] = CLANG_CC
 
608
      env['CXX'] = CLANG_CPP
 
609
      env['LD'] = CLANG
 
610
      env['CFLAGS'] = '-O2 -fno-math-errno'
 
611
      return env
 
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 ''
 
628
    return env
 
629
 
 
630
  @staticmethod
 
631
  def handle_CMake_toolchain(args, env):
 
632
    CMakeToolchain = ('''# the name of the target operating system
 
633
SET(CMAKE_SYSTEM_NAME Linux)
 
634
 
 
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)
 
642
 
 
643
# here is the target environment located
 
644
SET(CMAKE_FIND_ROOT_PATH  $EMSCRIPTEN_ROOT/system/include )
 
645
 
 
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))
 
659
    return args
 
660
 
 
661
  @staticmethod
 
662
  def configure(args, stdout=None, stderr=None, env=None):
 
663
    if not args:
 
664
      return
 
665
    if env is 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)
 
670
    try:
 
671
      process = Popen(args, stdout=stdout, stderr=stderr, env=env)
 
672
      process.communicate()
 
673
    except Exception, e:
 
674
      print >> sys.stderr, 'Error: Exception thrown when invoking Popen in configure with args: "%s"!' % ' '.join(args)
 
675
      raise
 
676
    del env['EMMAKEN_JUST_CONFIGURE']
 
677
    if process.returncode is not 0:
 
678
      raise subprocess.CalledProcessError(cmd=args, returncode=process.returncode)
 
679
 
 
680
  @staticmethod
 
681
  def make(args, stdout=None, stderr=None, env=None):
 
682
    if env is None:
 
683
      env = Building.get_building_env()
 
684
    if not args:
 
685
      print >> sys.stderr, 'Error: Executable to run not specified.'
 
686
      sys.exit(1)
 
687
    #args += ['VERBOSE=1']
 
688
    try:
 
689
      process = Popen(args, stdout=stdout, stderr=stderr, env=env)
 
690
      process.communicate()
 
691
    except Exception, e:
 
692
      print >> sys.stderr, 'Error: Exception thrown when invoking Popen in make with args: "%s"!' % ' '.join(args)
 
693
      raise
 
694
    if process.returncode is not 0:
 
695
      raise subprocess.CalledProcessError(cmd=args, returncode=process.returncode)
 
696
 
 
697
 
 
698
  @staticmethod
 
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|. '''
 
704
 
 
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', ''))
 
707
 
 
708
    temp_dir = build_dir
 
709
    if copy_project:
 
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
 
714
    else:
 
715
      project_dir = build_dir
 
716
    try:
 
717
      old_dir = os.getcwd()
 
718
    except:
 
719
      old_dir = None
 
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:
 
723
    #  try:
 
724
    #    os.unlink(lib) # make sure compilation completed successfully
 
725
    #  except:
 
726
    #    pass
 
727
    env = Building.get_building_env(native)
 
728
    for k, v in env_init.iteritems():
 
729
      env[k] = v
 
730
    if configure: # Useful in debugging sometimes to comment this out (and the lines below up to and including the |link| call)
 
731
      try:
 
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)
 
738
    
 
739
    def open_make_err(i, mode='r'):
 
740
      return open(os.path.join(project_dir, 'make_err' + str(i)), mode)
 
741
    
 
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:
 
745
          try:
 
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
 
750
      try:
 
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()))
 
756
        break
 
757
      except Exception, e:
 
758
        if i > 0:
 
759
          # Due to the ugly hack above our best guess is to output the first run
 
760
          with open_make_err(0) as ferr:
 
761
            for line in ferr:
 
762
              sys.stderr.write(line)
 
763
          raise Exception('could not build library ' + name + ' due to exception ' + str(e))
 
764
    if old_dir:
 
765
      os.chdir(old_dir)
 
766
    return generated_libs
 
767
 
 
768
  @staticmethod
 
769
  def link(files, target):
 
770
    actual_files = []
 
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()
 
773
    temp_dirs = []
 
774
    files = map(os.path.abspath, files)
 
775
    has_ar = False
 
776
    for f in files:
 
777
      has_ar = has_ar or Building.is_ar(f)
 
778
    for f in files:
 
779
      if not Building.is_ar(f):
 
780
        if Building.is_bitcode(f):
 
781
          if has_ar:
 
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)
 
786
      else:
 
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)
 
789
        cwd = os.getcwd()
 
790
        try:
 
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)
 
795
          os.chdir(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
 
800
          else:
 
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):
 
804
                os.makedirs(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()
 
809
            added = True
 
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
 
813
              added = False
 
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)
 
828
                    added = True
 
829
            #print >> sys.stderr, '  done running loop of archive including for', f
 
830
        finally:
 
831
          os.chdir(cwd)
 
832
    try_delete(target)
 
833
 
 
834
    # Finish link
 
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
 
837
 
 
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
 
842
    response_file = None
 
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)
 
846
 
 
847
      link_cmd = [LLVM_LINK, "@" + response_file]
 
848
 
 
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
 
852
        if " " in arg:
 
853
          link_cmd.append(arg)
 
854
        else:
 
855
          response_fh.write(arg + "\n")
 
856
      response_fh.close()
 
857
      link_cmd.append("-o")
 
858
      link_cmd.append(target)
 
859
 
 
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'
 
862
 
 
863
    output = Popen(link_cmd, stdout=PIPE).communicate()[0]
 
864
 
 
865
    if response_file:
 
866
      os.unlink(response_file)
 
867
 
 
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:
 
870
      try_delete(temp_dir)
 
871
 
 
872
  # Emscripten optimizations that we run on the .ll file
 
873
  @staticmethod
 
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))
 
880
    pass
 
881
 
 
882
  # LLVM optimizations
 
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
 
885
  @staticmethod
 
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)
 
894
 
 
895
  @staticmethod
 
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
 
901
 
 
902
  @staticmethod
 
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
 
913
 
 
914
  @staticmethod
 
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
 
925
 
 
926
  nm_cache = {} # cache results of nm - it can be slow to run
 
927
 
 
928
  @staticmethod
 
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]
 
933
 
 
934
    # LLVM binary ==> list of symbols
 
935
    output = Popen([LLVM_NM, filename], stdout=stdout, stderr=stderr).communicate()[0]
 
936
    class ret:
 
937
      defs = []
 
938
      undefs = []
 
939
      commons = []
 
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
 
945
        if status == 'U':
 
946
          ret.undefs.append(symbol)
 
947
        elif status != 'C':
 
948
          ret.defs.append(symbol)
 
949
        else:
 
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
 
955
    return ret
 
956
 
 
957
  @staticmethod
 
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
 
964
 
 
965
  @staticmethod
 
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()
 
969
    if 'c' in action:
 
970
      assert os.path.exists(output_filename), 'emar could not create output file: ' + output_filename
 
971
 
 
972
  @staticmethod
 
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'
 
976
 
 
977
    # Run Emscripten
 
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
 
982
 
 
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)
 
986
 
 
987
    return filename + '.o.js'
 
988
 
 
989
  @staticmethod
 
990
  def can_build_standalone():
 
991
    return not Settings.BUILD_AS_SHARED_LIB and not Settings.LINKABLE
 
992
 
 
993
  @staticmethod
 
994
  def can_use_unsafe_opts():
 
995
    return Settings.USE_TYPED_ARRAYS == 2
 
996
 
 
997
  @staticmethod
 
998
  def can_inline():
 
999
    return Settings.INLINING_LIMIT == 0
 
1000
 
 
1001
  @staticmethod
 
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]
 
1006
 
 
1007
  @staticmethod
 
1008
  def pick_llvm_opts(optimization_level):
 
1009
    '''
 
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.
 
1014
 
 
1015
      An easy way to see LLVM's standard list of passes is
 
1016
 
 
1017
        llvm-as < /dev/null | opt -std-compile-opts -disable-output -debug-pass=Arguments
 
1018
    '''
 
1019
    assert 0 <= optimization_level <= 3
 
1020
    unsafe = Building.can_use_unsafe_opts()
 
1021
    opts = []
 
1022
    if optimization_level > 0:
 
1023
      if unsafe:
 
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)
 
1031
      else:
 
1032
        allow_nonportable = False
 
1033
        optimize_size = True
 
1034
        use_aa = False
 
1035
 
 
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
 
1040
 
 
1041
        if Building.can_build_standalone():
 
1042
          opts += Building.get_safe_internalize()
 
1043
 
 
1044
        opts.append('-globalopt')
 
1045
        opts.append('-ipsccp')
 
1046
        opts.append('-deadargelim')
 
1047
        if allow_nonportable: opts.append('-instcombine')
 
1048
        opts.append('-simplifycfg')
 
1049
 
 
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')
 
1055
 
 
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')
 
1060
 
 
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')
 
1067
 
 
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 &not fixed)
 
1076
        if allow_nonportable: opts.append('-loop-idiom') # ?
 
1077
        opts.append('-loop-deletion')
 
1078
        opts.append('-loop-unroll')
 
1079
 
 
1080
        ##### not in llvm-3.0. but have |      #addExtensionsToPM(EP_LoopOptimizerEnd, MPM);| if allow_nonportable: opts.append('-instcombine')
 
1081
 
 
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')
 
1085
 
 
1086
        opts.append('-memcpyopt') # Danger?
 
1087
        opts.append('-sccp')
 
1088
 
 
1089
        if allow_nonportable: opts.append('-instcombine')
 
1090
        opts.append('-jump-threading')
 
1091
        opts.append('-correlated-propagation')
 
1092
        opts.append('-dse')
 
1093
        #addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
 
1094
 
 
1095
        opts.append('-adce')
 
1096
        opts.append('-simplifycfg')
 
1097
        if allow_nonportable: opts.append('-instcombine')
 
1098
 
 
1099
        opts.append('-strip-dead-prototypes')
 
1100
 
 
1101
        if Building.can_build_standalone():
 
1102
          opts.append('-globaldce')
 
1103
 
 
1104
        if optimization_level > 1: opts.append('-constmerge')
 
1105
 
 
1106
    Building.LLVM_OPT_OPTS = opts
 
1107
    return opts
 
1108
 
 
1109
  @staticmethod
 
1110
  def js_optimizer(filename, passes, jcache):
 
1111
    return js_optimizer.run(filename, passes, listify(NODE_JS), jcache)
 
1112
 
 
1113
  @staticmethod
 
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))
 
1117
 
 
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
 
1120
    args = [JAVA,
 
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)
 
1134
 
 
1135
    return filename + '.cc.js'
 
1136
 
 
1137
  _is_ar_cache = {}
 
1138
  @staticmethod
 
1139
  def is_ar(filename):
 
1140
    try:
 
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
 
1149
      return 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)
 
1152
      return False
 
1153
 
 
1154
  @staticmethod
 
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':
 
1159
      return True
 
1160
    # look for ar signature
 
1161
    elif Building.is_ar(filename):
 
1162
      return True
 
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'
 
1167
 
 
1168
    return False
 
1169
 
 
1170
  # Make sure the relooper exists. If it does not, check out the relooper code and bootstrap it
 
1171
  @staticmethod
 
1172
  def ensure_relooper(relooper):
 
1173
    if os.path.exists(relooper): return
 
1174
    Cache.ensure()
 
1175
    curr = os.getcwd()
 
1176
    try:
 
1177
      ok = False
 
1178
      print >> sys.stderr, '======================================='
 
1179
      print >> sys.stderr, 'bootstrapping relooper...'
 
1180
      os.chdir(path_from_root('src'))
 
1181
 
 
1182
      emcc_debug = os.environ.get('EMCC_DEBUG')
 
1183
      if emcc_debug: del os.environ['EMCC_DEBUG']
 
1184
 
 
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')
 
1200
        f.write('})();\n')
 
1201
        f.close()
 
1202
 
 
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'
 
1205
      make(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'
 
1208
      make(2)
 
1209
      print >> sys.stderr, 'bootstrapping relooper succeeded'
 
1210
      print >> sys.stderr, '======================================='
 
1211
      ok = True
 
1212
    finally:
 
1213
      os.chdir(curr)
 
1214
      if emcc_debug: os.environ['EMCC_DEBUG'] = emcc_debug
 
1215
      if not ok:
 
1216
        print >> sys.stderr, 'bootstrapping relooper failed. You may need to manually create relooper.js by compiling it, see src/relooper/emscripten'
 
1217
        1/0
 
1218
 
 
1219
  @staticmethod
 
1220
  def preprocess(infile, outfile):
 
1221
    '''
 
1222
      Preprocess source C/C++ in some special ways that emscripten needs. Returns
 
1223
      a filename (potentially the same one if nothing was changed).
 
1224
 
 
1225
      Currently this only does emscripten_jcache_printf(..) rewriting.
 
1226
    '''
 
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
 
1229
    def fix(m):
 
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] == '"':
 
1238
        # flatten out
 
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)
 
1243
    return outfile
 
1244
 
 
1245
# compatibility with existing emcc, etc. scripts
 
1246
Cache = cache.Cache(debug=DEBUG_CACHE)
 
1247
JCache = cache.JCache(Cache)
 
1248
chunkify = cache.chunkify
 
1249
 
 
1250
class JS:
 
1251
  @staticmethod
 
1252
  def to_nice_ident(ident): # limited version of the JS function toNiceIdent
 
1253
    return ident.replace('%', '$').replace('@', '_');
 
1254
 
 
1255
# Compression of code and data for smaller downloads
 
1256
class Compression:
 
1257
  on = False
 
1258
 
 
1259
  @staticmethod
 
1260
  def compressed_name(filename):
 
1261
    return filename + '.compress'
 
1262
 
 
1263
  @staticmethod
 
1264
  def compress(filename):
 
1265
    execute(Compression.encoder, stdin=open(filename, 'rb'), stdout=open(Compression.compressed_name(filename), 'wb'))
 
1266
 
 
1267
  @staticmethod
 
1268
  def worth_it(original, compressed):
 
1269
    return compressed < original - 1500 # save at least one TCP packet or so
 
1270
 
 
1271
def execute(cmd, *args, **kw):
 
1272
  try:
 
1273
    return Popen(cmd, *args, **kw).communicate() # let compiler frontend print directly, so colors are saved (PIPE kills that)
 
1274
  except:
 
1275
    if not isinstance(cmd, str):
 
1276
      cmd = ' '.join(cmd)
 
1277
    print >> sys.stderr, 'Invoking Process failed: <<< ' + cmd + ' >>>'
 
1278
    raise
 
1279
 
 
1280
def suffix(name):
 
1281
  parts = name.split('.')
 
1282
  if len(parts) > 1:
 
1283
    return parts[-1]
 
1284
  else:
 
1285
    return None
 
1286
 
 
1287
def unsuffixed(name):
 
1288
  return '.'.join(name.split('.')[:-1])
 
1289
 
 
1290
def unsuffixed_basename(name):
 
1291
  return os.path.basename(unsuffixed(name))
 
1292
 
 
1293
import js_optimizer
 
1294