# 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)../..)))

.PHONY: clean assets all

ifneq ($(words $(makefile_path))$(words $(MAKEFILE_LIST)),11)
all clean assets:
$(error Spaces not allowed in path)
else

# Audio DSP engine (audio_effects.h/.c, fx_*.h/.c): plain, portable C with no
# STM32 HAL/CMSIS dependency, so it builds as-is for the x86 simulator. Views
# reference it directly (AudioEffects_GetParams()), so it must be part of the
# simulator build too, per ST's documented ADDITIONAL_* mechanism below.
ADDITIONAL_SOURCES := \
	../Core/Src/audio_effects.c \
	../Core/Src/audio_analysis.c \
	../Core/Src/audio_looper.c \
	../Core/Src/fx_octaver.c \
	../Core/Src/fx_saturation.c \
	../Core/Src/fx_overdrive.c \
	../Core/Src/fx_echo.c \
	../Core/Src/fx_equalizer.c \
	../Core/Src/fx_reverb.c \
	../../Drivers/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_f32.c \
	../../Drivers/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_init_f32.c \
	../../Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_f32.c \
	../../Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_init_f32.c \
	../../Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix8_f32.c \
	../../Drivers/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal2.c \
	../../Drivers/CMSIS/DSP/Source/CommonTables/arm_common_tables.c \
	../../Drivers/CMSIS/DSP/Source/CommonTables/arm_const_structs.c
ADDITIONAL_INCLUDE_PATHS := ../Core/Inc ../../Drivers/CMSIS/DSP/Include ../../Drivers/CMSIS/DSP/PrivateInclude
ADDITIONAL_LIBRARY_PATHS :=
ADDITIONAL_LIBRARIES :=
export ADDITIONAL_SOURCES ADDITIONAL_INCLUDE_PATHS ADDITIONAL_LIBRARY_PATHS ADDITIONAL_LIBRARIES

all: $(filter assets,$(MAKECMDGOALS))
all assets: $(filter clean,$(MAKECMDGOALS))
all clean assets:
	@$(MAKE) -r -f generated/simulator/gcc/Makefile -s $(MFLAGS) $@ -C "$(application_path)"
endif

