#********************************************************************************
#
# @file    Makefile
# @brief   This file is for arm-none-eabi-gcc
#
# @version V1.0
# @date    02 Jan 2020
# @author  AE Team
# @note
#          Change Logs:
#          Date            Author          Notes
#          02 Jan 2020     AE Team         The first version
#
# Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the License); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#********************************************************************************


CC      := arm-none-eabi-gcc
LD      := arm-none-eabi-ld --gc-sections
AS      := arm-none-eabi-as
OBJCOPY := arm-none-eabi-objcopy
SIZE    := arm-none-eabi-size
RM      := rm -rf
ECHO    := echo
SED     := sed
DONE    := done

INCS := -I../app/
INCS += -I../../../../Drivers/ALD/ES32F065x/Include/
INCS += -I../../../../Drivers/CMSIS/Include/
INCS += -I../../../../Drivers/CMSIS/Device/EastSoft/ES32F065x/Include/
INCS += -I../../../../Middlewares/Eastsoft/USB_Library/
INCS += -I../../../../Middlewares/Eastsoft/USB_Library/device/
INCS += -I../../../../Middlewares/Eastsoft/USB_Library/host/

LIBDIR := ../../../../Middlewares/Eastsoft/Micro-Kernel/Library/

MACROS := -DES32F065x -DUSE_ASSERT
CFLAGS := -mthumb -mcpu=cortex-m0 -Wall $(MACROS) $(INCS) -std=c99 -O0 --specs=nosys.specs --specs=nano.specs
TARGET := image
SRCDIR := ../app ../../../../Drivers/ALD/ES32F065x/Source/
OBJDIR := ../obj
VPATH  := $(SRCDIR)


SOURCE := $(foreach i, $(SRCDIR), $(wildcard $(i)/*.c))
SOURCE := $(notdir $(SOURCE))
SRC_SD := ../../../../Drivers/CMSIS/Device/EastSoft/ES32F065x/Startup/gcc
SRC_S  := ../../../../Drivers/CMSIS/Device/EastSoft/ES32F065x/Startup/gcc/startup_es32f065x.s
FLDS   := es32f065x.ld

OBJS   := $(patsubst %.c, %.o, $(SOURCE))
OBJS   := $(OBJS) $(patsubst %.s, %.o, $(SRC_S))
OBJS   := $(addprefix $(OBJDIR)/, $(OBJS))
DOBJ   := $(patsubst %.o, %.d, $(OBJS))

all : $(TARGET)
$(TARGET) : $(OBJS)
	$(CC) $(CFLAGS) -o $@ $^ -T $(FLDS) -L $(LIBDIR) -Wl,-gc-sections -Wl,-Map,image.map
	$(OBJCOPY) $(TARGET) -O binary $(TARGET).bin
	$(OBJCOPY) $(TARGET) -O ihex $(TARGET).hex
	@$(SIZE) $@
	@$(ECHO) $(DONE)

sinclude $(SOURCE:.c=.d)

$(OBJDIR)/%.o : %.c $(OBJDIR)/%.d
	$(CC) $(CFLAGS) --function-sections -c $< -o $@

$(OBJDIR)/%.o : %.s
	$(CC) $(CFLAGS) -c $< -o $@

-include $(DOBJ)

$(OBJDIR)/%.d : %.c
	$(CC) -M $(CFLAGS) $< > $@.$$$$; \
	$(SED) 's,\($*\)\.o[:]*,\1.o $@:,g'<$@.$$$$>$@;\
	$(RM) $@.$$$$

.PHONY : clean
clean:
	$(RM) $(OBJDIR)/*.o $(OBJDIR)/*.d $(TARGET)
	$(RM) *.bin *.hex *.map
	$(RM) $(SRC_SD)/*.o
