Skip to content

Commit 1c08fd0

Browse files
author
Chad Austin
committed
Compile the relooper into the emscripten cache directory
1 parent 98f6c4d commit 1c08fd0

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

emscripten.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def fix(m):
497497
outfile.close()
498498

499499

500-
def main(args, compiler_engine=None, jcache=None):
500+
def main(args, compiler_engine, jcache, relooper):
501501
# Prepare settings for serialization to JSON.
502502
settings = {}
503503
for setting in args.settings:
@@ -571,7 +571,11 @@ def lookup(value):
571571
libraries = args.libraries[0].split(',') if len(args.libraries) > 0 else []
572572

573573
# Compile the assembly to Javascript.
574-
if settings.get('RELOOP'): shared.Building.ensure_relooper()
574+
if settings.get('RELOOP'):
575+
if not relooper:
576+
relooper = shared.Cache.get_path('relooper.js')
577+
settings.setdefault('RELOOPER', relooper)
578+
shared.Building.ensure_relooper(relooper)
575579

576580
emscript(configuration, args.infile, settings, args.outfile, libraries,
577581
compiler_engine=compiler_engine,
@@ -597,6 +601,9 @@ def _main(environ):
597601
parser.add_option('-c', '--compiler',
598602
default=shared.COMPILER_ENGINE,
599603
help='Which JS engine to use to run the compiler; defaults to the one in ~/.emscripten.')
604+
parser.add_option('--relooper',
605+
default=None,
606+
help='Which relooper file to use if RELOOP is enabled')
600607
parser.add_option('-s', '--setting',
601608
dest='settings',
602609
default=[],
@@ -628,14 +635,17 @@ def _main(environ):
628635
keywords.infile = os.path.abspath(positional[0])
629636
if isinstance(keywords.outfile, basestring):
630637
keywords.outfile = open(keywords.outfile, 'w')
638+
631639
if keywords.relooper:
632-
shared.RELOOPER = os.path.abspath(keywords.relooper)
633-
keywords.settings.append("RELOOPER=" + json.dumps(shared.RELOOPER))
640+
relooper = os.path.abspath(keywords.relooper)
641+
else:
642+
relooper = None # use the cache
634643

635644
temp_files.run_and_clean(lambda: main(
636645
keywords,
637646
compiler_engine=os.path.abspath(keywords.compiler),
638-
jcache=shared.JCache if keywords.jcache else None))
647+
jcache=shared.JCache if keywords.jcache else None,
648+
relooper=relooper))
639649

640650
if __name__ == '__main__':
641651
_main(environ=os.environ)

tools/shared.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ def build_llvm_tool_path(tool):
289289
BINDINGS_GENERATOR = path_from_root('tools', 'bindings_generator.py')
290290
EXEC_LLVM = path_from_root('tools', 'exec_llvm.py')
291291
FILE_PACKAGER = path_from_root('tools', 'file_packager.py')
292-
RELOOPER = path_from_root('src', 'relooper.js')
293292

294293
# Temp dir. Create a random one, unless EMCC_DEBUG is set, in which case use TEMP_DIR/emscripten_temp
295294

@@ -994,6 +993,7 @@ def emscripten(filename, append_ext=True, extra_args=[]):
994993
os.environ['EMSCRIPTEN_SUPPRESS_USAGE_WARNING'] = '1'
995994

996995
# Run Emscripten
996+
Settings.RELOOPER = Cache.get_path('relooper.js')
997997
settings = Settings.serialize()
998998
compiler_output = timeout_run(Popen([PYTHON, EMSCRIPTEN, filename + ('.o.ll' if append_ext else ''), '-o', filename + '.o.js'] + settings + extra_args, stdout=PIPE), None, 'Compiling')
999999
#print compiler_output
@@ -1187,25 +1187,26 @@ def is_bitcode(filename):
11871187

11881188
# Make sure the relooper exists. If it does not, check out the relooper code and bootstrap it
11891189
@staticmethod
1190-
def ensure_relooper():
1191-
if os.path.exists(RELOOPER): return
1190+
def ensure_relooper(relooper):
1191+
if os.path.exists(relooper): return
1192+
Cache.ensure()
11921193
curr = os.getcwd()
11931194
try:
11941195
ok = False
11951196
print >> sys.stderr, '======================================='
11961197
print >> sys.stderr, 'bootstrapping relooper...'
1197-
Cache.ensure()
11981198
os.chdir(path_from_root('src'))
11991199

12001200
def make(opt_level):
1201-
raw = RELOOPER + '.raw.js'
1201+
raw = relooper + '.raw.js'
12021202
Building.emcc(os.path.join('relooper', 'Relooper.cpp'), ['-I' + os.path.join('relooper'), '--post-js',
12031203
os.path.join('relooper', 'emscripten', 'glue.js'),
12041204
'-s', 'TOTAL_MEMORY=52428800',
12051205
'-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"]',
12061206
'-s', 'DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=["memcpy", "memset", "malloc", "free", "puts"]',
1207+
'-s', 'RELOOPER="' + relooper + '"',
12071208
'-O' + str(opt_level), '--closure', '0'], raw)
1208-
f = open(RELOOPER, 'w')
1209+
f = open(relooper, 'w')
12091210
f.write("// Relooper, (C) 2012 Alon Zakai, MIT license, https://github.com/kripken/Relooper\n")
12101211
f.write("var Relooper = (function() {\n");
12111212
f.write(open(raw).read())
@@ -1225,7 +1226,7 @@ def make(opt_level):
12251226
finally:
12261227
os.chdir(curr)
12271228
if not ok:
1228-
print >> sys.stderr, 'bootstrapping relooper failed. You may need to manually create src/relooper.js by compiling it, see src/relooper/emscripten'
1229+
print >> sys.stderr, 'bootstrapping relooper failed. You may need to manually create relooper.js by compiling it, see src/relooper/emscripten'
12291230
1/0
12301231

12311232
@staticmethod
@@ -1260,15 +1261,15 @@ class Cache:
12601261
if not dirname:
12611262
dirname = os.path.expanduser(os.path.join('~', '.emscripten_cache'))
12621263

1263-
@staticmethod
1264-
def ensure():
1265-
if not os.path.exists(Cache.dirname):
1266-
os.makedirs(Cache.dirname)
1264+
@classmethod
1265+
def ensure(self):
1266+
if not os.path.exists(self.dirname):
1267+
os.makedirs(self.dirname)
12671268

1268-
@staticmethod
1269-
def erase():
1269+
@classmethod
1270+
def erase(self):
12701271
try:
1271-
shutil.rmtree(Cache.dirname)
1272+
shutil.rmtree(self.dirname)
12721273
except:
12731274
pass
12741275
try_delete(RELOOPER)
@@ -1277,12 +1278,16 @@ def erase():
12771278
except:
12781279
print >> sys.stderr, 'failed to save last clear time'
12791280

1281+
@classmethod
1282+
def get_path(self, shortname):
1283+
return os.path.join(self.dirname, shortname)
1284+
12801285
# Request a cached file. If it isn't in the cache, it will be created with
12811286
# the given creator function
1282-
@staticmethod
1283-
def get(shortname, creator):
1284-
if not shortname.endswith('.bc'): shortname += '.bc'
1285-
cachename = os.path.join(Cache.dirname, shortname)
1287+
@classmethod
1288+
def get(self, shortname, creator, extension='.bc'):
1289+
if not shortname.endswith(extension): shortname += extension
1290+
cachename = os.path.join(self.dirname, shortname)
12861291
if os.path.exists(cachename):
12871292
return cachename
12881293
Cache.ensure()

0 commit comments

Comments
 (0)