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

Revision history [back]

click to hide/show revision 1
initial version

Your approach described in the question was almost right. As the CMake error indicates the path you specified as catkin_package(INCLUDE_DIRS ...) must exist when the function is being invoked. Therefore you have to move the MAKE_DIRECTORY line above the function. But since the function is also responsible to define the catkin DESTINATION variables you need to get them explicitly before using catkin_destinations().

Further more you need to export your target which is responsible to generate the headers in order for downstream packages be able to cleanly depend on them being generated before. Usually you should pass EXPORTED_TARGETS ${PROJECT_NAME}_generate_headers to catkin (but due to a bug until now you have to set the variable ${PROJECT_NAME}_EXPORTED_TARGETS instead).

Last but not least you need to install the header files.

The complete example would then look like this:

cmake_minimum_required(VERSION 2.8.3)
project(datatypes)

find_package(catkin REQUIRED)

catkin_destinations()
file(MAKE_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_INCLUDE_DESTINATION})
file(GLOB DATATYPE_RAW ${PROJECT_SOURCE_DIR}/datatypes/*)

# using a better target name for the custom target
add_custom_target(${PROJECT_NAME}_generate_headers
  ALL
  COMMAND generate_some_header_files_to ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  SOURCES ${DATATYPE_RAW}
)
set(${PROJECT_NAME}_EXPORTED_TARGETS ${PROJECT_NAME}_generate_headers)

catkin_package(
  INCLUDE_DIRS ${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}
  # instead of set(${PROJECT_NAME}_EXPORTED_TARGETS ...)
  # but will only work as of catkin 0.5.81
  # EXPORTED_TARGETS ${PROJECT_NAME}_generate_headers
)

# no need for patterns / excludes since the path should only contain the generated headers
# by using the directory name with a slash at the end
# the directory name and destination become more natural
# (both mentioning CATKIN_PACKAGE_INCLUDE_DESTINATION)
install(
  DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_INCLUDE_DESTINATION}/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)