#DEBUG = 1	# or include DEBUG=1 in the command line
MESSAGES = -fmessage-length=0

export DEBUG
export MESSAGES

TARGET_ARCH = -mcpu=cortex-m3 -mthumb

INCLUDE_DIRS = -I . -I inc -I fwlib -I fwlib/inc
LIBRARY_DIRS = -L tadd-stm32 -L fwlib

LINK_FWLIB = $(FWLIB)	# comment this if not using the firmware library

COMPILE_OPTS = $(WARNINGS) $(TARGET_OPTS) $(MESSAGES) $(INCLUDE_DIRS)
WARNINGS = -Wall -W $(W_CONVERSION) -Wshadow -Wcast-qual -Wwrite-strings -Winline
W_CONVERSION = -Wconversion

WARNINGS_CXX = -Weffc++ $(W_OLD_STYLE_CAST)
W_OLD_STYLE_CAST = -Wold-style-cast
F_NO_EXCEPTIONS = -fno-exceptions	# disabling exceptions saves code space

ifdef DEBUG
 TARGET_OPTS = -O0 -g3
 DEBUG_MACRO = -DDEBUG
else
 TARGET_OPTS = -Os #$(F_INLINE) $(F_INLINE_ONCE) $(F_UNROLL_LOOPS)
 F_INLINE = -finline
 F_INLINE_ONCE = -finline-functions-called-once
 F_UNROLL_LOOPS = -funroll-loops
endif

CPPFLAGS = $(DEBUG_MACRO)

CC = arm-none-eabi-gcc
CFLAGS = -std=gnu99 $(COMPILE_OPTS)

CXX = arm-none-eabi-g++
CXXFLAGS = -std=gnu++98 $(COMPILE_OPTS) $(WARNINGS_CXX) $(F_NO_EXCEPTIONS)

AS = $(CC) -x assembler-with-cpp -c $(TARGET_ARCH)
ASFLAGS = $(COMPILE_OPTS)

#LD = $(CC)
LD = $(CXX)
LDFLAGS = -Wl,--gc-sections,-Map=$(MAIN_MAP),-cref -T tadd-stm32.ld $(INCLUDE_DIRS) $(LIBRARY_DIRS) -ffunction-sections -fdata-sections

AR = arm-none-eabi-ar
ARFLAGS = cr

OBJCOPY = arm-none-eabi-objcopy
OBJCOPYFLAGS = -O binary

SIZE = arm-none-eabi-size

# Display size of file
HEXSIZE = $(SIZE) --target=$(MAIN_OUT)
ELFSIZE = $(SIZE) -A $(MAIN_OUT)

size:
	$(ELFSIZE)

flash_elf:
	@cp $(MAIN_OUT) jtag/main.elf

MAIN_OUT = main.elf
MAIN_MAP = $(MAIN_OUT:%.elf=%.map)
MAIN_BIN = $(MAIN_OUT:%.elf=%.bin)

MAIN_OBJS = $(sort \
 $(patsubst %.cpp,%.o,$(wildcard *.cpp)) \
 $(patsubst %.cc,%.o,$(wildcard *.cc)) \
 $(patsubst %.c,%.o,$(wildcard *.c)) \
 $(patsubst %.s,%.o,$(wildcard *.s)))\
 fwlib/src/stm32f10x_rcc.c\
 fwlib/src/stm32f10x_flash.c\
 fwlib/src/stm32f10x_nvic.c\
 fwlib/src/cortexm3_macro.s\
 fwlib/src/stm32f10x_adc.c\
 fwlib/src/stm32f10x_bkp.c\
 fwlib/src/stm32f10x_can.c\
 fwlib/src/stm32f10x_dma.c\
 fwlib/src/stm32f10x_exti.c\
 fwlib/src/stm32f10x_gpio.c\
 fwlib/src/stm32f10x_i2c.c\
 fwlib/src/stm32f10x_iwdg.c\
 fwlib/src/stm32f10x_lib.c\
 fwlib/src/stm32f10x_pwr.c\
 fwlib/src/stm32f10x_rtc.c\
 fwlib/src/stm32f10x_spi.c\
 fwlib/src/stm32f10x_systick.c\
 fwlib/src/stm32f10x_tim1.c\
 fwlib/src/stm32f10x_tim.c\
 fwlib/src/stm32f10x_usart.c\
 fwlib/src/stm32f10x_wwdg.c
 
FWLIB = fwlib/libstm32fw.a

# all

.PHONY: all
all: $(MAIN_BIN) size flash_elf 
# main

$(MAIN_OUT): $(MAIN_OBJS) $(LINK_FWLIB)
	$(LD) $(LDFLAGS) $(TARGET_ARCH) $^ -o $@

$(MAIN_OBJS): $(wildcard *.h) $(wildcard fwlib/*.h) $(wildcard fwlib/inc/*.h)

$(MAIN_BIN): $(MAIN_OUT)
	$(OBJCOPY) $(OBJCOPYFLAGS) $< $@


# fwlib

.PHONY: fwlib
fwlib: $(FWLIB)

$(FWLIB): $(wildcard fwlib/*.h) $(wildcard fwlib/inc/*.h)
	@cd fwlib && $(MAKE)

# clean

.PHONY: clean
clean:
	-rm -f $(MAIN_OUT) $(MAIN_MAP) $(MAIN_BIN) jtag/main.elf
	@cd fwlib && $(MAKE) clean
