-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SINGLE_FILE option to embed all subresources into emitted JS #5296
Changes from 1 commit
5ea8765
3a562fa
d2edd4c
e9b81cc
cb300b6
2e33fb0
32bee1e
a21e1d8
e124141
01b12b5
0c82c16
4fb1605
998f937
549a9b0
f7e889e
9e5c34a
eeb547a
b43f8ea
c16ca3e
bc0074e
a21f18f
19cbb02
7765795
73d1c91
a6f46aa
06ed6dc
8c74983
0539459
ec5cc8d
962576f
584b21c
362b79e
f0cb8da
63e225a
3711653
dc1b69e
bd11a5e
2e630bd
5dece1f
bd28f19
ca3af49
638732b
5f93654
200fb16
fd28b2f
ee64597
6fa3a88
e3f7566
e0f4e6c
6b8a3e6
67e41dc
0f72d92
97ae2ed
4452cb1
3a2ccc4
fa0ed7a
84c9e57
34d7e03
ebb8d7c
32f96f2
24e4356
b422419
1104ee0
8814a69
bc33edd
d190392
86c319b
62d9306
d55bc45
a4a864d
adbb60d
2bce73f
7489546
7e168bb
6fae984
eb1fd57
8e4be3e
f33363b
50e58bd
60fc52b
eae3ed5
e8fd9e9
8685abf
f98d511
42992ef
8e45599
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
if __name__ == '__main__': | ||
ToolchainProfiler.record_process_start() | ||
|
||
import os, sys, shutil, tempfile, subprocess, shlex, time, re, logging, urllib | ||
import os, sys, shutil, tempfile, subprocess, shlex, time, re, logging, urllib, base64 | ||
from subprocess import PIPE | ||
from tools import shared, jsrun, system_libs | ||
from tools.shared import execute, suffix, unsuffixed, unsuffixed_basename, WINDOWS, safe_move | ||
|
@@ -513,6 +513,16 @@ def filter_emscripten_options(argv): | |
|
||
# ---------------- Utilities --------------- | ||
|
||
# Returns the subresource location for run-time access | ||
def get_subresource_location(path): | ||
if shared.Settings.SINGLE_FILE: | ||
f = open(path, 'rb') | ||
data = base64.b64encode(f.read()) | ||
f.close() | ||
return 'data:application/octet-stream;base64,' + data | ||
else: | ||
return os.path.basename(path) | ||
|
||
seen_names = {} | ||
def uniquename(name): | ||
if name not in seen_names: | ||
|
@@ -799,7 +809,7 @@ def detect_fixed_language_mode(args): | |
options.separate_asm = True | ||
logging.warning('forcing separate asm output (--separate-asm), because -s PRECISE_F32=2 or -s USE_PTHREADS=2 was passed.') | ||
if options.separate_asm: | ||
shared.Settings.SEPARATE_ASM = os.path.basename(asm_target) | ||
shared.Settings.SEPARATE_ASM = get_subresource_location(asm_target) | ||
|
||
if 'EMCC_STRICT' in os.environ: | ||
shared.Settings.STRICT = os.environ.get('EMCC_STRICT') != '0' | ||
|
@@ -1116,9 +1126,9 @@ def check(input_file): | |
|
||
if shared.Settings.BINARYEN: | ||
# set file locations, so that JS glue can find what it needs | ||
shared.Settings.WASM_TEXT_FILE = os.path.basename(wasm_text_target) | ||
shared.Settings.WASM_BINARY_FILE = os.path.basename(wasm_binary_target) | ||
shared.Settings.ASMJS_CODE_FILE = os.path.basename(asm_target) | ||
shared.Settings.WASM_TEXT_FILE = get_subresource_location(wasm_text_target) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just went ahead and tested my change. (I was expecting a complicated and/or long setup process based on past experience installing from source with the SDK, but in retrospect I think it was just compiling clang that would take a long time.) Anyway, it fails right here, apparently because the wasm, wast, and asm.js don't exist yet. How should I handle this? |
||
shared.Settings.WASM_BINARY_FILE = get_subresource_location(wasm_binary_target) | ||
shared.Settings.ASMJS_CODE_FILE = get_subresource_location(asm_target) | ||
|
||
shared.Settings.ASM_JS = 2 # when targeting wasm, we use a wasm Memory, but that is not compatible with asm.js opts | ||
shared.Settings.GLOBAL_BASE = 1024 # leave some room for mapping global vars | ||
|
@@ -1624,12 +1634,12 @@ def repl(m): | |
# Copy into temp dir as well, so can be run there too | ||
shared.safe_copy(memfile, os.path.join(shared.get_emscripten_temp_dir(), os.path.basename(memfile))) | ||
if not shared.Settings.BINARYEN: | ||
return 'memoryInitializer = "%s";' % os.path.basename(memfile) | ||
return 'memoryInitializer = "%s";' % get_subresource_location(memfile) | ||
else: | ||
# with wasm, we may have the mem init file in the wasm binary already | ||
return ('memoryInitializer = Module["wasmJSMethod"].indexOf("asmjs") >= 0 || ' | ||
'Module["wasmJSMethod"].indexOf("interpret-asm2wasm") >= 0 ? "%s" : null;' | ||
% os.path.basename(memfile)) | ||
% get_subresource_location(memfile)) | ||
src = re.sub(shared.JS.memory_initializer_pattern, repl, open(final).read(), count=1) | ||
open(final + '.mem.js', 'w').write(src) | ||
final += '.mem.js' | ||
|
@@ -2412,7 +2422,7 @@ def generate_html(target, options, js_target, target_basename, | |
meminitXHR.responseType = 'arraybuffer'; | ||
meminitXHR.send(null); | ||
})(); | ||
''' % os.path.basename(memfile)) + script.inline | ||
''' % get_subresource_location(memfile)) + script.inline | ||
|
||
# Download .asm.js if --separate-asm was passed in an asm.js build, or if 'asmjs' is one | ||
# of the wasm run methods. | ||
|
@@ -2431,7 +2441,7 @@ def generate_html(target, options, js_target, target_basename, | |
}, 1); // delaying even 1ms is enough to allow compilation memory to be reclaimed | ||
}; | ||
document.body.appendChild(script); | ||
''' % (os.path.basename(asm_target), script.inline) | ||
''' % (get_subresource_location(asm_target), script.inline) | ||
else: | ||
# may need to modify the asm code, load it as text, modify, and load asynchronously | ||
script.inline = ''' | ||
|
@@ -2454,7 +2464,7 @@ def generate_html(target, options, js_target, target_basename, | |
document.body.appendChild(script); | ||
}; | ||
codeXHR.send(null); | ||
''' % (os.path.basename(asm_target), '\n'.join(asm_mods), script.inline) | ||
''' % (get_subresource_location(asm_target), '\n'.join(asm_mods), script.inline) | ||
|
||
if shared.Settings.BINARYEN and not shared.Settings.BINARYEN_ASYNC_COMPILATION: | ||
# We need to load the wasm file before anything else, it has to be synchronously ready TODO: optimize | ||
|
@@ -2468,7 +2478,7 @@ def generate_html(target, options, js_target, target_basename, | |
%s | ||
}; | ||
wasmXHR.send(null); | ||
''' % (os.path.basename(wasm_binary_target), script.inline) | ||
''' % (get_subresource_location(wasm_binary_target), script.inline) | ||
|
||
html = open(target, 'wb') | ||
html_contents = shell.replace('{{{ SCRIPT }}}', script.replacement()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -856,6 +856,13 @@ var FETCH = 0; // If nonzero, enables emscripten_fetch API. | |
|
||
var ASMFS = 0; // If set to 1, uses the multithreaded filesystem that is implemented within the asm.js module, using emscripten_fetch. Implies -s FETCH=1. | ||
|
||
var SINGLE_FILE = 0; // If set to 1, embeds all subresources in the emitted JS file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add examples of those files (data, asm.js, wasm etc.), i think it would help clarify things |
||
// by converting their file names into base64 data URIs. | ||
// | ||
// Note that using this option may require a change to consuming | ||
// pages' Content Security Policies -- specifically, adding data: | ||
// to their connect-src directives. | ||
|
||
var WASM_TEXT_FILE = ''; // name of the file containing wasm text, if relevant | ||
var WASM_BINARY_FILE = ''; // name of the file containing wasm binary, if relevant | ||
var ASMJS_CODE_FILE = ''; // name of the file containing asm.js, if relevant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about moving this to
tools/shared.py
, perhaps a static method on theJS
class?