ifeq ($(DEBUG),y)
	OPTFLAGCL:=-Og -ffunction-sections -fdata-sections
	OPTFLAGLD:=-Wl,--gc-sections
	BUILDDIR=Debug
else
	OPTFLAGCL:=-Os -ffunction-sections -fdata-sections -flto
	OPTFLAGLD:=-Wl,--gc-sections -flto
	BUILDDIR=Release
	MACROS:=NDEBUG
endif

INCLUDES:=include src utils system/src/newlib system/src/cmsis system/src/stm32f4-hal system/src/cortexm system/src/diag system/lwip/src/api system/lwip/src/core system/lwip/src/netif system/lwip/src/include/lwip system/lwip/src/include/ system/include/stm32f4-hal system/include/cmsis system/include system/lwip/system/ /mnt/gcc-arm-none-eabi-8-2018-q4-major/arm-none-eabi/include system/lwip/src/include/ipv4
MACROS+=STM32F437xx
ARCHFLAGS:=-mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16
CFLAGS:=-Wall -Wextra -std=gnu11 $(addprefix -I,$(INCLUDES)) $(addprefix -D,$(MACROS))
CXXFLAGS:=-Wall -Wextra -std=c++14 $(addprefix -I,$(INCLUDES)) $(addprefix -D,$(MACROS))
LDFLAGS:=-T ldscripts/mem.ld -T ldscripts/sections.ld -nostartfiles
PREFIX:=arm-none-eabi-

# rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

