#!/bin/sh
set -eu
A100_ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
A100_OUT="$A100_ROOT/build"
mkdir -p "$A100_OUT"
A100_FLAGS='-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DSTM32G431xx -std=c11 -Os -g3 -ffreestanding -fno-builtin -fno-common -ffunction-sections -fdata-sections -Wall -Wextra -Werror'
for source in runtime drill_safe_gpio drill_clock drill_smo motor_foc target_diag_main; do
 arm-none-eabi-gcc $A100_FLAGS -I"$A100_ROOT" -I"$A100_ROOT/Device/Include" -I"$A100_ROOT/CMSIS" -c "$A100_ROOT/$source.c" -o "$A100_OUT/$source.o"
done
arm-none-eabi-gcc $A100_FLAGS -I"$A100_ROOT/Device/Include" -I"$A100_ROOT/CMSIS" -c "$A100_ROOT/Device/Source/Templates/system_stm32g4xx.c" -o "$A100_OUT/system.o"
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -x assembler-with-cpp -c "$A100_ROOT/startup/startup_stm32g431_minimal.s" -o "$A100_OUT/startup.o"
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -nostdlib -T "$A100_ROOT/linker/stm32g431cb.ld" -Wl,-Map="$A100_OUT/drill_compute_A100.map",--cref,--build-id=none,--no-undefined,--gc-sections,--print-memory-usage -Wl,-z,noexecstack "$A100_OUT/startup.o" "$A100_OUT/system.o" "$A100_OUT/runtime.o" "$A100_OUT/drill_safe_gpio.o" "$A100_OUT/drill_clock.o" "$A100_OUT/drill_smo.o" "$A100_OUT/motor_foc.o" "$A100_OUT/target_diag_main.o" -lgcc -o "$A100_OUT/drill_compute_A100.elf"
arm-none-eabi-objcopy -O binary "$A100_OUT/drill_compute_A100.elf" "$A100_OUT/drill_compute_A100.bin"
arm-none-eabi-size "$A100_OUT/drill_compute_A100.elf" > "$A100_OUT/size.txt"
arm-none-eabi-objdump -d "$A100_OUT/drill_compute_A100.elf" > "$A100_OUT/disassembly.txt"
