From dced72141d8fc6cb5a19437ff3a417d3b59f7272 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Sun, 11 Dec 2016 06:39:08 -0500 Subject: [PATCH] grab git info during build for use in version strings --- CMakeLists.txt | 18 ++++++++++++++ cmake/GitInfo.cmake | 47 ++++++++++++++++++++++++++++++++++++ include/CMakeLists.txt | 1 + include/gitinfo.h.in | 9 +++++++ include/version.h | 4 +-- src/cmdline/cmdline.cpp | 14 ++++++++--- src/freespace2/freespace.cpp | 7 ++++++ src/menuui/mainhallmenu.cpp | 3 ++- 8 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 cmake/GitInfo.cmake create mode 100644 include/gitinfo.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index fcd423b..4be918d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,7 @@ include(${wxWidgets_USE_FILE}) include_directories( ${PROJECT_SOURCE_DIR}/include + ${CMAKE_BINARY_DIR}/include ${SDL2_INCLUDE_DIR} ${OPENAL_INCLUDE_DIR} ${LIBWEBSOCKETS_INCLUDE_DIR} @@ -110,6 +111,19 @@ add_subdirectory(include) # ############################################################################## # +# +# custom target for source tree version info +# + +add_custom_target(buildver + COMMAND ${CMAKE_COMMAND} + -DSOURCE_DIR=${CMAKE_SOURCE_DIR} + -DBINARY_DIR=${CMAKE_BINARY_DIR} + -P ${CMAKE_SOURCE_DIR}/cmake/GitInfo.cmake + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + SOURCES ${CMAKE_SOURCE_DIR}/cmake/GitInfo.cmake +) + # # main code/game library # @@ -124,6 +138,10 @@ add_library(code CreateSourceGroups(${code_SOURCE} ${platform_SOURCE}) +add_dependencies(code + buildver +) + # # the game itself # diff --git a/cmake/GitInfo.cmake b/cmake/GitInfo.cmake new file mode 100644 index 0000000..9c5a94e --- /dev/null +++ b/cmake/GitInfo.cmake @@ -0,0 +1,47 @@ + +find_package(Git) + +if(Git_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD + WORKING_DIRECTORY ${SOURCE_DIR} + OUTPUT_VARIABLE GIT_BRANCH + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 --format=%h + WORKING_DIRECTORY ${SOURCE_DIR} + OUTPUT_VARIABLE GIT_COMMIT_HASH + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 --format=%ad --date=format:%Y%m%d + WORKING_DIRECTORY ${SOURCE_DIR} + OUTPUT_VARIABLE GIT_COMMIT_DATE + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + execute_process( + COMMAND ${GIT_EXECUTABLE} describe + WORKING_DIRECTORY ${SOURCE_DIR} + OUTPUT_VARIABLE GIT_TAG + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + if(${GIT_TAG}) + message("Building for Git version: ${GIT_COMMIT_DATE}~${GIT_BRANCH}:${GIT_COMMIT_HASH} (${GIT_TAG})") + else() + message("Building for Git version: ${GIT_COMMIT_DATE}~${GIT_BRANCH}:${GIT_COMMIT_HASH}") + endif() +endif() + +configure_file( + ${SOURCE_DIR}/include/gitinfo.h.in + ${BINARY_DIR}/include/gitinfo.h +) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 70c3075..4859ba0 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -60,6 +60,7 @@ set(code_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/gameplayhelp.h ${CMAKE_CURRENT_SOURCE_DIR}/gamesequence.h ${CMAKE_CURRENT_SOURCE_DIR}/gamesnd.h + ${CMAKE_CURRENT_SOURCE_DIR}/gitinfo.h.in ${CMAKE_CURRENT_SOURCE_DIR}/grinternal.h ${CMAKE_CURRENT_SOURCE_DIR}/grgl1.h ${CMAKE_CURRENT_SOURCE_DIR}/grgl2.h diff --git a/include/gitinfo.h.in b/include/gitinfo.h.in new file mode 100644 index 0000000..12fb1c7 --- /dev/null +++ b/include/gitinfo.h.in @@ -0,0 +1,9 @@ +#ifndef GITINFO_H +#define GITINFO_H + +#cmakedefine GIT_BRANCH "@GIT_BRANCH@" +#cmakedefine GIT_COMMIT_HASH "@GIT_COMMIT_HASH@" +#cmakedefine GIT_COMMIT_DATE "@GIT_COMMIT_DATE@" +#cmakedefine GIT_TAG "@GIT_TAG@" + +#endif // GITINFO_H diff --git a/include/version.h b/include/version.h index 1d00baa..3b259b1 100644 --- a/include/version.h +++ b/include/version.h @@ -144,8 +144,8 @@ #define FS_VERSION_BUILD 00 // Build version #endif -#define IO_VERSION_MAJOR 00 -#define IO_VERSION_MINOR 95 +// build version info +#include "gitinfo.h" #define VERSION_LOC_FNAME "version.nfo" #define MOTD_LOC_FNAME "motd.txt" diff --git a/src/cmdline/cmdline.cpp b/src/cmdline/cmdline.cpp index c869341..7e14da5 100644 --- a/src/cmdline/cmdline.cpp +++ b/src/cmdline/cmdline.cpp @@ -466,8 +466,11 @@ static void print_instructions() printf("\n"); #endif - printf("%s v%d.%02d\n", Osreg_title, FS_VERSION_MAJOR, FS_VERSION_MINOR); - printf("icculus.org client v%d.%02d\n\n", IO_VERSION_MAJOR, IO_VERSION_MINOR); + printf("%s v%d.%02d", Osreg_title, FS_VERSION_MAJOR, FS_VERSION_MINOR); +#if defined(GIT_COMMIT_DATE) && defined(GIT_COMMIT_HASH) + printf(" ~ %s:%s", GIT_COMMIT_DATE, GIT_COMMIT_HASH); +#endif + printf("\n"); exit(0); } @@ -782,8 +785,11 @@ int parse_cmdline(const char *cmdline) // display game version if(fs_version.found()){ - printf("%s version: %d.%02d\n", Osreg_title, FS_VERSION_MAJOR, FS_VERSION_MINOR); - printf("icculus.org client version: %d.%02d\n", IO_VERSION_MAJOR, IO_VERSION_MINOR); + printf("%s version: %d.%02d", Osreg_title, FS_VERSION_MAJOR, FS_VERSION_MINOR); +#if defined(GIT_COMMIT_DATE) && defined(GIT_COMMIT_HASH) + printf(" ~ %s:%s", GIT_COMMIT_DATE, GIT_COMMIT_HASH); +#endif + printf("\n"); exit(0); } diff --git a/src/freespace2/freespace.cpp b/src/freespace2/freespace.cpp index a46774f..6483bcf 100644 --- a/src/freespace2/freespace.cpp +++ b/src/freespace2/freespace.cpp @@ -7420,6 +7420,9 @@ void get_version_string(char *str, const int str_len) //XSTR:OFF #ifdef FS1_DEMO SDL_snprintf(str, str_len, "Dv%d.%02d", FS_VERSION_MAJOR, FS_VERSION_MINOR); +#if !defined(NDEBUG) && defined(GIT_COMMIT_HASH) + SDL_strlcat(str, "~" GIT_COMMIT_HASH, str_len); +#endif return; #endif @@ -7429,6 +7432,10 @@ void get_version_string(char *str, const int str_len) SDL_snprintf(str, str_len, "v%d.%02d.%02d", FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD ); } +#if !defined(NDEBUG) && defined(GIT_COMMIT_HASH) + SDL_strlcat(str, "~" GIT_COMMIT_HASH, str_len); +#endif + #if defined (FS2_DEMO) SDL_strlcat(str, " D", str_len); #elif defined (OEM_BUILD) diff --git a/src/menuui/mainhallmenu.cpp b/src/menuui/mainhallmenu.cpp index 71c6d5d..87edfe0 100644 --- a/src/menuui/mainhallmenu.cpp +++ b/src/menuui/mainhallmenu.cpp @@ -1928,7 +1928,8 @@ void main_hall_blit_version() #ifdef MAKE_FS1 gr_string(gr_screen.max_w - (w + 10), gr_screen.max_h - 12, version_string); #else - gr_string(gr_screen.max_w - 55, gr_screen.max_h - 12, version_string); + // original offset: 55 pixels, default string length: 34 pixels + gr_string(gr_screen.max_w - (w + 21), gr_screen.max_h - 12, version_string); #endif } -- 2.39.2