#create:2018-10-12
#auther:zsl,wb

#Ķļ֪ʶ
#(1)ַ'\'ʾ
#(2)Ŀĵһָǰһtab4ո
#(3)$@  ʾĿļ
#   $^  ʾеļ
#   $<  ʾһļ
#   $?  ʾĿ껹Ҫµļб

#屾ļʹõıʵҪ޸ġ
#̨·
printf    := echo

#.Cļļ
SRC_C := ./srcc
#.Sļļ
SRC_S := ./srcs
#.oļĿ¼
D_OBJ := ./obj
#.dļĿ¼
D_MK  := ./dmk
#.Cļб
SRC_CList := $(wildcard ./srcc/*.c)
#.CPPļб
#20200305 1/8 &Ʋ .cppļб.cļб
SRC_CPPList := $(wildcard ./srcc/*.cpp)
#.sļб
#20200305 2/8 &Ʋ .Sļбԭ.s.S
SRC_sList := $(wildcard ./srcs/*.s)
#.Sļб
SRC_SList := $(wildcard ./srcs/*.S)

#ļ(.ld)Ĵ洢·
linkFile := "C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\03_MCU\Linker_file\STM32L431RCTX_FLASH.ld"
#ɵļ
finalGoal := BIOS-STM32L431-RT-V6.0-20210203
#ͷļڵ·
include := -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\.settings" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\01_Doc" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\02_CPU" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\03_MCU" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\04_GEC" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\05_UserBoard" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\06_SoftComponent" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\07_NosPrg" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\08_RT-Thread" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\09_RT-ThreadPrg" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\Debug" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\03_MCU\Linker_file" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\03_MCU\MCU_drivers" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\03_MCU\startup" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\08_RT-Thread\components" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\08_RT-Thread\include" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\08_RT-Thread\src" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\08_RT-Thread\components\finsh" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\08_RT-Thread\include\libc" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\Debug\obj" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\Debug\srcc" -I"C:\Users\lenovo\Desktop\BIOS-User-STM32L431-RT-V6.0-20210203\BIOS-STM32L431-RT-V6.0-20210203\Debug\srcs"
#Ż𡣹O0O1O2OsOgĬʹOg
opt := Og
#.oļб
#20200305 3/8 &Ʋ ԭOBJ_CΪOBJ-CS.S.oļ

#.cļ滻Ϊ.oļ
OBJ_CS := $(addprefix $(D_OBJ)/,$(patsubst %.c,%.o,$(notdir $(SRC_CList))))
#.cppļ滻Ϊ.oļ
OBJ_CS += $(addprefix $(D_OBJ)/,$(patsubst %.cpp,%.o,$(notdir $(SRC_CPPList))))
#.sļ滻Ϊ.oļ  
OBJ_CS += $(addprefix $(D_OBJ)/,$(patsubst %.s,%.o,$(notdir $(SRC_sList))))
#.Sļ滻Ϊ.oļ  
OBJ_CS += $(addprefix $(D_OBJ)/,$(patsubst %.S,%.o,$(notdir $(SRC_SList))))

#.dļб
#20200305 4/8 &Ʋ .S.dļ

#.cļ滻Ϊ.dļ
SRC_MK := $(addprefix $(D_OBJ)/, $(patsubst %.c,%.d,$(notdir $(SRC_CList))))
#.cppļ滻Ϊ.dļ
SRC_MK += $(addprefix $(D_OBJ)/, $(patsubst %.cpp,%.d,$(notdir $(SRC_CPPList))))
#.sļ滻Ϊ.dļ
SRC_MK += $(addprefix $(D_OBJ)/, $(patsubst %.s,%.d,$(notdir $(SRC_sList))))
#.Sļ滻Ϊ.dļ  
SRC_MK += $(addprefix $(D_OBJ)/, $(patsubst %.S,%.d,$(notdir $(SRC_SList))))


#ļ./Sourceе.cļΪ.oļ
#20200305 5/8 &Ʋ ȼ-O0ĳ-$(opt),о޸
$(D_OBJ)/%.o:$(SRC_C)/%.c
	arm-none-eabi-gcc "$^" -mcpu=cortex-m4 -std=gnu11 -g3 -DUSE_HAL_DRIVER -DDEBUG \
-DSTM32L431xx -c $(include) -$(opt) -ffunction-sections -fdata-sections -Wall -fstack-usage \
-MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "$@" 

#ļ./Sourceе.cppļΪ.oļ
#20200305 6/8 &Ʋ .cppı.oļĹʽ
$(D_OBJ)/%.o:$(SRC_C)/%.cpp
	arm-none-eabi-g++ "$<" -mcpu=cortex-m4 -std=gnu++14 -g3 -c $(include) -$(opt) \
-ffunction-sections -fdata-sections -fno-exceptions -fno-rtti -fno-threadsafe-statics \
-fno-use-cxa-atexit -Wall -fstack-usage -MMD -MP -MF"$(@:%.o=%.d)"  -MT"$@" \
--specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "$@"

#ļ./Sourceе.sļΪ.oļ 
$(D_OBJ)/%.o:$(SRC_S)/%.s
	arm-none-eabi-gcc -mcpu=cortex-m4 -g3 -c $(include) -Wa,-mimplicit-it=thumb -x assembler-with-cpp \
--specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "$@" "$<"

#ļ./Sourceе.SļΪ.oļ 
#20200305 7/8 &Ʋ .Sı.oļĹʽ
$(D_OBJ)/%.o:$(SRC_S)/%.S
	arm-none-eabi-gcc -mcpu=cortex-m4 -g3 -c $(include) -Wa,-mimplicit-it=thumb -x assembler-with-cpp \
--specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "$@" "$<"




# ļеĿꡣĿΪϣɵļСall͡cleanΪαĿ
all:default.size.stdout $(finalGoal).lst $(finalGoal).hex 

#20200305 8/8 &Ʋ ԭgccΪg++,OBJ_CΪOBJ_CS

$(finalGoal).elf: $(OBJ_CS)
	arm-none-eabi-g++ -o "$(finalGoal).elf" $(OBJ_CS) -mcpu=cortex-m4 -T $(linkFile) \
--specs=nosys.specs -Wl,-Map="$(finalGoal).map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 \
-mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-group
	@$(printf) 'Finished building target: $@'
	@$(printf) ' '

$(finalGoal).hex: $(finalGoal).elf $(finalGoal).lst
	arm-none-eabi-objcopy -O ihex "$<"  "$@"
	@$(printf) 'Finished building: $@'
	@$(printf) ' '

# 20200604 5/5 鿴оƬڴʹ
default.size.stdout: $(finalGoal).elf
	arm-none-eabi-size  $(finalGoal).elf
	@echo 'Finished building: $@'
	@echo ' '
	
$(finalGoal).lst: $(finalGoal).elf
	arm-none-eabi-objdump -h -S "$(finalGoal).elf" > "$(finalGoal).lst"
	@$(printf) 'Finished building: $@'
	@$(printf) ' '

#ռαĿ
#ɾĿļļδ䶯򲻱룬߱ٶ
fastClean:
	rm -rf "$(finalGoal).elf" "$(finalGoal).hex" "$(finalGoal).map"
	-@$(printf) 'make: fast clean has be done'
	-@$(printf) ' '
#ɾмļĿļȫ±	
clean:
	rm -rf  "$(finalGoal).elf" "$(finalGoal).hex" "$(finalGoal).map" "$(finalGoal).lst" "$(finalGoal).hex" $(OBJ_C) $(SRC_MK)
	-@$(printf) 'make: clean has be done'
	-@$(printf) ' '
test:
	arm-none-eabi-size  $(finalGoal).elf
	@$(printf) 'Finished building: $@'
	@$(printf) ' '
	-@$(printf)  $(OBJ_C)
	
#ָЩĿΪαĿ
.PHONY: all clean fastClean test


