简体   繁体   中英

running cmake commands in python script

Is it possible to run cmake commands in a python script? I want to set the boost libraries which I installed and compiled manually through the python code.I want something like set(BOOST_INCLUDEDIR "/path/to/boost/include") to happen via python script. So, before running cmake I want the cmake variables to set through the python code.

There are two ways of pre-initialising CMake variables before CMake processing starts, both using command-line arguments of cmake .

The simple one is to pass one or more variables to CMake using the -D command-line option. Something like this:

cmake -DBOOST_INCLUDEDIR:PATH="/path/to/boost/include" ...

The other option is to create an "initial cache file" (basically a file containing just set(...) CMake commands) and pass that initial cache file to CMake using -C :

echo 'set(BOOST_INCLUDEDIR "/path/to/boost/include" CACHE PATH "")' > initial_cache.cmake
cmake -C initial_cache.cmake ...

This option is intended for use the first time CMake is run with a given binary directory, ie before it creates its own CMakeCache.txt file.

How you can utilise one or both of these from your Python script depends on your particular setup.

Yes

Option 1 (if you invoke cmake via python)

By setting cmake cache variables from the command line. The syntax for defining this from the command line is as follows from here

-D <var>:<type>=<value>

So in your case, in the cmake list file

set(BOOST_INCLUDEDIR MY_BOOST_INCLUDE)

Then simply override that option when invoking cmake

cmake -DMY_BOOST_INCLUDE:STRING="/path/to/wherever"

Option 2 (if you want to invoke cmake at another point)

You can make a cmake module in python that sets the defines you want. For example, make a python script that populates my_module.cmake with any cache variables you want, ie

set(MY_BOOST_INCLUDE "script/generated/path")
#... other stuff you want to define

Then in your static cmake list file

include(my_module)

Some of CMake's find modules do support reading path hints from environment variables:

  • "Users may set these hints or results as cache entries. [...] One may specify these as environment variables if they are not specified as CMake variables or cache entries."

  • I prefer this method of setting find module directories, because it's something I could also set on system level and I don't have to give every single of my CMake projects the paths to my custom build libraries anymore.

  • FindBoost.cmake is an good example, because it offers various environment variable:

     Boost_DIR BOOSTROOT BOOST_ROOT BOOST_INCLUDEDIR BOOST_LIBRARYDIR 
  • Note : Those are considered "hints", since they will not overwrite any CMake cache values once was found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM