# Helper macros to convert spaces into question marks and back again
e :=
sp := $(e) $(e)
qs = $(subst ?,$(sp),$1)
sq = $(subst $(sp),?,$1)

# Get name of this Makefile (avoid getting word 0 and a starting space)
makefile_name := $(wordlist 1,1000,$(MAKEFILE_LIST))

# Get path of this Makefile
makefile_path := $(call qs,$(dir $(call sq,$(abspath $(call sq,$(makefile_name))))))

# Get path where the Application is
application_path := $(call qs,$(abspath $(call sq,$(makefile_path)..)))

# Change makefile_name to a relative path
makefile_name := $(subst $(call sq,$(application_path))/,,$(call sq,$(abspath $(call sq,$(makefile_name)))))

# Get relative path to makefile from application_path
makefile_path_relative := $(subst $(call sq,$(application_path))/,,$(call sq,$(abspath $(call sq,$(makefile_path)))))

# Get path to Drivers
Drivers_path := Drivers

# Get path to ExtMem_Manager
ExtMem_Manager_path := Middlewares/ST/STM32_ExtMem_Manager

# Get identification of this system
ifeq ($(OS),Windows_NT)
UNAME := MINGW32_NT-6.2
else
UNAME := $(shell uname -s)
endif

board_name := STM32H7S78-DK
platform := cortex_m7

.PHONY: all clean assets flash intflash

all: $(filter clean,$(MAKECMDGOALS))
all clean assets:
	@cd "$(application_path)" && $(MAKE) -r -f $(makefile_name) -s $(MFLAGS) _$@_

flash intflash: all
	@cd "$(application_path)" && $(MAKE) -r -f $(makefile_name) -s $(MFLAGS) _$@_

build_root_path := Boot/TouchGFX/build
object_output_path := $(build_root_path)/$(board_name)
binary_output_path := $(build_root_path)/bin

ifeq ($(UNAME), Linux)
else
stldr := MX66UW1G45G_STM32H7S78-DK.stldr
include $(application_path)/gcc/include/cube_programmer.mk
endif

target_executable := target.elf
target_hex := target.hex

assembler    := arm-none-eabi-gcc
c_compiler   := arm-none-eabi-gcc
cpp_compiler := arm-none-eabi-g++
linker       := arm-none-eabi-g++
objcopy      := arm-none-eabi-objcopy
archiver     := arm-none-eabi-ar
strip        := arm-none-eabi-strip

assembler_options += \
	-MMD \
	-MP \
	-Wall \
	-c \
	-fno-exceptions \
	-g \
	-mthumb \
	-x assembler-with-cpp 

c_compiler_options += \
	--specs=nano.specs \
	-MMD \
	-MP \
	-Os \
	-Wall \
	-fdata-sections \
	-fdata-sections \
	-ffunction-sections \
	-fno-exceptions \
	-fstack-usage \
	-g \
	-mthumb \
	-std=gnu11 

cpp_compiler_options += \
	--specs=nano.specs \
	-MMD \
	-MP \
	-Os \
	-fdata-sections \
	-ffunction-sections \
	-fno-exceptions \
	-fno-rtti \
	-g \
	-mthumb \
	-std=gnu++14 

linker_options += \
	--specs=nosys.specs \
	-Os \
	-Wl,--gc-sections -static \
	-Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-group \
	-fno-exceptions \
	-fno-rtti \
	-g \
	-mthumb 

# Additional toolchain configuration for Cortex-M4 targets.
float_options := -mfpu=fpv5-sp-d16 -mfloat-abi=hard

assembler_options    += $(float_options) -mcpu=cortex-m7 -DCORE_CM7
c_compiler_options   += $(float_options) -mcpu=cortex-m7 -DCORE_CM7
cpp_compiler_options += $(float_options) -mcpu=cortex-m7 -DCORE_CM7
linker_options       += $(float_options) -mcpu=cortex-m7 
   
c_compiler_options   += -DST -DSTM32H7S7xx -DUSE_HAL_DRIVER -DUSE_STM32H7S78_DK -DUSE_PWR_LDO_SUPPLY__N -DUSE_FLOATING_POINT 
cpp_compiler_options += -DST -DSTM32H7S7xx -DUSE_HAL_DRIVER -DUSE_STM32H7S78_DK -DUSE_PWR_LDO_SUPPLY__N -DUSE_FLOATING_POINT 

# Drivers/STM32H7RSxx_HAL_Driver
source_files += \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_cortex.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_dma.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_dma_ex.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_exti.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_flash.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_flash_ex.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_gpio.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_pwr.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_pwr_ex.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_rcc.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_rcc_ex.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_tim.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_tim_ex.c \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_xspi.c 

source_files += \
    Boot/Core/Src/main.c \
	Boot/Core/Src/stm32h7rsxx_it.c \
	Boot/Core/Src/stm32h7rsxx_hal_msp.c \
	Boot/Core/Src/system_stm32h7rsxx.c \
	Boot/Core/Src/extmem_manager.c \
	$(ExtMem_Manager_path)/stm32_extmem.c \
	$(ExtMem_Manager_path)/boot/stm32_boot_xip.c \
	$(ExtMem_Manager_path)/nor_sfdp/stm32_sfdp_data.c \
	$(ExtMem_Manager_path)/nor_sfdp/stm32_sfdp_driver.c \
	$(ExtMem_Manager_path)/psram/stm32_psram_driver.c \
	$(ExtMem_Manager_path)/sal/stm32_sal_sd.c \
	$(ExtMem_Manager_path)/sal/stm32_sal_xspi.c \
	$(ExtMem_Manager_path)/sdcard/stm32_sdcard_driver.c \
	$(ExtMem_Manager_path)/user/stm32_user_driver.c

