问题
随着软件更新,现场版本需要和开发版本保持一致,所以最好能在软件中显示当前运行的版本号以作追踪维护
实现
├── cmake
│ ├── CheckGit.cmake
│ ├── git_version.cpp.in
│ └── git_version.h
└── embedded-git-hash
├── CMakeLists.txt
└── main.cpp
2 directories, 5 files
#CheckGit.cmake
set(CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR})
if (NOT DEFINED pre_configure_dir)
set(pre_configure_dir ${CMAKE_CURRENT_LIST_DIR})
endif ()
if (NOT DEFINED post_configure_dir)
set(post_configure_dir ${CMAKE_BINARY_DIR}/generated)
endif ()
set(pre_configure_file ${pre_configure_dir}/git_version.cpp.in)
set(post_configure_file ${post_configure_dir}/git_version.cpp)
function(CheckGitWrite git_hash)
file(WRITE ${CMAKE_BINARY_DIR}/git-state.txt ${git_hash})
endfunction()
function(CheckGitRead git_hash)
if (EXISTS ${CMAKE_BINARY_DIR}/git-state.txt)
file(STRINGS ${CMAKE_BINARY_DIR}/git-state.txt CONTENT)
LIST(GET CONTENT 0 var)
set(${git_hash} ${var} PARENT_SCOPE)
endif ()
endfunction()
function(CheckGitVersion)
# Get the latest abbreviated commit hash of the working branch
#message(WARNING "result: Get the latest abbreviated commit hash of