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:

set(COMPARATOR_MESSAGES
  msg/Foo.msg
  msg/Bar.msg
  msg/Baz.msg)

add_custom_command(
  OUTPUT include/message_comparators.hpp
  COMMAND scripts/generate_message_comparators.py
      --msg ${COMPARATOR_MESSAGES}
      --out include/message_comparators.hpp
  DEPENDS ${COMPARATOR_MESSAGES} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS} ${PROJECT_NAME}_genpy
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  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/generate_message_comparators.py", line 65, in <module>
    generate_comparator(message)
  File "scripts/generate_message_comparators.py", line 22, in generate_comparator
    i = importlib.import_module(message_namespace + ".msg")
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
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?

Thanks!

edit retag flag offensive close merge delete