在后端生产环境中,为了方便定位问题,我们需要根据一个binary文件获取其对应的git信息,包括编译出该binary所用的代码分支、commitid、时间戳、commit message等。但是随着代码版本的升级,这些git信息也会随之变化。有没有办法在每次编译binary的时候,将git信息动态注入到其中呢?答案是有! U. S! V! \' w1 g$ S2 o- J# z, ?: X
创建以下几个文件,创建头文件version.hh,内容如下,其中声明了表示git信息的数据结构:
% ^! j. Z2 A# n {
- #pragma once
-
- #include <string>
-
- namespace my_app
- {
- struct Version
- {
- static const std::string GIT_SHA1;
- static const std::string GIT_BRANCH;
- static const std::string GIT_DATE;
- static const std::string GIT_COMMIT_SUBJECT;
- };
- }
创建模板文件,version.cc.in,其中定义了Version中静态变量的值。其中@XX@表示该变量通过cmake注入:, i" U! |( v7 E! ]# i0 _
#include "version.hh"$ i: k) c' z: c# [2 T4 y
- #include "version.hh"
-
- using namespace my_app;
-
- const std::string Version::GIT_SHA1 = "@GIT_SHA1@";
- const std::string Version::GIT_BRANCH = "@GIT_BRANCH@";
- const std::string Version::GIT_DATE = "@GIT_DATE@";
- const std::string Version::GIT_COMMIT_SUBJECT = "@GIT_COMMIT_SUBJECT@";
创建main.cc8 g- T6 r8 k. p3 B! s
- #include <iostream>
- #include "version.hh"
-
- using namespace my_app;
-
- int main() {
- std::cout << "building sha1: " << Version::GIT_SHA1 << std::endl;
- std::cout << "building branch: " << Version::GIT_BRANCH << std::endl;
- std::cout << "building date: " << Version::GIT_DATE << std::endl;
- std::cout << "building commit subject: " << Version::GIT_COMMIT_SUBJECT << std::endl;
- return 0;
创建CMakeLists.txt3 q2 a- z" a! `2 r# ^! R
- cmake_minimum_required(VERSION 3.5)
- project (inject_git)
- set(GIT_EXECUTABLE /usr/bin/git)
-
- # 执行git rev-parse HEAD, 生成commitid, 将结果保存于变量GIT_SHA1
- execute_process(COMMAND
- "${GIT_EXECUTABLE}" rev-parse HEAD
- WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
- OUTPUT_VARIABLE GIT_SHA1
- ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
-
- # 执行git rev-parse --abbrev-ref HEAD, 生成branch name, 将结果保存于变量GIT_BRANCH
- execute_process(COMMAND
- "${GIT_EXECUTABLE}" rev-parse --abbrev-ref HEAD
- WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
- OUTPUT_VARIABLE GIT_BRANCH
- ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-
- # 执行git log -1 --format=%ad --date=local, 生成提交日期, 将结果保存于变量GIT_DATE
- execute_process(COMMAND
- "${GIT_EXECUTABLE}" log -1 --format=%ad --date=local
- WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
- OUTPUT_VARIABLE GIT_DATE
- ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
-
- # 执行git log -1 --format=%s, 生成提交日志, 将结果保存于变量GIT_COMMIT_SUBJECT
- execute_process(COMMAND
- "${GIT_EXECUTABLE}" log -1 --format=%s
- WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
- OUTPUT_VARIABLE GIT_COMMIT_SUBJECT
- ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
-
- # 上述变量初始化后,将其注入到模板文件version.cc.in中,生成version.cc
- configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.cc.in" "${CMAKE_CURRENT_BINARY_DIR}/version.cc" @ONLY)
-
- set(SOURCES
- main.cc
- version.cc
- )
-
- # 由version.cc和main.cc生成二进制
- add_executable(inject_git ${SOURCES})
二进制文件执行结果如下:
. P3 z6 ~. ]# u, D$ ./inject_git ! [6 g2 D& Z; t1 T& U1 z) V9 c
building sha1: 74b25fc30d18001f72ec0cf0495165441df40a091 G4 x& l! z4 N7 S4 \* m
building branch: master$ d, g# Q5 [2 z9 ?( }
building date: Fri May 28 14:49:16 2021
. |7 b' c; {/ O7 f0 X5 rbuilding commit subject: add again |