From 02a45f9cbc1b1cb7ad765087e11aa444c809a4ee Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 25 Dec 2020 15:24:48 -0800 Subject: [PATCH] Don't recompile all files when the commit hash is changed The previous implementation of showing the commit hash on the title screen used a preprocessor definition added at CMake time to pass the hash and date. This was passed for every file compiled, so if the date or hash changed, then every file would be recompiled. This is especially annoying if you're working on the game and switching branches all the time - the game has at least 50 source files to recompile! To fix this, we'll switch to using a generated file, named Version.h.out, that only gets included by the necessary files (which there is only one of - Render.cpp). It will be autogenerated by CMake (by using CONFIGURE_FILE(), which takes a templated file and does a find-and-replace on it, not unlike C macros), and since there's only one file that includes it, only one file will need to be recompiled when it changes. And also to prevent Version.h.out being a required file, it will only be included if necessary (i.e. OFFICIAL_BUILD is off). Since the C preprocessor can't ignore non-existing include files and will always error on them, I wrapped the #include in an #ifdef VERSION_H_EXISTS, and CMake will add the VERSION_H_OUT_EXISTS define when generating Version.h.out. The wrapper is named Version.h, so any file that #includes the commit hash and date should #include Version.h instead of Version.h.out. As an added bonus, I've also made it so CMake will print "This is interim commit [HASH] (committed [DATE])" at configure time if the game is going to be compiled with an interim commit hash. Now, there is also the issue that the commit hash change will only be noticed in the first place if CMake needs to be re-ran for anything, but that's a less severe issue than requiring recompilation of 50(!) or so files. --- desktop_version/.gitignore | 1 + desktop_version/CMakeLists.txt | 11 ++++++++--- desktop_version/src/Render.cpp | 1 + desktop_version/src/Version.h | 8 ++++++++ desktop_version/src/Version.h.in | 7 +++++++ 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 desktop_version/src/Version.h create mode 100644 desktop_version/src/Version.h.in diff --git a/desktop_version/.gitignore b/desktop_version/.gitignore index 92608479..241f712d 100644 --- a/desktop_version/.gitignore +++ b/desktop_version/.gitignore @@ -9,6 +9,7 @@ VVVVVV.exe VVVVVV *.a *.gch +src/Version.h.out # Game data data.zip diff --git a/desktop_version/CMakeLists.txt b/desktop_version/CMakeLists.txt index fb6afe77..b603f638 100644 --- a/desktop_version/CMakeLists.txt +++ b/desktop_version/CMakeLists.txt @@ -28,15 +28,20 @@ ELSE() ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE ) - ADD_DEFINITIONS(-DINTERIM_COMMIT="${INTERIM_COMMIT}") - EXECUTE_PROCESS( COMMAND "${GIT_EXECUTABLE}" log -1 --format=%cs OUTPUT_VARIABLE COMMIT_DATE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE ) - ADD_DEFINITIONS(-DCOMMIT_DATE="${COMMIT_DATE}") + MESSAGE(STATUS "This is interim commit ${INTERIM_COMMIT} (committed ${COMMIT_DATE})") + + # Take template file and replace the macros with what we have + # Unfortunately the output is taken relative to binary path so we have to qualify it + CONFIGURE_FILE(src/Version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h.out) + + # This lets Version.h know that Version.h.out exists + ADD_DEFINITIONS(-DVERSION_H_OUT_EXISTS) ENDIF() ENDIF() diff --git a/desktop_version/src/Render.cpp b/desktop_version/src/Render.cpp index cf79cfb8..2cca6838 100644 --- a/desktop_version/src/Render.cpp +++ b/desktop_version/src/Render.cpp @@ -10,6 +10,7 @@ #include "Music.h" #include "Script.h" #include "UtilityClass.h" +#include "Version.h" int tr; int tg; diff --git a/desktop_version/src/Version.h b/desktop_version/src/Version.h new file mode 100644 index 00000000..e1bf0cbc --- /dev/null +++ b/desktop_version/src/Version.h @@ -0,0 +1,8 @@ +#ifndef VERSION_H +#define VERSION_H + +#ifdef VERSION_H_OUT_EXISTS +#include "Version.h.out" +#endif + +#endif /* VERSION_H */ diff --git a/desktop_version/src/Version.h.in b/desktop_version/src/Version.h.in new file mode 100644 index 00000000..0837b9cb --- /dev/null +++ b/desktop_version/src/Version.h.in @@ -0,0 +1,7 @@ +#ifndef VERSION_H_OUT +#define VERSION_H_OUT + +#define INTERIM_COMMIT "@INTERIM_COMMIT@" +#define COMMIT_DATE "@COMMIT_DATE@" + +#endif /* VERSION_H_OUT */