Skip to content

Commit 5bcc575

Browse files
committed
自动生成版本号
1 parent c7f1cf2 commit 5bcc575

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

COPYRIGHT.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ Comment: Documents for LuaSTGPlus Danmaku Game Engine
5454
Copyright: 2022, 9chu.
5555
License: CC-BY-NC-SA-4.0
5656

57+
Files: ./cmake/GenerateVersionHeader.cmake
58+
Comment: https://github.com/nocnokneo/cmake-git-versioning-example
59+
Copyright: 2021, Taylor Braun-Jones.
60+
License: Expat
61+
5762
Files: ./include/lstg/Core/Math/Collider2D/IntersectCheck.hpp
5863
Comment: https://github.com/Xrysnow/lstgx_Math
5964
Copyright: 2018, Xrysnow.

cmake/GenerateVersionHeader.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if(GIT_EXECUTABLE)
2+
get_filename_component(SRC_DIR ${SRC} DIRECTORY)
3+
# Generate a git-describe version string from Git repository tags
4+
execute_process(
5+
COMMAND ${GIT_EXECUTABLE} describe --tags --dirty --match "v*"
6+
WORKING_DIRECTORY ${SRC_DIR}
7+
OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
8+
RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE
9+
OUTPUT_STRIP_TRAILING_WHITESPACE)
10+
if(NOT GIT_DESCRIBE_ERROR_CODE)
11+
set(LSTG_VERSION ${GIT_DESCRIBE_VERSION})
12+
endif()
13+
endif()
14+
15+
# Final fallback: Just use a bogus version string that is semantically older
16+
# than anything else and spit out a warning to the developer.
17+
if(NOT DEFINED LSTG_VERSION)
18+
set(LSTG_VERSION v0.0.0-unknown)
19+
message(WARNING "Failed to determine LSTG_VERSION from Git tags. Using default version \"${LSTG_VERSION}\".")
20+
endif()
21+
22+
configure_file(${SRC} ${DST} @ONLY)

src/v2/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
### 生成版本号信息
2+
3+
find_package(Git)
4+
add_custom_target(LuaSTGPlus2Version
5+
${CMAKE_COMMAND} -D SRC=${CMAKE_CURRENT_SOURCE_DIR}/Version.hpp.in
6+
-D DST=${CMAKE_CURRENT_BINARY_DIR}/Version.hpp
7+
-D GIT_EXECUTABLE=${GIT_EXECUTABLE}
8+
-P ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/GenerateVersionHeader.cmake)
9+
110
### 目标
211
# Lua 自动封装代码
312
file(GLOB_RECURSE LSTG_V2_BRIDGE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../include/lstg/v2/Bridge/*.hpp)
@@ -27,6 +36,7 @@ else()
2736
endif()
2837
target_include_directories(LuaSTGPlus2 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include ${CMAKE_CURRENT_BINARY_DIR})
2938
target_link_libraries(LuaSTGPlus2 PUBLIC LuaSTGPlusCore SDL2main cjson imgui implot)
39+
add_dependencies(LuaSTGPlus2 LuaSTGPlus2Version)
3040

3141
#set(LSTG_V2_DEFS_PRIVATE)
3242
#message("[LSTG-v2] Private compiler definitions: ${LSTG_V2_DEFS_PRIVATE}")

src/v2/Main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include <lstg/Core/Pal.hpp>
1212
#include <lstg/Core/Logging.hpp>
1313

14+
// 版本信息
15+
#include <Version.hpp>
16+
1417
using namespace std;
1518
using namespace lstg;
1619
using namespace lstg::v2;
@@ -23,6 +26,11 @@ SDLMAIN_DECLSPEC int main(int argc, char* argv[])
2326

2427
// 强制日志系统初始化
2528
Logging::GetInstance();
29+
#ifdef LSTG_DEVELOPMENT
30+
LSTG_LOG_INFO("Version: {} (Development mode)", LSTG_VERSION);
31+
#else
32+
LSTG_LOG_INFO("Version: {} (Shipping mode)", LSTG_VERSION);
33+
#endif
2634

2735
// 初始化 GameApp
2836
#ifdef LSTG_PLATFORM_EMSCRIPTEN

src/v2/Version.hpp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#pragma once
2+
#define LSTG_VERSION "@LSTG_VERSION@"

0 commit comments

Comments
 (0)