CMake生成版本号
(金庆的专栏)
原来的CMake需要用shell脚本生成SVN版本号,再作为cmake参数传入。
CMake调用脚本示例:
#!/bin/sh
# cmake.sh
ServerCodeRoot=~/Code/Server
CodeRevNum=`svn info ${ServerCodeRoot} | grep Revision | awk -F' ' '{ print $2 }'`
cmake S{ServerCodeRoot} -DCMAKE_BUILD_TYPE=Release -DVERSION_REVISION=S{CodeRevNum}
CMakeLists.txt中如下配置:
configure_file(
"Version.h.in"
"Version.h"
)
Version.h.in如下:
#include <string>
const std::string VERSION_MAJOY("1");
const std::string VERSION_MINOR("0");
const std::string VERSION_REVISION("@VERSION_REVISION@");
Linux下执行cmake.sh会自动替换SVN版本号。
现改为用FIND_PACKAGE(Subversion)方式,不必用shell脚本生成SVN版本号,
并且在Windows上也可以生成版本号, cmake调用时不必传VERSION_REVISION参数。
CMakeLists.txt改为(参考了开源网游ryzom的CMake):
SET(ROOT_DIR ${PROJECT_SOURCE_DIR})
IF(EXISTS "${ROOT_DIR}/.svn/")
FIND_PACKAGE(Subversion)
IF(SUBVERSION_FOUND)
Subversion_WC_INFO(${ROOT_DIR} Project)
SET(VERSION_REVISION ${Project_WC_REVISION})
ENDIF(SUBVERSION_FOUND)
ENDIF(EXISTS "${ROOT_DIR}/.svn/")
CONFIGURE_FILE(
"Version.h.in"
"Version.h"
)
如果不是SVN, 而是用Hg, 可用如下脚本:
IF(EXISTS "${ROOT_DIR}/.hg/")
FIND_PACKAGE(Mercurial)
IF(MERCURIAL_FOUND)
Mercurial_WC_INFO(${ROOT_DIR} ER)
SET(REVISION ${ER_WC_REVISION})
SET(CHANGESET ${ER_WC_CHANGESET})
SET(BRANCH ${ER_WC_BRANCH})
ENDIF(MERCURIAL_FOUND)
ENDIF(EXISTS "${ROOT_DIR}/.hg/")