27
27
if __name__ == '__main__' :
28
28
ToolchainProfiler .record_process_start ()
29
29
30
- import os , sys , shutil , tempfile , subprocess , shlex , time , re , logging , urllib
30
+ import os , sys , shutil , tempfile , subprocess , shlex , time , re , logging , urllib , base64
31
31
from subprocess import PIPE
32
32
from tools import shared , jsrun , system_libs
33
33
from tools .shared import execute , suffix , unsuffixed , unsuffixed_basename , WINDOWS , safe_move
@@ -513,6 +513,16 @@ def filter_emscripten_options(argv):
513
513
514
514
# ---------------- Utilities ---------------
515
515
516
+ # Returns the run-time subresource location for accessing by such means as XHR
517
+ def get_subresource_location (path , media_type ):
518
+ if shared .Settings .SINGLE_FILE :
519
+ f = open (path , 'rb' )
520
+ data = base64 .b64encode (f .read ())
521
+ f .close ()
522
+ return 'data:' + media_type + ';base64,' + data
523
+ else :
524
+ return os .path .basename (path )
525
+
516
526
seen_names = {}
517
527
def uniquename (name ):
518
528
if name not in seen_names :
@@ -799,7 +809,7 @@ def detect_fixed_language_mode(args):
799
809
options .separate_asm = True
800
810
logging .warning ('forcing separate asm output (--separate-asm), because -s PRECISE_F32=2 or -s USE_PTHREADS=2 was passed.' )
801
811
if options .separate_asm :
802
- shared .Settings .SEPARATE_ASM = os . path . basename (asm_target )
812
+ shared .Settings .SEPARATE_ASM = get_subresource_location (asm_target , 'application/javascript' )
803
813
804
814
if 'EMCC_STRICT' in os .environ :
805
815
shared .Settings .STRICT = os .environ .get ('EMCC_STRICT' ) != '0'
@@ -1116,9 +1126,9 @@ def check(input_file):
1116
1126
1117
1127
if shared .Settings .BINARYEN :
1118
1128
# set file locations, so that JS glue can find what it needs
1119
- shared .Settings .WASM_TEXT_FILE = os . path . basename (wasm_text_target )
1120
- shared .Settings .WASM_BINARY_FILE = os . path . basename (wasm_binary_target )
1121
- shared .Settings .ASMJS_CODE_FILE = os . path . basename (asm_target )
1129
+ shared .Settings .WASM_TEXT_FILE = get_subresource_location (wasm_text_target , 'text/plain' )
1130
+ shared .Settings .WASM_BINARY_FILE = get_subresource_location (wasm_binary_target , 'application/octet-stream' )
1131
+ shared .Settings .ASMJS_CODE_FILE = get_subresource_location (asm_target , 'application/javascript' )
1122
1132
1123
1133
shared .Settings .ASM_JS = 2 # when targeting wasm, we use a wasm Memory, but that is not compatible with asm.js opts
1124
1134
shared .Settings .GLOBAL_BASE = 1024 # leave some room for mapping global vars
@@ -1624,12 +1634,12 @@ def repl(m):
1624
1634
# Copy into temp dir as well, so can be run there too
1625
1635
shared .safe_copy (memfile , os .path .join (shared .get_emscripten_temp_dir (), os .path .basename (memfile )))
1626
1636
if not shared .Settings .BINARYEN :
1627
- return 'memoryInitializer = "%s";' % os . path . basename (memfile )
1637
+ return 'memoryInitializer = "%s";' % get_subresource_location (memfile , 'application/octet-stream' )
1628
1638
else :
1629
1639
# with wasm, we may have the mem init file in the wasm binary already
1630
1640
return ('memoryInitializer = Module["wasmJSMethod"].indexOf("asmjs") >= 0 || '
1631
1641
'Module["wasmJSMethod"].indexOf("interpret-asm2wasm") >= 0 ? "%s" : null;'
1632
- % os . path . basename (memfile ))
1642
+ % get_subresource_location (memfile , 'application/octet-stream' ))
1633
1643
src = re .sub (shared .JS .memory_initializer_pattern , repl , open (final ).read (), count = 1 )
1634
1644
open (final + '.mem.js' , 'w' ).write (src )
1635
1645
final += '.mem.js'
@@ -2397,6 +2407,8 @@ def generate_html(target, options, js_target, target_basename,
2397
2407
''' % (shared .Settings .EMTERPRETIFY_FILE , script .inline )
2398
2408
2399
2409
if options .memory_init_file :
2410
+ memfile_location = get_subresource_location (memfile , 'application/octet-stream' )
2411
+
2400
2412
# start to load the memory init file in the HTML, in parallel with the JS
2401
2413
script .un_src ()
2402
2414
script .inline = ('''
@@ -2412,13 +2424,15 @@ def generate_html(target, options, js_target, target_basename,
2412
2424
meminitXHR.responseType = 'arraybuffer';
2413
2425
meminitXHR.send(null);
2414
2426
})();
2415
- ''' % os . path . basename ( memfile ) ) + script .inline
2427
+ ''' % memfile_location ) + script .inline
2416
2428
2417
2429
# Download .asm.js if --separate-asm was passed in an asm.js build, or if 'asmjs' is one
2418
2430
# of the wasm run methods.
2419
2431
if not options .separate_asm or (shared .Settings .BINARYEN and 'asmjs' not in shared .Settings .BINARYEN_METHOD ):
2420
2432
assert len (asm_mods ) == 0 , 'no --separate-asm means no client code mods are possible'
2421
2433
else :
2434
+ asm_target_location = get_subresource_location (asm_target , 'application/javascript' )
2435
+
2422
2436
script .un_src ()
2423
2437
if len (asm_mods ) == 0 :
2424
2438
# just load the asm, then load the rest
@@ -2431,7 +2445,7 @@ def generate_html(target, options, js_target, target_basename,
2431
2445
}, 1); // delaying even 1ms is enough to allow compilation memory to be reclaimed
2432
2446
};
2433
2447
document.body.appendChild(script);
2434
- ''' % (os . path . basename ( asm_target ) , script .inline )
2448
+ ''' % (asm_target_location , script .inline )
2435
2449
else :
2436
2450
# may need to modify the asm code, load it as text, modify, and load asynchronously
2437
2451
script .inline = '''
@@ -2454,9 +2468,11 @@ def generate_html(target, options, js_target, target_basename,
2454
2468
document.body.appendChild(script);
2455
2469
};
2456
2470
codeXHR.send(null);
2457
- ''' % (os . path . basename ( asm_target ) , '\n ' .join (asm_mods ), script .inline )
2471
+ ''' % (asm_target_location , '\n ' .join (asm_mods ), script .inline )
2458
2472
2459
2473
if shared .Settings .BINARYEN and not shared .Settings .BINARYEN_ASYNC_COMPILATION :
2474
+ wasm_binary_target_location = get_subresource_location (wasm_binary_target , 'application/octet-stream' )
2475
+
2460
2476
# We need to load the wasm file before anything else, it has to be synchronously ready TODO: optimize
2461
2477
script .un_src ()
2462
2478
script .inline = '''
@@ -2468,7 +2484,7 @@ def generate_html(target, options, js_target, target_basename,
2468
2484
%s
2469
2485
};
2470
2486
wasmXHR.send(null);
2471
- ''' % (os . path . basename ( wasm_binary_target ) , script .inline )
2487
+ ''' % (wasm_binary_target_location , script .inline )
2472
2488
2473
2489
html = open (target , 'wb' )
2474
2490
html_contents = shell .replace ('{{{ SCRIPT }}}' , script .replacement ())
0 commit comments