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

Run script at colcon build

asked 2022-09-13 07:12:11 -0500

sharp-0000ff gravatar image

updated 2022-09-13 08:33:18 -0500

HI everyone. I need your help.

I want to run a script during the build of a project that is inside a package, is there any way to do this?

In ROS1(Melodic) I used this code:

CMakeLists.txt

add_custom_target(my_script ALL)
add_custom_command(
    TARGET my_script
    COMMAND sh -c ". ${CATKIN_DEVEL_PREFIX}/setup.sh && \
    scripts/my_script.py"
    VERBATIM
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

But in ROS2(Foxy) the approach to building has changed and it doesn't work anymore.

I tried to use it to just check if the script will work during the build, but the project does not have time to build (I'm not sure).

CMakeLists.txt

add_custom_target(my_script ALL)
add_custom_command(
TARGET my_script
     COMMAND sh -c ". /path/to/project/install/setup.sh && \
     /path/to/project/install/my_package/lib/my_package/my_script.py"
     #VERBATIM
     #WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}

)

I get an error:

The first start colcon build.

--- stderr: my_package                         
sh: 1: .: Cant open /path/to/project/install/setup.sh
make[2]: *** [CMakeFiles/my_script.dir/build.make:57: my_script] Error 127
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/my_script.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
---
Failed   <<< my_package [1.28s, exited with code 2]                            
Summary: 2 packages finished [1.44s]
1 package failed: my_package
1 package had stderr output: my_package

The second start colcon build.

--- stderr: my_package
not found: "/path/to/project/install/my_package/share/my_package/local_setup.sh"
sh: 1: /path/to/project/install/my_package/lib/my_package/my_script.py: not found
make[2]: *** [CMakeFiles/my_script.dir/build.make:57: my_script] Error 127
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/my_script.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
---
Failed   <<< my_package [0.18s, exited with code 2]
Summary: 2 packages finished [0.34s]
1 package failed: my_package
1 package had stderr output: my_package
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-14 04:13:07 -0500

ljaniec gravatar image

updated 2022-09-14 04:14:13 -0500

I think this can be related to your problem:

add_custom_command(
  TARGET ${PROJECT_NAME}
  POST_BUILD
  COMMAND echo "FOO"
  COMMENT ""
  VERBATIM)

It seems the syntax is a bit different + a POST_BUILD flag:

POST_BUILD

Run after all other rules within the target have been executed.
edit flag offensive delete link more

Comments

I added the POST_BUILD flag, but I still get the same errors.

add_custom_target(my_script ALL)
add_custom_command(
    TARGET my_script
    POST_BUILD
    COMMAND sh -c ". /path/to/project/install/setup.sh && \
    /path/to/project/install/my_package/lib/my_package/my_script.py"
    VERBATIM
)

I have a feeling that the command is not executed after the package is built

sharp-0000ff gravatar image sharp-0000ff  ( 2022-09-14 05:45:44 -0500 )edit

Can you start debugging with a simple COMMAND echo "Hello"? So we can see if this is executed at all after colcon build

ljaniec gravatar image ljaniec  ( 2022-09-14 06:30:50 -0500 )edit

Hmm, I don't see "hello" in terminal after build

sharp-0000ff gravatar image sharp-0000ff  ( 2022-09-14 07:09:40 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2022-09-13 07:12:11 -0500

Seen: 405 times

Last updated: Sep 14 '22