CMake添加文件版本信息
windows
如何添加版本信息,主要分三步
从git获取版本信息
# 获取git hash
execute_process(
COMMAND git log -1 --format=%H
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE COMMIT_HASH
)
string(SUBSTRING ${COMMIT_HASH} 0 8 COMMIT_HASH)
message(STATUS "git hash= ${COMMIT_HASH}")
# 获取当前分支名称
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE CURRENT_BRANCH
)
string(SUBSTRING ${CURRENT_BRANCH} 0 1 CURRENT_BRANCH)
message(STATUS "git current branch = ${CURRENT_BRANCH}")
# 获取提交次数
execute_process(
COMMAND git rev-list HEAD --count
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE COMMIT_COUNT
)
string(STRIP ${COMMIT_COUNT} COMMIT_COUNT)
message(STATUS "git COMMIT_COUNT = ${COMMIT_COUNT}")
# 生成版本信息
set(VER "${COMMIT_COUNT}-${CURRENT_BRANCH}-${COMMIT_HASH}")
message(STATUS "version info = ${VER}")
将版本信息添加到.rc文件中
版本信息资源文件模版
// version.rc.in
1 VERSIONINFO
FILEVERSION 1,0,0,${COMMIT_COUNT}
PRODUCTVERSION 1,0,0,${COMMIT_COUNT}
FILEFLAGSMASK 0x17L
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x0L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "乘风科技责任有限公司"
VALUE "FileDescription", "library to teach add file version infomation"
VALUE "FileVersion", "1,0,0,${COMMIT_COUNT}"
VALUE "InternalName", "test.dll"
VALUE "LegalCopyright", "raise wing 2017-2013 copyright"
VALUE "OriginalFilename", "test.dll"
VALUE "ProductName", "main"
VALUE "ProductVersion", "${VER}"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
将上面的资源文件模板放在你的源代码目录中
生成资源文件
IF(MSVC)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in ${CMAKE_BINARY_DIR}/version.rc)
ENDIF()
configure_file 语句的作用是用一个文件生成另一个文件,并且替换里面的cmake变量,在模板中有${COMMIT_COUNT}
和${VER}
是前面我们获取到的版本信息
3.将.rc添加到动态库项目中
file(GLOB SOURCEFILE "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
file(GLOB RCFILE ${CMAKE_BINARY_DIR}/*.rc)
add_library(test SHARED ${SOURCEFILE} ${RCFILE})
上面的代码主要是file(GLOB RCFILE ${CMAKE_BINARY_DIR}/*.rc)
获取资源文件的路径
OSX
很不幸的是cmake并不能添加macos dylib的版本信息,但是我们可以另辟蹊径先通过cmake生成xcode工程,然后再通过xcodeBuild添加版本信息
WORKSPACE=$(cd "$(dirname "$0")";pwd)
BUILD_DIR=$WORKSPACE/build
if [ ! -d "$BUILD_DIR" ]; then
echo "$BUILD_DIR不存在,重新创建"
mkdir "$BUILD_DIR"
fi
cmake -S $WORKSPACE -B "$BUILD_DIR" -G Xcode
echo 获取git版本号
svnInfoResult=`git rev-list HEAD --count`
svnVersion=${svnInfoResult##* }
DYLIB_CURRENT_VERSION="1.1.${svnVersion}"
DYLIB_COMPATIBILITY_VERSION="1.0.1"
GCC_PREPROCESSOR_DEFINITIONS="LIB_VERSION=\\\"${DYLIB_CURRENT_VERSION}\\\""
echo 编译代码
BUILD_SETTING=" -configuration $CONFIG DYLIB_CURRENT_VERSION=${DYLIB_CURRENT_VERSION} DYLIB_COMPATIBILITY_VERSION=${DYLIB_COMPATIBILITY_VERSION} GCC_PREPROCESSOR_DEFINITIONS=${GCC_PREPROCESSOR_DEFINITIONS} build"
xcodebuild -project $BUILD_DIR/test.xcodeproj -target test $BUILD_SETTING
xcodebuild -project $BUILD_DIR/test.xcodeproj -target main $BUILD_SETTING
源码地址
最后附上源码地址
https://github.com/2963663242/cmake_add_version