Cmake在变量上重生

时间:2013-08-07 14:38:31

标签: c++ mercurial cmake

Y部分:

我有一个运行外部程序并创建项目特定变量的CMake宏。我想让CMake做的是在每次构建时运行这个脚本,检查变量是否已经改变,如果有的话(如果没有则防止完整的项目重建),重新生成一个CONFIGURE_FILE标题......我我不确定如何做到这一点。 (添加自定义目标每次都会重建头文件,并且无法调用宏,与添加自定义命令相同。)

X部分:

所以我编写了以下脚本来提取用于c ++项目的hg版本信息:

macro (ReadProjectRevisionStatus)
exec_program(hg ${PROJECT_SOURCE_DIR} ARGS paths OUTPUT_VARIABLE ${PROJECT_NAME}_HGPATHS)
message(STATUS "${PROJECT_NAME}_HGPATHS=${${PROJECT_NAME}_HGPATHS}}")
if (NOT(${PROJECT_NAME}_HGPATHS STREQUAL ""))

string(REPLACE "\n" ";" ${PROJECT_NAME}_HGPATHS ${${PROJECT_NAME}_HGPATHS})

foreach(HGPATH ${${PROJECT_NAME}_HGPATHS})
string(SUBSTRING "${HGPATH}" 0 10 HGPATHSTART)
if (HGPATHSTART MATCHES "default = ")
string(LENGTH "${HGPATH}" HGPATHLENGTH)
math(EXPR HGSUBLEN "${HGPATHLENGTH}-10")
string(SUBSTRING "${HGPATH}" 10 ${HGSUBLEN} ${PROJECT_NAME}_HGREMOTEDIR)
endif()
endforeach()

endif()

if (NOT ${PROJECT_NAME}_HGREMOTEDIR)
message(WARNING "No remote repository set. Will use current direcoty for build number, but this value may be inaccurate.")
set(${PROJECT_NAME}_HGREMOTEDIR ${PROJECT_SOURCE_DIR})
else()
exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR} ARGS status RETURN_VALUE HGREMOTESTATUSVALUE OUTPUT_VARIABLE NUL)
if (NOT HGREMOTESTATUSVALUE EQUAL 0)
message(WARNING "Cannot connect to remote repository at ${${PROJECT_NAME}_HGREMOTEDIR}. Will use current direcoty for build number, but this value may be inaccurate.")
set(${PROJECT_NAME}_HGREMOTEDIR ${PROJECT_SOURCE_DIR})
endif()
endif()

#Identify changeset
exec_program(hg ${PROJECT_SOURCE_DIR} ARGS "id" "-i" OUTPUT_VARIABLE OUTPUT_VARIABLE ${PROJECT_NAME}_HGHASHCODE)

if (${PROJECT_NAME}_HGHASHCODE MATCHES ".*\\+")
MESSAGE(STATUS "Node is dirty. Will generate temporary version number...")
set (${PROJECT_NAME}_HGDIRTY 1)
string(LENGTH ${${PROJECT_NAME}_HGHASHCODE} HGHASHLEN)
MATH(EXPR HGHASHLEN "${HGHASHLEN}-1")
string(SUBSTRING ${${PROJECT_NAME}_HGHASHCODE} 0 ${HGHASHLEN} ${PROJECT_NAME}_HGHASHCODE)
endif()
#check if remote repository contains changeset
exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR}
ARGS "log" "-r" "${${PROJECT_NAME}_HGHASHCODE}"
RETURN_VALUE HGREMOTEHASCHANGESET
OUTPUT_VARIABLE NUL)
if (NOT HGREMOTEHASCHANGESET EQUAL 0)
message(WARNING "Remote repository ${${PROJECT_NAME}_HGREMOTEDIR} does not have changeset ${${PROJECT_NAME}_HGHASHCODE}. Will use current direcoty for build number, but this value may be inaccurate.")
set(${PROJECT_NAME}_HGREMOTEDIR ${PROJECT_SOURCE_DIR})
endif()

exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR}
ARGS "log" "-r" ${${PROJECT_NAME}_HGHASHCODE} "--template" "{latesttag}"
OUTPUT_VARIABLE ${PROJECT_NAME}_HGMAJORMINORVERSION)

MESSAGE(STATUS "${PROJECT_NAME}_HGMAJORMINORVERSION=${${PROJECT_NAME}_HGMAJORMINORVERSION}")

exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR} 
ARGS "log" "-r" ${${PROJECT_NAME}_HGHASHCODE} "--template" "{latesttagdistance}"
 OUTPUT_VARIABLE ${PROJECT_NAME}_HGBUILDNUMBER)

if (${PROJECT_NAME}_HGDIRTY)
MATH(EXPR ${PROJECT_NAME}_HGBUILDNUMBER "${${PROJECT_NAME}_HGBUILDNUMBER}+1")
set(${PROJECT_NAME}_HGHASHCODE "${${PROJECT_NAME}_HGHASHCODE}+")
endif()

MESSAGE(STATUS "Version=${${PROJECT_NAME}_HGMAJORMINORVERSION}.${${PROJECT_NAME}_HGBUILDNUMBER}.${${PROJECT_NAME}_HGHASHCODE}")
endmacro()

我现在称之为。

ReadProjectRevisionStatus()
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/main.h.in ${CMAKE_CURRENT_BINARY_DIR}/main.h)

main.h.in是:

#ifndef MAIN_H
#define MAIN_H
#include <string>

const std::string ${PROJECT_NAME}_HGMAJORMINORVERSION = "${${PROJECT_NAME}_HGMAJORMINORVERSION}";
const std::string ${PROJECT_NAME}_HGBUILDNUMBER = "${${PROJECT_NAME}_HGBUILDNUMBER}";
const std::string ${PROJECT_NAME}_HGHASHCODE = "${${PROJECT_NAME}_HGHASHCODE}";

const std::string ${PROJECT_NAME}_HG_SHORT_VERSION = 
    ${PROJECT_NAME}_HGMAJORMINORVERSION+"."+
    ${PROJECT_NAME}_HGBUILDNUMBER;

const std::string ${PROJECT_NAME}_HG_VERSION = 
    ${PROJECT_NAME}_HG_SHORT_VERSION + "." +
    ${PROJECT_NAME}_HGHASHCODE;

#endif

我想做的是在每个构建命令上运行这个宏(我可以使用ADD_CUSTOM_TARGET,然后设置变量而不是从项目中读取它们),但我只想重新生成main.h如果变量有改变了(以防止不必要的重新编译)。

编辑:工作解决方案

制作了HGVersion.CMake

cmake_minimum_required (VERSION 2.8)

macro (ReadProjectRevisionStatus)
message(STATUS PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR})

exec_program(hg ${PROJECT_SOURCE_DIR} ARGS paths OUTPUT_VARIABLE ${PROJECT_NAME}_HGPATHS)
message(STATUS "${PROJECT_NAME}_HGPATHS=${${PROJECT_NAME}_HGPATHS}}")
if (NOT(${PROJECT_NAME}_HGPATHS STREQUAL ""))

string(REPLACE "\n" ";" ${PROJECT_NAME}_HGPATHS ${${PROJECT_NAME}_HGPATHS})

foreach(HGPATH ${${PROJECT_NAME}_HGPATHS})
string(SUBSTRING "${HGPATH}" 0 10 HGPATHSTART)
if (HGPATHSTART MATCHES "default = ")
string(LENGTH "${HGPATH}" HGPATHLENGTH)
math(EXPR HGSUBLEN "${HGPATHLENGTH}-10")
string(SUBSTRING "${HGPATH}" 10 ${HGSUBLEN} ${PROJECT_NAME}_HGREMOTEDIR)
endif()
endforeach()

endif()

if (NOT ${PROJECT_NAME}_HGREMOTEDIR)
message(WARNING "No remote repository set. Will use current direcoty for build number, but this value may be inaccurate.")
set(${PROJECT_NAME}_HGREMOTEDIR ${PROJECT_SOURCE_DIR})
else()
exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR} ARGS status RETURN_VALUE HGREMOTESTATUSVALUE OUTPUT_VARIABLE NUL)
if (NOT HGREMOTESTATUSVALUE EQUAL 0)
message(WARNING "Cannot connect to remote repository at ${${PROJECT_NAME}_HGREMOTEDIR}. Will use current direcoty for build number, but this value may be inaccurate.")
set(${PROJECT_NAME}_HGREMOTEDIR ${PROJECT_SOURCE_DIR})
endif()
endif()

