#############################################################[Chinese Manual]###################################################################
# *****工程路径不要包含中文字符和空格,否则可能会报错！*****
# 第一步： 确认机器上已经安装了MRS的工具链(假设当前平台为Linux,工具链路径为"/path/to/toolchain/RISC-V Embedded GCC12",工具链相关前缀为"riscv-wch-elf-")
# 第二步: 设置工具链路径,例如修改替换下方的set(TOOLCHAIN_FOLDER "/path/to/toolchain/RISC-V Embedded GCC12")
# 第三步: 设置工具链中相关可执行文件的前缀,例如修改替换下方的set(TOOLCHAIN_PREFIX "riscv-wch-elf-")
# 第四步: 执行 cd your/path/of/project
# 第五步: 执行 cmake -B build -G "Unix Makefiles"
# 第六步: 执行 cmake --build build
# 注意: 在Windows平台运行,需要a.安装GNU MAKE并将其路径(bin目录)添加到系统环境变量,之后执行cmake -B build -G "Unix Makefiles"(推荐)
#       或者b.安装MinGW,将其路径(bin目录)添加到系统环境变量,之后执行cmake -B build -G "MinGW Makefiles"
# 如有相关疑问,可通过support@mounriver.com联系我们
#################################################################################################################################################


#############################################################[English Manual]###################################################################
# Step 1: Confirm that the MRS toolchain has been installed on the machine. Assuming the current platform is Linux, 
#         the toolchain path is "/path/to/toolchain/RISC-V Embedded GCC12", and the toolchain-related prefix is "riscv-wch-elf-"
# Step 2: Set the toolchain path, for example, modify and replace the following "set (TOOLCHAIN_FOLDER "/path/to/toolchain/RISC-V Embedded GCC12")"
# Step 3: Set the prefix of the executable files in the toolchain,for example, modify and replace the following "set(TOOLCHAIN_PREFIX "riscv-wch-elf-")"
# Step 4: Call "cd your/path/of/project"
# Step 5: Call "cmake -B build -G "Unix Makefiles""
# Step 6: Call "cmake --build build"
# Note:   To run on Windows, you need: a. Install GNU MAKE and add its path (the bin directory) to the system environment PATH. 
#         Then call the execution of "cmake -B build -G "Unix Makefiles""(Recommanded).
#         or b. Install MinGW and add its path (the bin directory) to the system environment PATH. 
#         Then call the execution of "cmake -B build -G "MinGW Makefiles"".
# If you have any questions, please contact us at support@mounriver.com
#################################################################################################################################################


# Set CMake minimum require
cmake_minimum_required(VERSION 3.16)

# Set CMake sysytem name
set(CMAKE_SYSTEM_NAME Generic)


# Skipping compiler detection and use custom toolchain
SET(CMAKE_C_COMPILER_WORKS TRUE)
SET(CMAKE_CXX_COMPILER_WORKS TRUE)
SET(CMAKE_ASM_COMPILER_WORKS TRUE)

# Use MRS toolchian to compile
set(TOOLCHAIN_FOLDER "e:/MounRiver/MounRiver_Studio2/resources/app/resources/win32/components/WCH/Toolchain/RISC-V Embedded GCC12")
set(TOOLCHAIN_PREFIX "riscv-wch-elf-")
set(CMAKE_C_COMPILER "${TOOLCHAIN_FOLDER}/bin/riscv-wch-elf-gcc.exe")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_FOLDER}/bin/riscv-wch-elf-g++.exe")
set(CMAKE_ASM_COMPILER "${TOOLCHAIN_FOLDER}/bin/riscv-wch-elf-gcc.exe")
set(CMAKE_OBJCOPY "${TOOLCHAIN_FOLDER}/bin/${TOOLCHAIN_PREFIX}objcopy.exe")
set(CMAKE_OBJDUMP "${TOOLCHAIN_FOLDER}/bin/riscv-wch-elf-objdump.exe")
set(CMAKE_SIZE "${TOOLCHAIN_FOLDER}/bin/${TOOLCHAIN_PREFIX}size.exe")
set(CMAKE_AR "${TOOLCHAIN_FOLDER}/bin/${TOOLCHAIN_PREFIX}ar.exe")

