标签:TAG GIT const 04 REV char version 2021
【转】Compile-time version strings in CMake
At compile-time, I would like to embed a few strings into my code that reflects the current state of the Git repository:
- The short commit hash, with a trailing
+
if there are uncommitted changes. - The tag (if any)
- The current branch
This is the kind of thing that should be easy, but is surprisingly non-obvious.
First, I write a standalone CMake script named version.cmake
:
execute_process(COMMAND git log --pretty=format:'%h' -n 1
OUTPUT_VARIABLE GIT_REV
ERROR_QUIET)
# Check whether we got any revision (which isn't
# always the case, e.g. when someone downloaded a zip
# file from Github instead of a checkout)
if ("${GIT_REV}" STREQUAL "")
set(GIT_REV "N/A")
set(GIT_DIFF "")
set(GIT_TAG "N/A")
set(GIT_BRANCH "N/A")
else()
execute_process(
COMMAND bash -c "git diff --quiet --exit-code || echo +"
OUTPUT_VARIABLE GIT_DIFF)
execute_process(
COMMAND git describe --exact-match --tags
OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
OUTPUT_VARIABLE GIT_BRANCH)
string(STRIP "${GIT_REV}" GIT_REV)
string(SUBSTRING "${GIT_REV}" 1 7 GIT_REV)
string(STRIP "${GIT_DIFF}" GIT_DIFF)
string(STRIP "${GIT_TAG}" GIT_TAG)
string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
endif()
set(VERSION "const char* GIT_REV=\"${GIT_REV}${GIT_DIFF}\";
const char* GIT_TAG=\"${GIT_TAG}\";
const char* GIT_BRANCH=\"${GIT_BRANCH}\";")
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp VERSION_)
else()
set(VERSION_ "")
endif()
if (NOT "${VERSION}" STREQUAL "${VERSION_}")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp "${VERSION}")
endif()
This runs git
to get relevant info, inserts version and revision strings into a preformatted chunk of C code, then writes it to a local file if it has changed.
The resulting file looks like this:
const char* GIT_REV="c629110+";
const char* GIT_TAG="";
const char* GIT_BRANCH="master";
Next, I add the following lines to my main CMakeLists.txt
:
# Add a custom command that produces version.cpp, plus
# a dummy output that's not actually produced, in order
# to force version.cmake to always be re-run before the build
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.cpp
${CMAKE_CURRENT_BINARY_DIR}/_version.cpp
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_SOURCE_DIR}/version.cmake)
Finally, I add the generated file
${CMAKE_CURRENT_BINARY_DIR}/version.cpp
to the list of files in my build.
This works beautifully: whenever I run a build, it checks the state of the repo and writes a new copy of version.cpp
if things have changed. In turn, if that file is modified, then the usual compilation process rebuilds it and re-links the resulting libraries.
This is also a fast solution. If nothing has changed, a build runs in about 0.09 seconds (using ninja
); if we need to regenerate version.cpp
, rebuild it, and re-link the libraries, it takes 0.28 sec.
To actually use these variables, I add a few small accessor functions (to ensure that they’re accessible outside of the shared library):
// These variables are autogenerated and compiled
// into the library by the version.cmake script
extern "C"
{
extern const char* GIT_TAG;
extern const char* GIT_REV;
extern const char* GIT_BRANCH;
}
const char* libfive_git_version(void)
{
return GIT_TAG;
}
const char* libfive_git_revision(void)
{
return GIT_REV;
}
const char* libfive_git_branch(void)
{
return GIT_BRANCH;
}
标签:TAG,GIT,const,04,REV,char,version,2021 来源: https://blog.csdn.net/hengxe/article/details/115432030
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。