ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Generating a .msg file dynamically at build time

asked 2020-07-27 18:26:49 -0500

rgov gravatar image

updated 2022-01-22 16:16:17 -0500

Evgeny gravatar image

I have a non-ROS type definition file and a tool that translates these definitions to .msg files. I would catkin to first run the translation to produce the .msgs, and then add them to the package.

My attempt looked something like this:

set(GENERATED_MSGS_DIR "${CMAKE_CURRENT_BINARY_DIR}/msg")
add_custom_command(
    OUTPUT ${GENERATED_MSGS_DIR}
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/translate.py
            -o ${GENERATED_MSGS_DIR}
            ${CMAKE_CURRENT_SOURCE_DIR}/types.txt
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

file(GLOB_RECURSE GENERATED_MSG_FILES RELATIVE ${GENERATED_MSGS_DIR} *.msg)
add_message_files(FILES ${GENERATED_MSG_FILES})
generate_messages()

However this doesn't quite work. CMake will only run the custom command if an output file is needed by a target that gets built. In this case, I don't know the name of the .msg files that will come out of the translator, just the directory they'll be added to. And add_message_files also doesn't seem to like getting files outside of the package's msg directory.

As an alternative I could have the translate script produce a complete ROS package, if there's a way to dynamically add that package directory as a dependency.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-07-28 11:14:01 -0500

rgov gravatar image

I now use execute_process which runs the command when CMake is invoked to configure the process. This is OK but it does not track the dependency of the generated messages on the source types file.

set(GENERATED_MSGS_DIR "${CMAKE_CURRENT_BINARY_DIR}/msg")
execute_process(
    COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/translate.py"
            -o "${GENERATED_MSGS_DIR}"
            "${CMAKE_CURRENT_SOURCE_DIR}/types.txt"
    RESULT_VARIABLE COMMAND_RESULT
)
if(NOT COMMAND_RESULT EQUAL 0)
    message(FATAL_ERROR "Translation command failed.")
endif()

add_message_files(DIRECTORY "${GENERATED_MSGS_DIR}")
generate_messages()
edit flag offensive delete link more

Comments

1

You might want to use add_custom_command instead where you can specify DEPENDSexplicitly. As an alternative you might want to "stamp" your input files (see https://github.com/ament/ament_cmake/...) which will ensure CMake to reconfigure in case they change.

Dirk Thomas gravatar image Dirk Thomas  ( 2020-07-28 11:26:22 -0500 )edit

@Dirk I would rather do that, but what would I put under DEPENDS? Note I want generate_messages to depend on the output of my command, not the other way around. I thought I would use OUTPUT but I don't know how to wire it through to add_message_files etc.

rgov gravatar image rgov  ( 2020-07-28 12:24:22 -0500 )edit
1

DEPENDS would be the input files you use to derive the msg files from. OUTPUT would then indeed be the msg files.

Dirk Thomas gravatar image Dirk Thomas  ( 2020-07-28 12:31:29 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2020-07-27 18:26:49 -0500

Seen: 176 times

Last updated: Jul 28 '20