Add support to cmake for showing commit info

This commit is contained in:
NepDisk 2024-07-29 18:19:46 -04:00
parent e470520710
commit 80f496667f
9 changed files with 162 additions and 43 deletions

View file

@ -122,15 +122,6 @@ if(NOT ${SRB2_CONFIG_DEV_BUILD})
add_subdirectory(assets)
endif()
## config.h generation
set(GIT_EXECUTABLE "git" CACHE FILEPATH "Path to git binary")
include(GitUtilities)
git_latest_commit(SRB2_COMP_COMMIT "${CMAKE_SOURCE_DIR}")
git_current_branch(SRB2_GIT_BRANCH "${CMAKE_SOURCE_DIR}")
set(SRB2_COMP_BRANCH "${SRB2_GIT_BRANCH}")
set(SRB2_COMP_REVISION "${SRB2_COMP_COMMIT}")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/src/config.h)
##### PACKAGE CONFIGURATION #####
set(SRB2_CPACK_GENERATOR "" CACHE STRING "Generator to use for making a package. E.g., ZIP, TGZ, DragNDrop (OSX only). Leave blank for default generator.")

17
cmake/Comptime.cmake Normal file
View file

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
set(CMAKE_BINARY_DIR "${BINARY_DIR}")
set(CMAKE_CURRENT_BINARY_DIR "${BINARY_DIR}")
# Set up CMAKE path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
include(GitUtilities)
git_current_branch(SRB2_COMP_BRANCH)
git_working_tree_dirty(SRB2_COMP_UNCOMMITTED)
git_latest_commit(SRB2_COMP_REVISION)
git_subject(SRB2_COMP_LASTCOMMIT)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/src/config.h")

View file

