include Makefile.common

#ProjectPath PP
PP=$(TOP)/STM32F4-Discovery_FW_V1.1.0/Project/Peripheral_Examples/DAC_SignalsGeneration/
#DiscoveryPath header DP
DP=$(TOP)/STM32F4-Discovery_FW_V1.1.0/Utilities/STM32F4-Discovery/
#StartUpPath SUP
SUP=$(TOP)/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/gcc_ride7/
PROGRAM = main

#LDLIBS+=-lm
LDLIBS+=-lstm32
LDLIBS+=-lstm32Discovery

LDFLAGS=$(COMMONFLAGS) -fno-exceptions -ffunction-sections -fdata-sections -L$(LIBDIR) -nostartfiles -Wl,--gc-sections,-Map=$(PP)$(PROGRAM).map,-T$(LINKFILE)

all: libstm32.a libstm32Discovery.a
	@echo -n "Building DAC_SignalsGeneration  $@ ... "
	@echo
	@echo -n "Top:" $(TOP)
	@echo
	@echo -n "PP:" $(PP)
	@echo
	$(CC) $(CFLAGS) -o $(PP)$(PROGRAM).elf $(PP)$(PROGRAM).c $(LDFLAGS) \
		-Wl,--no-whole-archive $(LDLIBS)
	$(OBJCOPY) -O ihex   $(PP)$(PROGRAM).elf $(PP)$(PROGRAM).hex
	$(OBJCOPY) -O binary $(PP)$(PROGRAM).elf $(PP)$(PROGRAM).bin
	$(OBJDUMP) $(ODFLAGS) $(PP)$(PROGRAM).elf > $(PP)$(PROGRAM).lst
	cp $(PP)$(PROGRAM).{bin,hex,lst,elf,map} $(TOP)
	arm-none-eabi-size $(PROGRAM).elf
	@echo -n "Program build done."
	@echo
	

libstm32.a:
	@echo -n "Building $@ ... "
	@cd $(STMLIB)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/gcc_ride7/ && \
	$(CC) $(CFLAGS) -x assembler-with-cpp -c $(COMMONFLAGS) \
		-Wa,-amhls=startup_stm32f4xx.lst startup_stm32f4xx.s -o startup_stm32f4xx.o
	@cd $(STMLIB)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates && \
		$(CC) $(CFLAGS) -c \
			system_stm32f4xx.c
	@cd $(STMLIB)/Libraries/STM32F4xx_StdPeriph_Driver/src && \
		$(CC) $(CFLAGS) -c \
			-D"assert_param(expr)=((void)0)" \
			-I../inc *.c
	@$(AR) cr $(LIBDIR)/$@ \
		$(STMLIB)/Libraries/STM32F4xx_StdPeriph_Driver/src/*.o \
		$(STMLIB)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/gcc_ride7/startup_stm32f4xx.o \
		$(STMLIB)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/*.o
	@echo "done."

libstm32Discovery.a:
	@echo -n "Building $@ ... "
	@cd $(STMLIB)/Utilities/STM32F4-Discovery && \
		$(CC) $(CFLAGS) -c \
			-D"assert_param(expr)=((void)0)" \
			-I$(STMLIB)/Libraries/STM32F4xx_StdPeriph_Driver/inc \
			*.c
	@$(AR) cr $(LIBDIR)/$@ \
		$(STMLIB)/Utilities/STM32F4-Discovery/*.o \
		$(STMLIB)/Libraries/STM32F4xx_StdPeriph_Driver/src/*.o
	@echo "done."

.PHONY: libstm32.a libstm32Discovery.a clean

clean:
	rm -f $(PP)$(PROGRAM).{elf,hex,bin,lst,map}
	rm -f $(PP)*.o
	rm -f *.{a,bin,hex,lst,elf,map}
	rm -f $(STMLIB)/Utilities/STM32F4-Discovery/*.o
	rm -f $(STMLIB)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/*.o
	rm -f $(STMLIB)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/gcc_ride7/*.{o,lst}
	rm -f $(STMLIB)/Libraries/STM32F4xx_StdPeriph_Driver/src/*.o
