# common include for all STM32 chips
BOOTPORT	?= /dev/ttyUSB0
BOOTSPEED	?= 115200

ARCH_FLAGS	= $(ASM_FLAGS) $(FP_FLAGS) $(DEFINES)

###############################################################################
# Executables
# PREFIX		?= arm-none-eabi
# gcc from arm web site 
PREFIX		?= /opt/bin/arm-none-eabi
#TOOLCHLIB	?= /opt/arm-none-eabi/lib
RM			:= rm -f
RMDIR		:= rmdir
CC			:= $(PREFIX)-gcc
# don't replace ld with gcc: the binary size would be much greater!!
LD			:= $(PREFIX)-gcc
AR			:= $(PREFIX)-ar
AS			:= $(PREFIX)-as
SIZE 		:= $(PREFIX)-size
OBJCOPY		:= $(PREFIX)-objcopy
OBJDUMP		:= $(PREFIX)-objdump
GDB			:= $(PREFIX)-gdb
STFLASH		:= $(shell which st-flash)
STBOOT		:= $(shell which stm32flash)
DFUUTIL		:= $(shell which dfu-util)

###############################################################################
# Source files
OBJDIR 		:= mk
# target (debug/release)
TARGFILE := $(OBJDIR)/TARGET
# autoincremental version & build date
VERSION_FILE = version.inc
SRC			:= $(wildcard *.c)
OBJS		:= $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o))
STARTUP		:= $(OBJDIR)/startup.o
MAP			:= $(OBJDIR)/$(BINARY).map
OBJS 		+= $(STARTUP)
# dependencies: we need them to recompile files if their headers-dependencies changed
DEPS		:= $(OBJS:.o=.d)

INC_DIR ?= ../inc

INCLUDE 	:= -I$(INC_DIR)/Fx -I$(INC_DIR)/cm
LIB_DIR		:= $(INC_DIR)/ld

###############################################################################
# C flags
CFLAGS		+= -D__thumb2__=1 -MD
CFLAGS		+= -Wall -Wextra -Wshadow -Wdouble-promotion
CFLAGS		+= -fshort-enums -ffunction-sections -fdata-sections
#CFLAGS		+= -fno-common -fno-stack-protector
CFLAGS		+=  $(ARCH_FLAGS)

###############################################################################
# Linker flags
LDFLAGS		+= -nostartfiles --static -specs=nosys.specs -specs=nano.specs
LDFLAGS		+= $(ARCH_FLAGS)
LDFLAGS		+= -L$(LIB_DIR) 
#-L$(TOOLCHLIB)
LDFLAGS		+= -T$(LDSCRIPT)
LDFLAGS		+= -Wl,-Map=$(MAP),--cref -Wl,--gc-sections -Wl,--print-memory-usage

###############################################################################
# Used libraries
LDLIBS		+= -lc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)

DEFS		+= -DSTM32$(FAMILY) -DSTM32$(MCU)

ELF		:= $(OBJDIR)/$(BINARY).elf
LIST	:= $(OBJDIR)/$(BINARY).list
BIN		:= $(BINARY).bin
HEX		:= $(BINARY).hex

ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes)
	TARGET := $(file < $(TARGFILE))
else
	TARGET := RELEASE
endif

ifeq ($(TARGET), DEBUG)
	.DEFAULT_GOAL := debug
endif

ifeq ($(shell test -e $(VERSION_FILE) && echo -n yes), yes)
    NEXTVER := $(shell expr $$(awk '/#define BUILD_NUMBER/' $(VERSION_FILE) | tr -cd "[0-9]") + 1)
else
    NEXTVER := "1"
endif
BUILDDATE := $(shell date +%Y-%m-%d)

# release: add LTO
release: CFLAGS += -O2 -flto
release: LDFLAGS += -flto
release: $(TARGFILE) bin list size

#debug: add debug flags
debug: CFLAGS += -O0 -DEBUG -Werror -g3 -gdwarf-2
debug: TARGET := DEBUG
debug: $(TARGFILE) bin list size

$(TARGFILE): $(OBJDIR)
	@echo -e "\t\tTARGET: $(TARGET)"
	@echo "$(TARGET)" > $(TARGFILE)

elf: $(ELF)
bin: $(BIN)
hex: $(HEX)
list: $(LIST)

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif

$(OBJDIR):
	mkdir $(OBJDIR)

$(STARTUP): $(INC_DIR)/startup/vector.c
	$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<

$(VERSION_FILE): *.[ch]
	@[ -f $(VERSION_FILE) ] || echo -e "#define BUILD_NUMBER \"0\"\n#define BUILD_DATE \"none\"" > $(VERSION_FILE)
	@echo "  Generate version: $(NEXTVER) for date $(BUILDDATE)"
	@sed -i "s/#define BUILD_NUMBER.*/#define BUILD_NUMBER \"$(NEXTVER)\"/" $(VERSION_FILE)
	@sed -i "s/#define BUILDNO.*/#define BUILDNO $(NEXTVER)/" $(VERSION_FILE)
	@sed -i "s/#define BUILD_DATE.*/#define BUILD_DATE \"$(BUILDDATE)\"/" $(VERSION_FILE)

$(OBJDIR)/proto.o: proto.c $(VERSION_FILE)

$(OBJDIR)/%.o: %.c
	@echo "  CC      $<"
	$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<

$(BIN): $(ELF)
	@echo "  OBJCOPY $(BIN)"
	$(OBJCOPY) -Obinary $(ELF) $(BIN)

$(HEX): $(ELF)
	@echo "  OBJCOPY $(HEX)"
	$(OBJCOPY) -Oihex $(ELF) $(HEX)

$(LIST): $(ELF)
	@echo "  OBJDUMP $(LIST)"
	$(OBJDUMP) -S $(ELF) > $(LIST)

$(ELF): $(OBJDIR) $(OBJS)
	@echo "  LD      $(ELF)"
	$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF) $(LDADD)

size: $(ELF)
	$(SIZE) $(ELF)    

clean:
	@echo "  CLEAN"
	@rm -rf $(OBJDIR) 2>/dev/null || true


flash: $(BIN)
	@echo "  FLASH  $(BIN)"
	$(STFLASH) write $(BIN) 0x8000000
	$(STFLASH) reset

boot: $(BIN)
	@echo "  LOAD $(BIN) through bootloader"
	$(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN)

dfuboot: $(BIN)
	@echo "  LOAD  $(BIN) THROUGH DFU"
	$(DFUUTIL) -a0 -D $(BIN) -s 0x08000000

openocd:
	openocd -f openocd.cfg

dbg:
	arm-none-eabi-gdb $(ELF) -ex 'target remote localhost:3333' -ex 'monitor reset halt'

.PHONY: size clean flash boot dfuboot openocd dbg
