简体   繁体   English

在python脚本中运行cmake命令

[英]running cmake commands in python script

Is it possible to run cmake commands in a python script? 是否可以在python脚本中运行cmake命令? 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. 我想设置通过python代码手动安装和编译的boost库。我希望通过python脚本来执行set(BOOST_INCLUDEDIR "/path/to/boost/include") So, before running cmake I want the cmake variables to set through the python code. 因此,在运行cmake之前,我希望cmake变量通过python代码设置。

There are two ways of pre-initialising CMake variables before CMake processing starts, both using command-line arguments of cmake . 有两种方法可以在CMake处理开始之前预先初始化CMake变量,两种方法都使用cmake命令行参数

The simple one is to pass one or more variables to CMake using the -D command-line option. 一种简单的方法是使用-D命令行选项将一个或多个变量传递给CMake。 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 : 另一个选择是创建一个“初始缓存文件”(基本上是一个仅包含set(...) CMake命令的文件),然后使用-C将该初始缓存文件传递给CMake:

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. 此选项适用于第一次使用给定的二进制目录运行CMake,即它创建自己的CMakeCache.txt文件之前。

How you can utilise one or both of these from your Python script depends on your particular setup. 如何从Python脚本中利用这两者之一或两者取决于您的特定设置。

Yes

Option 1 (if you invoke cmake via python) 选项1(如果您通过python调用cmake)

By setting cmake cache variables from the command line. 通过从命令行设置cmake缓存变量。 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 因此,在您的情况下,在cmake列表文件中

set(BOOST_INCLUDEDIR MY_BOOST_INCLUDE)

Then simply override that option when invoking cmake 然后在调用cmake时只需覆盖该选项

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

Option 2 (if you want to invoke cmake at another point) 选项2(如果要在另一点调用cmake)

You can make a cmake module in python that sets the defines you want. 您可以在python中创建一个cmake模块,以设置所需的定义。 For example, make a python script that populates my_module.cmake with any cache variables you want, ie 例如,制作一个Python脚本,用所需的任何缓存变量填充my_module.cmake ,即

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

Then in your static cmake list file 然后在您的静态cmake列表文件中

include(my_module)

Some of CMake's find modules do support reading path hints from environment variables: CMake的某些查找模块确实支持从环境变量读取路径提示:

  • "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." “用户可以将这些提示或结果设置为缓存条目。如果未将其指定为CMake变量或缓存条目,则可以将其指定为环境变量。”

  • 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. 我更喜欢这种设置查找模块目录的方法,因为这也是我可以在系统级别上设置的方法,而且我不必再为每个CMake项目提供自定义构建库的路径了。

  • FindBoost.cmake is an good example, because it offers various environment variable: FindBoost.cmake是一个很好的例子,因为它提供了各种环境变量:

     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. 注意 :这些被认为是“提示”,因为一旦发现 ,它们将不会覆盖任何CMake缓存值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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