C_SOURCES:=src/_initialize_hardware.c src/lwip.c src/ethernetif.c src/eeprom.c src/board_configuration.c system/src/stm32f4-hal/stm32f4xx_ll_usb.c system/src/stm32f4-hal/stm32f4xx_ll_sdmmc.c system/src/stm32f4-hal/stm32f4xx_ll_fsmc.c system/src/stm32f4-hal/stm32f4xx_ll_fmc.c system/src/stm32f4-hal/stm32f4xx_hal_wwdg.c system/src/stm32f4-hal/stm32f4xx_hal_usart.c system/src/stm32f4-hal/stm32f4xx_hal_uart.c system/src/stm32f4-hal/stm32f4xx_hal_tim_ex.c system/src/stm32f4-hal/stm32f4xx_hal_tim.c system/src/stm32f4-hal/stm32f4xx_hal_sram.c system/src/stm32f4-hal/stm32f4xx_hal_spi.c system/src/stm32f4-hal/stm32f4xx_hal_smartcard.c system/src/stm32f4-hal/stm32f4xx_hal_sdram.c system/src/stm32f4-hal/stm32f4xx_hal_sd.c system/src/stm32f4-hal/stm32f4xx_hal_sai.c system/src/stm32f4-hal/stm32f4xx_hal_rtc_ex.c system/src/stm32f4-hal/stm32f4xx_hal_rtc.c system/src/stm32f4-hal/stm32f4xx_hal_rng.c system/src/stm32f4-hal/stm32f4xx_hal_rcc_ex.c system/src/stm32f4-hal/stm32f4xx_hal_rcc.c system/src/stm32f4-hal/stm32f4xx_hal_pwr_ex.c system/src/stm32f4-hal/stm32f4xx_hal_pwr.c system/src/stm32f4-hal/stm32f4xx_hal_pcd_ex.c system/src/stm32f4-hal/stm32f4xx_hal_pcd.c system/src/stm32f4-hal/stm32f4xx_hal_pccard.c system/src/stm32f4-hal/stm32f4xx_hal_nor.c system/src/stm32f4-hal/stm32f4xx_hal_nand.c system/src/stm32f4-hal/stm32f4xx_hal_msp_template.c system/src/stm32f4-hal/stm32f4xx_hal_ltdc.c system/src/stm32f4-hal/stm32f4xx_hal_iwdg.c system/src/stm32f4-hal/stm32f4xx_hal_irda.c system/src/stm32f4-hal/stm32f4xx_hal_i2s_ex.c system/src/stm32f4-hal/stm32f4xx_hal_i2s.c system/src/stm32f4-hal/stm32f4xx_hal_i2c_ex.c system/src/stm32f4-hal/stm32f4xx_hal_i2c.c system/src/stm32f4-hal/stm32f4xx_hal_hcd.c system/src/stm32f4-hal/stm32f4xx_hal_hash_ex.c system/src/stm32f4-hal/stm32f4xx_hal_hash.c system/src/stm32f4-hal/stm32f4xx_hal_gpio.c system/src/stm32f4-hal/stm32f4xx_hal_flash_ramfunc.c system/src/stm32f4-hal/stm32f4xx_hal_flash_ex.c system/src/stm32f4-hal/stm32f4xx_hal_flash.c system/src/stm32f4-hal/stm32f4xx_hal_eth.c system/src/stm32f4-hal/stm32f4xx_hal_dma_ex.c system/src/stm32f4-hal/stm32f4xx_hal_dma2d.c system/src/stm32f4-hal/stm32f4xx_hal_dma.c system/src/stm32f4-hal/stm32f4xx_hal_dcmi.c system/src/stm32f4-hal/stm32f4xx_hal_dac_ex.c system/src/stm32f4-hal/stm32f4xx_hal_dac.c system/src/stm32f4-hal/stm32f4xx_hal_cryp_ex.c system/src/stm32f4-hal/stm32f4xx_hal_cryp.c system/src/stm32f4-hal/stm32f4xx_hal_crc.c system/src/stm32f4-hal/stm32f4xx_hal_cortex.c system/src/stm32f4-hal/stm32f4xx_hal_can.c system/src/stm32f4-hal/stm32f4xx_hal_adc_ex.c system/src/stm32f4-hal/stm32f4xx_hal_adc.c system/src/stm32f4-hal/stm32f4xx_hal.c system/src/newlib/_write.c system/src/newlib/_syscalls.c system/src/newlib/_startup.c system/src/newlib/_sbrk.c system/src/newlib/_exit.c system/src/newlib/assert.c system/src/diag/trace_impl.c system/src/diag/Trace.c system/src/cortexm/_reset_hardware.c system/src/cortexm/_initialize_hardware.c system/src/cortexm/exception_handlers.c system/src/cmsis/vectors_stm32f4xx.c system/src/cmsis/system_stm32f4xx.c system/lwip/src/netif/slipif.c system/lwip/src/netif/ppp/vj.c system/lwip/src/netif/ppp/randm.c system/lwip/src/netif/ppp/ppp_oe.c system/lwip/src/netif/ppp/ppp.c system/lwip/src/netif/ppp/pap.c system/lwip/src/netif/ppp/md5.c system/lwip/src/netif/ppp/magic.c system/lwip/src/netif/ppp/lcp.c system/lwip/src/netif/ppp/ipcp.c system/lwip/src/netif/ppp/fsm.c system/lwip/src/netif/ppp/chpms.c system/lwip/src/netif/ppp/chap.c system/lwip/src/netif/ppp/auth.c system/lwip/src/netif/etharp.c system/lwip/src/core/udp.c system/lwip/src/core/tcp_out.c system/lwip/src/core/tcp_in.c system/lwip/src/core/tcp.c system/lwip/src/core/sys.c system/lwip/src/core/stats.c system/lwip/src/core/snmp/msg_out.c system/lwip/src/core/snmp/msg_in.c system/lwip/src/core/snmp/mib_structs.c system/lwip/src/core/snmp/mib2.c system/lwip/src/core/snmp/asn1_enc.c system/lwip/src/core/snmp/asn1_dec.c system/lwip/src/core/raw.c system/lwip/src/core/pbuf.c system/lwip/src/core/netif.c system/lwip/src/core/memp.c system/lwip/src/core/mem.c system/lwip/src/core/lwip_timers.c system/lwip/src/core/ipv4/ip_frag.c system/lwip/src/core/ipv4/ip_addr.c system/lwip/src/core/ipv4/ip.c system/lwip/src/core/ipv4/inet_chksum.c system/lwip/src/core/ipv4/inet.c system/lwip/src/core/ipv4/igmp.c system/lwip/src/core/ipv4/icmp.c system/lwip/src/core/ipv4/autoip.c system/lwip/src/core/init.c system/lwip/src/core/dns.c system/lwip/src/core/dhcp.c system/lwip/src/core/def.c system/lwip/src/api/tcpip.c system/lwip/src/api/sockets.c system/lwip/src/api/netifapi.c system/lwip/src/api/netdb.c system/lwip/src/api/netbuf.c system/lwip/src/api/err.c system/lwip/src/api/api_msg.c system/lwip/src/api/api_lib.c


