PageRenderTime 136ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Avc/make.py

http://github.com/mbebenita/Broadway
Python | 139 lines | 97 code | 25 blank | 17 comment | 6 complexity | 58364846a2c25674f53a5a5d315b07a8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #!/usr/bin/python
  2. import os, sys, re, json, shutil
  3. from subprocess import Popen, PIPE, STDOUT
  4. exec(open(os.path.expanduser('~/.emscripten'), 'r').read())
  5. sys.path.append(EMSCRIPTEN_ROOT)
  6. import tools.shared as emscripten
  7. EMSCRIPTEN_SETTINGS = {
  8. 'SKIP_STACK_IN_SMALL': 1,
  9. 'INIT_STACK': 0,
  10. 'AUTO_OPTIMIZE': 0,
  11. 'CHECK_OVERFLOWS': 0,
  12. 'CHECK_SIGNED_OVERFLOWS': 0,
  13. 'CORRECT_OVERFLOWS': 0,
  14. 'CHECK_SIGNS': 0,
  15. 'CORRECT_SIGNS': 2,
  16. 'CORRECT_SIGNS_LINES': emscripten.read_auto_optimize_data('avc.pgo')['signs_lines'],
  17. 'DISABLE_EXCEPTION_CATCHING': 1,
  18. 'RUNTIME_TYPE_INFO': 0,
  19. 'TOTAL_MEMORY': 50*1024*1024,
  20. 'FAST_MEMORY': 12*1024*1024,
  21. 'PROFILE': 0,
  22. 'OPTIMIZE': 1,
  23. 'RELOOP': 1, # XXX 1 makes compilation slower!
  24. 'USE_TYPED_ARRAYS': 2,
  25. 'USE_FHEAP': 0,
  26. 'SAFE_HEAP': 0,
  27. 'ASSERTIONS': 0,
  28. 'QUANTUM_SIZE': 4,
  29. 'INVOKE_RUN': 0, # we do it ourselves
  30. 'EXPORTED_FUNCTIONS': ['_main', '__Z11runMainLoopv'],
  31. 'IGNORED_FUNCTIONS': ['_paint'],
  32. }
  33. EMSCRIPTEN_ARGS = []#['--dlmalloc'] # Optimize does not appear to help
  34. JS_DIR = "js"
  35. if not os.path.exists(JS_DIR):
  36. os.makedirs(JS_DIR)
  37. print 'Build'
  38. env = os.environ.copy()
  39. env['CC'] = env['CXX'] = env['RANLIB'] = env['AR'] = emscripten.EMMAKEN
  40. env['LINUX'] = '1'
  41. env['EMMAKEN_CFLAGS'] = '-U__APPLE__ -DJS'
  42. Popen(['make', '-j', '4'], env=env).communicate()
  43. if 0:
  44. print 'LLVM optimizations'
  45. shutil.move('avc.bc', 'avc.orig.bc')
  46. output = Popen([emscripten.LLVM_OPT, 'avc.orig.bc'] +
  47. emscripten.pick_llvm_opts(3, handpicked=False) +
  48. ['-o=avc.bc'], stdout=PIPE, stderr=STDOUT).communicate()[0]
  49. assert os.path.exists('avc.bc'), 'Failed to run llvm optimizations: ' + output
  50. print 'LLVM binary => LL assembly'
  51. print Popen([emscripten.LLVM_DIS] + emscripten.LLVM_DIS_OPTS + ['avc.bc', '-o=avc.ll']).communicate()
  52. if 0:
  53. print '[Autodebugger]'
  54. shutil.move('avc.ll', 'avc.orig.ll')
  55. output = Popen(['python', emscripten.AUTODEBUGGER, 'avc.orig.ll', 'avc.ll'], stdout=PIPE, stderr=STDOUT).communicate()[0]
  56. assert 'Success.' in output, output
  57. shutil.move('avc.bc', 'avc.orig.bc')
  58. print Popen([emscripten.LLVM_AS, 'avc.ll', '-o=avc.bc']).communicate()
  59. print 'Emscripten: LL assembly => JavaScript'
  60. settings = ['-s %s=%s' % (k, json.dumps(v)) for k, v in EMSCRIPTEN_SETTINGS.items()]
  61. filename = JS_DIR + '/avc.js'
  62. print Popen(['python', os.path.join(EMSCRIPTEN_ROOT, 'emscripten.py')] + EMSCRIPTEN_ARGS + ['avc.ll'] + settings,# ).communicate()
  63. stdout=open(filename, 'w'), stderr=STDOUT).communicate()
  64. print 'Appending stuff'
  65. src = open(filename, 'a')
  66. if EMSCRIPTEN_SETTINGS['QUANTUM_SIZE'] == 1:
  67. src.write(
  68. '''
  69. _malloc = function(size) {
  70. while (STATICTOP % 4 != 0) STATICTOP++;
  71. var ret = STATICTOP;
  72. STATICTOP += size;
  73. return ret;
  74. }
  75. '''
  76. )
  77. if 0: # Console debugging
  78. src.write(
  79. '''
  80. _paint = _SDL_Init = _SDL_LockSurface = _SDL_UnlockSurface = function() {
  81. };
  82. _SDL_SetVideoMode = function() {
  83. return _malloc(1024);
  84. };
  85. FS.createDataFile('/', 'admiral.264', %s, true, false);
  86. FS.root.write = true;
  87. print('zz go!');
  88. run(['admiral.264']);
  89. print('zz gone');
  90. ''' % str(map(ord, open('../Media/admiral.264').read()[0:1024*100]))
  91. )
  92. # ~/Dev/mozilla-central/js/src/fast/js -m avc.js
  93. else:
  94. src.write(open('hooks.js').read())
  95. src.write(open('paint_%s.js' % EMSCRIPTEN_SETTINGS['USE_TYPED_ARRAYS'], 'r').read())
  96. src.close()
  97. print 'Eliminating unneeded variables'
  98. eliminated = Popen([emscripten.COFFEESCRIPT, emscripten.VARIABLE_ELIMINATOR], stdin=PIPE, stdout=PIPE).communicate(open(filename, 'r').read())[0]
  99. filename = JS_DIR + '/avc.elim.js'
  100. f = open(filename, 'w')
  101. f.write(eliminated)
  102. f.close()
  103. print 'Closure compiler'
  104. Popen(['java', '-jar', emscripten.CLOSURE_COMPILER,
  105. '--compilation_level', 'SIMPLE_OPTIMIZATIONS', # XXX TODO: use advanced opts for code size (they cause slow startup though)
  106. '--js', filename, '--js_output_file', JS_DIR + '/avc.elim.cc.js'], stdout=PIPE, stderr=STDOUT).communicate()