From 0809803bfd410e74191201c312e7ef227c04001c Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sun, 25 Jan 2026 20:20:31 +0100 Subject: [PATCH] embed the .aot file into the host binary as C-array --- .gitignore | 4 ++- wasm-base/examples/Makefile | 49 +++++++++++++++++++++++++-------- wasm-base/examples/embed/host.c | 26 ++++++++--------- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index d50b5cb..b179f85 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ -wasm-base/examples/*.aot wasm-base/examples/*.wasm +wasm-base/examples/*.aot +wasm-base/examples/embed/*_host.c wasm-base/examples/*.host +wasm-base/examples/*_wasm.c diff --git a/wasm-base/examples/Makefile b/wasm-base/examples/Makefile index 50edcb9..7f8dafa 100644 --- a/wasm-base/examples/Makefile +++ b/wasm-base/examples/Makefile @@ -8,34 +8,37 @@ IWASM_LIB := /opt/wamr-libiwasm WASI_ROOT := /opt/wasi-sdk WASI_CC := $(WASI_ROOT)/bin/clang WASI_CFLAGS := --target=wasm32-wasi \ - --sysroot=$(WASI_ROOT)/share/wasi-sysroot \ - -O0 -g -Wall + --sysroot=$(WASI_ROOT)/share/wasi-sysroot \ + -O0 -g -Wall # Embedding EMBED_CC := gcc EMBED_INCL := -I$(WAMR_ROOT)/core/iwasm/include \ -I$(WAMR_ROOT)/core/iwasm/common \ - -I$(WAMR_ROOT)/core/shared/platform/linux \ - -I$(WAMR_ROOT)/core/shared/utils \ - -I$(WAMR_ROOT)/core/shared/utils/uncommon + -I$(WAMR_ROOT)/core/shared/platform/linux \ + -I$(WAMR_ROOT)/core/shared/utils \ + -I$(WAMR_ROOT)/core/shared/utils/uncommon EMBED_SOURCES := $(WAMR_ROOT)/core/shared/utils/uncommon/bh_read_file.c EMBED_CFLAGS := -O0 -g -Wall $(EMBED_INCL) $(EMBED_SOURCES) AOT_LDFLAGS := -Wl,-rpath,$(WAMRC_LIB) AOT_LDLIBS := -L$(WAMRC_LIB) -lvmlib -lm INTER_LDFLAGS := -Wl,-rpath,$(IWASM_LIB) INTER_LDLIBS := -L$(IWASM_LIB) -liwasm -lm +XXD := busybox xxd # Files SRCS := $(wildcard *.c) WASMS := $(SRCS:.c=.wasm) -AOTS := $(WASMS:.wasm=.aot) +CWASMS := $(SRCS:.c=_wasm.c) +AOTS := $(SRCS:.c=.aot) OBJS := $(SRCS:.c=.o) -DEPS := $(OBJS:.o=.d) +DEPS := $(SRCS:.c=.d) +HOSTS := $(SRCS:.c=.host) -.PHONY: all build-wasms build-aots clean embed_host +.PHONY: all build-wasms build-aots build-cwasms build-hosts clean -all: build-wasms build-aots embed_host +all: build-wasms build-aots build-cwasms build-hosts # Compile to wasm bytecode using wasi-sdk build-wasms: $(WASMS) @@ -50,11 +53,35 @@ build-aots: $(AOTS) $(WAMRC) -o $@ $< # $(WAMRC) --enable-wasi -o $@ $< +# Convert the .aot files to C-style arrays (to embed them into the resulting host binary) +build-cwasms: $(CWASMS) + +%_wasm.c: %.aot + $(XXD) -i $< > $@ + +# Compile the host that will load and run the compiled wasm module +# The compiled wasm module is embedded as C-style array +build-hosts: $(HOSTS) + +# The C-style array is called %_aot, e.g. test_aot +# We have to modify the host to refer to that with the correct name +%.host: %_wasm.c + cp embed/host.c embed/$*_host.c + sed -i \ + -e "s/__WASM_ARRAY_FILE__/$*_wasm.c/g" \ + -e "s/__WASM_ARRAY__/$*_aot/g" \ + -e "s/__WASM_ARRAY_LEN__/$*_aot_len/g" \ + embed/$*_host.c + $(EMBED_CC) $(EMBED_CFLAGS) embed/$*_host.c -o $@ $(INTER_LDFLAGS) $(INTER_LDLIBS) + clean: rm -f *.wasm rm -f *.aot + rm -f *_wasm.c rm -f *.d rm -f *.o + rm -f embed/*_host.c + rm -f *.host # Compile to C-embedded # embed: $(OBJS) @@ -63,5 +90,5 @@ clean: # %.o: %.c # $(EMBED_CC) $(EMBED_CFLAGS) -o $@ $< -embed_host: - $(EMBED_CC) $(EMBED_CFLAGS) embed/host.c -o host $(INTER_LDFLAGS) $(INTER_LDLIBS) +# embed_host: +# $(EMBED_CC) $(EMBED_CFLAGS) embed/host.c -o host $(INTER_LDFLAGS) $(INTER_LDLIBS) diff --git a/wasm-base/examples/embed/host.c b/wasm-base/examples/embed/host.c index f01d55a..ce9ef19 100644 --- a/wasm-base/examples/embed/host.c +++ b/wasm-base/examples/embed/host.c @@ -7,29 +7,21 @@ #include "bh_read_file.h" #include "wasm_export.h" +#include "../__WASM_ARRAY_FILE__" + #define STACK_SIZE (8 * 1024) #define HEAP_SIZE (8 * 1024) // TODO: Set this up so the lsp actually finds the includes... int main(int argc, char **argv) { - if (argc != 2) { - printf("Missing .wasm or .aot file argument!\n"); - return 1; - } - - if (!strstr(argv[1], ".wasm") && !strstr(argv[1], ".aot")) { - printf("Only .wasm or .aot files are supported!\n"); - return 1; - } - - uint8 *buffer; + // uint8 *buffer; + // uint32 size; char error_buf[128]; wasm_module_t module; wasm_module_inst_t module_inst; wasm_function_inst_t func; wasm_exec_env_t exec_env; - uint32 size; uint32 stack_size = 8 * 1024; uint32 heap_size = 8 * 1024; @@ -46,14 +38,18 @@ int main(int argc, char **argv) { } /* read WASM file into a memory buffer */ - printf("Instantiating module from file '%s'\n", argv[1]); - buffer = bh_read_file_to_buffer(argv[1], &size); + // printf("Instantiating module from file '%s'\n", argv[1]); + // buffer = bh_read_file_to_buffer(argv[1], &size); /* add line below if we want to export native functions to WASM app */ // wasm_runtime_register_natives(...); /* parse the WASM file from buffer and create a WASM module */ - module = wasm_runtime_load(buffer, size, error_buf, sizeof(error_buf)); + // module = wasm_runtime_load(buffer, size, error_buf, sizeof(error_buf)); + + printf("Instantiating module from buffer\n"); + module = wasm_runtime_load(__WASM_ARRAY__, __WASM_ARRAY_LEN__, error_buf, + sizeof(error_buf)); /* create an instance of the WASM module (WASM linear memory is ready) */ module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,