# ================================================
# LT168 LVGL SDK Makefile
# @author: julong@111.com
# ================================================

# Toolchain Path
TOOLCHAIN_ROOT := E:/Levetop/CCore_IDE/tool-chain

# 编译可选配置
#      USER_PRODUCT - 用户产品
#      DEMO_LVGL_TEST - (LVGL简单测试  bin < 435K)
#      DEMO_WIDGETS - (LVGL widgets演示)
#      DEMO_MUSIC_PLAYER - (LVGL music player演示)
#      DEMO_BENCHMARK - (LVGL benchmark性能测试)
#      DEMO_COFFEE_MACHINE - (咖啡机示例)    需要设置SPI_FLASH_FLAG = 1
TARGET_PROFILE := USER_PRODUCT
#TARGET_PROFILE := DEMO_LVGL_TEST
#TARGET_PROFILE := DEMO_WIDGETS
#TARGET_PROFILE := DEMO_MUSIC_PLAYER
#TARGET_PROFILE := DEMO_BENCHMARK
#TARGET_PROFILE := DEMO_COFFEE_MACHINE

# 编译目标配置
#      0  - 默认编译至MCU eFlash
#      1  -  编译至外部 SPI NorFlash，使用XIP模式需要NorFlash，XIP模式不支持NAND Flash。 需要刷入 LT168B_USB_SD_Uart_XIP_Boot_V1.1.bin
SPI_NORFLASH_FLAG ?= 0
#SPI_NORFLASH_FLAG ?= 1

# LVGL 版本
#  可选:
#     LVGL_V950
TARGET_LVGL_VERSION := LVGL_V950

BUILD_DIR := build/$(TARGET_PROFILE)

STARTUP  := Startup
MCU_DRV  := MCU_Driver
MCU_FUNC := MCU_Function
DEV_DRV  := Device_Driver
DEV_FUNC := Device_Function
PROTOCOL := Protocol
UTILITY  := Utility
USER     := User
LT_SDK   := lt_sdk
LVGL     := lvgl
DEMO     := Demo


rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))

include makefiles/demo.mk

ifeq ($(strip $(TARGET_PROFILE)),USER_PRODUCT)
    DEMO_NAME := USER_PRODUCT
endif
# USER_PRODUCT
ifeq ($(strip $(TARGET_PROFILE)),USER_PRODUCT)
    DEMO_SRC := $(USER)/user_product.c
    DEMO_OBJS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(DEMO_SRC))
endif

ifndef DEMO_NAME
    $(error Unknown TARGET_PROFILE: $(TARGET_PROFILE))
endif

include makefiles/flags.mk
include makefiles/driver_lt168.mk
include makefiles/lvgl_v950.mk

ALL_SRC := $(DRIVER_SRC) $(LVGL_SRC)

# Default exclude Demo directory
ALL_SRC := $(filter-out $(DEMO)/%,$(ALL_SRC))
# Default exclude lvgl demos directory
ALL_SRC := $(filter-out $(LVGL)/demos/%,$(ALL_SRC))

# Compile object files (grouped by source type)
DRIVER_OBJS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(filter %.c,$(DRIVER_SRC)))
DRIVER_OBJS += $(patsubst %.s,$(BUILD_DIR)/%.o,$(filter %.s,$(DRIVER_SRC)))
LVGL_OBJS   := $(patsubst %.c,$(BUILD_DIR)/%.o,$(filter %.c,$(LVGL_SRC)))
LT_SDK_LVGL_OBJS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(filter %.c,$(LT_SDK_LVGL_SRC)))

OBJS := $(DRIVER_OBJS) $(LVGL_OBJS) $(LT_SDK_LVGL_OBJS) $(DEMO_OBJS)

TARGET := $(BUILD_DIR)/$(DEMO_NAME).elf
MAPFILE := $(BUILD_DIR)/$(DEMO_NAME).map

$(BUILD_DIR):
	cmd /C "(if not exist $(subst /,\,$@) mkdir $(subst /,\,$@)) 2>nul & exit /b 0"

# Normal build rules
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
	cmd /C "(if not exist $(subst /,\,$(dir $@)) mkdir $(subst /,\,$(dir $@))) 2>nul & exit /b 0"
	$(CC) $(CFLAGS) -c -o $@ $<

$(BUILD_DIR)/%.o: %.s | $(BUILD_DIR)
	cmd /C "(if not exist $(subst /,\,$(dir $@)) mkdir $(subst /,\,$(dir $@))) 2>nul & exit /b 0"
	$(CC) $(CFLAGS) -c -o $@ $<

# build rules with project.h
-include makefiles/lvgl_compile.mk

# Music demo: pack objects into static archive to avoid Windows cmdline length limit
$(MUSIC_DEMO_LIB): $(DEMO_OBJS)
	$(AR) rcs $@ $^

$(TARGET): $(OBJS) $(MUSIC_DEMO_LIB)
	$(LD) $(LDFLAGS) -o $@ $(OBJS) $(MUSIC_DEMO_LIB) $(LIBS)

BIN_FILE := $(BUILD_DIR)/$(DEMO_NAME).bin
OBJ_FILE := $(BUILD_DIR)/$(DEMO_NAME).obj

$(BIN_FILE): $(TARGET)
	$(OBJCOPY) -O binary $(TARGET) $(BIN_FILE)

$(OBJ_FILE): $(TARGET)
	$(OBJDUMP) -D $(TARGET) > $(OBJ_FILE)

all: $(TARGET)
	$(SIZE) -B $(TARGET)
	$(SIZE) -A -x $(TARGET)
	$(OBJCOPY) -O binary $(TARGET) $(BIN_FILE)
#	$(OBJDUMP) -D $(TARGET) > $(OBJ_FILE)

clean:
	rm -rf build/

.PHONY: all clean