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/*.wasm
|
||||||
|
wasm-base/examples/*.aot
|
||||||
|
wasm-base/examples/embed/*_host.c
|
||||||
wasm-base/examples/*.host
|
wasm-base/examples/*.host
|
||||||
|
wasm-base/examples/*_wasm.c
|
||||||
|
|||||||
@ -8,34 +8,37 @@ IWASM_LIB := /opt/wamr-libiwasm
|
|||||||
WASI_ROOT := /opt/wasi-sdk
|
WASI_ROOT := /opt/wasi-sdk
|
||||||
WASI_CC := $(WASI_ROOT)/bin/clang
|
WASI_CC := $(WASI_ROOT)/bin/clang
|
||||||
WASI_CFLAGS := --target=wasm32-wasi \
|
WASI_CFLAGS := --target=wasm32-wasi \
|
||||||
--sysroot=$(WASI_ROOT)/share/wasi-sysroot \
|
--sysroot=$(WASI_ROOT)/share/wasi-sysroot \
|
||||||
-O0 -g -Wall
|
-O0 -g -Wall
|
||||||
|
|
||||||
# Embedding
|
# Embedding
|
||||||
EMBED_CC := gcc
|
EMBED_CC := gcc
|
||||||
EMBED_INCL := -I$(WAMR_ROOT)/core/iwasm/include \
|
EMBED_INCL := -I$(WAMR_ROOT)/core/iwasm/include \
|
||||||
-I$(WAMR_ROOT)/core/iwasm/common \
|
-I$(WAMR_ROOT)/core/iwasm/common \
|
||||||
-I$(WAMR_ROOT)/core/shared/platform/linux \
|
-I$(WAMR_ROOT)/core/shared/platform/linux \
|
||||||
-I$(WAMR_ROOT)/core/shared/utils \
|
-I$(WAMR_ROOT)/core/shared/utils \
|
||||||
-I$(WAMR_ROOT)/core/shared/utils/uncommon
|
-I$(WAMR_ROOT)/core/shared/utils/uncommon
|
||||||
EMBED_SOURCES := $(WAMR_ROOT)/core/shared/utils/uncommon/bh_read_file.c
|
EMBED_SOURCES := $(WAMR_ROOT)/core/shared/utils/uncommon/bh_read_file.c
|
||||||
EMBED_CFLAGS := -O0 -g -Wall $(EMBED_INCL) $(EMBED_SOURCES)
|
EMBED_CFLAGS := -O0 -g -Wall $(EMBED_INCL) $(EMBED_SOURCES)
|
||||||
AOT_LDFLAGS := -Wl,-rpath,$(WAMRC_LIB)
|
AOT_LDFLAGS := -Wl,-rpath,$(WAMRC_LIB)
|
||||||
AOT_LDLIBS := -L$(WAMRC_LIB) -lvmlib -lm
|
AOT_LDLIBS := -L$(WAMRC_LIB) -lvmlib -lm
|
||||||
INTER_LDFLAGS := -Wl,-rpath,$(IWASM_LIB)
|
INTER_LDFLAGS := -Wl,-rpath,$(IWASM_LIB)
|
||||||
INTER_LDLIBS := -L$(IWASM_LIB) -liwasm -lm
|
INTER_LDLIBS := -L$(IWASM_LIB) -liwasm -lm
|
||||||
|
XXD := busybox xxd
|
||||||
|
|
||||||
# Files
|
# Files
|
||||||
SRCS := $(wildcard *.c)
|
SRCS := $(wildcard *.c)
|
||||||
WASMS := $(SRCS:.c=.wasm)
|
WASMS := $(SRCS:.c=.wasm)
|
||||||
AOTS := $(WASMS:.wasm=.aot)
|
CWASMS := $(SRCS:.c=_wasm.c)
|
||||||
|
AOTS := $(SRCS:.c=.aot)
|
||||||
OBJS := $(SRCS:.c=.o)
|
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
|
# Compile to wasm bytecode using wasi-sdk
|
||||||
build-wasms: $(WASMS)
|
build-wasms: $(WASMS)
|
||||||
@ -50,11 +53,35 @@ build-aots: $(AOTS)
|
|||||||
$(WAMRC) -o $@ $<
|
$(WAMRC) -o $@ $<
|
||||||
# $(WAMRC) --enable-wasi -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:
|
clean:
|
||||||
rm -f *.wasm
|
rm -f *.wasm
|
||||||
rm -f *.aot
|
rm -f *.aot
|
||||||
|
rm -f *_wasm.c
|
||||||
rm -f *.d
|
rm -f *.d
|
||||||
rm -f *.o
|
rm -f *.o
|
||||||
|
rm -f embed/*_host.c
|
||||||
|
rm -f *.host
|
||||||
|
|
||||||
# Compile to C-embedded
|
# Compile to C-embedded
|
||||||
# embed: $(OBJS)
|
# embed: $(OBJS)
|
||||||
@ -63,5 +90,5 @@ clean:
|
|||||||
# %.o: %.c
|
# %.o: %.c
|
||||||
# $(EMBED_CC) $(EMBED_CFLAGS) -o $@ $<
|
# $(EMBED_CC) $(EMBED_CFLAGS) -o $@ $<
|
||||||
|
|
||||||
embed_host:
|
# embed_host:
|
||||||
$(EMBED_CC) $(EMBED_CFLAGS) embed/host.c -o host $(INTER_LDFLAGS) $(INTER_LDLIBS)
|
# $(EMBED_CC) $(EMBED_CFLAGS) embed/host.c -o host $(INTER_LDFLAGS) $(INTER_LDLIBS)
|
||||||
|
|||||||
@ -7,29 +7,21 @@
|
|||||||
#include "bh_read_file.h"
|
#include "bh_read_file.h"
|
||||||
#include "wasm_export.h"
|
#include "wasm_export.h"
|
||||||
|
|
||||||
|
#include "../__WASM_ARRAY_FILE__"
|
||||||
|
|
||||||
#define STACK_SIZE (8 * 1024)
|
#define STACK_SIZE (8 * 1024)
|
||||||
#define HEAP_SIZE (8 * 1024)
|
#define HEAP_SIZE (8 * 1024)
|
||||||
|
|
||||||
// TODO: Set this up so the lsp actually finds the includes...
|
// TODO: Set this up so the lsp actually finds the includes...
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if (argc != 2) {
|
// uint8 *buffer;
|
||||||
printf("Missing .wasm or .aot file argument!\n");
|
// uint32 size;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!strstr(argv[1], ".wasm") && !strstr(argv[1], ".aot")) {
|
|
||||||
printf("Only .wasm or .aot files are supported!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8 *buffer;
|
|
||||||
char error_buf[128];
|
char error_buf[128];
|
||||||
wasm_module_t module;
|
wasm_module_t module;
|
||||||
wasm_module_inst_t module_inst;
|
wasm_module_inst_t module_inst;
|
||||||
wasm_function_inst_t func;
|
wasm_function_inst_t func;
|
||||||
wasm_exec_env_t exec_env;
|
wasm_exec_env_t exec_env;
|
||||||
uint32 size;
|
|
||||||
uint32 stack_size = 8 * 1024;
|
uint32 stack_size = 8 * 1024;
|
||||||
uint32 heap_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 */
|
/* read WASM file into a memory buffer */
|
||||||
printf("Instantiating module from file '%s'\n", argv[1]);
|
// printf("Instantiating module from file '%s'\n", argv[1]);
|
||||||
buffer = bh_read_file_to_buffer(argv[1], &size);
|
// buffer = bh_read_file_to_buffer(argv[1], &size);
|
||||||
|
|
||||||
/* add line below if we want to export native functions to WASM app */
|
/* add line below if we want to export native functions to WASM app */
|
||||||
// wasm_runtime_register_natives(...);
|
// wasm_runtime_register_natives(...);
|
||||||
|
|
||||||
/* parse the WASM file from buffer and create a WASM module */
|
/* 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) */
|
/* create an instance of the WASM module (WASM linear memory is ready) */
|
||||||
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
|
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
|
||||||
|
|||||||
Reference in New Issue
Block a user