怎麼在C++二進制文件中註入git信息詳解

在後端生產環境中,為瞭方便定位問題,我們需要根據一個binary文件獲取其對應的git信息,包括編譯出該binary所用的代碼分支、commitid、時間戳、commit message等。但是隨著代碼版本的升級,這些git信息也會隨之變化。有沒有辦法在每次編譯binary的時候,將git信息動態註入到其中呢?

答案是有!

創建以下幾個文件

創建頭文件version.hh,內容如下,其中聲明瞭表示git信息的數據結構

#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註入
#include “version.hh”

#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.cc

#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.txt

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})

二進制文件執行結果如下:

$ ./inject_git                                                                                                                
building sha1: 74b25fc30d18001f72ec0cf0495165441df40a09
building branch: master
building date: Fri May 28 14:49:16 2021
building commit subject: add again

總結

到此這篇關於怎麼在C++二進制文件中註入git信息的文章就介紹到這瞭,更多相關C++二進制註入git信息內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: