Skip to content
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 option 'wasm' to compile to WebAssembly in web export #6994

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions platform/javascript/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ javascript_files = [
"javascript_eval.cpp"
]

#obj = env.SharedObject('godot_javascript.cpp')

env_javascript = env.Clone()
if env['target'] == "profile":
env_javascript.Append(CPPFLAGS=['-DPROFILER_ENABLED'])
Expand All @@ -21,9 +19,19 @@ for x in javascript_files:
javascript_objects.append( env_javascript.Object( x ) )

env.Append(LINKFLAGS=["-s","EXPORTED_FUNCTIONS=\"['_main','_audio_server_mix_function','_main_after_fs_sync']\""])

prog = None

#env_javascript.SharedLibrary("#platform/javascript/libgodot_javascript.so",[javascript_objects])

env.Program('#bin/godot',javascript_objects,PROGSUFFIX=env["PROGSUFFIX"]+".html")
env.Append(LINKFLAGS=["--shell-file",'"platform/javascript/godot_shell.html"'])

build = env.Program('#bin/godot',javascript_objects,PROGSUFFIX=env["PROGSUFFIX"]+".html")

def make_html_shell(target, source, env):
html_path = target[0].rstr()
assert html_path[:4] == 'bin/'
assert html_path[-5:] == '.html'
basename = html_path[4:-5]
with open(html_path, 'r+') as html_file:
fixed_html = html_file.read().replace('.html.mem', '.mem').replace(basename, '$GODOT_BASE')
html_file.seek(0)
html_file.truncate()
html_file.write(fixed_html)

env.AddPostAction(build, Action(make_html_shell, "Creating HTML shell file"))
12 changes: 9 additions & 3 deletions platform/javascript/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def can_build():
def get_opts():

return [
['wasm','Compile to WebAssembly','no'],
['javascript_eval','Enable JavaScript eval interface','yes'],
]

Expand All @@ -42,7 +43,6 @@ def configure(env):
em_path=os.environ["EMSCRIPTEN_ROOT"]

env['ENV']['PATH'] = em_path+":"+env['ENV']['PATH']

env['CC'] = em_path+'/emcc'
env['CXX'] = em_path+'/emcc'
#env['AR'] = em_path+"/emar"
Expand Down Expand Up @@ -77,14 +77,20 @@ def configure(env):
env.Append(CPPFLAGS=['-DJAVASCRIPT_ENABLED', '-DUNIX_ENABLED', '-DPTHREAD_NO_RENAME', '-DNO_FCNTL','-DMPC_FIXED_POINT','-DTYPED_METHOD_BIND','-DNO_THREADS'])
env.Append(CPPFLAGS=['-DGLES2_ENABLED'])
env.Append(CPPFLAGS=['-DGLES_NO_CLIENT_ARRAYS'])
env.Append(CPPFLAGS=['-s','ASM_JS=1'])
env.Append(CPPFLAGS=['-s','FULL_ES2=1'])
# env.Append(CPPFLAGS=['-DANDROID_ENABLED', '-DUNIX_ENABLED','-DMPC_FIXED_POINT'])

if env['wasm'] == 'yes':
env.Append(LINKFLAGS=['-s','BINARYEN=1'])
env.Append(LINKFLAGS=['-s','\'BINARYEN_METHOD="native-wasm"\''])
env["PROGSUFFIX"]+=".webassembly"
else:
env.Append(CPPFLAGS=['-s','ASM_JS=1'])
env.Append(LINKFLAGS=['-s','ASM_JS=1'])

if env['javascript_eval'] == 'yes':
env.Append(CPPFLAGS=['-DJAVASCRIPT_EVAL_ENABLED'])

env.Append(LINKFLAGS=['-s','ASM_JS=1'])
env.Append(LINKFLAGS=['-O2'])
#env.Append(LINKFLAGS=['-g4'])

Expand Down
11 changes: 6 additions & 5 deletions platform/javascript/export/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t>& p_html, const St

String current_line = lines[i];
current_line = current_line.replace("$GODOT_TMEM",itos((1<<(max_memory+5))*1024*1024));
current_line = current_line.replace("$GODOT_FS",p_name+"fs.js");
current_line = current_line.replace("$GODOT_MEM",p_name+".mem");
current_line = current_line.replace("$GODOT_JS",p_name+".js");
current_line = current_line.replace("$GODOT_BASE",p_name);
current_line = current_line.replace("$GODOT_CANVAS_WIDTH",Globals::get_singleton()->get("display/width"));
current_line = current_line.replace("$GODOT_CANVAS_HEIGHT",Globals::get_singleton()->get("display/height"));
current_line = current_line.replace("$GODOT_HEAD_TITLE",!html_title.empty()?html_title:(String) Globals::get_singleton()->get("application/name"));
Expand Down Expand Up @@ -319,16 +317,19 @@ Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool
}
if (file=="godot.js") {

//_fix_godot(data);
file=p_path.get_file().basename()+".js";
}

if (file=="godot.mem") {

//_fix_godot(data);
file=p_path.get_file().basename()+".mem";
}

if (file=="godot.wasm") {

file=p_path.get_file().basename()+".wasm";
}

String dst = p_path.get_base_dir().plus_file(file);
FileAccess *f=FileAccess::open(dst,FileAccess::WRITE);
if (!f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,24 +351,7 @@
};
};
//]]></script>
<script type="text/javascript" src="$GODOT_FS"></script>
<script>
(function() {
var memoryInitializer = "$GODOT_MEM";
if (typeof Module.locateFile === "function") {
memoryInitializer = Module.locateFile(memoryInitializer);
} else if (Module.memoryInitializerPrefixURL) {
memoryInitializer = Module.memoryInitializerPrefixURL + memoryInitializer;
}
var xhr = Module.memoryInitializerRequest = new XMLHttpRequest();
xhr.open("GET", memoryInitializer, true);
xhr.responseType = "arraybuffer";
xhr.send(null);
})();

var script = document.createElement("script");
script.src = "$GODOT_JS";
document.body.appendChild(script);
</script>
<script type="text/javascript" src="$GODOT_BASEfs.js"></script>
{{{ SCRIPT }}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part no longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Emscripten now inserts this piece of code in place of the {{ SCRIPT }} placeholder at compile-time, because we pass the --shell-file argument to the linker. This code was so far hard-coded, but for WebAssembly we need different code here, so we leave it to Emscripten.

</body>
</html>