SWI-Prolog / swipl-devel

SWI-Prolog Main development repository
http://www.swi-prolog.org
Other
945 stars 171 forks source link

Emscripten Web Assembly build not working #576

Open goshdarngames opened 4 years ago

goshdarngames commented 4 years ago

The emscripten setup seems a bit of a mess. After some tweaking with various scripts I got emscripten to run (from the latest git). Then I get somewhere using this, which notably changes the location of the toolchain file.

WASM_HOME=$HOME/wasm
source $WASM_HOME/emsdk/emsdk_env.sh
TOOLCHAIN=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
[ -f $TOOLCHAIN ] || echo "Could not find emscripten toolchain"

cmake -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN \
          -DCMAKE_BUILD_TYPE=Release \
      -DZLIB_LIBRARY=$WASM_HOME/zlib-1.2.11/libz.a \
      -DZLIB_INCLUDE_DIR=$WASM_HOME/zlib-1.2.11 \
      -DMULTI_THREADED=OFF \
      -DUSE_SIGNALS=OFF \
      -DUSE_GMP=OFF \
      -DBUILD_SWIPL_LD=OFF \
      -DSWIPL_PACKAGES=OFF \
      -DINSTALL_DOCUMENTATION=OFF \
      -DSWIPL_NATIVE_FRIEND=linux \
      -G Ninja ..

The compiler works fine, but then the toolchain file is broken and sets emar wrong and we fail using

/bin/sh: 1: /home/janw/wasm/emsdk/emscripten/1.38.4/emar: not found

I didn't try any further ... Requires some determination and syncing with the emscripting project. I'm afraid I have other priorities. Happy to comment and merge pull requests though.

Originally posted by @JanWielemaker in https://github.com/SWI-Prolog/swipl-devel/issues/575#issuecomment-620468515

goshdarngames commented 4 years ago

Build process described here: https://github.com/SWI-Prolog/swipl-devel/blob/master/CMAKE.md

jacobfriedman commented 3 years ago

I've managed to get past it, but I have some errors. Jan: I'm really looking forward to getting SWIPL as a WASM.

JanWielemaker commented 3 years ago

Patches are welcome! I'm happy to share some knowledge from the earlier work by @rla and possibly (help) changing some stuff to core that better facilitates WASM. I think most of that work has already been done, mostly making saved states and .qlf file word-size independent and allow finding and opening files from the initial resource file which allows packaging the whole eco system in a single file.

I'm afraid I'm not going to pull the wagon though ...

rla commented 3 years ago

https://github.com/jacobfriedman/p2p-prolog/issues/1#issuecomment-701250912

I will go over the build process again some time but the current instructions skip wasm zlib build.

jacobfriedman commented 3 years ago

Maybe it's included in emscripten- just have to find out how to inject it at build

"...port functionality of emscripten with -s USE_ZLIB=1 when running emcc – "

jacobfriedman commented 3 years ago

jacobfriedman/p2p-prolog#1 (comment)

I will go over the build process again some time but the current instructions skip wasm zlib build.

do you have time for a quick call? I've built a wasm and am trying to instantiate via JS.

jacobfriedman commented 3 years ago

Alright - I'm getting somewhere. The process will be documented in a dockerfile.

Two errors remain, I believe:

Error # 1 The loadpackage gives absolute locations for directories, rather than relative to the /build/src location. Thus, the virtual FS residing in the module can't resolve upon JS execution. This DRY issue was found by rewiring the directories with a find/replace (not ideal). I'll look into the emscripten makefile tonight and see if I can't fix it.

loadPackage({
 "files": [{
      "filename": "home/jacob/projects/wasm-prolog/swipl-devel/build.wasm/src/wasm-preload/boot.prc",
       ...
    }, ...

Error # 2: Once #1 is resolved, console outputs: To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking

I'm not sure dynamic linking is needed at runtime. I haven't executed anything yet - but it seems like adding extra items to the VFS at runtime would be an ideal feature.

jacobfriedman commented 3 years ago

See https://github.com/SWI-Prolog/swipl-wasm.

jeswr commented 1 year ago

More relevant now is https://github.com/SWI-Prolog/npm-swipl-wasm