aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xemcc22
-rwxr-xr-xtests/runner.py18
2 files changed, 40 insertions, 0 deletions
diff --git a/emcc b/emcc
index 8fb93f78..249ba398 100755
--- a/emcc
+++ b/emcc
@@ -181,6 +181,11 @@ Options that are modified or new in %s include:
will be run). Note that this by itself
will not minify the code (closure does
that)
+ --embed-file <filename> A file to embed inside the generated
+ JavaScript. The compiled code will be able
+ to access the file in the current directory
+ with the same basename as given here (that is,
+ just the filename, without a path to it).
--shell-file <path> The path name to a skeleton HTML file used
when generating HTML output. The shell file
used needs to have this token inside it:
@@ -311,6 +316,7 @@ try:
closure = None
js_transform = None
compress_whitespace = None
+ embed_files = []
shell_path = shared.path_from_root('src', 'shell.html')
def check_bad_eq(arg):
@@ -344,6 +350,11 @@ try:
compress_whitespace = int(newargs[i+1])
newargs[i] = ''
newargs[i+1] = ''
+ elif newargs[i].startswith('--embed-file'):
+ check_bad_eq(newargs[i])
+ embed_files.append(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')
@@ -662,6 +673,17 @@ try:
final = shared.Building.emscripten(final, append_ext=False)
if DEBUG: save_intermediate('original')
+ # Embed files
+ if len(embed_files) > 0:
+ if DEBUG: print >> sys.stderr, 'emcc: embedding files'
+ src = open(final).read().replace(
+ '// {{PRE_RUN_ADDITIONS}}',
+ '\n'.join(map(lambda embed_file: "FS.createDataFile('/', '%s', %s, true, false);" % (os.path.basename(embed_file), str(map(ord, open(embed_file, 'rb').read()))), embed_files))
+ )
+ final += '.ef.js'
+ open(final, 'w').write(src)
+ if DEBUG: save_intermediate('embedded_files')
+
# Apply a source code transformation, if requested
if js_transform:
shutil.copyfile(final, final + '.tr.js')
diff --git a/tests/runner.py b/tests/runner.py
index a95dfff0..7dde21d7 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -6150,6 +6150,24 @@ f.close()
Popen([EMCC, os.path.join(self.get_dir(), 'main.cpp'), '-L' + os.path.join(self.get_dir(), 'libdir'), '-lfile'], stdout=PIPE, stderr=STDOUT).communicate()
self.assertContained('hello from lib', run_js(os.path.join(self.get_dir(), 'a.out.js')))
+ def test_emcc_embed_file(self):
+ open(os.path.join(self.get_dir(), 'somefile.txt'), 'w').write('''hello from a file with lots of data and stuff in it thank you very much''')
+ open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(r'''
+ #include <stdio.h>
+ int main() {
+ FILE *f = fopen("somefile.txt", "r");
+ char buf[100];
+ fread(buf, 1, 20, f);
+ buf[20] = 0;
+ fclose(f);
+ printf("|%s|\n", buf);
+ return 0;
+ }
+ ''')
+
+ Popen([EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--embed-file', 'somefile.txt']).communicate()
+ self.assertContained('|hello from a file wi|', run_js(os.path.join(self.get_dir(), 'a.out.js')))
+
def test_eliminator(self):
input = open(path_from_root('tools', 'eliminator', 'eliminator-test.js')).read()
expected = open(path_from_root('tools', 'eliminator', 'eliminator-test-output.js')).read()