# Drivers
board_include_paths := \
	$(Drivers_path)/CMSIS/Include \
	$(Drivers_path)/CMSIS/Device/ST/STM32H7RSxx/Include \
	$(Drivers_path)/STM32H7RSxx_HAL_Driver/Inc \
	$(Drivers_path)/CMSIS/RTOS2/Include \
    Boot/Core/Inc \
	$(ExtMem_Manager_path) \
	$(ExtMem_Manager_path)/boot \
	$(ExtMem_Manager_path)/nor_sfdp \
	$(ExtMem_Manager_path)/psram \
	$(ExtMem_Manager_path)/sal \
	$(ExtMem_Manager_path)/sdcard \
	$(ExtMem_Manager_path)/user

asm_source_files := \
	gcc/startup_stm32h7s7l8hxh_boot.s

include_paths += platform/os $(board_include_paths) $(os_include_paths)

c_source_files := $(makefile_path_relative)/sysmem.c $(makefile_path_relative)/syscalls.c $(call find, $(source_paths),*.c) $(os_source_files) $(board_c_files)
source_files += $(board_cpp_files)
object_files := $(source_files) $(c_source_files)

# start converting paths
object_files := $(object_files:%.cpp=$(object_output_path)/%.o)
object_files := $(object_files:%.c=$(object_output_path)/%.o)
object_files := $(object_files:$(Drivers_path)/%.c=$(object_output_path)/Drivers/%.o)

# remove templates files
# object_files := $(filter-out %template.o,$(object_files))

dependency_files := $(object_files:%.o=%.d)

object_asm_files := $(asm_source_files:%.s=$(object_output_path)/%.o)
object_asm_files := $(patsubst $(object_output_path)/%,$(object_output_path)/%,$(object_asm_files))

.PHONY: _all_ _clean_ _assets_ _flash_ _intflash_ generate_assets build_executable

# Force linking each time
.PHONY: $(binary_output_path)/$(target_executable)

_all_: generate_assets

# if ExtFlashSection or FontFlashSection is detected inside the executable, enable external flash
flash_section_count := 0
elf_filename := "$(application_path)/$(binary_output_path)/$(target_executable)"
ifeq ($(shell test -e $(elf_filename) && echo -n has_elf_file),has_elf_file)
flash_section_count := $(shell readelf -S $(elf_filename) | grep FlashSection | wc -l)
endif

ifeq ($(shell test $(flash_section_count) -gt 0; echo $$?),0)
_flash_: _extflash_
else
_flash_: _intflash_
endif

include $(application_path)/gcc/include/flash_sections_int_ext.mk

generate_assets: _assets_
	@$(MAKE) -f $(makefile_name) -r -s $(MFLAGS) build_executable
    
build_executable: $(binary_output_path)/$(target_executable)

$(binary_output_path)/$(target_executable): $(object_files) $(object_asm_files)
	@echo Linking $(@)
	@mkdir -p $(@D)
	@$(linker) \
		$(linker_options) -T $(makefile_path_relative)/../Boot/stm32h7s7xx_flash.ld -Wl,-Map=$(@D)/application.map \
		$(patsubst %,-L%,$(library_include_paths)) \
		$(object_files) $(object_asm_files) -o $@ \
		-Wl,--start-group -Wl,--end-group
	@echo "Producing additional output formats..."
	@echo "  target.hex   - Combined internal+external hex"
	@$(objcopy) -O ihex $@ $(@D)/target.hex
	@echo "  intflash.elf - Internal flash, elf debug"
	@$(objcopy) $@ $(@D)/intflash.elf 2>/dev/null
	@echo "  intflash.hex - Internal flash, hex"
	@$(objcopy) -O ihex $@ $(@D)/intflash.hex
	# Re-enable the lines below if your workflow requires extflash.bin files
	# @echo "  extflash.bin - External flash, binary"
	# @$(objcopy) -O binary $@ $(@D)/extflash.bin

$(object_output_path)/touchgfx/%.o: $(touchgfx_path)/%.cpp 
	@echo Compiling $<
	@mkdir -p $(@D)
	@$(cpp_compiler) \
		-MMD -MP $(cpp_compiler_options) $(user_cflags) \
		$(patsubst %,-I%,$(include_paths)) \
		-c $< -o $@

$(object_output_path)/%.o: %.cpp 
	@echo Compiling $<
	@mkdir -p $(@D)
	@$(cpp_compiler) \
		-MMD -MP $(cpp_compiler_options) $(user_cflags) \
		$(patsubst %,-I%,$(include_paths)) \
		-c $< -o $@

$(object_output_path)/touchgfx/%.o: $(touchgfx_path)/%.c 
	@echo Compiling $<
	@mkdir -p $(@D)
	@$(c_compiler) \
		-MMD -MP $(c_compiler_options) $(user_cflags) \
		$(patsubst %,-I%,$(include_paths)) \
		-c $< -o $@

$(object_output_path)/%.o: %.c 
	@echo Compiling $<
	@mkdir -p $(@D)
	@$(c_compiler) \
		-MMD -MP $(c_compiler_options) $(user_cflags) \
		$(patsubst %,-I%,$(include_paths)) \
		-c $< -o $@

$(object_output_path)/%.o: %.s 
	@echo Compiling ASM $<
	@mkdir -p $(@D)
	@$(assembler) \
		$(assembler_options) \
		$(patsubst %,-I %,$(os_include_paths)) \
		-c $< -o $@

ifeq ($(MAKECMDGOALS),build_executable)
$(firstword $(dependency_files)): 
	@mkdir -p $(object_output_path)
-include $(dependency_files)
endif

_clean_:
	@echo Cleaning: $(board_name)
	@rm -rf $(build_root_path)
