add linux x64 arch makefile

This commit is contained in:
2026-02-05 23:45:03 +01:00
parent 6381727851
commit 2f8f737bb8
3 changed files with 109 additions and 87 deletions

View File

@ -1,86 +0,0 @@
# This makefile embeds an application compiled to WASM into a native C program.
# The WASM module will be loaded by the WASM runtime.
# Paths
WAMR_ROOT := /opt/wamr
WAMRC := /opt/wamr-wamrc/wamrc
IWASM := /opt/wamr-iwasm/iwasm
WAMRC_LIB := /opt/wamr-libvmlib
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
# 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
EMBED_SOURCES := $(WAMR_ROOT)/core/shared/utils/uncommon/bh_read_file.c # Not used when reading from buffer
EMBED_CFLAGS := -O0 -g -Wall $(EMBED_INCL)
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)
CWASMS := $(SRCS:.c=_wasm.c)
AOTS := $(SRCS:.c=.aot)
OBJS := $(SRCS:.c=.o)
DEPS := $(SRCS:.c=.d)
HOSTS := $(SRCS:.c=_host.elf)
.PHONY: all build-wasms build-aots build-cwasms build-hosts clean
all: build-wasms build-aots build-cwasms build-hosts
# Compile to wasm bytecode using wasi-sdk
build-wasms: $(WASMS)
%.wasm: %.c
$(WASI_CC) $(WASI_CFLAGS) $< -o $@
# Compile ahead-of-time module using wamrc
build-aots: $(AOTS)
%.aot: %.wasm
$(WAMRC) -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.elf: %_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.elf

88
examples/arch/linux.mk Normal file
View File

@ -0,0 +1,88 @@
# C -> WASM
WASI_ROOT := /opt/wasi-sdk
WASI_CC := ${WASI_ROOT}/bin/clang
WASI_CFLAGS := --target=wasm64 \
--sysroot=${WASI_ROOT}/share/wasi-sysroot \
-z stack-size=4096 \
-O0 -g -nostdlib \
-Wl,--no-entry \
-Wl,--initial-memory=65536 \
-Wl,--export-all \
-Wl,--export=__heap_base \
-Wl,--export=__data_end
# WASM -> Baremetal
WAMR := /opt/wamr
IWASM_LIB := /opt/wamr-libiwasm-64
CC := gcc
# NOTE: When compiling I get "error: bp cannot be used in asm here"
# I could remove "ebp" from the clobber list (ARCH_ASM_CLOBBER_ALL) or
# use the -fomit-frame-pointer flag to tell gcc it shouldn't rely on ebp for enter/leave...
CFLAGS := -I. -O0 -g -ffunction-sections -std=c11 -fomit-frame-pointer
LDFLAGS = $^ -Wl,--build-id=none -static -nostdlib \
-Wl,-rpath,${IWASM_LIB} -L${IWASM_LIB} -liwasm -lgcc
INCL := -I${WAMR}/core/iwasm/include \
-I${WAMR}/core/shared/utils \
-I${WAMR}/core/shared/platform/baremetal
WAMRC := /opt/wamr-wamrc/wamrc
WAMRCFLAGS := --target=i386 --format=object
XXD := busybox xxd
################################################################
# C -> WASM
${BUILD_DIR}/%/module.wasm: %.c
mkdir -p $(shell dirname $@)
${WASI_CC} ${WASI_CFLAGS} $< -o $@
################################################################
# WASM -> Native Object File
# ${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module.wasm
# ${WAMRC} ${WAMRCFLAGS} -o ${BUILD_DIR}/$*/system.o ${BUILD_DIR}/$*/module.wasm
#
# ${BUILD_DIR}/startup.o: arch/bochs/startup.s
# ${CC} $< ${CFLAGS} -c -o $@
#
# ${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
# ${CC} ${LDFLAGS} -o $@
################################################################
# WASM -> AOT -> Loaded by Runtime
${BUILD_DIR}/%/module.aot: ${BUILD_DIR}/%/module.wasm
${WAMRC} -o $@ $<
${BUILD_DIR}/%/module_wasm.c: ${BUILD_DIR}/%/module.aot
${XXD} -i $< > $@
${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module_wasm.c
cp embed/host.c ${BUILD_DIR}/$*/module_host.c
sed -i \
-e "s/__WASM_ARRAY_FILE__/module_wasm.c/g" \
-e "s/__WASM_ARRAY__/build_linux_$*_module_aot/g" \
-e "s/__WASM_ARRAY_LEN__/build_linux_$*_module_aot_len/g" \
${BUILD_DIR}/$*/module_host.c
${CC} ${CFLAGS} ${INCL} -c ${BUILD_DIR}/$*/module_host.c -o $@
${BUILD_DIR}/startup.o: arch/bochs/startup.s
${CC} $< ${CFLAGS} -c -o $@
${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
${CC} ${LDFLAGS} -o $@
define arch-make-targets
build-$1: ${BUILD_DIR}/$1/system.elf
gdb-$1:
gdb --tui ${BUILD_DIR}/$1/system.elf
valgrind-$1:
valgrind ${BUILD_DIR}/$1/system.elf
objdump-$1:
objdump --disassemble --start-address=0x401000 --stop-address=0x401200 --disassembler-options intel --disassembler-color=on --source ${BUILD_DIR}/$1/system.elf
endef

View File

@ -114,6 +114,23 @@ RUN mkdir build_libiwasm && cd build_libiwasm \
.. \
&& make -j$(nproc)
RUN mkdir build_libiwasm_64 && cd build_libiwasm_64 \
&& cmake \
-DWAMR_BUILD_PLATFORM=baremetal \
-DWAMR_BUILD_TARGET=X86_64 \
-DWAMR_BUILD_AOT=0 \
-DWAMR_BUILD_WAMR_COMPILER=0 \
-DWAMR_BUILD_INTERP=1 \
-DWAMR_BUILD_FAST_INTERP=0 \
-DWAMR_BUILD_JIT=0 \
-DWAMR_BUILD_FAST_JIT=0 \
-DWAMR_BUILD_LIBC_BUILTIN=1 \
-DWAMR_BUILD_LIBC_WASI=0 \
-DWAMR_BUILD_SIMD=0 \
-DCMAKE_COLOR_DIAGNOSTICS=ON \
.. \
&& make -j$(nproc)
# =============================================================================
FROM ghcr.io/webassembly/wasi-sdk:wasi-sdk-29
@ -129,6 +146,8 @@ RUN apt-get update \
neovim \
ranger \
wabt \
gdb \
valgrind \
fish \
grub-common \
xorriso \
@ -141,7 +160,8 @@ COPY --from=wamr-builder /wamr/product-mini/platforms/linux/build_iwasm /opt/wam
RUN ln -sf /opt/wamr/product-mini/platforms/linux/build_iwasm /opt/wamr-iwasm \
&& ln -sf /opt/wamr/wamr-compiler/build_wamrc /opt/wamr-wamrc \
&& ln -sf /opt/wamr/wamr-compiler/build_libvmlib /opt/wamr-libvmlib \
&& ln -sf /opt/wamr/build_libiwasm /opt/wamr-libiwasm
&& ln -sf /opt/wamr/build_libiwasm /opt/wamr-libiwasm \
&& ln -sf /opt/wamr/build_libiwasm_64 /opt/wamr-libiwasm-64
COPY ./examples /home/ubuntu/examples
WORKDIR /home/ubuntu/examples