使用CMake获得构建时svn修订版 [英] use CMake to get build-time svn revision

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

问题描述

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

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

我想在 build time时获得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目标基本上是一个no-op。

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获得构建时svn修订版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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