CollisionAvoidance/CMakeLists.txt

128 lines
3.5 KiB
CMake

cmake_minimum_required(VERSION 3.14...3.27)
# 设置 CMake 策略
cmake_policy(SET CMP0074 NEW) # 使用 <PackageName>_ROOT 变量
cmake_policy(SET CMP0167 NEW) # 使用新的 Boost 查找模块
cmake_policy(SET CMP0135 NEW) # 设置解压时间戳行为
project(airport_collision_avoidance)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 检查编译器版本
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
message(FATAL_ERROR "GCC 版本需要 >= 10.0 以支持 C++20")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 13.0)
message(FATAL_ERROR "Clang 版本需要 >= 13.0 以支持 C++20")
endif()
endif()
# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# 设置 Boost
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_ROOT /opt/homebrew/Cellar/boost/1.86.0_2)
find_package(Boost REQUIRED COMPONENTS system)
# 添加 nlohmann_json
include(FetchContent)
FetchContent_Declare(
json
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
)
FetchContent_MakeAvailable(json)
# 源文件列表
set(LIB_SOURCES
src/collector/DataCollector.cpp
src/core/System.cpp
src/detector/CollisionDetector.cpp
src/network/HTTPDataSource.cpp
src/spatial/AirportBounds.cpp
src/spatial/CoordinateConverter.cpp
src/types/BasicTypes.cpp
src/types/VehicleData.cpp
src/vehicle/ControllableVehicles.cpp
)
# 创建主库
add_library(${PROJECT_NAME}_lib ${LIB_SOURCES})
target_include_directories(${PROJECT_NAME}_lib
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
${Boost_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}_lib
PUBLIC
Boost::system
nlohmann_json::nlohmann_json
)
# 创建主可执行文件
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_lib)
# 添加测试
if(EXISTS "${CMAKE_SOURCE_DIR}/tests")
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
)
FetchContent_MakeAvailable(googletest)
enable_testing()
# 测试源文件列表
set(TEST_SOURCES
tests/CollisionDetectorTest.cpp
tests/BasicTypesTest.cpp
tests/HTTPDataSourceTest.cpp
tests/DataCollectorTest.cpp
)
# 创建测试可执行文件
add_executable(unit_tests ${TEST_SOURCES})
target_link_libraries(unit_tests
PRIVATE
${PROJECT_NAME}_lib
GTest::gtest_main
GTest::gmock_main
)
# 添加测试
include(GoogleTest)
gtest_discover_tests(unit_tests)
endif()
# 打印配置信息
message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message(STATUS "PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS}")
# 复制配置文件到构建目录
configure_file(
${CMAKE_SOURCE_DIR}/config/airport_bounds.json
${CMAKE_BINARY_DIR}/bin/config/airport_bounds.json
COPYONLY
)
configure_file(
${CMAKE_SOURCE_DIR}/config/controllable_vehicles.json
${CMAKE_BINARY_DIR}/bin/config/controllable_vehicles.json
COPYONLY
)