CMake custom command that depends on message generation

asked 2020-04-20 11:11:01 -0500

NickDP gravatar image

I have a catkin package that contains a bunch of custom messages. In that same package I have a python script that generates a file based on the custom message definitions. The python script imports the messages in question to generate its output file.

I am having trouble getting this to work from a clean workspace.

Here is how I have added my custom command:


  OUTPUT include/message_comparators.hpp
  COMMAND scripts/
      --out include/message_comparators.hpp
  COMMENT "Generating message comparators"

add_custom_target(part_out ALL DEPENDS include/message_comparators.hpp)

This works fine if this catkin package has already been build once. The problem I have is that when I run catkin clean --yes; catkin build my_msgs, it fails with the following error:

Traceback (most recent call last):
  File "scripts/", line 65, in <module>
  File "scripts/", line 22, in generate_comparator
    i = importlib.import_module(message_namespace + ".msg")
  File "/usr/lib/python2.7/importlib/", line 37, in import_module
ImportError: No module named my_msgs.msg
make[2]: *** [include/message_comparators.hpp] Error 1
make[1]: *** [CMakeFiles/part_out.dir/all] Error 2
make: *** [all] Error 2

I thought I fixed the dependency problems with all the DEPENDS I added. I used to have the problem that, when I e.g. added a field to Foo.msg, the output of my custom command would not reflect that the first time I ran catkin build my_package because my command apparently ran before the messages were generated. catkin build-ing a second time would generate the correct result. Since adding ${${PROJECT_NAME}_EXPORTED_TARGETS} (I think that's the only important one), this problem has been solved.

However, for some reason I still can't import the messages during the initial build.

How can I fix that?


edit retag flag offensive close merge delete