From 2a634e567ba90254928d621e30f0657162f1908e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 22 Dec 2024 17:19:32 +0800 Subject: [PATCH] feat: add metadata header --- .gitignore | 2 ++ src/core/CMakeLists.txt | 64 ++++++++++++++++++++++++++++++++++++ src/core/utils/metadata.h.in | 16 +++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/core/utils/metadata.h.in diff --git a/.gitignore b/.gitignore index 98b29d2..b233b3b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ /cmake-build/ /cmake-build-debug/ /cmake-build-release/ + +/src/core/utils/metadata.h /src/klotski_core/utils/metadata.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index b91a904..d0776af 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -3,6 +3,70 @@ project(klotski_core VERSION 0.0.2 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 23) +macro(git_tag _tag) + find_package(Git QUIET) + if (GIT_FOUND) + execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags + OUTPUT_VARIABLE ${_tag} + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) + endif() +endmacro() + +macro(git_branch _branch) + find_package(Git QUIET) + if (GIT_FOUND) + execute_process(COMMAND ${GIT_EXECUTABLE} symbolic-ref --short -q HEAD + OUTPUT_VARIABLE ${_branch} + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) + endif() +endmacro() + +macro(git_commit_id _hash) + find_package(Git QUIET) + if (GIT_FOUND) + execute_process(COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%H + OUTPUT_VARIABLE ${_hash} + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) + endif() +endmacro() + +set(GIT_TAG_LONG "") +git_tag(GIT_TAG_LONG) +if(GIT_TAG_LONG STREQUAL "") # without git tag + set(GIT_TAG_LONG "unknown") +endif() + +set(GIT_BRANCH "") +git_branch(GIT_BRANCH) +if(GIT_BRANCH STREQUAL "") # without git branch + set(GIT_BRANCH "unknown") +endif() + +set(GIT_COMMIT_LONG "") +git_commit_id(GIT_COMMIT_LONG) +if(GIT_COMMIT_LONG STREQUAL "") # without git commit + set(GIT_COMMIT_LONG "unknown") +endif() + +set(PLAT "${CMAKE_SYSTEM_NAME}") # TODO: add `KLSK_` prefix +set(ARCH "${CMAKE_SYSTEM_PROCESSOR}") + +set(VERSION_MAJOR ${klotski_core_VERSION_MAJOR}) +set(VERSION_MINOR ${klotski_core_VERSION_MINOR}) +set(VERSION_ALTER ${klotski_core_VERSION_PATCH}) + +string(TIMESTAMP VERSION_BUILD "%Y-%m-%d %H:%M:%S") + +set(COMPILER "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}") + +configure_file( + ${PROJECT_SOURCE_DIR}/utils/metadata.h.in + ${PROJECT_SOURCE_DIR}/utils/metadata.h +) + set(KLSK_CORE_SRC ranges/internal/spawn.cc ranges/internal/ranges.cc diff --git a/src/core/utils/metadata.h.in b/src/core/utils/metadata.h.in new file mode 100644 index 0000000..d2d17d4 --- /dev/null +++ b/src/core/utils/metadata.h.in @@ -0,0 +1,16 @@ +#pragma once + +#define VERSION_MAJOR uint32_t(@VERSION_MAJOR@) +#define VERSION_MINOR uint32_t(@VERSION_MINOR@) +#define VERSION_PATCH uint32_t(@VERSION_ALTER@) +#define VERSION_STR "v@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_ALTER@" + +#define AUTHOR "dnomd343" +#define SYSTEM "@PLAT@ @ARCH@" +#define COMPILER "@COMPILER@" +#define BUILD_TIME "@VERSION_BUILD@" + +#define GIT_BRANCH "@GIT_BRANCH@" +#define GIT_TAG_ID "@GIT_TAG_LONG@" +#define GIT_PROJECT "https://github.com/dnomd343/klotski.git" +#define GIT_COMMIT_ID "@GIT_COMMIT_LONG@"