CXX_SOURCES:=src/Watchdog.cpp src/udp.cpp src/Stepgen.cpp src/Pwm.cpp src/Output.cpp src/main.cpp src/Input.cpp src/Encoder.cpp src/Configuration.cpp system/src/newlib/_cxx.cpp

S_SOURCES:=$(call rwildcard,,*.s)
OBJECTS_C := $(addprefix $(BUILDDIR)/,$(C_SOURCES:%.c=%.o))
OBJECTS_CXX := $(addprefix $(BUILDDIR)/,$(CXX_SOURCES:%.cpp=%.o))
OBJECTS_S := $(addprefix $(BUILDDIR)/,$(S_SOURCES:%.s=%.o))
OBJECTS := $(OBJECTS_C) $(OBJECTS_S) $(OBJECTS_CXX)
DEPS:=$(addprefix $(BUILDDIR)/,$(C_SOURCES:%.c=%.d)$(CXX_SOURCES:%.cpp=%.d))
EXENAME:=Prog

all : $(BUILDDIR)/$(EXENAME).elf $(BUILDDIR)/$(EXENAME).bin $(BUILDDIR)/$(EXENAME).hex $(BUILDDIR)/$(EXENAME).disas

$(BUILDDIR)/$(EXENAME).elf : $(OBJECTS)
	$(PREFIX)g++ $(ARCHFLAGS) $(OPTFLAGLD) $(LDFLAGS) -o $@ $^

$(BUILDDIR)/$(EXENAME).bin : $(BUILDDIR)/$(EXENAME).elf
	$(PREFIX)objcopy -O binary $< $@

$(BUILDDIR)/$(EXENAME).hex : $(BUILDDIR)/$(EXENAME).elf
	$(PREFIX)objcopy -O ihex $< $@

$(BUILDDIR)/$(EXENAME).disas : $(BUILDDIR)/$(EXENAME).elf
	$(PREFIX)objdump -d -C $< > $@

-include $(DEPS)

$(OBJECTS_C) : $(BUILDDIR)/%.o : %.c
	@if ! [ -d $(dir $@) ] ; then mkdir -p $(dir $@) || exit 1 ; fi
	$(PREFIX)gcc $(ARCHFLAGS) $(OPTFLAGCL) $(CFLAGS) -MD -c -o $@ $<

$(OBJECTS_CXX) : $(BUILDDIR)/%.o : %.cpp
	@if ! [ -d $(dir $@) ] ; then mkdir -p $(dir $@) || exit 1 ; fi
	$(PREFIX)g++ $(ARCHFLAGS) $(OPTFLAGCL) $(CXXFLAGS) -MD -c -o $@ $<

$(OBJECTS_S) : $(BUILDDIR)/%.o : %.s
	@if ! [ -d $(dir $@) ] ; then mkdir -p $(dir $@) || exit 1 ; fi
	$(PREFIX)as $(ARCHFLAGS) -c -o $@ $<

.PHONY : clean all

clean :
	rm -f $(OBJECTS) $(BUILDDIR)/$(EXENAME).elf $(BUILDDIR)/$(EXENAME).bin $(BUILDDIR)/$(EXENAME).hex $(BUILDDIR)/$(EXENAME).disas $(DEPS)

