remko / waforth

Small but complete dynamic Forth Interpreter/Compiler for and in WebAssembly
https://mko.re/waforth
MIT License
497 stars 27 forks source link

Use `ld` instead of custom bin2h script #53

Closed nagy closed 1 year ago

nagy commented 1 year ago

This is just a suggestion: You can use ld to create object files directly with a make target like so:

waforth_core_wasm.o: waforth_core.wasm
    ld --relocatable --format=binary -o waforth_core_wasm.o waforth_core.wasm

Then you can have:

extern const char _binary_waforth_core_wasm_start, _binary_waforth_core_wasm_size;
const char *waforth_core = &_binary_waforth_core_wasm_start;
size_t waforth_core_size = (size_t)&_binary_waforth_core_wasm_size;
wasm_byte_vec_t core = {.data = (wasm_byte_t *)waforth_core, .size = waforth_core_size};

More info: http://elm-chan.org/junk/32bit/binclude.html

This could get rid of the nodejs dependency.

Working draft patch ```diff diff --git a/src/standalone/Makefile b/src/standalone/Makefile index e703e9f..a46665d 100644 --- a/src/standalone/Makefile +++ b/src/standalone/Makefile @@ -90,14 +90,14 @@ WAT2WASM=wat2wasm WAT2WASM_FLAGS=--debug-names CFLAGS:=-Wall -DVERSION='"$(VERSION)"' $(CFLAGS) -OBJECTS=main.o $(RESOURCE_OBJECTS) +OBJECTS=main.o waforth_core_wasm.o $(RESOURCE_OBJECTS) all: waforth waforth: $(OBJECTS) $(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LDFLAGS) $(LIBS) -main.o: waforth_core.h +main.o: icon.ico: ../../doc/logo.svg convert -background transparent $< -define icon:auto-resize=16,32,48,64,256 $@ @@ -105,8 +105,9 @@ icon.ico: ../../doc/logo.svg waforth_core.wasm: ../waforth.wat $(WAT2WASM) $(WAT2WASM_FLAGS) -o $@ $< -waforth_core.h: waforth_core.wasm - $(BIN2H) $< $@ +waforth_core_wasm.o: waforth_core.wasm + ld --relocatable --format=binary -o waforth_core_wasm.o waforth_core.wasm + .PHONY: install-deps package @@ -129,4 +130,4 @@ run-sieve: run_sieve.f .PHONY: clean clean: - -rm -f waforth_core.wasm waforth_core.h $(OBJECTS) waforth *.exe *.tgz *.zip test.out + -rm -f waforth_core.wasm $(OBJECTS) waforth *.exe *.tgz *.zip test.out diff --git a/src/standalone/main.c b/src/standalone/main.c index 895d009..3248760 100644 --- a/src/standalone/main.c +++ b/src/standalone/main.c @@ -4,7 +4,6 @@ #include #endif -#include "waforth_core.h" #include "wasm.h" #include @@ -136,7 +135,10 @@ int main(int argc, char *argv[]) { wasm_engine_t *engine = wasm_engine_new(); store = wasm_store_new(engine); - wasm_byte_vec_t core = {.data = (wasm_byte_t *)waforth_core, .size = sizeof(waforth_core)}; + extern const char _binary_waforth_core_wasm_start, _binary_waforth_core_wasm_size; + const char *waforth_core = &_binary_waforth_core_wasm_start; + size_t waforth_core_size = (size_t)&_binary_waforth_core_wasm_size; + wasm_byte_vec_t core = {.data = (wasm_byte_t *)waforth_core, .size = waforth_core_size}; wasm_module_t *module = wasm_module_new(store, &core); if (!module) { printf("error compiling\n"); ```

Otherwise, you could use xxd with the -include flag. https://linux.die.net/man/1/xxd

remko commented 1 year ago

Thanks for the tip, I didn't know you could do this with ld.

I'm going to stick with bin2h: node is a dependency for lots of other things anyway, and this keeps it cross-platform.