cmake_minimum_required(VERSION 3.22)

#
# This file is generated only once,
# and is not re-generated if converter is called multiple times.
#
# User is free to modify the file as much as necessary
#
find_package(Python3 REQUIRED)
# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)


# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Set the project name
set(CMAKE_PROJECT_NAME externApp)

# Include toolchain file
include("cmake/gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Enable CMake support for ASM and C languages
enable_language(C ASM)

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})


add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
    # 创建 output 目录（确保存在）
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/output
    # 生成 HEX 和 BIN 文件
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/${CMAKE_PROJECT_NAME}.hex
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/${CMAKE_PROJECT_NAME}.bin
    COMMAND ${CMAKE_COMMAND} -E echo "Padding BIN to 0x100 alignment..."
    COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tool/fill_bin.py
        $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/${CMAKE_PROJECT_NAME}.bin
        ${CMAKE_SOURCE_DIR}/output/${CMAKE_PROJECT_NAME}_padded.bin
        0x100
        ${CMAKE_SOURCE_DIR}/output/${CMAKE_PROJECT_NAME}_info.txt
    COMMENT "Generate HEX and BIN files from ELF"
)

# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined library search paths
)
# 遍历 asw 目录及其子目录下的所有 .c 文件
file(GLOB_RECURSE USER_SOURCES_ASW "${PROJECT_SOURCE_DIR}/asw/*.c")
# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user sources here
    ${USER_SOURCES_ASW}
)



# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined include paths
    ${PROJECT_SOURCE_DIR}/asw/
    ${PROJECT_SOURCE_DIR}/asw/UartDiagApp/
    ${PROJECT_SOURCE_DIR}/asw/UartDiagApp/Uart2CanDiag
    ${PROJECT_SOURCE_DIR}/ASW/CherryRB
)

# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined symbols
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
    stm32cubemx

    # Add user defined libraries
)

add_custom_command(TARGET ${CMAKE_PROJECT_NAME} PRE_BUILD
    COMMAND powershell -ExecutionPolicy Bypass -File ${CMAKE_SOURCE_DIR}/tool/fresh_extern.ps1 ${CMAKE_SOURCE_DIR}/ASW/projcfg.c
    COMMENT "Executing pre-build PowerShell script"
)

# ... existing code ...
