Skip to content

Commit 74955d5

Browse files
author
Chad Austin
committed
make it possible to manually specify a temp directory when running emscripten.py
1 parent f507b18 commit 74955d5

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

emscripten.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def process_funcs((i, funcs, meta, settings_file, compiler, forwarded_file, libr
4646
tempfiles.try_delete(funcs_file)
4747
return out
4848

49-
def emscript(configuration, infile, settings, outfile, libraries=[], compiler_engine=None,
50-
jcache=None, temp_files=None):
49+
def emscript(infile, settings, outfile, libraries=[], compiler_engine=None,
50+
jcache=None, temp_files=None, DEBUG=False):
5151
"""Runs the emscripten LLVM-to-JS compiler. We parallelize as much as possible
5252
5353
Args:
@@ -66,7 +66,7 @@ def emscript(configuration, infile, settings, outfile, libraries=[], compiler_en
6666
# 2 aka 'funcs': Process functions. We can parallelize this, working on each function independently.
6767
# 3 aka 'post' : Process globals, generate postamble and finishing touches.
6868

69-
configuration.debug_log('emscript: ll=>js')
69+
if DEBUG: print >> sys.stderr, 'emscript: ll=>js'
7070

7171
if jcache: jcache.ensure()
7272

@@ -491,7 +491,7 @@ def fix(m):
491491

492492
outfile.close()
493493

494-
def main(args, compiler_engine, cache, jcache, relooper, temp_files, configuration):
494+
def main(args, compiler_engine, cache, jcache, relooper, temp_files, DEBUG):
495495
# Prepare settings for serialization to JSON.
496496
settings = {}
497497
for setting in args.settings:
@@ -572,10 +572,8 @@ def lookup(value):
572572
from tools import shared
573573
shared.Building.ensure_relooper(relooper)
574574

575-
emscript(configuration, args.infile, settings, args.outfile, libraries,
576-
compiler_engine=compiler_engine,
577-
jcache=jcache,
578-
temp_files=temp_files)
575+
emscript(args.infile, settings, args.outfile, libraries, compiler_engine=compiler_engine,
576+
jcache=jcache, temp_files=temp_files, DEBUG=DEBUG)
579577

580578
def _main(environ):
581579
parser = optparse.OptionParser(
@@ -599,7 +597,7 @@ def _main(environ):
599597
help='Which JS engine to use to run the compiler; defaults to the one in ~/.emscripten.')
600598
parser.add_option('--relooper',
601599
default=None,
602-
help='Which relooper file to use if RELOOP is enabled')
600+
help='Which relooper file to use if RELOOP is enabled.')
603601
parser.add_option('-s', '--setting',
604602
dest='settings',
605603
default=[],
@@ -611,6 +609,9 @@ def _main(environ):
611609
action='store_true',
612610
default=False,
613611
help=('Enable jcache (ccache-like caching of compilation results, for faster incremental builds).'))
612+
parser.add_option('-T', '--temp-dir',
613+
default=None,
614+
help=('Where to create temporary files.'))
614615
parser.add_option('--suppressUsageWarning',
615616
action='store_true',
616617
default=environ.get('EMSCRIPTEN_SUPPRESS_USAGE_WARNING'),
@@ -639,12 +640,21 @@ def _main(environ):
639640

640641
from tools import shared
641642
configuration = shared.Configuration(environ=os.environ)
642-
temp_files = configuration.get_temp_files()
643+
644+
if keywords.temp_dir is None:
645+
temp_files = configuration.get_temp_files()
646+
else:
647+
temp_dir = os.path.abspath(keywords.temp_dir)
648+
if not os.path.exists(temp_dir):
649+
os.makedirs(temp_dir)
650+
temp_files = tempfiles.TempFiles(temp_dir)
643651

644652
if keywords.compiler is None:
645653
from tools import shared
646654
keywords.compiler = shared.COMPILER_ENGINE
647655

656+
DEBUG = configuration.DEBUG
657+
648658
cache = cache_module.Cache()
649659
temp_files.run_and_clean(lambda: main(
650660
keywords,
@@ -653,7 +663,7 @@ def _main(environ):
653663
jcache=cache_module.JCache(cache) if keywords.jcache else None,
654664
relooper=relooper,
655665
temp_files=temp_files,
656-
configuration=configuration
666+
DEBUG=DEBUG
657667
))
658668

659669
if __name__ == '__main__':

tools/shared.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import shutil, time, os, sys, json, tempfile, copy, shlex, atexit, subprocess, hashlib, cPickle, zlib, re
22
from subprocess import Popen, PIPE, STDOUT
33
from tempfile import mkstemp
4-
from . import jsrun, cache
4+
from . import jsrun, cache, tempfiles
55

66
def listify(x):
77
if type(x) is not list: return [x]
@@ -318,44 +318,14 @@ def __init__(self, environ):
318318
print >> sys.stderr, e, 'Could not create canonical temp dir. Check definition of TEMP_DIR in ~/.emscripten'
319319

320320
def get_temp_files(self):
321-
return TempFiles(
321+
return tempfiles.TempFiles(
322322
tmp=self.TEMP_DIR if not self.DEBUG else self.EMSCRIPTEN_TEMP_DIR,
323323
save_debug_files=os.environ.get('EMCC_DEBUG_SAVE'))
324324

325325
def debug_log(self, msg):
326326
if self.DEBUG:
327327
print >> sys.stderr, msg
328328

329-
class TempFiles:
330-
def __init__(self, tmp, save_debug_files=False):
331-
self.tmp = tmp
332-
self.save_debug_files = save_debug_files
333-
334-
self.to_clean = []
335-
336-
def note(self, filename):
337-
self.to_clean.append(filename)
338-
339-
def get(self, suffix):
340-
"""Returns a named temp file with the given prefix."""
341-
named_file = tempfile.NamedTemporaryFile(dir=self.tmp, suffix=suffix, delete=False)
342-
self.note(named_file.name)
343-
return named_file
344-
345-
def clean(self):
346-
if self.save_debug_files:
347-
print >> sys.stderr, 'not cleaning up temp files since in debug-save mode, see them in %s' % (self.tmp,)
348-
return
349-
for filename in self.to_clean:
350-
try_delete(filename)
351-
self.to_clean = []
352-
353-
def run_and_clean(self, func):
354-
try:
355-
return func()
356-
finally:
357-
self.clean()
358-
359329
configuration = Configuration(environ=os.environ)
360330
DEBUG = configuration.DEBUG
361331
EMSCRIPTEN_TEMP_DIR = configuration.EMSCRIPTEN_TEMP_DIR

tools/tempfiles.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
import os
22
import shutil
3+
import tempfile
34

45
def try_delete(filename):
56
try:
67
os.unlink(filename)
78
except:
89
shutil.rmtree(filename, ignore_errors=True)
10+
11+
class TempFiles:
12+
def __init__(self, tmp, save_debug_files=False):
13+
self.tmp = tmp
14+
self.save_debug_files = save_debug_files
15+
16+
self.to_clean = []
17+
18+
def note(self, filename):
19+
self.to_clean.append(filename)
20+
21+
def get(self, suffix):
22+
"""Returns a named temp file with the given prefix."""
23+
named_file = tempfile.NamedTemporaryFile(dir=self.tmp, suffix=suffix, delete=False)
24+
self.note(named_file.name)
25+
return named_file
26+
27+
def clean(self):
28+
if self.save_debug_files:
29+
print >> sys.stderr, 'not cleaning up temp files since in debug-save mode, see them in %s' % (self.tmp,)
30+
return
31+
for filename in self.to_clean:
32+
try_delete(filename)
33+
self.to_clean = []
34+
35+
def run_and_clean(self, func):
36+
try:
37+
return func()
38+
finally:
39+
self.clean()

0 commit comments

Comments
 (0)