set(PROJECT_NAME "zzztest6copy")
set(TARGET_ARTIFACT "zzztest6copy.elf")

# mylib_c（灯棒动画/指令引擎）已抽成静态库，源码不再参与本工程编译。
# 两个变体，用 -DMYLIB_VARIANT=<lite|full> 选：
#   lite（默认）—— 精简版：基本模式 + 跳频相位同步 + colordb，无几何。
#                 工程在 ../../mylib_c_lite_lib/，产出 lib/libmylib_c_lite.a
#   full        —— 完整版：含 beat/simple_graphic/follow_path 与全部几何。
#                 工程在 ../../mylib_c_lib/，产出 lib/libmylib_c.a
#
# ⚠ 切变体请用**新的 build 目录**（或删掉 build/ 重新 configure）：
#   旧的 CMakeCache.txt 里缓存着上一次的 MYLIB_C_LIB_DIR。
set(MYLIB_VARIANT "lite" CACHE STRING "mylib_c 变体：lite(默认，无几何) 或 full")
set_property(CACHE MYLIB_VARIANT PROPERTY STRINGS lite full)

if(MYLIB_VARIANT STREQUAL "lite")
    set(_mylib_proj    "mylib_c_lite_lib")
    set(_mylib_libname "mylib_c_lite")
    set(_mylib_hdr_root "I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../../mylib_c_lite_lib")
elseif(MYLIB_VARIANT STREQUAL "full")
    set(_mylib_proj    "mylib_c_lib")
    set(_mylib_libname "mylib_c")
    set(_mylib_hdr_root "I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/mylib_c")
else()
    message(FATAL_ERROR "MYLIB_VARIANT must be 'lite' or 'full', got: '${MYLIB_VARIANT}'")
endif()

if(NOT DEFINED MYLIB_C_LIB_DIR)
    set(MYLIB_C_LIB_DIR "I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../../${_mylib_proj}/lib")
endif()

if(NOT EXISTS "${MYLIB_C_LIB_DIR}/lib${_mylib_libname}.a")
    message(FATAL_ERROR
        "lib${_mylib_libname}.a not found in: ${MYLIB_C_LIB_DIR}\n"
        "Build it first (variant '${MYLIB_VARIANT}'):\n"
        "  cmake -S ../../${_mylib_proj} -B ../../${_mylib_proj}/build -G \"Unix Makefiles\"\n"
        "  <make> -C ../../${_mylib_proj}/build -j8\n"
        "Or point elsewhere with -DMYLIB_C_LIB_DIR=<dir containing lib${_mylib_libname}.a>")
endif()

set(CMAKE_C_OUTPUT_EXTENSION .o)
set(CMAKE_CXX_OUTPUT_EXTENSION .o)

project("${PROJECT_NAME}" LANGUAGES C ASM CXX)

# Use relative path
add_compile_options(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=..)


