CollisionAvoidance/CMakeLists.txt

156 lines
4.7 KiB
CMake

cmake_minimum_required(VERSION 3.14...3.27)
# 设置编译器
if(NOT APPLE)
set(CMAKE_C_COMPILER "/opt/rh/devtoolset-11/root/usr/bin/gcc")
set(CMAKE_CXX_COMPILER "/opt/rh/devtoolset-11/root/usr/bin/g++")
endif()
# 设置 CMake 策略
cmake_policy(SET CMP0074 NEW) # 使用 <PackageName>_ROOT 变量
# 包含 FetchContent 模块
include(FetchContent)
project(collision_avoidance)
set(CMAKE_CXX_STANDARD 17)
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 7.0)
message(FATAL_ERROR "GCC 版本需要 >= 7.0 以支持 C++17")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
message(FATAL_ERROR "Clang 版本需要 >= 5.0 以支持 C++17")
endif()
endif()
# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# 设置 Boost
set(Boost_NO_WARN_NEW_VERSIONS 1)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
if(NOT APPLE)
# 在 CentOS 上使用 Boost 1.69
set(BOOST_ROOT "/usr/include/boost169")
set(BOOST_LIBRARYDIR "/usr/lib64/boost169")
else()
# 在 macOS 上使用 Homebrew 安装的 Boost
set(BOOST_ROOT "/opt/homebrew/Cellar/boost/1.87.0")
set(BOOST_LIBRARYDIR "/opt/homebrew/Cellar/boost/1.87.0/lib")
endif()
find_package(Boost 1.69.0 REQUIRED COMPONENTS system filesystem thread)
# 添加 libcurl
find_package(CURL REQUIRED)
# 添加 JSON 库
if(APPLE)
# macOS 使用 Homebrew 安装的 nlohmann_json
find_package(nlohmann_json 3.11.3 REQUIRED)
else()
# CentOS 使用系统安装的 nlohmann_json
find_package(nlohmann_json REQUIRED)
endif()
set(JSON_TARGET nlohmann_json::nlohmann_json)
# 源文件列表
set(LIB_SOURCES
src/collector/DataCollector.cpp
src/core/System.cpp
src/detector/CollisionDetector.cpp
src/detector/SafetyZone.cpp
src/network/HTTPDataSource.cpp
src/network/WebSocketServer.cpp
src/network/HTTPClient.cpp
src/spatial/CoordinateConverter.cpp
src/types/BasicTypes.cpp
src/types/VehicleData.cpp
src/vehicle/ControllableVehicles.cpp
src/config/AirportBounds.cpp
src/config/SystemConfig.cpp
src/config/IntersectionConfig.cpp
src/detector/TrafficLightDetector.cpp
)
# 创建主库
add_library(${PROJECT_NAME}_lib ${LIB_SOURCES})
target_include_directories(${PROJECT_NAME}_lib
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
${Boost_INCLUDE_DIRS}
${CURL_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}_lib
PUBLIC
Boost::system
${JSON_TARGET}
CURL::libcurl
)
# 创建主可执行文件
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_lib)
# 添加一个选项来控制是否编译测试
if(APPLE)
option(BUILD_TESTS "Build test cases" ON)
else()
option(BUILD_TESTS "Build test cases" OFF)
endif()
# 添加测试
if(BUILD_TESTS AND EXISTS "${CMAKE_SOURCE_DIR}/tests")
if(APPLE)
# macOS 使用 FetchContent 下载 googletest
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
)
FetchContent_MakeAvailable(googletest)
else()
# CentOS 也使用 FetchContent 下载 googletest
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
)
FetchContent_MakeAvailable(googletest)
endif()
# 添加 tests 子目录
add_subdirectory(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/system_config.json ${CMAKE_BINARY_DIR}/config/system_config.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/config/intersections.json ${CMAKE_BINARY_DIR}/config/intersections.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/config/vehicle_control.json ${CMAKE_BINARY_DIR}/config/vehicle_control.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/config/airport_bounds.json ${CMAKE_BINARY_DIR}/config/airport_bounds.json COPYONLY)
if(NOT APPLE)
target_link_libraries(collision_avoidance
PRIVATE
${PROJECT_NAME}_lib
pthread
)
endif()