78 lines
2.3 KiB
CMake
78 lines
2.3 KiB
CMake
cmake_minimum_required(VERSION 3.10)
|
|
project(DataRouter C)
|
|
|
|
set(CMAKE_C_STANDARD 11)
|
|
|
|
# ======== 静态库 ========
|
|
add_library(DataRouter STATIC datarouter.c)
|
|
target_include_directories(DataRouter PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
|
|
|
# ======== 共享库(可选,通过 BUILD_SHARED 控制) ========
|
|
option(BUILD_SHARED "Build shared library" ON)
|
|
if(BUILD_SHARED)
|
|
add_library(DataRouter_shared SHARED datarouter.c)
|
|
target_include_directories(DataRouter_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
|
set_target_properties(DataRouter_shared PROPERTIES OUTPUT_NAME DataRouter)
|
|
endif()
|
|
|
|
# ======== 示例程序 ========
|
|
if(BUILD_EXAMPLES OR NOT DEFINED BUILD_EXAMPLES)
|
|
add_executable(example example.c)
|
|
target_link_libraries(example DataRouter)
|
|
endif()
|
|
|
|
# ======== 测试 ========
|
|
if(BUILD_TESTING OR NOT DEFINED BUILD_TESTING)
|
|
enable_testing()
|
|
add_executable(test_datarouter tests/test_datarouter.c)
|
|
target_link_libraries(test_datarouter DataRouter)
|
|
add_test(NAME datarouter COMMAND test_datarouter)
|
|
endif()
|
|
|
|
# ======== 安装规则 ========
|
|
|
|
# 静态库 → lib/
|
|
install(TARGETS DataRouter
|
|
EXPORT DataRouterTargets
|
|
ARCHIVE DESTINATION lib
|
|
)
|
|
|
|
# 共享库 → lib/(可选)
|
|
if(TARGET DataRouter_shared)
|
|
install(TARGETS DataRouter_shared
|
|
EXPORT DataRouterTargets
|
|
ARCHIVE DESTINATION lib # .lib (Windows import lib)
|
|
LIBRARY DESTINATION lib # .so
|
|
RUNTIME DESTINATION lib # .dll
|
|
)
|
|
endif()
|
|
|
|
# 示例 → bin/
|
|
if(TARGET example)
|
|
install(TARGETS example RUNTIME DESTINATION bin)
|
|
endif()
|
|
|
|
# 头文件 → include/
|
|
install(FILES datarouter.h DESTINATION include)
|
|
|
|
# 文档 → share/
|
|
install(FILES datarouter_config_template.h LICENSE DESTINATION share)
|
|
if(EXISTS ${CMAKE_SOURCE_DIR}/README.md)
|
|
install(FILES README.md DESTINATION share)
|
|
endif()
|
|
|
|
# CMake 包配置文件 → cmake/
|
|
set(INCLUDE_INSTALL_DIR include)
|
|
set(LIB_INSTALL_DIR lib)
|
|
|
|
include(CMakePackageConfigHelpers)
|
|
configure_package_config_file(
|
|
cmake/DataRouterConfig.cmake.in
|
|
${CMAKE_CURRENT_BINARY_DIR}/DataRouterConfig.cmake
|
|
INSTALL_DESTINATION cmake
|
|
PATH_VARS INCLUDE_INSTALL_DIR LIB_INSTALL_DIR
|
|
)
|
|
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/DataRouterConfig.cmake DESTINATION cmake)
|
|
install(EXPORT DataRouterTargets FILE DataRouterTargets.cmake DESTINATION cmake)
|