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?