-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
120 lines (101 loc) · 3.09 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
cmake_minimum_required(VERSION 3.8)
project(qt6_ros2_template VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# 控制是否使用 ROS 的选项
option(USE_ROS "Build with ROS2 support" ON)
# 检查是否在 ROS 环境中
if(DEFINED ENV{ROS_DISTRO})
message(STATUS "ROS environment detected: $ENV{ROS_DISTRO}")
set(USE_ROS ON)
else()
message(STATUS "ROS environment not detected. Building without ROS support.")
set(USE_ROS OFF)
endif()
# 尝试查找 Qt6
find_package(Qt6 COMPONENTS Widgets QUIET)
if(Qt6_FOUND)
message(STATUS "Qt6 found. Building with Qt support.")
set(USE_QT ON)
else()
message(WARNING "Qt6 not found. Building without Qt support.")
set(USE_QT OFF)
endif()
# ROS 2 依赖
if(USE_ROS)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
endif()
# Qt 相关设置
if(USE_QT)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/ui)
file(GLOB HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h *.hpp)
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp *.c)
file(GLOB UI_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ui/*.ui)
file(GLOB RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/resources/*.qrc)
add_executable(${PROJECT_NAME}
${HEADERS}
${SOURCES}
${UI_FILES}
${RESOURCES}
)
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)
if(USE_ROS)
ament_target_dependencies(${PROJECT_NAME}
rclcpp
std_msgs
)
endif()
target_link_libraries(${PROJECT_NAME}
Qt6::Widgets
)
# 为 Windows 设置 GUI 应用属性
if(WIN32)
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE TRUE)
endif()
# 为 macOS 设置 bundle 属性
if(APPLE)
set_target_properties(${PROJECT_NAME} PROPERTIES
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_GUI_IDENTIFIER com.example.${PROJECT_NAME}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
endif()
endif()
# 安装目标
if(USE_ROS)
install(TARGETS ${PROJECT_NAME}
DESTINATION lib/${PROJECT_NAME}
)
# 安装启动文件
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME}
)
ament_package()
else()
if(USE_QT)
install(TARGETS ${PROJECT_NAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
else()
message(WARNING "Neither ROS nor Qt support available. No targets will be built.")
endif()
endif()
# 添加编译定义
if(USE_ROS)
target_compile_definitions(${PROJECT_NAME} PRIVATE USE_ROS)
endif()