PROJECT=main

# Use newlib-nano. To disable it, specify USE_NANO=
USE_NANO=--specs=nano.specs

# Use semihosting 
USE_SEMIHOST=--specs=rdimon.specs

# Use retarget
# USE_SEMIHOST=--specs=nosys.specs

# Compiler & Linker
CC=arm-none-eabi-gcc
AR=arm-none-eabi-ar
AS=arm-none-eabi-as
LD=arm-none-eabi-ld
SIZE=arm-none-eabi-size
OBJCOPY=arm-none-eabi-objcopy
OBJDUMP=arm-none-eabi-objdump

LDSCRIPT=STM32L432KCUx_FLASH.ld

INCLUDES = \
	-I../Include \
	-I../../../../Include \
	-I../../../../../STM32L4xx_HAL_Driver/Inc

SOURCES = main.c \
	system_stm32l4xx.c \
	startup_stm32l432xx.s \

# Options for specific architecture
ARCH_FLAGS=-mthumb -mcpu=cortex-m4

# -Os -flto -ffunction-sections -fdata-sections to compile for code size
# CFLAGS=$(ARCH_FLAGS) $(INCLUDES) -Os -flto -ffunction-sections -fdata-sections
CFLAGS=$(ARCH_FLAGS) $(INCLUDES) -g -flto -ffunction-sections -fdata-sections

# Link for code size and create a map file
# If not using semihosting/retarget:
# LFLAGS=$(USE_NANO) $(USE_SEMIHOST) -nostartfiles -Wl,--gc-sections,-Map=$(PROJECT).map
# Else:
LFLAGS=$(USE_NANO) $(USE_SEMIHOST) -Wl,--gc-sections,-Map=$(PROJECT).map -T $(LDSCRIPT)

all: $(PROJECT).bin $(PROJECT).dis $(PROJECT).elf

# Build ELF w/ symbols
$(PROJECT).elf: $(SOURCES)
	$(CC) $^ $(CFLAGS) $(LFLAGS) -o $@

# Generate binary to flash
$(PROJECT).bin: $(PROJECT).elf
	$(OBJCOPY) -O binary $< $@

# Export dissassembly of binary
$(PROJECT).dis: $(PROJECT).elf	
	$(OBJDUMP) -S -d -marm $< -Mforce-thumb > $@

clean: 
	rm -f *.elf *.bin *.map *.dis *.o
