Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support standalone executable in gz-msgs11 #357

Merged
merged 10 commits into from
Jan 3, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/ci/packages.apt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
libgz-cmake4-dev
libgz-math8-dev
libgz-tools2-dev
libgz-utils3-cli-dev
libprotobuf-dev
libprotoc-dev
libtinyxml2-dev
Expand Down
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ gz_find_package(GzProtobuf

#--------------------------------------
# Find gz-utils
gz_find_package(gz-utils3 REQUIRED)
gz_find_package(gz-utils3 REQUIRED COMPONENTS cli)
set(GZ_UTILS_VER ${gz-utils3_VERSION_MAJOR})

#--------------------------------------
Expand All @@ -96,7 +96,7 @@ set(GZ_TOOLS_VER 1)

#--------------------------------------
# Find Tinyxml2
gz_find_package(TINYXML2 REQUIRED PRIVATE PRETTY tinyxml2)
gz_find_package(TINYXML2 REQUIRED PRETTY tinyxml2)

#--------------------------------------
# Find DL if doing relocatable installation
Expand Down Expand Up @@ -172,7 +172,6 @@ install(
configure_file(${CMAKE_SOURCE_DIR}/api.md.in ${CMAKE_BINARY_DIR}/api.md)
configure_file(${CMAKE_SOURCE_DIR}/tutorials.md.in ${CMAKE_BINARY_DIR}/tutorials.md)


gz_create_docs(
API_MAINPAGE_MD "${CMAKE_BINARY_DIR}/api.md"
TUTORIALS_MAINPAGE_MD "${CMAKE_BINARY_DIR}/tutorials.md"
Expand Down
39 changes: 29 additions & 10 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,35 @@ target_include_directories(${PROJECT_LIBRARY_TARGET_NAME}
SYSTEM PUBLIC $<TARGET_PROPERTY:protobuf::libprotobuf,INTERFACE_INCLUDE_DIRECTORIES>)

##################################################
# Build unit tests
# Build the unit tests.
gz_build_tests(TYPE UNIT
SOURCES
${gtest_sources}
LIB_DEPS
TINYXML2::TINYXML2
# Generate messages library to be use for command line tools
# and integration tests.
# This message library WILL NOT be installed and is not part of ABI/API
include(${PROJECT_SOURCE_DIR}/cmake/gz_msgs_string_utils.cmake)
include(${PROJECT_SOURCE_DIR}/cmake/gz_msgs_factory.cmake)
include(${PROJECT_SOURCE_DIR}/cmake/gz_msgs_generate.cmake)
include(${PROJECT_SOURCE_DIR}/cmake/gz_msgs_protoc.cmake)

file (GLOB proto_files ${PROJECT_SOURCE_DIR}/proto/gz/msgs/*.proto)

# Using the impl, rather than the extras function because we are building in-source
gz_msgs_generate_messages_impl(
MSGS_GEN_SCRIPT
${PROJECT_SOURCE_DIR}/tools/gz_msgs_generate.py
FACTORY_GEN_SCRIPT
${PROJECT_SOURCE_DIR}/tools/gz_msgs_generate_factory.py
GZ_PROTOC_PLUGIN
$<TARGET_FILE:${PROJECT_NAME}_protoc_plugin>
INPUT_PROTOS
${proto_files}
PROTO_PACKAGE
"gz.msgs"
PROTO_PATH
${PROJECT_SOURCE_DIR}/proto
MSGS_LIB
gz-msgs${PROJECT_VERSION_MAJOR}
TARGET
gz_msgs_msgs
)

add_subdirectory(include/gz/msgs)
if(NOT WIN32)
add_subdirectory(src/cmd)
endif()
add_subdirectory(cmd)
25 changes: 25 additions & 0 deletions core/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
set(msgs_executable gz-msgs)
add_executable(${msgs_executable} msgs_main.cc)
target_link_libraries(${msgs_executable}
gz_msgs_msgs
gz-utils${GZ_UTILS_VER}::cli
${PROJECT_LIBRARY_TARGET_NAME}
)

set(EXE_INSTALL_DIR ${CMAKE_INSTALL_LIBEXECDIR}/gz/${GZ_DESIGNATION}${PROJECT_VERSION_MAJOR}/)
install(TARGETS ${msgs_executable} DESTINATION ${EXE_INSTALL_DIR})
set(executable_location "../../../${EXE_INSTALL_DIR}/$<TARGET_FILE_NAME:${msgs_executable}>")

message(STATUS ${EXE_INSTALL_DIR})
message(STATUS ${executable_location})

configure_file(
"cmd${GZ_DESIGNATION}.rb.in"
"${cmd_script_configured}"
@ONLY)

file(GENERATE
OUTPUT "${cmd_script_generated}"
INPUT "${cmd_script_configured}")

install(FILES ${cmd_script_generated} DESTINATION lib/ruby/gz)
49 changes: 49 additions & 0 deletions core/cmd/cmdmsgs.rb.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env ruby

# Copyright (C) 2016 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'open3'
require 'pathname'

COMMANDS = {
'msg' => '@executable_location@'
}.freeze

# Class for the Gazebo msgs command line tools.
#
class Cmd
def execute(args)
exe_name = COMMANDS[args[0]]

unless Pathname.new(exe_name).absolute?
# We're assuming that the library path is relative to the current
# location of this script.
exe_name = File.expand_path(File.join(File.dirname(__FILE__), LIBRARY_NAME))
end

# Drop command from list of arguments
Open3.popen2e(exe_name, *args[1..1]) do |_in, out_err, wait_thr|
begin
out_err.each do |line|
print line
end
exit(wait_thr.value.exitstatus)
rescue Interrupt => e
print e.message
exit(-1)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ GZ_MSGS_COMPLETION_LIST="
-i --info
-l --list
-h --help
--force-version
--versions
--version
"

function _gz_msg
Expand Down
135 changes: 135 additions & 0 deletions core/cmd/msgs_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <string>
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
#include <vector>

#include <gz/utils/cli/CLI.hpp>
#include <gz/utils/cli/GzFormatter.hpp>

#include <gz/msgs/config.hh>
#include <gz/msgs/Factory.hh>
#include <gz/msgs/MessageTypes.hh>

//////////////////////////////////////////////////
/// \brief Enumeration of available commands
enum class MsgCommand
{
kNone,
kMsgInfo,
kMsgList,
};

//////////////////////////////////////////////////
/// \brief Structure to hold all available message options
struct MsgOptions
{
/// \brief Command to execute
MsgCommand command {MsgCommand::kNone};

/// \brief List of messages for message info command
std::vector<std::string> msgNames;
};

//////////////////////////////////////////////////
void runMsgInfo(const MsgOptions &_opt)
{
for (auto msgName: _opt.msgNames)
{
auto msg = gz::msgs::Factory::New(msgName);
if (msg)
{
auto descriptor = msg->GetDescriptor();
auto fileDescriptor = descriptor->file();
std::cout << "Name: " << descriptor->full_name() << std::endl;
std::cout << "File: " << fileDescriptor->name() << std::endl << std::endl;
std::cout << descriptor->DebugString() << std::endl;
}
else
{
std::cerr << "Unable to create message of type[" << msgName << "]\n";
}
}
}

//////////////////////////////////////////////////
void runMsgList(const MsgOptions &/*_opt*/)
{
std::vector<std::string> types;
gz::msgs::Factory::Types(types);

for (auto const &type : types)
std::cout << type << std::endl;
}

//////////////////////////////////////////////////
void runMsgCommand(const MsgOptions &_opt)
{
switch(_opt.command)
{
case MsgCommand::kMsgInfo:
runMsgInfo(_opt);
break;
case MsgCommand::kMsgList:
runMsgList(_opt);
break;
case MsgCommand::kNone:
default:
// In the event that there is no command, display help
throw CLI::CallForHelp();
break;
}
}

//////////////////////////////////////////////////
void addMsgFlags(CLI::App &_app)
{
auto opt = std::make_shared<MsgOptions>();

auto listOpt = _app.add_flag_callback("-l,--list",
[opt](){
opt->command = MsgCommand::kMsgList;
}, "List all message types.");

auto infoOpt = _app.add_option("-i,--info",
opt->msgNames, "Get info about the specified message type.")
->excludes(listOpt);

_app.callback([opt, infoOpt](){
if(infoOpt->count() > 0) {
opt->command = MsgCommand::kMsgInfo;
}
runMsgCommand(*opt);
});
}

//////////////////////////////////////////////////
int main(int argc, char** argv)
{
CLI::App app{"Print information about Gazebo messages"};

gz::msgs::RegisterAll();

app.add_flag_callback("-v,--version", [](){
std::cout << GZ_MSGS_VERSION_FULL << std::endl;
throw CLI::Success();
});

addMsgFlags(app);
app.formatter(std::make_shared<GzFormatter>(&app));
CLI11_PARSE(app, argc, argv);
}
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 0 additions & 35 deletions core/include/gz/msgs/gz.hh

This file was deleted.

58 changes: 0 additions & 58 deletions core/src/cmd/CMakeLists.txt

This file was deleted.

Loading