build.sh (2636B)
1 #!/bin/sh 2 3 TARGET="${1:-atmega2560}" 4 5 # toolchain 6 AS="clang" 7 CC="clang" 8 CXX="clang++" 9 LD="ld.lld" 10 11 OBJCOPY="llvm-objcopy" 12 13 # toolchain flags 14 WARNINGS="-Wall -Wextra ${WERROR:+-Werror} -Wno-unused-parameter" 15 FLAGS="-ffreestanding -fno-builtin" 16 17 # generic language flags 18 ASFLAGS="-g3" 19 CFLAGS="-std=c11 -O${OPT:-0} -g3" 20 CXXFLAGS="-std=c++11 -O${OPT:-0} -g3" 21 22 CPPFLAGS="-Idep" 23 LDFLAGS="-nostdlib" 24 25 # target-specific toolchain flags 26 # -- 27 # see: clang -target avr-none --print-supported-cpus 28 # see: clang -target arm-none-eabi --print-supported-cpus 29 # see: clang -target arm-none-eabi --print-supported-extensions 30 # 31 # see: https://gcc.gnu.org/onlinedocs/gcc/AVR-Options.html 32 # see: https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html 33 case $TARGET in 34 atmega2560) # arduino mega 35 TRIPLE="avr-none" 36 TARGET_FLAGS="-mmcu=atmega2560" 37 38 ARCH="AVR" 39 PLATFORM="ATMEGA2560" 40 41 BOARD="boards/atmega2560" 42 LDSCRIPT="atmega2560.lds" 43 ;; 44 45 nucleo) # stm nucleo 46 TRIPLE="arm-none-eabi" 47 TARGET_FLAGS="-mcpu=cortex-m4 -march=armv7e-m+fp" 48 49 ARCH="ARM" 50 PLATFORM="NUCLEO" 51 52 BOARD="boards/stm32f401re" 53 LDSCRIPT="stm32f401re.lds" 54 ;; 55 56 nucleo-64) # stm nucleo-64 57 TRIPLE="arm-none-eabi" 58 TARGET_FLAGS="-mcpu=cortex-m3 -march=armv7-m" 59 60 ARCH="ARM" 61 PLATFORM="NUCLEO64" 62 63 BOARD="boards/stm32l152re" 64 LDSCRIPT="stm32l152re.lds" 65 ;; 66 67 68 nucleo-144) # stm32 nucleo-144 69 TRIPLE="arm-none-eabi" 70 TARGET_FLAGS="-mcpu=cortex-m4 -march=armv7e-m+fp" 71 72 ARCH="ARM" 73 PLATFORM="NUCLEO144" 74 75 BOARD="boards/stm32l4r5zi" 76 LDSCRIPT="stm32l4r5zi.lds" 77 ;; 78 79 *) 80 echo "Unsupported target: $TARGET, must be one of: " 81 echo "\tatmega2560 nucleo nucleo-64 nucleo-144" 82 exit 1 83 ;; 84 esac 85 86 # target-specific language flags 87 ARCH_ASFLAGS="-target $TRIPLE $TARGET_FLAGS $ASFLAGS $FLAGS" 88 ARCH_CFLAGS="-target $TRIPLE $TARGET_FLAGS $CFLAGS $FLAGS" 89 ARCH_CXXFLAGS="-target $TRIPLE $TARGET_FLAGS $CXXFLAGS $FLAGS" 90 91 ARCH_CPPFLAGS="$CPPFLAGS -D__ARCH_${ARCH}__=1 -D__PLATFORM_${PLATFORM}__=1" 92 ARCH_LDFLAGS="-T $BOARD/$LDSCRIPT $LDFLAGS" 93 94 set -ex 95 96 mkdir -p bin obj 97 98 # build board-specific asm platform code and HAL implementation 99 $AS $ARCH_ASFLAGS $ARCH_CPPFLAGS -o obj/plat.o -c $BOARD/plat.S -I$BOARD 100 $CC $ARCH_CFLAGS $ARCH_CPPFLAGS -o obj/hal.o -c $BOARD/hal.c -I$BOARD 101 102 # build target-independent code 103 $CXX $ARCH_CXXFLAGS $ARCH_CPPFLAGS -o obj/gp24.o -c src/gp24.cpp 104 105 # link together asm helpers, HAL, and main code 106 $LD $ARCH_LDFLAGS --Map=bin/gp24.map --print-memory-usage \ 107 obj/plat.o obj/hal.o obj/gp24.o -o bin/gp24.elf 108 109 # prepare binary for flashing to target 110 $OBJCOPY -S -O binary bin/gp24.elf bin/gp24.bin 111 $OBJCOPY -S -O ihex bin/gp24.elf bin/gp24.hex