Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - change current directory ( for example: project/build ) from CMakeLists.txt?

Tags:

linux

bash

cmake

I'm building project with CMake. While configuration and building I'm in directory project/build . How can I change the directory in CMake and execute a bash script from another directory.

execute_process( COMMAND cd ../ ) - doesn't work. When I execute this CMake doesn't change its directory and I'm again in project/build.

like image 918
vir2al Avatar asked Dec 06 '13 08:12

vir2al


People also ask

How do I specify a path in CMake?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .

How do I edit CMakeLists text?

You can edit CMakeLists. txt files right in the Editor. Make sure to reload the project after editing. Automatic reload feature is disabled by default, you can turn it on by selecting the Automatically reload CMake project on editing checkbox in Settings / Preferences | Build, Execution, Deployment | CMake.

Where does CMake look CMakeLists txt?

CMakeLists. txt is placed at the root of the source tree of any application, library it will work for. If there are multiple modules, and each module can be compiled and built separately, CMakeLists. txt can be inserted into the sub folder.


2 Answers

The WORKING_DIRECTORY directive of the execute_process command lets you directly specify the directory the script is to be run from.

execute_process(COMMAND ${CMAKE_SOURCE_DIR}/script.sh args
                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
like image 106
Peter Avatar answered Sep 17 '22 14:09

Peter


With ${CMAKE_SOURCE_DIR} you get the source path. Executing a binary from the source directory would be

execute_process(${CMAKE_SOURCE_DIR}/bin/myscript.sh)

If you need to handle files from the build directory you have to add them like

execute_process(${CMAKE_SOURCE_DIR}/bin/myscript.sh ${CMAKE_CURRENT_BINARY_DIR}/input.txt)

Overall the variables CMAKE_SOURCE_DIR, CMAKE_CURRENT_SOURCE_DIR, CMAKE_BINARY_DIR, and CMAKE_CURRENT_BINARY_DIR are very helpful. You can find a more complete list at http://www.cmake.org/Wiki/CMake_Useful_Variables

like image 38
usr1234567 Avatar answered Sep 20 '22 14:09

usr1234567