如何存储 CMake 构建设置 [英] How to store CMake build settings

查看:34
本文介绍了如何存储 CMake 构建设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试构建使用 CMake 的项目时,通常需要启用/禁用许多开关.

There are often many swiches to enable/disable when trying to build a project that uses CMake.

您如何存储某些用户所做的构建设置以使构建可在另一台机器上重现?是否有某种导出功能,或者您只是复制构建(缓存)文件夹?

How do you store the build settings made by some user to make a build reproduceable on another machine? Is there some kind of export functionality or do you just copy the build (cache) folder?

推荐答案

有一个选项可以预加载一个脚本,用于使用 cmake 填充缓存文件

There is an option to pre-load a script for populating the cache file with cmake using

cmake -C <initial-cache>

initial-cache 是一个包含以下方式设置的变量的文件,例如对于安装前缀:

The initial-cache is a file containing variables set in the following way, e.g. for the install prefix:

set(CMAKE_INSTALL_PREFIX "/my/install/prefix" CACHE PATH "")

然后在使用 cmake 填充缓存的同时传递这个文件.很简单,但我不知道这一点,也没有找到好的样本.另外,这是一种独立于平台的方式,而不是编写脚本或批处理文件.

Then just pass this file while populating the cache with cmake. Easy, but I didn't know that and found no good sample. As a plus, this is an platform independent way instead of writing a script or batch file.

我在生成的源外构建文件夹中的源旁边创建了一个单独的脚本文件夹.我的包含设置的文件存储在每个要构建的库/可执行文件中.

I create a separate script folder next to the sources out of the generated out-of-source build folder. My files containing the settings are stored there for each lib/executable to build.

您可以将所有设置放入一个单独的文件中,在一天结束时只剩下几个调用:

You can put all the settings into a separate file and at the end of the day there are just a few calls left:

cmake -E make_directory build/somelib
cmake -E chdir build/somelib cmake -C ../../script/cmake/somelib.cmake ../../source/somelib/src
cmake --build build/somelib --target install

很简单,不是吗?

如果您使用的是 *nix 系统,则可以在构建目录中运行以下内容:

If you are on a *nix system you can run the following inside your build dir:

cmake -N -LA | tail -n+2 | sed -r 's/([A-Za-z_0-9]+):([A-Z]+)=(.*)/set(1 "3" CACHE 2 "")/' >cmake-init.txt

在 Windows 上,类似下面的 cmake 脚本应该可以工作:

On Windows, something like the following cmake script should work:

# list all cache variables
# this should be run in your build dir

set(filename ${CMAKE_ARGV3})
message(STATUS "Writing to ${filename}")
if(NOT filename)
    message(FATAL_ERROR "Must provide an output filename")
    return()
endif()

execute_process(COMMAND "${CMAKE_COMMAND}" "-N" "-LA" OUTPUT_VARIABLE cacheVars)
string(REPLACE "
" ";" cacheVars ${cacheVars})
file(WRITE ${filename} "")

foreach (variable ${cacheVars})
    string(REGEX REPLACE "([A-Za-z_0-9]+):([A-Z]+)=(.*)$" "set(\1 "\3" CACHE \2 "")
" output ${variable})
    if(CMAKE_MATCH_0)
        file(APPEND ${filename} ${output})
    endif()
endforeach()

将其保存到例如 get_cache_vars.cmake 并像这样运行:

Save it to, e.g., get_cache_vars.cmake and run it like:

cd <your build-dir>
cmake -P path	oget_cache_vars.cmake <outputfile.txt>

这篇关于如何存储 CMake 构建设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