chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

linking libchibi-scheme with emscripten program

Open ansdor opened this issue 3 years ago • 5 comments

I'm writing a C program that embeds chibi, and I want to compile it with emscripten. I got it working partially, but the chibi modules written in C don't work, like srfi-69

is there a way to make this work? compile a static version of libchibi-scheme that could be used by another program with emscripten?

ansdor avatar Jul 14 '21 21:07 ansdor

"don't work" is not exactly enough info to even investigate. What exactly went wrong? What error did yo see? ....

pclouds avatar Jul 15 '21 02:07 pclouds

oh, sorry. my first attempt was just compiling the library with emmake (I know the docs say to not use it, but it just wasn't recognized by emscripten when I used chibi's make js), it went fine until the load_standard_env() call, which returned an exception saying it couldn't find init-7.scm or meta-7.scm

second time I added chibi's lib/ dir to emscripten's --preload-file and it managed to get it to load the standard environment and evaluate my scheme code, but if I tried to (import (scheme base)) it said it couldn't load srfi-69. I assume this is because srfi-69 and some other modules are written in C and need to be compiled with emscripten too

I tried all possible combinations of emmake and make js, but nothing worked. compiling chibi with emmake throws an error when the makefile tries to run the chibi-ffi generator on the libraries, and make js just compiles a standalone interpreter but not the libchibi-scheme I need

what I want is some way to compile libchibi-scheme with all the modules/srfis statically linked, with emscripten, so I can link libchibi from my program.

ansdor avatar Jul 15 '21 02:07 ansdor

I think you need to make sure make js works first. What you describe sounds like missing .scm files (not .c files). We need to populate emscripten's filesystem with a bunch of .scm files, so if you do it manually or something, then it might be missing.

The key is in the find command in

js/chibi.js: chibi-scheme-emscripten chibi-scheme-static.bc js/pre.js js/post.js js/exported_functions.json
	emcc -O0 chibi-scheme-static.bc -o $@ -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s EXPORT_NAME=\"Chibi\" -s EXPORTED_FUNCTIONS=@js/exported_functions.json `find  lib -type f \( -name "*.scm" -or -name "*.sld" \) -printf " --preload-file %p"` -s 'EXTRA_EXPORTED_RUNTIME_METHODS=["ccall", "cwrap"]' --pre-js js/pre.js --post-js js/post.js

And all the code should be in chibi-scheme-static.bc. So if you can do make chibi-scheme-static.bc (no emmake) then you can run an emcc command similar to the above, adjusting your pre/post js and other stuff accordingly

pclouds avatar Jul 16 '21 09:07 pclouds

Also chibi has the ability to generate an image file. I wonder if it's simpler to make that image first (with native chibi, no emscripten) then put that image in emscripten fs. That would simplify the build command a bit.

pclouds avatar Jul 16 '21 09:07 pclouds

make js does work, but it doesn't produce a libchibi-scheme I can link. emmake make produces the right library, but only the base module works (not (scheme base), just the default environment you'd get by loading a file without any imports). anything under lib/ written in C doesn't, srfi-69 specifically gives an error message when I try to import (scheme base) compiling the libraries individually with emcc gives an error about dlopen() not working by default or something

I believe what I need is just a static version of libchibi-scheme, an .a file with all the modules included. an image might work too, I will try that, thanks.

ansdor avatar Jul 16 '21 14:07 ansdor