Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions cmake/gz_msgs_generate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function(gz_msgs_generate_messages_impl)
PYTHON_INTERPRETER
PROTOC_EXEC
PROTO_PACKAGE MSGS_GEN_SCRIPT GZ_PROTOC_PLUGIN FACTORY_GEN_SCRIPT PROTO_PATH
DEPENDENCY_DESCRIPTIONS
DLLEXPORT_DECL
OUTPUT_DIRECTORY
# Outputs
Expand All @@ -33,7 +32,7 @@ function(gz_msgs_generate_messages_impl)
OUTPUT_DETAIL_HEADERS
OUTPUT_PYTHON
)
set(multiValueArgs INPUT_PROTOS)
set(multiValueArgs INPUT_PROTOS DEPENDENCY_DESCRIPTIONS)

cmake_parse_arguments(generate_messages "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Expand Down Expand Up @@ -159,10 +158,9 @@ function(gz_msgs_generate_desc_impl)
# Inputs
PROTOC_EXEC
PROTO_PATH
DEPENDENCY_DESCRIPTIONS
OUTPUT_DIRECTORY
OUTPUT_FILENAME)
set(multiValueArgs INPUT_PROTOS)
set(multiValueArgs INPUT_PROTOS DEPENDENCY_DESCRIPTIONS)

cmake_parse_arguments(generate_messages "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Expand All @@ -175,17 +173,22 @@ function(gz_msgs_generate_desc_impl)
list(APPEND ARGS --descriptor_set_out=${generate_messages_OUTPUT_FILENAME})
list(APPEND ARGS --include_imports)

foreach(dep ${generate_messages_DEPENDENCY_DESCRIPTIONS})
list(APPEND ARGS --descriptor_set_in="${msgs_desc_file}")
endforeach()
if(generate_messages_DEPENDENCY_DESCRIPTIONS)
if(WIN32)
list(JOIN generate_messages_DEPENDENCY_DESCRIPTIONS "$<SEMICOLON>" _desc_joined)
else()
list(JOIN generate_messages_DEPENDENCY_DESCRIPTIONS ":" _desc_joined)
endif()
list(APPEND ARGS "--descriptor_set_in=${_desc_joined}")
endif()

list(APPEND ARGS ${generate_messages_INPUT_PROTOS})

add_custom_command(
OUTPUT ${generate_messages_OUTPUT_FILENAME}
COMMAND ${generate_messages_PROTOC_EXEC}
ARGS ${ARGS}
DEPENDS ${generate_messages_INPUT_PROTOS}
DEPENDS ${generate_messages_INPUT_PROTOS} ${generate_messages_DEPENDENCY_DESCRIPTIONS}
COMMENT "Generating descriptor set"
)
endfunction()
Expand Down Expand Up @@ -225,7 +228,17 @@ function(gz_msgs_generate_messages_lib)
set(depends_proto_paths)
set(depends_includes)
foreach(dep ${generate_messages_DEPENDENCIES})
get_target_property(msgs_desc_file ${dep} GZ_MSGS_DESC_FILE)
# Prefer the build-time path for same-project targets; fall back to
# the exported install-prefix path for imported targets.
get_target_property(msgs_desc_file ${dep} GZ_MSGS_DESC_FILE_BUILD)
if(NOT msgs_desc_file)
get_target_property(msgs_desc_file ${dep} GZ_MSGS_DESC_FILE)
endif()
if(NOT msgs_desc_file)
message(FATAL_ERROR
"Dependency target '${dep}' does not have a GZ_MSGS_DESC_FILE or "
"GZ_MSGS_DESC_FILE_BUILD property. Is it a valid gz-msgs target?")
endif()
list(APPEND depends_msgs_desc ${msgs_desc_file})
endforeach()

Expand Down Expand Up @@ -279,6 +292,7 @@ function(gz_msgs_generate_messages_lib)
SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${PROJECT_VERSION}
GZ_MSGS_DESC_FILE "\$\{_IMPORT_PREFIX\}/share/gz/protos/${generate_messages_TARGET}.gz_desc"
GZ_MSGS_DESC_FILE_BUILD "${CMAKE_CURRENT_BINARY_DIR}/${target_name}.gz_desc"
)
set_property(TARGET ${target_name} PROPERTY EXPORT_PROPERTIES "GZ_MSGS_DESC_FILE")

Expand Down
20 changes: 19 additions & 1 deletion examples/generating_custom_msgs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,27 @@ gz_msgs_generate_messages(
DEPENDENCIES gz-msgs::gz-msgs
)

# Second message library with two dependencies.
# Uses a separate proto2/ directory so that protoc cannot resolve
# gz/custom_msgs/foo.proto from the filesystem via -I; it must come
# from the dependency descriptor.
gz_msgs_generate_messages(
TARGET msgs2
PROTO_PACKAGE "gz.custom_msgs2"
MSGS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/proto2
MSGS_PROTOS
${CMAKE_CURRENT_SOURCE_DIR}/proto2/gz/custom_msgs2/qux.proto
DEPENDENCIES
${PROJECT_NAME}-msgs
gz-msgs::gz-msgs
)

add_executable(${PROJECT_NAME} main.cc)

target_link_libraries(${PROJECT_NAME} PUBLIC ${PROJECT_NAME}-msgs)
target_link_libraries(${PROJECT_NAME} PUBLIC
${PROJECT_NAME}-msgs
${PROJECT_NAME}-msgs2
)

install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib
Expand Down
11 changes: 11 additions & 0 deletions examples/generating_custom_msgs/main.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gz/custom_msgs/foo.pb.h>
#include <gz/custom_msgs/bar.pb.h>
#include <gz/custom_msgs/baz.pb.h>
#include <gz/custom_msgs2/qux.pb.h>

#include <google/protobuf/text_format.h>

Expand Down Expand Up @@ -62,4 +63,14 @@ int main()
std::cout << "===============================" << std::endl;
std::cout << "Populated Message: \n" << msg.DebugString() << std::endl;
}

{
// Use Qux from the second message library (depends on both gz.msgs and
// gz.custom_msgs).
gz::custom_msgs2::Qux qux;
qux.mutable_header()->mutable_stamp()->set_sec(42);
qux.mutable_foo()->set_value(3.14);
qux.set_extra(2.718);
std::cout << "Qux Message: \n" << qux.DebugString() << std::endl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";
package gz.custom_msgs2;

import "gz/msgs/header.proto";
import "gz/custom_msgs/foo.proto";

message Qux
{
gz.msgs.Header header = 1;
gz.custom_msgs.Foo foo = 2;
double extra = 3;
}
3 changes: 1 addition & 2 deletions tools/gz_msgs_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def main(argv=sys.argv[1:]):
cmd += [f'--proto_path={path}']

if args.dependency_proto_descs:
for path in args.dependency_proto_descs:
cmd += [f'--descriptor_set_in={path}']
cmd += [f'--descriptor_set_in={os.pathsep.join(args.dependency_proto_descs)}']

if args.generate_cpp:
cmd += [f'--plugin=protoc-gen-gzmsgs={args.gz_generator_bin}']
Expand Down
Loading