Skip to content

Commit

Permalink
Support standalone executable in gz-msgs11 (#357)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll authored Jan 3, 2024
1 parent 171e1bd commit c93aab8
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 414 deletions.
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
15 changes: 1 addition & 14 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ install(FILES ${msgs_python} DESTINATION ${GZ_PYTHON_INSTALL_PATH}/gz/msgs${PROJ
gz_get_libsources_and_unittests(sources gtest_sources)

gz_create_core_library(SOURCES
src/gz.cc
src/Factory.cc
src/MessageFactory.cc
src/DynamicFactory.cc
Expand Down Expand Up @@ -121,17 +120,5 @@ target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME}
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
)

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

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}>")

set(cmd_script_generated "${CMAKE_CURRENT_BINARY_DIR}/cmd${GZ_DESIGNATION}${PROJECT_VERSION_MAJOR}.rb")
set(cmd_script_configured "${cmd_script_generated}.configured")

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)

#===============================================================================
# Bash completion

# Tack version onto and install the bash completion script
configure_file(
"msgs.bash_completion.sh"
"${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh
DESTINATION
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${GZ_TOOLS_VER}.completion.d)
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
134 changes: 134 additions & 0 deletions core/cmd/msgs_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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 <iostream>
#include <string>
#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 (const 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"};

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);
}
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

0 comments on commit c93aab8

Please sign in to comment.