使用CMake获取构建时Subversion修订版 [英] Use CMake to get build-time Subversion revision

查看:209
本文介绍了使用CMake获取构建时Subversion修订版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CMake,我可以使用 Subversion_WC_INFO 获得Subversion修订版。但是,这只会在配置时发生-每个后续make都会使用此缓存的修订。

With CMake I can get the Subversion revision using Subversion_WC_INFO. However, this only happens at configure time -- every subsequent make will use this cached revision.

我想在构建时获取svn修订(即每次运行Make时)。我该怎么做?

I'd like to get the svn revision at build time (ie, every time Make is run). How can I do this?

推荐答案

这比要做的事更痛苦。您可以在构建时运行程序以从Subversion提取版本信息。 CMake本身可以使用其脚本语言用作此程序。您可以使用-P命令行标志运行任何CMake脚本。这样做的代码片段是(要放在文件 getsvn.cmake 中):

This is more of a pain to do than it needs to be. You can run a program at build time to extract the version information from subversion. CMake itself can be used as this program using its scripting language. You can run any CMake script using the -P command line flag. The piece of code to do this would be (To be placed in file getsvn.cmake):

# the FindSubversion.cmake module is part of the standard distribution
include(FindSubversion)
# extract working copy information for SOURCE_DIR into MY_XXX variables
Subversion_WC_INFO(${SOURCE_DIR} MY)
# write a file with the SVNVERSION define
file(WRITE svnversion.h.txt "#define SVNVERSION ${MY_WC_REVISION}\n")
# copy the file to the final header only if the version changes
# reduces needless rebuilds
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
                        svnversion.h.txt svnversion.h)

这将提取Subversion修订信息并将其写入头文件。仅在需要时才更新此头文件,以避免一直重新编译。在您的程序中,您将包括该头文件以获取SVNVERSION定义。

This extracts the subversion revision information and writes it to a header file. This header file is only updated when needed to avoid recompiling all the time. In your program, you would include that header file to get at the SVNVERSION define.

CMakeLists.txt 文件您需要运行脚本的内容将是这样的:

The CMakeLists.txt file that you would need to run the script would be something like this:

# boilerplate
cmake_minimum_required(VERSION 2.8)
project(testsvn)

# the test executable
add_executable(test main.c ${CMAKE_CURRENT_BINARY_DIR}/svnversion.h)

# include the output directory, where the svnversion.h file is generated
include_directories(${CMAKE_CURRENT_BINARY_DIR})

# a custom target that is always built
add_custom_target(svnheader ALL
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/svnheader.h)

# creates svnheader.h using cmake script
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/svnheader.h
    COMMAND ${CMAKE_COMMAND} -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
                         -P ${CMAKE_CURRENT_SOURCE_DIR}/getsvn.cmake)

# svnversion.h is a generated file
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/svnversion.h
    PROPERTIES GENERATED TRUE
    HEADER_FILE_ONLY TRUE)

# explicitly say that the executable depends on the svnheader
add_dependencies(test svnheader)

您需要使用 add_custom_target add_custom_command ,因为svnheader.h文件没有输入,所以它从无处显示到cmake。我已经在getsvn.cmake文件中处理了不必要的重建问题,因此,如果未更改Subversion修订版,则重建 svnheader目标基本上是一项禁止操作。

You need to use add_custom_target and add_custom_command since there are no inputs to the svnheader.h file, it "appears from nowhere" to cmake. I already dealt with the issue of needless rebuilds in the getsvn.cmake file though, so rebuilding the "svnheader" target is basically a no-op if the subversion revision has not changed.

最后,举例说明 main.c 文件:

#include "svnversion.h"

int main(int argc, const char *argv[])
{
    printf("%d\n", SVNVERSION);
    return 0;
}

这篇关于使用CMake获取构建时Subversion修订版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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