#Identify changeset
exec_program(hg ${PROJECT_SOURCE_DIR} ARGS "id" "-i" OUTPUT_VARIABLE OUTPUT_VARIABLE ${PROJECT_NAME}_HGHASHCODE)

if (${PROJECT_NAME}_HGHASHCODE MATCHES ".*\\+")
MESSAGE(STATUS "Node is dirty. Will generate temporary version number...")
set (${PROJECT_NAME}_HGDIRTY 1)
string(LENGTH ${${PROJECT_NAME}_HGHASHCODE} HGHASHLEN)
MATH(EXPR HGHASHLEN "${HGHASHLEN}-1")
string(SUBSTRING ${${PROJECT_NAME}_HGHASHCODE} 0 ${HGHASHLEN} ${PROJECT_NAME}_HGHASHCODE)
endif()
#check if remote repository contains changeset
exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR}
ARGS "log" "-r" "${${PROJECT_NAME}_HGHASHCODE}"
RETURN_VALUE HGREMOTEHASCHANGESET
OUTPUT_VARIABLE NUL)
if (NOT HGREMOTEHASCHANGESET EQUAL 0)
message(WARNING "Remote repository ${${PROJECT_NAME}_HGREMOTEDIR} does not have changeset ${${PROJECT_NAME}_HGHASHCODE}. Will use current direcoty for build number, but this value may be inaccurate.")
set(${PROJECT_NAME}_HGREMOTEDIR ${PROJECT_SOURCE_DIR})
endif()

exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR}
ARGS "log" "-r" ${${PROJECT_NAME}_HGHASHCODE} "--template" "{latesttag}"
OUTPUT_VARIABLE ${PROJECT_NAME}_HGMAJORMINORVERSION)

MESSAGE(STATUS "${PROJECT_NAME}_HGMAJORMINORVERSION=${${PROJECT_NAME}_HGMAJORMINORVERSION}")

exec_program(hg ${${PROJECT_NAME}_HGREMOTEDIR} 
ARGS "log" "-r" ${${PROJECT_NAME}_HGHASHCODE} "--template" "{latesttagdistance}"
 OUTPUT_VARIABLE ${PROJECT_NAME}_HGBUILDNUMBER)

if (${PROJECT_NAME}_HGDIRTY)
MATH(EXPR ${PROJECT_NAME}_HGBUILDNUMBER "${${PROJECT_NAME}_HGBUILDNUMBER}+1")
set(${PROJECT_NAME}_HGHASHCODE "${${PROJECT_NAME}_HGHASHCODE}+")
endif()

MESSAGE(STATUS "Version=${${PROJECT_NAME}_HGMAJORMINORVERSION}.${${PROJECT_NAME}_HGBUILDNUMBER}.${${PROJECT_NAME}_HGHASHCODE}")
endmacro()
message(STATUS "GETTING HG VERSION")
ReadProjectRevisionStatus()
CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/main.h.in ${PROJECT_BINARY_DIR}/main.h)

在主cmake文件中

add_custom_target(
${PROJECT_NAME}_hg_version_target
 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/main.h.in
 COMMAND ${CMAKE_COMMAND}
 ARGS -DPROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
 -DPROJECT_NAME=${PROJECT_NAME}
 -DPROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}
 -P "${CMAKE_CURRENT_SOURCE_DIR}/HGVersion.CMake")

add_dependencies($ {PROJECT_NAME} $ {PROJECT_NAME} _hg_version_target)

工作但看起来......真的很乱。谁有更好的东西?

1 个答案:

答案 0 :(得分:1)

如果构建过程中的某些内容确定要生成的文件的内容,则无法使用configure_file生成该文件,因为configure_file在配置阶段而不是在构建时运行。这是CMake的设计选择。

在你的情况下,你必须使用add_custom_command和add_custom_target在构建过程中生成该文件,正如你已经发现的那样。

对于您提出的解决方案,可能有机会简化这一点。在生成C / C ++头文件时,CMake内部解析器应该能够自动发现哪些常用源代码文件依赖于main.h.因此,使用add_custom_command提供自定义命令就足够了,该命令将OUTPUT声明为它将生成的main.h文件。一旦尝试构建其中一个C / C ++文件依赖于它的目标,CMake应该自动调用此命令。所以你可能不需要自定义目标和手动声明的依赖。