embed the .aot file into the host binary as C-array
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@ -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
|
||||
|
||||
@ -24,18 +24,21 @@ 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)
|
||||
|
||||
@ -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,
|
||||
|
||||
Reference in New Issue
Block a user