PageRenderTime 34ms CodeModel.GetById 25ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/H264Dec/make.py

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