Linux – cmake execute_process could not find the source command

cmake execute_process could not find the source command… here is a solution to the problem.

cmake execute_process could not find the source command

I recently had a very interesting issue with cmake.
The questions are as follows:

I have a CMakeLists .txt which defines a fairly simple project. I want to be able to set a linux environment variable when building the project. To do this, I want to call a mini-script var_set.sh to complete it. It looks like this :

#!/bin/bash
MY_VAR=$1
export MY_VAR

In CMakeLists.txt, I run it as follows:

execute_process(COMMAND source var_set.sh VALUE
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            RESULT_VARIABLE variable_RESULT)
message(STATUS ${variable_RESULT})
message(STATUS $ENV{MY_VAR})

The output of this is

-- No such file or directory

I think the problem is with the source command. If I use ./var_set.sh instead of source var_set.sh the script will actually be executed, but, of course, I can’t get the variable I want to export

Does anyone know what the reason for this behavior is?

Update:

I

began to understand that it might not have been possible to achieve what I originally wanted.

The initial thoughts and motivations are as follows. We have a relatively large project that uses ROS. It builds everything with catkin_make. What it basically does is call a cmake for all the packages it finds in the subfolder in the order defined by the CMakeLists.txt and package.xml files (it doesn’t matter how this file looks here). It also provides an alternative to using find_package and so on.

The problem is that a large part of the code is written in a way that I can’t influence. This relies in part on a library, G2O (Graphics Optimized), and G2O packages are not “catkinized”, i.e. catkin has no way of knowing what libraries are provided.

G2O itself defines quite a few libraries, which we can find through the FindG2O.cmake file (but if a library is “core”, we know it by the name “G2O_CORE_LIBRARY” set in FindG2O.cmake G2O_ROOT).

My idea is to set this environment variable when building G2O (using the catkin metadata package that surrounds it) and make this knowledge available to anyone who builds later, as this will make the use of FindG2O.cmake easy.

I understand that this explanation is messy. If there is anything you don’t understand, please ask.

It seems like what I’m trying to achieve is impossible, because catkin_make might call different cmake in different child processes, and then, of course, I can’t modify the parent process.

Solution

It’s just that you’re doing it wrong. Even if source works (but it can’t because it’s a bash internal command, not a real binary/executable present in the filesystem), it won’t affect anything! Because cmake starts a child process (via execute_process()) and it will be terminated, w/execution results are available in the corresponding variables. Setting any environment variables in the child process does not affect the parent environment.

Therefore, you are better off using $ENV{blah} to set the required variables. Or (better) just use CMake variables… There are many ways to do this. Just detail what you want to achieve.

Related Problems and Solutions