feat(cmake): embed git info in binary (#644)

This commit is contained in:
Yimura 2022-11-28 18:19:03 +01:00 committed by GitHub
parent 6374552a80
commit a17aed317e
6 changed files with 63 additions and 0 deletions

3
.gitignore vendored
View File

@ -10,3 +10,6 @@ build/
*.ipch *.ipch
*.gch *.gch
*.pch *.pch
# generated by CMAKE
version.cpp

View File

@ -4,6 +4,8 @@ project(YimMenu CXX ASM_MASM)
set(SRC_DIR "${PROJECT_SOURCE_DIR}/src") set(SRC_DIR "${PROJECT_SOURCE_DIR}/src")
# Git commit embed
include(scripts/git.cmake)
# Fetch modules # Fetch modules
message("\nFetching modules") message("\nFetching modules")

35
scripts/git.cmake Normal file
View File

@ -0,0 +1,35 @@
find_package(Git)
if(Git_FOUND)
message("Git found: ${GIT_EXECUTABLE}")
# the commit's SHA1, and whether the building workspace was dirty or not
execute_process(COMMAND
"${GIT_EXECUTABLE}" describe --match=NeVeRmAtCh --always --abbrev=40 --dirty
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_SHA1
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
# the date of the commit
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)
# the subject of the commit
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)
# branch name
execute_process(COMMAND
"${GIT_EXECUTABLE}" branch --show-current
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_BRANCH
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
# generate version.cpp
configure_file("${SRC_DIR}/version.cpp.in" "${SRC_DIR}/version.cpp" @ONLY)
endif()

View File

@ -8,6 +8,7 @@
#include "renderer.hpp" #include "renderer.hpp"
#include "script_mgr.hpp" #include "script_mgr.hpp"
#include "thread_pool.hpp" #include "thread_pool.hpp"
#include "version.hpp"
#include "backend/backend.hpp" #include "backend/backend.hpp"
#include "native_hooks/native_hooks.hpp" #include "native_hooks/native_hooks.hpp"
@ -58,6 +59,7 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
try try
{ {
LOG(INFO) << "Yim's Menu Initializing"; LOG(INFO) << "Yim's Menu Initializing";
LOGF(INFO, "Git Info\n\tBranch:\t%s\n\tHash:\t%s\n\tDate:\t%s", version::GIT_BRANCH, version::GIT_SHA1, version::GIT_DATE);
g->load(); g->load();
LOG(INFO) << "Settings Loaded."; LOG(INFO) << "Settings Loaded.";

9
src/version.cpp.in Normal file
View File

@ -0,0 +1,9 @@
#include "version.hpp"
namespace big
{
const char* version::GIT_SHA1 = "@GIT_SHA1@";
const char* version::GIT_BRANCH = "@GIT_BRANCH@";
const char* version::GIT_DATE = "@GIT_DATE@";
const char* version::GIT_COMMIT_SUBJECT = "@GIT_COMMIT_SUBJECT@";
};

12
src/version.hpp Normal file
View File

@ -0,0 +1,12 @@
#pragma once
namespace big
{
struct version
{
static const char* GIT_SHA1;
static const char* GIT_DATE;
static const char* GIT_COMMIT_SUBJECT;
static const char* GIT_BRANCH;
};
}