ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org

# Optionally build a package with catkin

I have a package that provides a library. This library performs functions that do not explicitly rely on ROS and I want to be able to use this library in non-ROS contexts, i.e. in c++ programs I build with pure CMake.

I am looking for a way to set up my package, specifically the CMakeLists.txt, such that this package builds both as a catkin package and stand alone. When building with catkin, I want to declare the library s.t. other ROS packages can find and use it. In the pure CMake case I want to just build the library and have other packages find it themselves, e.g. with a FindMYPACKAGE.cmake.

I can already do this by manually defining my own variable in CMakeLists.txt, set(ROS 1), and then only doing catkiny stuff if that's defined. I tried using catkin_FOUND for this, but I have two problems: Even when the package is not actually in a catkin workspace and I'm not building with the catkin command, catkin_FOUND is true (on systems with ROS installed). On systems without ROS, find_package(catkin ...) naturally causes an error, so I can't check for catkin that way.

What variables exist that are already set at the top of a packages CMakeLists.txt with which I can check if catkin is the build tool in use?

edit retag close merge delete

Sort by » oldest newest most voted

I'm achieving this by checking if CATKIN_DEVEL_PREFIX is set. With this, I can detect if the package is being built with catkin even if ROS is installed on the machine and the catkin workspace is sourced (which happens in my .bashrc, so it's always sourced)

Alternative solution:

If it doesn't have to work if the catkin workspace is sourced you can also check for catkin_FOUND, as suggested by @gvdhoorn. This is a bit nicer, but it doesn't work for my needs.

more

I think it would be good to mention that you chose this solution because:

[..] I want to test both cases, with and without ROS. I have the package cloned in the catkin ws and somewhere else. For pure cmake I don't want to have to remember to 'un-source' the setup.bash. I'm just lazy.

( 2016-04-11 03:07:58 -0600 )edit

By accepting this answer, you are essentially recommending that other people do it similarly, but I think most would be better off following the approach recommended by @gvdhoorn

( 2016-04-11 10:36:52 -0600 )edit

What variables exist [..] with which I can check if catkin is the build tool in use?

I think I got confused by the title, which implies that optionally building with catkin was the requirement.

( 2016-04-11 11:17:41 -0600 )edit

I can already do this by manually defining my own variable in CMakeLists.txt, set(ROS 1), and then only doing catkiny stuff if that's defined. I tried using catkin_FOUND for this, but I have two problems: Even when the package is not actually in a catkin workspace and I'm not building with the catkin command, catkin_FOUND is true (on systems with ROS installed).

Is that even without having sourced the distribution setup.bash? A quick test on my machine shows that find_package(catkin ..) actually fails if I don't have setup.bash sourced.

On systems without ROS, find_package(catkin ...) naturally causes an error, so I can't check for catkin that way.

Unless you add the QUIET argument. As that suppresses errors, and lets you use something like:

find_package(catkin QUIET)
if (catkin_FOUND)
..
else()
..
endif()


This won't error out, even if Findcatkin.cmake isn't on the CMake search path.

As to your question: the above technique only allows you to automatically skip building the ROS parts if ROS isn't installed (or can't be found). If it is found, your catkiny stuff will always still be build. To avoid that, I don't think you can avoid adding a CMake option that can be set by your user.

more

The comment setup.bash is a good point. However, I source it in my .bashrc so it'd inconvenient if I always have to make sure that it's not sourced. I'm currently checking if CATKIN_DEVEL_PREFIX is set, which seems to work. It's fine if the ROS stuff is always built if possible.

( 2016-04-04 06:52:22 -0600 )edit

Right now I'm just having issues with linking the library when not building with catkin, although that is likely just a cmake issue.

( 2016-04-04 06:53:24 -0600 )edit

I'm a bit confused: if it's ok that "the ROS stuff is always built if possible" (ie: when catkin can be found, ie: when setup.bash is sourced), and it's also possible to detect when catkin is not found, then what is the issue with the proposed solution? Why check CATKIN_DEVEL_PREFIX?

( 2016-04-04 07:43:49 -0600 )edit

Because catkin_FOUND is true even when not building with catkin. That tells me if the setup.bash was sourced. I.e. I would have to make sure that I don't source it (which I do in .bashrc), which is inconvenient and doesn't test what I care about.

( 2016-04-04 08:46:41 -0600 )edit

I'm probably dense, but I still don't get how this does not cover your requirements: catkin_FOUND is only true if ROS is actually installed and sourced (case 1). If not, it silently fails and sets catkin_FOUND=False, in which case you build without ROS (case 2).

( 2016-04-04 09:07:33 -0600 )edit

That's true. But on my machine I want to test both cases, with and without ROS. So I have the same package cloned in the catkin ws and somewhere else. For the pure cmake case I don't want to have to remember to 'un-source' the setup.bash which I automatically source in my .bashrc. I'm just lazy.

( 2016-04-04 09:26:27 -0600 )edit