Files
failnix/wasm.just

345 lines
10 KiB
Plaintext

# =================================================================================================================== #
# Build WASM module recipes
# =================================================================================================================== #
WASI_CC := f"{{WASI_ROOT}}/bin/clang"
WASI_CFLAGS := "\
--target=wasm32 \
--sysroot={{WASI_ROOT}}/share/wasi-sysroot \
-z stack-size=4096 \
-O0 \
-nostdlib \
-Wl,--no-entry \
-Wl,--export-all \
-Wl,--no-gc-sections \
-Wl,--initial-memory=65536 \
-Wl,--export=__heap_base \
-Wl,--export=__data_end \
"
CROSS_CFLAGS_NOWASM := "\
-O0 \
-m32 \
-ffunction-sections \
-fdata-sections \
-ffreestanding \
-fomit-frame-pointer \
-ggdb3 \
"
CROSS_LDFLAGS_NOWASM := "\
-Wl,--build-id=none \
-static \
-nostdlib \
-m32 \
-lc \
-lgcc \
-lm \
"
LINUX_CFLAGS_NOWASM := "\
-O0 \
-m32 \
-ffunction-sections \
-fdata-sections \
-ggdb3 \
"
LINUX_LDFLAGS_NOWASM := "\
-Wl,--build-id=none \
-m32 \
-lm \
"
WAMRC := "wamrc"
WAMRCFLAGS := "\
--target=i386 \
--cpu=generic \
--opt-level=0 \
"
XXD := "xxd"
[doc("C -> WASM: Compile a C function to a WASM module using WASI-SDK")]
[group("1: build module")]
build-wasm-module module:
{{ WASI_CC }} {{ WASI_CFLAGS }} targets/wasm-module/{{ module }}.c -o {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm
[doc("WASM -> AOT: Compile a WASM module ahead-of-time using WAMR")]
[group("1: build module")]
build-wasm-aot module:
{{ WAMRC }} {{ WAMRCFLAGS }} -o {{ BUILD_DIR }}-{{ module }}/wasm_module.aot {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm
[doc("AOT -> C-Array: Dump a WASM module compiled ahead-of-time to a binary array")]
[group("1: build module")]
build-wasm-aot-array module:
{{ XXD }} -i {{ BUILD_DIR }}-{{ module }}/wasm_module.aot > {{ BUILD_DIR }}-{{ module }}/wasm_aot_array.c
[doc("WASM -> C-Array: Dump a WASM module to a binary array")]
[group("1: build module")]
build-wasm-interp-array module:
{{ XXD }} -i {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm > {{ BUILD_DIR }}-{{ module }}/wasm_interp_array.c
[private]
build-c-module-fail module:
{{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \
-c targets/wasm-module/{{ module }}.c \
-o {{ BUILD_DIR }}-{{ module }}/c_module.o
[private]
build-c-module-linux module:
{{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \
-c targets/wasm-module/{{ module }}.c \
-o {{ BUILD_DIR }}-{{ module }}/c_module.o
[doc("C -> Object: Compile a C function (no WASM)")]
[group("1: build module")]
build-c-module module target="fail":
#!/usr/bin/env sh
if [ "{{ target }}" = "fail" ]; then
just build-c-module-fail "{{ module }}"
elif [ "{{ target }}" = "linux" ]; then
just build-c-module-linux "{{ module }}"
else
echo "unknown target: {{ target }}" >&2
exit 1
fi
# =================================================================================================================== #
# Host program recipes
# =================================================================================================================== #
# FAIL*
CROSS_CFLAGS := f"-I./targets/wasm-host {{CROSS_CFLAGS_NOWASM}}"
CROSS_LDFLAGS := f"-L{{LIBIWASM_DEBUG}} -liwasm {{CROSS_LDFLAGS_NOWASM}}"
CROSS_INCLUDES := f"\
-I{{WAMR_ROOT}}/core/iwasm/include \
-I{{WAMR_ROOT}}/core/shared/utils \
-I{{WAMR_ROOT}}/core/shared/platform/baremetal \
"
# LINUX-POSIX
LINUX_CFLAGS := f"-I./targets/wasm-host {{ LINUX_CFLAGS_NOWASM }}"
LINUX_LDFLAGS := f"-Wl,-rpath,{{LIBIWASM_LINUX_DEBUG}} -L{{LIBIWASM_LINUX_DEBUG}} -liwasm {{LINUX_LDFLAGS_NOWASM}}"
LINUX_INCLUDES := f"\
-I{{WAMR_ROOT}}/core/iwasm/include \
-I{{WAMR_ROOT}}/core/shared/utils \
-I{{WAMR_ROOT}}/core/shared/platform/linux \
"
# LINUX-Baremetal
LINUX_BAREMETAL_CFLAGS := "\
-I./targets/wasm-host \
-O0 \
-m32 \
-ffunction-sections \
-fdata-sections \
-ffreestanding \
-ggdb3 \
"
LINUX_BAREMETAL_LDFLAGS := f"\
-Wl,--build-id=none \
-static \
-nostdlib \
-m32 \
-L{{LIBIWASM_DEBUG}} \
-liwasm \
-lc \
-lgcc \
-lm \
--entry main \
"
LINUX_BAREMETAL_INCLUDES := f"\
-I{{WAMR_ROOT}}/core/iwasm/include \
-I{{WAMR_ROOT}}/core/shared/utils \
-I{{WAMR_ROOT}}/core/shared/platform/baremetal \
"
[doc("Insert the AOT array into the host program")]
[group("2: build host")]
prepare-aot-host module target="fail":
cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c
sed -i \
-e "s/__WASM_ARRAY_FILE__/wasm_aot_array.c/g" \
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_aot/g" \
-e "s/__WASM_ARRAY_LEN__/build_{{ module }}_wasm_module_aot_len/g" \
{{ BUILD_DIR }}-{{ module }}/module_host.c
[doc("Insert the WASM array into the host program")]
[group("2: build host")]
prepare-interp-host module target="fail":
cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c
sed -i \
-e "s/__WASM_ARRAY_FILE__/wasm_interp_array.c/g" \
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_wasm/g" \
-e "s/__WASM_ARRAY_LEN__/build_{{ module }}_wasm_module_wasm_len/g" \
{{ BUILD_DIR }}-{{ module }}/module_host.c
[private]
build-wasm-host-fail module:
{{ CROSS_CC }} {{ CROSS_CFLAGS }} {{ CROSS_INCLUDES }} \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
[private]
build-wasm-host-linux module:
{{ LINUX_CC }} {{ LINUX_CFLAGS }} {{ LINUX_INCLUDES }} \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
[private]
build-wasm-host-linux-baremetal module:
{{ CROSS_CC }} {{ LINUX_BAREMETAL_CFLAGS }} {{ LINUX_BAREMETAL_INCLUDES }} \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
[doc("Compile C-Host: The host uses WAMR to load the WASM/AOT module")]
[group("2: build host")]
build-wasm-host module target="fail":
#!/usr/bin/env sh
if [ "{{ target }}" = "fail" ]; then
just build-wasm-host-fail "{{ module }}"
elif [ "{{ target }}" = "linux" ]; then
just build-wasm-host-linux "{{ module }}"
elif [ "{{ target }}" = "linux-baremetal" ]; then
just build-wasm-host-linux-baremetal "{{ module }}"
else
echo "unknown target: {{ target }}" >&2
exit 1
fi
[private]
build-c-host-fail module:
{{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \
-c targets/c-host/fail.c \
-o {{ BUILD_DIR }}-{{ module }}/c_host.o
[private]
build-c-host-linux module:
{{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \
-c targets/c-host/linux.c \
-o {{ BUILD_DIR }}-{{ module }}/c_host.o
[doc("Insert the C function into the host program (no WASM)")]
[group("2: build host")]
build-c-host module target="fail":
#!/usr/bin/env sh
if [ "{{ target }}" = "fail" ]; then
just build-c-host-fail "{{ module }}"
elif [ "{{ target }}" = "linux" ]; then
just build-c-host-linux "{{ module }}"
else
echo "unknown target: {{ target }}" >&2
exit 1
fi
[private]
build-system-startup-fail module:
{{ CROSS_CC }} targets/startup.s {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/startup.o
[doc("Compile bootloader")]
[group("2: build host")]
build-system-startup module target="fail":
#!/usr/bin/env sh
if [ "{{ target }}" = "fail" ]; then
just build-system-startup-fail "{{ module }}"
else
echo "{{ target }} doesn't need bootloader"
fi
[private]
build-system-syscalls-fail module:
{{ CROSS_CC }} targets/syscalls.c {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/syscalls.o
[private]
build-system-syscalls-linux-baremetal module:
{{ CROSS_CC }} targets/syscalls.c {{ LINUX_BAREMETAL_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/syscalls.o
[doc("Compile newlib syscall stubs")]
[group("2: build host")]
build-system-syscalls module target="fail":
#!/usr/bin/env sh
if [ "{{ target }}" = "fail" ]; then
just build-system-syscalls-fail "{{ module }}"
elif [ "{{ target }}" = "linux-baremetal" ]; then
just build-system-syscalls-linux-baremetal "{{ module }}"
else
echo "{{ target }} doesn't require syscall stubs"
fi
[private]
link-system-fail module:
{{ CROSS_CC }} \
-Wl,-T targets/linker.ld \
{{ BUILD_DIR }}-{{ module }}/system.o \
{{ BUILD_DIR }}-{{ module }}/startup.o \
{{ BUILD_DIR }}-{{ module }}/syscalls.o \
{{ CROSS_LDFLAGS }} \
-o {{ BUILD_DIR }}-{{ module }}/system.elf
[private]
link-system-linux module:
{{ LINUX_CC }} \
{{ BUILD_DIR }}-{{ module }}/system.o \
{{ LINUX_LDFLAGS }} \
-o {{ BUILD_DIR }}-{{ module }}/system.elf
[private]
link-system-linux-baremetal module:
{{ CROSS_CC }} \
{{ BUILD_DIR }}-{{ module }}/system.o \
{{ BUILD_DIR }}-{{ module }}/syscalls.o \
{{ LINUX_BAREMETAL_LDFLAGS }} \
-o {{ BUILD_DIR }}-{{ module }}/system.elf
[doc("Link C-Host, syscall stubs and bootloader")]
[group("2: build host")]
link-system module target="fail":
#!/usr/bin/env sh
if [ "{{ target }}" = "fail" ]; then
just link-system-fail "{{ module }}"
elif [ "{{ target }}" = "linux" ]; then
just link-system-linux "{{ module }}"
elif [ "{{ target }}" = "linux-baremetal" ]; then
just link-system-linux-baremetal "{{ module }}"
else
echo "unknown target: {{ target }}" >&2
exit 1
fi
[private]
link-c-system-fail module:
{{ CROSS_CC }} \
-Wl,-T targets/linker.ld \
{{ BUILD_DIR }}-{{ module }}/c_host.o \
{{ BUILD_DIR }}-{{ module }}/startup.o \
{{ BUILD_DIR }}-{{ module }}/c_module.o \
{{ CROSS_LDFLAGS_NOWASM }} \
-o {{ BUILD_DIR }}-{{ module }}/system.elf
[private]
link-c-system-linux module:
{{ LINUX_CC }} \
{{ BUILD_DIR }}-{{ module }}/c_host.o \
{{ BUILD_DIR }}-{{ module }}/c_module.o \
{{ LINUX_LDFLAGS_NOWASM }} \
-o {{ BUILD_DIR }}-{{ module }}/system.elf
[doc("Link C-Host, C-function and bootloader")]
[group("2: build host")]
link-c-system module target="fail":
#!/usr/bin/env sh
if [ "{{ target }}" = "fail" ]; then
just link-c-system-fail "{{ module }}"
elif [ "{{ target }}" = "linux" ]; then
just link-c-system-linux "{{ module }}"
else
echo "unknown target: {{ target }}" >&2
exit 1
fi
[doc("Create bootdisk")]
[group("2: build host")]
build-iso module:
mkdir -p {{ BUILD_DIR }}-{{ module }}/grub/boot/grub
cp targets/grub.cfg {{ BUILD_DIR }}-{{ module }}/grub/boot/grub/
cp {{ BUILD_DIR }}-{{ module }}/system.elf {{ BUILD_DIR }}-{{ module }}/grub/boot/
grub-mkrescue -o {{ BUILD_DIR }}-{{ module }}/system.iso {{ BUILD_DIR }}-{{ module }}/grub