From d80db6f51d99ac320cf8bdf4440339e899bc7024 Mon Sep 17 00:00:00 2001 From: Eidolon Date: Sat, 8 Oct 2022 21:19:04 -0500 Subject: [PATCH] cmake: add COMPVERSION_UNCOMMITTED --- CMakeLists.txt | 11 ++++++----- cmake/Modules/GitUtilities.cmake | 16 ++++++++++++++++ src/comptime.c | 12 ++++++------ src/config.h.in | 6 ++++++ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d37514f2..0771f712c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,20 +117,21 @@ set(SRB2_SDL2_EXE_NAME ringracers CACHE STRING "Executable binary output name") include_directories(${CMAKE_CURRENT_BINARY_DIR}/src) -add_subdirectory(src) -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}") +git_working_tree_dirty(SRB2_COMP_UNCOMMITTED "${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) +add_subdirectory(src) +if(NOT ${SRB2_CONFIG_DEV_BUILD}) + add_subdirectory(assets) +endif() + ##### 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.") diff --git a/cmake/Modules/GitUtilities.cmake b/cmake/Modules/GitUtilities.cmake index d29e6b509..4a94b5b5c 100644 --- a/cmake/Modules/GitUtilities.cmake +++ b/cmake/Modules/GitUtilities.cmake @@ -40,4 +40,20 @@ function(git_latest_commit variable path) ) set(${variable} "${output}" PARENT_SCOPE) +endfunction() + +function(git_working_tree_dirty variable path) + execute_process(COMMAND ${GIT_EXECUTABLE} "status" "--porcelain" "-uno" + WORKING_DIRECTORY "${path}" + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + if(output STREQUAL "") + set(${variable} FALSE PARENT_SCOPE) + else() + set(${variable} TRUE PARENT_SCOPE) + endif() endfunction() \ No newline at end of file diff --git a/src/comptime.c b/src/comptime.c index 81b5ec7d6..2baef79d6 100644 --- a/src/comptime.c +++ b/src/comptime.c @@ -15,6 +15,12 @@ const char *comprevision = SRB2_COMP_REVISION; #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 +28,5 @@ const int compuncommitted = 0; #endif -#else -const char *compbranch = "Unknown"; -const char *comprevision = "illegal"; - -#endif - const char *compdate = __DATE__; const char *comptime = __TIME__; diff --git a/src/config.h.in b/src/config.h.in index 1636a6731..c27628ffd 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -22,6 +22,12 @@ #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 CMAKE_ASSETS_DIR "${CMAKE_SOURCE_DIR}/assets"