From 347dcc040d2d1ab24c5d48a25c1fc8511d8a3de8 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Thu, 12 Mar 2026 14:06:45 +0100 Subject: [PATCH] use shebang recipes for conditional dependencies instead of inline templating --- justfile | 125 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 102 insertions(+), 23 deletions(-) diff --git a/justfile b/justfile index dff1079..8576377 100644 --- a/justfile +++ b/justfile @@ -157,9 +157,41 @@ LINUX_BAREMETAL_INCLUDES := f"\ # TODO: Grave mistake to template the commands depending on the target... Should've used shebang recipes instead +[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 + +[private] +build-wasm-host-dispatch 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 + [doc("Compile C-Host: The host uses WAMR to load the AOT module")] [group("build-host")] -build-wasm-host module target="fail": (build-wasm-aot-array module) +build-wasm-host module target="fail": (build-wasm-aot-array module) && (build-wasm-host-dispatch module target) cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c sed -i \ -e "s/__WASM_ARRAY_FILE__/wasm_module_array.c/g" \ @@ -167,37 +199,79 @@ build-wasm-host module target="fail": (build-wasm-aot-array module) -e "s/__WASM_ARRAY_LEN__/build_{{ module }}_wasm_module_aot_len/g" \ {{ BUILD_DIR }}-{{ module }}/module_host.c - # Build depending on the target platform - {{ if target == "fail" { CROSS_CC + " " + CROSS_CFLAGS + " " + CROSS_INCLUDES } else if target == "linux" { LINUX_CC + " " + LINUX_CFLAGS + " " + LINUX_INCLUDES } else if target == "linux-baremetal" { CROSS_CC + " " + LINUX_BAREMETAL_CFLAGS + " " + LINUX_BAREMETAL_INCLUDES } else { error("invalid target") } }} \ - -c {{ BUILD_DIR }}-{{ module }}/module_host.c \ - -o {{ BUILD_DIR }}-{{ module }}/system.o +[private] +build-system-startup-fail module: + {{ CROSS_CC }} targets/startup.s {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/startup.o [doc("Compile bootloader")] [group("build-host")] build-system-startup module target="fail": - {{ if target == "fail" { CROSS_CC } else if target == "linux" { LINUX_CC } else if target == "linux-baremetal" { CROSS_CC } else { error("invalid target") } }} \ - targets/startup.s \ - {{ if target == "fail" { CROSS_CFLAGS } else if target == "linux" { LINUX_CFLAGS } else if target == "linux-baremetal" { LINUX_BAREMETAL_CFLAGS } else { error("invalid target") } }} \ - -c -o {{ BUILD_DIR }}-{{ module }}/startup.o + #!/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("build-host")] build-system-syscalls module target="fail": - {{ if target == "fail" { CROSS_CC } else if target == "linux" { LINUX_CC } else if target == "linux-baremetal" { CROSS_CC } else { error("invalid target") } }} \ - targets/syscalls.c \ - {{ if target == "fail" { CROSS_CFLAGS } else if target == "linux" { LINUX_CFLAGS } else if target == "linux-baremetal" { LINUX_BAREMETAL_CFLAGS } else { error("invalid target") } }} \ - -c -o {{ BUILD_DIR }}-{{ module }}/syscalls.o + #!/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("build-host")] link-system module target="fail": (build-wasm-host module target) (build-system-syscalls module target) (build-system-startup module target) - {{ if target == "fail" { CROSS_CC } else if target == "linux" { LINUX_CC } else if target == "linux-baremetal" { CROSS_CC } else { error("invalid target") } }} \ - {{ if target == "fail" { "-Wl,-T targets/linker.ld" } else { "" } }} \ - {{ BUILD_DIR }}-{{ module }}/system.o \ - {{ if target == "fail" { f"{{BUILD_DIR}}-{{module}}/syscalls.o" + " " + f"{{BUILD_DIR}}-{{module}}/startup.o" } else { "" } }} \ - {{ if target == "linux-baremetal" { f"{{BUILD_DIR}}-{{module}}/syscalls.o" } else { "" } }} \ - {{ if target == "fail" { CROSS_LDFLAGS } else if target == "linux" { LINUX_LDFLAGS } else if target == "linux-baremetal" { LINUX_BAREMETAL_LDFLAGS } else { error("invalid target") } }} \ - -o {{ BUILD_DIR }}-{{ module }}/system.elf + #!/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 [doc("Create bootdisk")] [group("build-host")] @@ -363,12 +437,17 @@ resultbrowser: # Debugging recipes # =================================================================================================================== # -[doc("Launch radare2 at os_main")] +[doc("Run binary")] [group("debug")] -radare module: - radare2 -AA -c "s dbg.os_main; pdf" {{ BUILD_DIR }}-{{ module }}/system.elf +run module: + {{ BUILD_DIR }}-{{ module }}/system.elf [doc("Launch gdb")] [group("debug")] gdb module: gdb --tui {{ BUILD_DIR }}-{{ module }}/system.elf + +[doc("Launch radare2 at os_main")] +[group("debug")] +radare module: + radare2 -AA -c "s dbg.os_main; pdf" {{ BUILD_DIR }}-{{ module }}/system.elf