-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
365 lines (293 loc) · 11.6 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
cmake_minimum_required(VERSION 3.18)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
project(gz-python)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
#============================================================================
# Options
option(BUILD_TESTS "Build tests." OFF)
#============================================================================
# Find Python
#
# macOS: $ brew install python
# macOS: $ brew install pybind11
#
find_package(Python 3.10 EXACT COMPONENTS Interpreter Development)
#============================================================================
# Find Gazebo dependencies
# Default versions to Gazebo Garden
set(GZ_MSGS_VER 9)
set(GZ_TRANSPORT_VER 12)
if("$ENV{GZ_VERSION}" STREQUAL "garden")
# Garden
message(STATUS "Compiling against Gazebo Garden")
else()
# Default to Garden
message(STATUS "Compiling against Gazebo Garden")
endif()
find_package(gz-msgs${GZ_MSGS_VER} REQUIRED)
find_package(gz-transport${GZ_TRANSPORT_VER} REQUIRED)
#============================================================================
# Build dependencies
set(_protobuf_repository "https://github.com/protocolbuffers/protobuf.git")
set(_protobuf_version 21.5)
set(_protobuf_tag v21.5)
set(_pybind11_protobuf_repository "https://github.com/pybind/pybind11_protobuf.git")
set(_pybind11_protobuf_version 0.0.0)
set(_pybind11_protobuf_tag main)
add_subdirectory(cmake/dependencies dependencies)
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_BINARY_DIR}/dependencies/install)
message("pybind11_protobuf_FOUND: ${pybind11_protobuf_FOUND}")
message("pybind11_protobuf_SOURCE_DIR: ${pybind11_protobuf_SOURCE_DIR}")
message("pybind11_protobuf_INCLUDE_DIRS: ${pybind11_protobuf_INCLUDE_DIRS}")
# TODO fix upstream...
set(pybind11_protobuf_INCLUDE_DIRS ${pybind11_protobuf_SOURCE_DIR})
#============================================================================
# gz_msgs_extras_lib C++ library
add_library(gz_msgs_extras_lib SHARED
src/pybind11/gz/msgs/extras.cc
)
target_link_libraries(gz_msgs_extras_lib
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
)
target_include_directories(gz_msgs_extras_lib
PRIVATE
${PROJECT_SOURCE_DIR}/include
)
#============================================================================
# msgs extras Python extension module
pybind11_add_module(extras MODULE
src/pybind11/gz/msgs/_gz_msgs_extras_pybind11.cc
)
target_link_libraries(extras
PRIVATE
gz_msgs_extras_lib
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
protobuf::libprotobuf
pybind11_native_proto_caster
)
target_include_directories(extras
PRIVATE
${PROJECT_SOURCE_DIR}/include
${pybind11_protobuf_INCLUDE_DIRS}
)
add_dependencies(extras
gz_msgs_extras_lib
)
set_target_properties(extras
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python/gz/msgs"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python/gz/msgs"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python/gz/msgs"
)
#============================================================================
# transport Python extension module
pybind11_add_module(transport MODULE
src/pybind11/gz/transport/_gz_transport_pybind11.cc
)
target_link_libraries(transport
PRIVATE
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
gz-transport${GZ_TRANSPORT_VER}::gz-transport${GZ_TRANSPORT_VER}
protobuf::libprotobuf
pybind11_native_proto_caster
)
target_include_directories(transport
PRIVATE
${protobuf_INCLUDE_DIRS}
${pybind11_protobuf_INCLUDE_DIRS}
)
set_target_properties(transport
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python/gz"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python/gz"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python/gz"
)
#============================================================================
# main
add_executable(main src/main.cc)
target_link_libraries(main
gz_msgs_extras_lib
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
)
target_include_directories(main
PRIVATE
${PROJECT_SOURCE_DIR}/include
)
add_dependencies(main
gz_msgs_extras_lib
)
#============================================================================
# msg_example
add_executable(msg_example src/msg_example.cc)
target_link_libraries(msg_example
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
)
#============================================================================
# publisher
add_executable(publisher src/publisher.cc)
target_link_libraries(publisher
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
gz-transport${GZ_TRANSPORT_VER}::gz-transport${GZ_TRANSPORT_VER}
)
#============================================================================
# subscriber
add_executable(subscriber src/subscriber.cc)
target_link_libraries(subscriber
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
gz-transport${GZ_TRANSPORT_VER}::gz-transport${GZ_TRANSPORT_VER}
)
#============================================================================
# rover_publisher
add_executable(rover_publisher src/rover_publisher.cc)
target_link_libraries(rover_publisher
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
gz-transport${GZ_TRANSPORT_VER}::gz-transport${GZ_TRANSPORT_VER}
)
#============================================================================
# rover_subscriber
add_executable(rover_subscriber src/rover_subscriber.cc)
target_link_libraries(rover_subscriber
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
gz-transport${GZ_TRANSPORT_VER}::gz-transport${GZ_TRANSPORT_VER}
)
#============================================================================
# gz_topic_echo
add_executable(gz_topic_echo src/gz_topic_echo.cc)
target_link_libraries(gz_topic_echo
gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER}
gz-transport${GZ_TRANSPORT_VER}::gz-transport${GZ_TRANSPORT_VER}
)
#============================================================================
# generate protobuf Python bindings
# Get the proto files from the installed location of gz-msgs
set(PROTO_PATH ${gz-msgs${GZ_MSGS_VER}_INCLUDE_DIRS})
file(GLOB PROTO_FILES ${PROTO_PATH}/gz/msgs/*.proto)
#============================================================================
# Modified from gz-msgs/src/CMakeLists.txt for Python
#============================================================================
# A function that calls protoc on a protobuf file
# Options:
# GENERATE_RUBY - generates ruby code for the message if specified
# GENERATE_CPP - generates c++ code for the message if specified
# GENERATE_PY - generates Python code for the message if specified
# One value arguments:
# PROTO_PACKAGE - Protobuf package the file belongs to (e.g. ".gz.msgs")
# PROTOC_EXEC - Path to protoc
# INPUT_PROTO - Path to the input .proto file
# OUTPUT_CPP_DIR - Path where C++ files are saved
# OUTPUT_RUBY_DIR - Path where Ruby files are saved
# OUTPUT_PY_DIR - Path where Python files are saved
# OUTPUT_CPP_HH_VAR - A CMake variable name containing a list that the C++ header path should be appended to
# OUTPUT_CPP_CC_VAR - A CMake variable name containing a list that the C++ source path should be appended to
# OUTPUT_RUBY_VAR - A CMake variable name containing a list that the ruby file should be appended to
# OUTPUT_PY_VAR - A CMake variable name containing a list that the Python file should be appended to
# Multi value arguments
# PROTO_PATH - Passed to protoc --proto_path
function(gz_msgs_protoc)
set(options GENERATE_RUBY GENERATE_CPP GENERATE_PY)
set(oneValueArgs PROTO_PACKAGE PROTOC_EXEC INPUT_PROTO OUTPUT_CPP_DIR OUTPUT_RUBY_DIR OUTPUT_PY_DIR OUTPUT_CPP_HH_VAR OUTPUT_CPP_CC_VAR OUTPUT_RUBY_VAR OUTPUT_PY_VAR)
set(multiValueArgs PROTO_PATH)
cmake_parse_arguments(gz_msgs_protoc "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
get_filename_component(ABS_FIL ${gz_msgs_protoc_INPUT_PROTO} ABSOLUTE)
get_filename_component(FIL_WE ${gz_msgs_protoc_INPUT_PROTO} NAME_WE)
set(protoc_args)
set(output_files)
foreach(proto_path ${gz_msgs_protoc_PROTO_PATH})
list(APPEND protoc_args "--proto_path=${proto_path}")
endforeach()
set(proto_package_dir ".")
if(gz_msgs_protoc_PROTO_PACKAGE)
string(REPLACE "." "/" proto_package_dir ${gz_msgs_protoc_PROTO_PACKAGE})
endif()
if(gz_msgs_protoc_GENERATE_CPP)
set(output_header "${gz_msgs_protoc_OUTPUT_CPP_DIR}${proto_package_dir}/${FIL_WE}.pb.h")
set(output_source "${gz_msgs_protoc_OUTPUT_CPP_DIR}${proto_package_dir}/${FIL_WE}.pb.cc")
list(APPEND ${gz_msgs_protoc_OUTPUT_CPP_HH_VAR} ${output_header})
list(APPEND ${gz_msgs_protoc_OUTPUT_CPP_CC_VAR} ${output_source})
list(APPEND output_files ${output_header})
list(APPEND output_files ${output_source})
list(APPEND protoc_args "--plugin=protoc-gen-gzmsgs=${GZ_MSGS_GEN_EXECUTABLE}")
list(APPEND protoc_args "--cpp_out=dllexport_decl=GZ__MSGS_VISIBLE:${gz_msgs_protoc_OUTPUT_CPP_DIR}")
list(APPEND protoc_args "--gzmsgs_out" "${gz_msgs_protoc_OUTPUT_CPP_DIR}")
set(${gz_msgs_protoc_OUTPUT_CPP_HH_VAR} ${${gz_msgs_protoc_OUTPUT_CPP_HH_VAR}} PARENT_SCOPE)
set(${gz_msgs_protoc_OUTPUT_CPP_CC_VAR} ${${gz_msgs_protoc_OUTPUT_CPP_CC_VAR}} PARENT_SCOPE)
endif()
if(gz_msgs_protoc_GENERATE_RUBY)
file(MAKE_DIRECTORY ${gz_msgs_protoc_OUTPUT_RUBY_DIR})
set(output_ruby "${gz_msgs_protoc_OUTPUT_RUBY_DIR}${proto_package_dir}/${FIL_WE}_pb.rb")
list(APPEND ${gz_msgs_protoc_OUTPUT_RUBY_VAR} ${output_ruby})
list(APPEND output_files ${output_ruby})
list(APPEND protoc_args "--ruby_out=${gz_msgs_protoc_OUTPUT_RUBY_DIR}")
set(${gz_msgs_protoc_OUTPUT_RUBY_VAR} ${${gz_msgs_protoc_OUTPUT_RUBY_VAR}} PARENT_SCOPE)
endif()
if(gz_msgs_protoc_GENERATE_PY)
file(MAKE_DIRECTORY ${gz_msgs_protoc_OUTPUT_PY_DIR})
set(output_py "${gz_msgs_protoc_OUTPUT_PY_DIR}${proto_package_dir}/${FIL_WE}_pb2.py")
list(APPEND ${gz_msgs_protoc_OUTPUT_PY_VAR} ${output_py})
list(APPEND output_files ${output_py})
list(APPEND protoc_args "--python_out=${gz_msgs_protoc_OUTPUT_PY_DIR}")
set(${gz_msgs_protoc_OUTPUT_PY_VAR} ${${gz_msgs_protoc_OUTPUT_PY_VAR}} PARENT_SCOPE)
endif()
#get_cmake_property(_variableNames VARIABLES)
#foreach (_variableName ${_variableNames})
# message(STATUS "${_variableName}=${${_variableName}}")
#endforeach()
add_custom_command(
OUTPUT
${output_files}
COMMAND
${gz_msgs_protoc_PROTOC_EXEC}
ARGS
${protoc_args}
${ABS_FIL}
DEPENDS
${ABS_FIL}
# gz_msgs_gen
COMMENT "Running protoc on ${gz_msgs_protoc_INPUT_PROTO}"
VERBATIM)
endfunction()
#============================================================================
# run the code generation
foreach(proto_file ${PROTO_FILES})
gz_msgs_protoc(
PROTO_PACKAGE
.gz.msgs
# GENERATE_CPP
# GENERATE_RUBY
GENERATE_PY
INPUT_PROTO
${proto_file}
PROTOC_EXEC
protobuf::protoc
OUTPUT_CPP_DIR
"${PROJECT_BINARY_DIR}/include"
OUTPUT_RUBY_DIR
"${PROJECT_BINARY_DIR}/ruby"
OUTPUT_PY_DIR
"${PROJECT_BINARY_DIR}/python"
OUTPUT_CPP_HH_VAR
gen_headers
OUTPUT_CPP_CC_VAR
gen_sources
OUTPUT_RUBY_VAR
gen_ruby_scripts
OUTPUT_PY_VAR
gen_py_scripts
PROTO_PATH
"${PROTO_PATH}")
endforeach()
#============================================================================
# add custom target to force .proto compilation
set_source_files_properties(
${gen_headers}
${gen_sources}
${gen_ruby_scripts}
${gen_py_scripts}
PROPERTIES GENERATED TRUE
)
foreach(gen_py_script ${gen_py_scripts})
get_filename_component(FIL_WE ${gen_py_script} NAME_WE)
add_custom_target(${FIL_WE} ALL DEPENDS ${gen_py_script})
endforeach()