You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.9 KiB
66 lines
2.9 KiB
cmake_minimum_required(VERSION 3.10)
|
|
|
|
get_filename_component(TARGET_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
|
|
|
|
option(A_BUILD_MAIN_AS_STATIC_LIB "Build as a static library instead of an executable" OFF)
|
|
|
|
# ─── 创建目标 ────────────────────────────────────────────────────────
|
|
if(A_BUILD_MAIN_AS_STATIC_LIB)
|
|
add_library(${TARGET_NAME} STATIC main.c)
|
|
set_target_properties(${TARGET_NAME} PROPERTIES
|
|
ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output
|
|
)
|
|
else()
|
|
add_executable(${TARGET_NAME} main.c)
|
|
set_target_properties(${TARGET_NAME} PROPERTIES
|
|
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output
|
|
)
|
|
endif()
|
|
|
|
# ─── 编译宏定义 ─────────────────────────────────────────────────────
|
|
target_compile_definitions(${TARGET_NAME} PRIVATE
|
|
$<$<CONFIG:Debug>:DEBUG>
|
|
${GLOBAL_ENABLED_MACROS}
|
|
)
|
|
|
|
# ─── Include 目录 ────────────────────────────────────────────────────
|
|
target_include_directories(${TARGET_NAME}
|
|
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
|
|
)
|
|
|
|
# ─── 链接模块 ────────────────────────────────────────────────────────
|
|
if(GLOBAL_ENABLED_MODULES)
|
|
message(STATUS "[Main] Linking modules: ${GLOBAL_ENABLED_MODULES}")
|
|
target_link_libraries(${TARGET_NAME} PUBLIC ${GLOBAL_ENABLED_MODULES})
|
|
else()
|
|
message(STATUS "[Main] No additional modules linked.")
|
|
endif()
|
|
|
|
# ─── 平台链接依赖 ───────────────────────────────────────────────────
|
|
string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME_LOWER)
|
|
if(CMAKE_CROSSCOMPILING AND NOT SYSTEM_NAME_LOWER MATCHES "linux|windows|darwin|android")
|
|
message(STATUS ">>> 裸机/RTOS 环境 (${CMAKE_SYSTEM_NAME}),屏蔽 pthread")
|
|
else()
|
|
find_package(Threads REQUIRED)
|
|
target_link_libraries(${TARGET_NAME}
|
|
PUBLIC Threads::Threads
|
|
PRIVATE ${CMAKE_DL_LIBS}
|
|
)
|
|
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
|
target_link_libraries(${TARGET_NAME} PRIVATE rt)
|
|
endif()
|
|
endif()
|
|
|
|
# ─── 编译选项 ────────────────────────────────────────────────────────
|
|
target_compile_options(${TARGET_NAME} PRIVATE
|
|
-Wall -Werror
|
|
-ffunction-sections -fdata-sections
|
|
-Wno-unused-function
|
|
$<$<CONFIG:Debug>:-g -O0>
|
|
$<$<CONFIG:Release>:-O2>
|
|
)
|
|
|
|
target_link_options(${TARGET_NAME} PRIVATE
|
|
-Wl,--gc-sections
|
|
$<$<CONFIG:Release>:-s>
|
|
)
|
|
|