set(SRC_FILES 
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_adc.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_clk.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_flash.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_gpio.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_i2c.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_lcd.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_pwm.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_pwr.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_spi0.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_spi1.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_sys.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_timer0.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_timer1.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_timer2.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_timer3.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_uart0.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_uart1.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_uart2.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_uart3.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_usbdev.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_usbhostBase.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/CH58x_usbhostClass.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/Startup/startup_CH585.S"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/ble/profile/devinfoservice.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/ble/profile/gattprofile.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../LIB/ble_task_scheduler.S"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../HAL/MCU.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../HAL/RTC.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../HAL/SLEEP.c"
# mylib_c/*.c 不再在此编译 —— 见 ../mylib_c_lib/ 静态库工程，链接 libmylib_c.a
"${CMAKE_CURRENT_SOURCE_DIR}/APP/hw_config.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/hal_irq.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/src/ledx.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/ble/peripheral.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/src/RF_main.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/src/RF_PHY.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/src/rx_hop.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/src/sir24drv.c"
"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/src/ukey.c"
)
set(CMAKE_C_FLAGS " -march=rv32imc_zba_zbb_zbc_zbs_xw -mabi=ilp32 -mcmodel=medany -msmall-data-limit=8 -mno-save-restore -fmax-errors=20 -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common --param=highcode-gen-section-name=1 -g -I\"${_mylib_hdr_root}/include\" -I\"${_mylib_hdr_root}\" -I\"${_mylib_hdr_root}/lwpkt/lwpkt/src/include\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/Startup\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/include\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/ble\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP/ble/profile/include\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver/inc\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../HAL/include\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/Ld\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../LIB\" -I\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/RVMSIS\" -DBLE_BUFF_MAX_LEN=512 -DMCU_ENVIRONMENT=1 -DUSE_MEMORY_POOL=1 -std=gnu99")
set(CMAKE_CXX_FLAGS " -march=rv32imc_zba_zbb_zbc_zbs_xw -mabi=ilp32 -mcmodel=medany -msmall-data-limit=8 -mno-save-restore -fmax-errors=20 -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common --param=highcode-gen-section-name=1 -g -std=gnu++11 -fabi-version=0")
set(CMAKE_ASM_FLAGS " -march=rv32imc_zba_zbb_zbc_zbs_xw -mabi=ilp32 -mcmodel=medany -msmall-data-limit=8 -mno-save-restore -fmax-errors=20 -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common --param=highcode-gen-section-name=1 -g -x assembler-with-cpp")

add_executable("${TARGET_ARTIFACT}" ${SRC_FILES})

set_target_properties("${TARGET_ARTIFACT}" PROPERTIES LINK_FLAGS " -march=rv32imc_zba_zbb_zbc_zbs_xw -mabi=ilp32 -mcmodel=medany -msmall-data-limit=8 -mno-save-restore -fmax-errors=20 -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common --param=highcode-gen-section-name=1 -g -T \"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/Ld/Link.ld\"  -L\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0\" -L\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../LIB\" -L\"I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/../SRC/StdPeriphDriver\" -L\"${MYLIB_C_LIB_DIR}\"  -nostartfiles -Xlinker --gc-sections -Xlinker --print-memory-usage -Wl,-Map,\"zzztest6copy.map\" --specs=nano.specs --specs=nosys.specs -o \"lrg_mogai_dianchi-new-2-0813-tiaopin-tpl.elf\" ")

target_link_libraries(
"${TARGET_ARTIFACT}"
ISP585
CH58xBLE
${_mylib_libname}
m
)

add_custom_command(TARGET "${TARGET_ARTIFACT}" 
POST_BUILD 
COMMAND ${CMAKE_OBJCOPY} ARGS -O ihex  "zzztest6copy.elf" "zzztest6copy.hex"
COMMAND ${CMAKE_OBJDUMP} ARGS  --source --all-headers --demangle -M xw --line-numbers --wide "zzztest6copy.elf" > "zzztest6copy.lst"
COMMAND ${CMAKE_SIZE} ARGS  --format=berkeley "zzztest6copy.elf"
)


# ---- App-specific source files ----
target_sources("${TARGET_ARTIFACT}" PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/APP/hw_config.c"
)

# ---- Mixed-compilation include paths ----
# App headers first (override template), template APP/ as fallback
set(CMAKE_C_FLAGS "-I\"${CMAKE_CURRENT_SOURCE_DIR}/APP/include\" -I\"${CMAKE_CURRENT_SOURCE_DIR}/APP\" -I\"${CMAKE_CURRENT_SOURCE_DIR}\" ${CMAKE_C_FLAGS}")
target_include_directories("${TARGET_ARTIFACT}" PRIVATE
    "I:/temp/lowenergyscanner/CH585EVTx0729/CH585EVTx/EVT/EXAM/BLE/lrg_mogai_dianchi-new-2-0813-tiaopin-tpl/v1.0.0/APP"
)
