Run bash script in current process

This is a long shot, but I’d like to check that I haven’t missed something in the documentation before I embark on a complicated workaround.

I’m using a third-party library which, when using it in Linux, requires a bash script to be run that configures various environment variables which need to be set when compilation and linking take place.

I thought this would be simple: I created a very small CMake script, let’s call it RunScript.cmake, containing just:

execute_process(COMMAND script.sh)
message("The script has been run")

…and then used add_custom_target to ensure my script was run at build time:

add_custom_target(runLibraryScript
                  COMMAND ${CMAKE_COMMAND} -P RunScript.cmake
                  COMMENT "Running initialisation script")

Having made ‘runLibraryScript’ a dependency of the first target to be built, the script runs nicely. Then, needless to say, compilation fails because execute_process didn’t run the script in the current process, but in a child process: so all the environment variables created by the script were immediately lost when control returned to the current process.

Is there any way for CMake to run a shell script in the same process as it’s going to use for the build?

Otherwise, I’m faced with having to embed my shell script in a second shell script which which run the first script in its own process, then output all its environment variable to a file, then get CMake (still at build time) to parse the file, extract the environment variables, and re-set them in the build process. That looks like more than the work of an afternoon.

So is there a simpler way that I’ve missed?

You can’t have a script modify the environment of its parent process.

CMake expects you to run both cmake and the build tool in a shell where the environment is already fully prepared. If you are running a script of some kind in a build step, you can incorporate logic in that script to modify that script’s environment, but not the environment of the whole build process.

Yes, I had a nasty feeling that that was the case. I suppose what I was hoping for was some equivalent of execute_process that ‘sourced’ the script instead of running it.

Never mind; I’ll have to think of something else.

Thank you.