@ -6,38 +6,54 @@ endif()
set(__GitUtilities ON)
function(git_describe variable path)
execute_process(COMMAND "${GIT_EXECUTABLE}" "describe"
WORKING_DIRECTORY "${path}"
RESULT_VARIABLE result
macro(_git_command)
execute_process(
COMMAND "${GIT_EXECUTABLE}" ${ARGN}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endmacro()
macro(_git_easy_command)
_git_command(${ARGN})
set(${variable} "${output}" PARENT_SCOPE)
endmacro()
function(git_current_branch variable)
_git_command(symbolic-ref -q --short HEAD)
# If a detached head, a ref could still be resolved.
if("${output}" STREQUAL "")
_git_command(describe --all --exact-match --exclude */HEAD)
# Get the ref, in the form heads/master or
# remotes/origin/master so isolate the final part.
string(REGEX REPLACE ".*/" "" output "${output}")
endif()
set(${variable} "${output}" PARENT_SCOPE)
endfunction()
function(git_current_branch variable path)
execute_process(COMMAND ${GIT_EXECUTABLE} "symbolic-ref" "--short" "HEAD"
WORKING_DIRECTORY "${path}"
RESULT_VARIABLE result
OUTPUT_VARIABLE output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(${variable} "${output}" PARENT_SCOPE)
function(git_latest_commit variable)
_git_easy_command(rev-parse --short HEAD)
endfunction()
function(git_latest_commit variable path)
execute_process(COMMAND ${GIT_EXECUTABLE} "rev-parse" "--short" "HEAD"
WORKING_DIRECTORY "${path}"
RESULT_VARIABLE result
OUTPUT_VARIABLE output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
function(git_working_tree_dirty variable)
_git_command(status --porcelain -uno)
set(${variable} "${output}" PARENT_SCOPE)
endfunction()
if(output STREQUAL "")
set(${variable} FALSE PARENT_SCOPE)
else()
set(${variable} TRUE PARENT_SCOPE)
endif()
endfunction()
function(git_subject variable)
_git_easy_command(log -1 --format=%s)
endfunction()
function(get_git_dir variable)
_git_easy_command(rev-parse --git-dir)
endfunction()

View file

@ -43,7 +43,38 @@ set(SRB2_CONFIG_STATIC_OPENGL OFF CACHE BOOL
"Use statically linked OpenGL. NOT RECOMMENDED.")
set(SRB2_CONFIG_DEV_BUILD OFF CACHE BOOL
"Compile a development build of SRB2Kart.")
set(SRB2_CONFIG_SKIP_COMPTIME OFF CACHE BOOL
"Skip Comptime rebuild.")
if(NOT SRB2_CONFIG_SKIP_COMPTIME)
# This updates the modification time for comptime.c at the
# end of building so when the build system is ran next time,
# that file gets flagged. comptime.c will always be rebuilt.
#
# This begs the question, why always rebuild comptime.c?
# Some things like the git commit must be checked each time
# the program is built. But the build system determines which
# files should be rebuilt before anything else. So
# comptime.c, which only needs to be rebuilt based on
# information known at build time, must be told to rebuild
# before that information can be ascertained.
add_custom_command(
TARGET SRB2SDL2
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_CURRENT_SOURCE_DIR}/comptime.c
)
endif()
# config.h is generated by this command. It should be done at
# build time for accurate git information and before anything
# that needs it, obviously.
set(GIT_EXECUTABLE "git" CACHE FILEPATH "Path to git binary")
add_custom_target(_SRB2_reconf ALL
COMMAND ${CMAKE_COMMAND} -DGIT_EXECUTABLE=${GIT_EXECUTABLE} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DBINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}/.. -P ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Comptime.cmake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.."
)
add_dependencies(SRB2SDL2 _SRB2_reconf)
### use internal libraries?
if(${CMAKE_SYSTEM} MATCHES "Windows") ###set on Windows only
set(SRB2_CONFIG_USE_INTERNAL_LIBRARIES OFF CACHE BOOL

View file

@ -11,10 +11,17 @@
#include "config.h"
const char *compbranch = SRB2_COMP_BRANCH;
const char *comprevision = SRB2_COMP_REVISION;
const char *complast = SRB2_COMP_LASTCOMMIT;
#elif (defined(COMPVERSION))
#include "comptime.h"
#else
const char *compbranch = "Unknown";
const char *comprevision = "illegal";
#endif
const int compuncommitted =
#if (defined(COMPVERSION_UNCOMMITTED))
1;
@ -22,11 +29,5 @@ const int compuncommitted =
0;
#endif
#else
const char *compbranch = "Unknown";
const char *comprevision = "illegal";
#endif
const char *compdate = __DATE__;
const char *comptime = __TIME__;

53
src/config.h Normal file
View file

@ -0,0 +1,53 @@
/** SRB2 CMake Configuration */
#ifndef __CONFIG_H__
#define __CONFIG_H__
/* DO NOT MODIFY config.h DIRECTLY! It will be overwritten by cmake.
* If you want to change a configuration option here, modify it in
* your CMakeCache.txt. config.h.in is used as a template for CMake
* variables, so you can insert them here too.
*/
#ifdef CMAKECONFIG
#define ASSET_HASH_MAIN_KART ""
#define ASSET_HASH_GFX_PK3 ""
#define ASSET_HASH_TEXTURES_PK3 ""
#define ASSET_HASH_CHARS_PK3 ""
#define ASSET_HASH_MAPS_PK3 ""
#ifdef USE_PATCH_FILE
#define ASSET_HASH_PATCH_PK3 ""
#endif
#define SRB2_COMP_REVISION "209e81c9b"
#define SRB2_COMP_BRANCH "v2dev3"
// This is done with configure_file instead of defines in order to avoid
// recompiling the whole target whenever the working directory state changes
#define SRB2_COMP_UNCOMMITTED
#ifdef SRB2_COMP_UNCOMMITTED
#define COMPVERSION_UNCOMMITTED
#endif
#define SRB2_COMP_LASTCOMMIT "Add support to cmake for uncommited changes text and clean up git utils"
#define CMAKE_ASSETS_DIR "/home/maple/build/blankart/assets"
#else
/* Manually defined asset hashes for non-CMake builds
* Last updated 2019 / 01 / 18 - Kart v1.0.2 - Main assets
* Last updated 2020 / 08 / 30 - Kart v1.3 - patch.kart
*/
#define ASSET_HASH_MAIN_KART "00000000000000000000000000000000"
#define ASSET_HASH_GFX_PK3 "00000000000000000000000000000000"
#define ASSET_HASH_TEXTURES_PK3 "00000000000000000000000000000000"
#define ASSET_HASH_CHARS_PK3 "00000000000000000000000000000000"
#define ASSET_HASH_MAPS_PK3 "00000000000000000000000000000000"
#ifdef USE_PATCH_FILE
#define ASSET_HASH_PATCH_PK3 "00000000000000000000000000000000"
#endif
#endif
#endif

View file

@ -22,6 +22,14 @@
#define SRB2_COMP_REVISION "${SRB2_COMP_REVISION}"
#define SRB2_COMP_BRANCH "${SRB2_COMP_BRANCH}"
// This is done with configure_file instead of defines in order to avoid
// recompiling the whole target whenever the working directory state changes
#cmakedefine SRB2_COMP_UNCOMMITTED
#ifdef SRB2_COMP_UNCOMMITTED
#define COMPVERSION_UNCOMMITTED
#endif
#define SRB2_COMP_LASTCOMMIT "${SRB2_COMP_LASTCOMMIT}"
#define CMAKE_ASSETS_DIR "${CMAKE_SOURCE_DIR}/assets"

View file

@ -613,7 +613,7 @@ UINT32 quickncasehash (const char *p, size_t n)
#define PUNCTUATION "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
// Compile date and time and revision.
extern const char *compdate, *comptime, *comprevision, *compbranch;
extern const char *compdate, *comptime, *comprevision, *compbranch, *complast;
extern int compuncommitted;
// Disabled code and code under testing

View file

@ -2013,13 +2013,15 @@ void F_TitleScreenDrawer(void)
addtext(V_ALLOWLOWERCASE|V_REDMAP, "Netgame host for testers");
addtext(V_ALLOWLOWERCASE|V_TRANSLUCENT, va("%s", compdate));
#elif defined(DEVELOP)
addtext(V_ALLOWLOWERCASE|V_TRANSLUCENT, comprevision);
addtext(V_ALLOWLOWERCASE|V_TRANSLUCENT, compbranch);
addtext(V_ALLOWLOWERCASE|V_GREENMAP|V_TRANSLUCENT, comprevision);
addtext(V_ALLOWLOWERCASE|V_YELLOWMAP|V_TRANSLUCENT, compbranch);
addtext(V_ALLOWLOWERCASE|V_ORANGEMAP|V_TRANSLUCENT, va("%s", complast));
#else // Regular build
addtext(V_ALLOWLOWERCASE|V_TRANSLUCENT, va("%s", VERSIONSTRING));
#endif
if (compuncommitted)
addtext(V_REDMAP|V_STRINGDANCE, "! UNCOMMITTED CHANGES !");
addtext(V_REDMAP|V_STRINGDANCE|V_TRANSLUCENT, "! UNCOMMITTED CHANGES !");
}
#undef addtext
}