moo/wasm/emcc.txt

104 lines
3.6 KiB
Plaintext
Raw Permalink Normal View History

Temporary notes on how to build libmoo to web assembly with emscripten.
#### emscriptten APIS to look into
emscripten_set_main_loop();
#### How to Build
mkdir -p bld/emcc
cd blc/emcc
2019-11-02 06:34:18 +00:00
## run configure and make under emconfigure and emmake respectively
emconfigure ../../configure --prefix=${HOME}/xxx-emcc --disable-shared --enable-dynamic-module --disable-mod-x11 --disable-mod-con --disable-mod-sck CFLAGS="-Wall -O2 -g"
emmake make
2019-11-02 06:34:18 +00:00
## convert the library to web assembly - the command produces libmoo.js and libmoo.wasm
##emcc lib/.libs/libmoo.a -s WASM=1 -s EXPORT_ALL=1 -s LINKABLE=1 -o libmoo.js
#if you have defined MOO_EXPORT as EMSCRIPTEN_KEEPALIVE, the following will do
##emcc lib/.libs/libmoo.a -s WASM=1 -s -s LINKABLE=1 -o libmoo.js
2019-11-02 06:34:18 +00:00
###emcc lib/.libs/libmoo.a lib/.libs/libmoox.a -s WASM=1 -s LINKABLE=1 -o libmoo.js
## -s ALLOW_MEMORY_GROWTH=1
emcc lib/.libs/libmoo.a lib/.libs/libmoox.a -s WASM=1 -s LINKABLE=1 -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" -o libmoo.js --embed-file ../../kernel/
### --embed-file, if given a directory, includes all files under the directory.
### the result libmoo.js will contain lines like below:
### Module['FS_createDataFile']('/kernel', 'Apex.moo', fileData35, true, true, false);
### var fileData36 = [];
### fileData36.push.apply(fileData36, [35, 105, ...
###
## when i executed the command above, some versions of wasm-ld complained like below
## if the library is compiled with -pthread.
## wasm-ld: error: 'atomics' feature is disallowed by atomic_2835fb94.c.o, so --shared-memory must not be used
## a quick work around was to remove -pthread before building with 'emmake make'
## if defined some javascript functions in moo.cb.js...
2019-11-02 06:34:18 +00:00
emcc -Wall -O2 -g lib/.libs/libmoo.a ws.c -DMOO_HAVE_CFG_H -Ilib -I../../lib -s WASM=1 -s LINKABLE=1 -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" -o libmoo.js --pre-js moo.cb.js
#### moo.worker.js
self.importScripts('libmoo.js');
self.__ready = false;
Module.onRuntimeInitialized = function()
{
self.__ready = true;
console.log ('runtime is ready now...');
}
const libmoo = {
open: Module.cwrap('moo_openstd', 'number', ['number', 'number', 'number']),
close: Module.cwrap('moo_close', '', ['number'])
};
self.onmessage = function (evt) {
var objData = evt.data;
var cmd = objData.cmd;
if (cmd === "test-moo")
{
//var x = Module.ccall('moo_openstd', [0, null, null]);
//Module.ccall('moo_close', x);
if (self.__ready)
{
var moo = libmoo.open(0, null, null);
self.postMessage ('XXXXXXXXXXXXXXXx - ' + moo);
libmoo.close (moo);
}
}
}
#### moo.html
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /></head>
<body>
<input type="button" value="Test" onclick="do_test();" />
<script type="text/javascript">
//var g_WebWorker = new Worker("moo.worker.js");
var g_WebWorker = new SharedWorker("moo.worker.js");
//if (typeof(WebAssembly) === 'undefined')
//{
// alert ('No web assebly support in this browser');
//}
g_WebWorker.onerror = function (evt) { console.log(`Error from Web Worker: ${evt.message}`); }
g_WebWorker.onmessage = function (evt) { console.log(`Message from the Web Worker:\n\n ${evt.data}`); }
function do_test() {
g_WebWorker.postMessage({ "cmd": "test-moo", "data": "nothing as of now.." });
}
</script>
</body>
</html>