--js-transform option for emcc

This commit is contained in:
Alon Zakai 2012-01-04 14:36:02 -08:00
Родитель c684311cd2
Коммит c3c090dc47
2 изменённых файлов: 52 добавлений и 14 удалений

27
emcc
Просмотреть файл

@ -138,6 +138,20 @@ Options that are modified or new in %s include:
generated code!
--closure <on> 0: No closure compiler (default in -O0, -O1)
1: Run closure compiler (default in -O2, -O3)
--js-transform <cmd> <cmd> will be called on the generated code
before it is optimized. This lets you modify
the JavaScript, for example adding some code
or removing some code, in a way that those
modifications will be optimized together with
the generated code properly. <cmd> will be
called with the filename of the generated
code as a parameter; to modify the code, you
can read the original data and then append to
it or overwrite it with the modified data.
<cmd> is interpreted as a space-separated
list of arguments, for example, <cmd> of
"python processor.py" will cause a python
script to be run.
The target file, if specified (-o <target>), defines what will
be generated:
@ -254,6 +268,7 @@ try:
opt_level = 0
llvm_opt_level = None
closure = None
js_transform = None
def check_bad_eq(arg):
assert '=' not in arg, 'Invalid parameter (do not use "=" with "--" options)'
@ -277,6 +292,11 @@ try:
closure = int(newargs[i+1])
newargs[i] = ''
newargs[i+1] = ''
elif newargs[i].startswith('--js-transform'):
check_bad_eq(newargs[i])
js_transform = newargs[i+1]
newargs[i] = ''
newargs[i+1] = ''
elif newargs[i] == '-MF': # clang cannot handle this, so we fake it
f = open(newargs[i+1], 'w')
f.write('\n')
@ -478,12 +498,11 @@ try:
if DEBUG: save_intermediate('original')
# Apply a source code transformation, if requested
source_transform = os.environ.get('EMCC_JS_PROCESSOR')
if source_transform:
exec source_transform in locals()
if js_transform:
shutil.copyfile(final, final + '.tr.js')
final += '.tr.js'
process(final)
if DEBUG: print >> sys.stderr, 'emcc: applying transform: %s' % js_transform
Popen(js_transform.split(' ') + [os.path.abspath(final)]).communicate()
if DEBUG: save_intermediate('transformed')
if opt_level >= 1:

Просмотреть файл

@ -105,14 +105,20 @@ class RunnerCore(unittest.TestCase):
shutil.copyfile(filename + '.o.js', filename + '.o.js.prepost.js')
process(filename + '.o.js')
else:
if post_build is not None:
os.environ['EMCC_JS_PROCESSOR'] = post_build
else:
try:
del os.environ['EMCC_JS_PROCESSOR']
except:
pass
Building.emcc(filename + '.o.ll', Settings.serialize() + self.emcc_args, filename + '.o.js')
transform_args = []
if post_build:
transform_filename = os.path.join(self.get_dir(), 'transform.py')
transform = open(transform_filename, 'w')
transform.write('''
import sys
''')
transform.write(post_build)
transform.write('''
process(sys.argv[1])
''')
transform.close()
transform_args = ['--js-transform', "python %s" % transform_filename]
Building.emcc(filename + '.o.ll', Settings.serialize() + self.emcc_args + transform_args, filename + '.o.js')
# Build JavaScript code from source code
def build(self, src, dirname, filename, output_processor=None, main_file=None, additional_files=[], libraries=[], includes=[], build_ll_hook=None, extra_emscripten_args=[], post_build=None):
@ -5275,8 +5281,21 @@ Options that are modified or new in %s include:
assert os.path.exists('combined.bc'), '\n'.join(output)
self.assertContained('side got: hello from main, over', self.run_llvm_interpreter(['combined.bc']))
# TODO: Add an argument for EMCC_JS_PROCESSOR to make it simpler to use, other simplifications there (allow non-py, just run it if not .py)
# Add in files test a clear example of using disablePermissions, and link to it from the wiki
# --js-transform <transform>
clear()
trans = os.path.join(self.get_dir(), 't.py')
trans_file = open(trans, 'w')
trans_file.write('''
import sys
f = open(sys.argv[1], 'w')
f.write('transformed!')
f.close()
''')
trans_file.close()
output = Popen([compiler, path_from_root('tests', 'hello_world' + suffix), '--js-transform', 'python t.py'], stdout=PIPE, stderr=PIPE).communicate()
assert open('a.out.js').read() == 'transformed!', 'Transformed output must be as expected'
# TODO: Add in files test a clear example of using disablePermissions, and link to it from the wiki
# TODO: test normal project linking, static and dynamic: get_library should not need to be told what to link!
# TODO: deprecate llvm optimizations, dlmalloc, etc. in emscripten.py.