From 38f0d27901bcda0d46179ab0d122764072b380b9 Mon Sep 17 00:00:00 2001 From: baldurk Date: Thu, 20 Jun 2019 13:29:55 +0100 Subject: [PATCH] Use configure_file in CMake to force rebuild if git commit changes * We also only use GIT_COMMIT_HASH where necessary to avoid rebuilding many files for no reason, including splitting version.cpp out into a separate project as we do with VS since otherwise changing its preprocessor defines rebuilds the whole renderdoc project. * At the same time, move the git hash to be internal only so we don't have to try to link version.cpp into other projects like renderdoccmd or qrenderdoc. --- CMakeLists.txt | 23 +++++++++++++++++++-- qrenderdoc/CMakeLists.txt | 4 ---- qrenderdoc/Code/qrenderdoc.cpp | 2 +- qrenderdoc/Windows/Dialogs/AboutDialog.cpp | 2 +- qrenderdoc/Windows/MainWindow.cpp | 4 ++-- qrenderdoc/qrenderdoc.pro | 2 -- renderdoc/CMakeLists.txt | 7 ++++++- renderdoc/api/replay/version.h | 10 +++++---- renderdoc/renderdoc_version.vcxproj | 2 +- renderdoc/renderdoc_version.vcxproj.filters | 2 +- renderdoc/{api => }/replay/version.cpp | 0 renderdoccmd/CMakeLists.txt | 2 +- renderdoccmd/renderdoccmd.cpp | 3 ++- 13 files changed, 42 insertions(+), 21 deletions(-) rename renderdoc/{api => }/replay/version.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3ab4682a..75ead2615 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ endif() # version setting variables. See renderdoc/api/replay/version.h -set(BUILD_VERSION_HASH "" CACHE STRING "The current git commit hash. See GIT_COMMIT_HASH in renderdoc/api/replay/version.cpp") +set(BUILD_VERSION_HASH "" CACHE STRING "The current git commit hash. See renderdoc/replay/version.cpp") option(BUILD_VERSION_STABLE "If this is a stable build. See RENDERDOC_STABLE_BUILD in renderdoc/api/replay/version.h" OFF) set(BUILD_VERSION_DIST_NAME "" CACHE STRING "The name of the distribution. See DISTRIBUTION_NAME in renderdoc/api/replay/version.h") set(BUILD_VERSION_DIST_VER "" CACHE STRING "The distribution-specific version number. See DISTRIBUTION_VERSION in renderdoc/api/replay/version.h") @@ -134,7 +134,26 @@ endfunction(get_git_hash) # get git commit hash get_git_hash(GIT_COMMIT_HASH) string(STRIP ${GIT_COMMIT_HASH} GIT_COMMIT_HASH) -add_definitions(-DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}") + +if(EXISTS "${CMAKE_SOURCE_DIR}/.git/HEAD") + # Use configure_file to force a re-configure if the HEAD file changes. That means changing branch. + # The re-configure will pick up a new git commit hash above, if it exists and is valid. + # This will be slightly redundant if BUILD_VERSION_HASH is specified but that is not a case we expect to care about. + configure_file("${CMAKE_SOURCE_DIR}/.git/HEAD" "${CMAKE_BINARY_DIR}/git_HEAD" COPYONLY) + + # in addition, if HEAD is a ref, do the same on the file it's pointing to (since HEAD won't change for commits to the current branch) + # if we change branch then this will correspondingly + file(READ ${CMAKE_SOURCE_DIR}/.git/HEAD HEAD_CONTENTS) + string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS) + + if("${HEAD_CONTENTS}" MATCHES "ref: ") + string(REPLACE "ref: " "" REF_LOCATION "${HEAD_CONTENTS}") + + if(EXISTS "${CMAKE_SOURCE_DIR}/.git/${REF_LOCATION}") + configure_file("${CMAKE_SOURCE_DIR}/.git/${REF_LOCATION}" "${CMAKE_BINARY_DIR}/git_ref" COPYONLY) + endif() + endif() +endif() project(RenderDoc CXX C) diff --git a/qrenderdoc/CMakeLists.txt b/qrenderdoc/CMakeLists.txt index 9fd65b2d3..fb9b193bd 100644 --- a/qrenderdoc/CMakeLists.txt +++ b/qrenderdoc/CMakeLists.txt @@ -153,10 +153,6 @@ if(CMAKE_COMPILER_IS_GNUCXX) endif() # propagate build version info. Lots of escaping needed here to pass ""s into the define value -file(APPEND - ${CMAKE_BINARY_DIR}/qrenderdoc/qrenderdoc_cmake.pri - "DEFINES+=GIT_COMMIT_HASH='\\\\\"${GIT_COMMIT_HASH}\\\\\"'\n") - if(BUILD_VERSION_STABLE) file(APPEND ${CMAKE_BINARY_DIR}/qrenderdoc/qrenderdoc_cmake.pri diff --git a/qrenderdoc/Code/qrenderdoc.cpp b/qrenderdoc/Code/qrenderdoc.cpp index b26077c94..21188ad2e 100644 --- a/qrenderdoc/Code/qrenderdoc.cpp +++ b/qrenderdoc/Code/qrenderdoc.cpp @@ -183,7 +183,7 @@ int main(int argc, char *argv[]) if(parser.isSet(versionOption)) { - printf("QRenderDoc v%s (%s)\n", MAJOR_MINOR_VERSION_STRING, GitVersionHash); + printf("QRenderDoc v%s (%s)\n", MAJOR_MINOR_VERSION_STRING, RENDERDOC_GetCommitHash()); #if defined(DISTRIBUTION_VERSION) printf("Packaged for %s - %s\n", DISTRIBUTION_NAME, DISTRIBUTION_CONTACT); #endif diff --git a/qrenderdoc/Windows/Dialogs/AboutDialog.cpp b/qrenderdoc/Windows/Dialogs/AboutDialog.cpp index 951a0cf2d..6cec9f2ac 100644 --- a/qrenderdoc/Windows/Dialogs/AboutDialog.cpp +++ b/qrenderdoc/Windows/Dialogs/AboutDialog.cpp @@ -34,7 +34,7 @@ AboutDialog::AboutDialog(QWidget *parent) : QDialog(parent), ui(new Ui::AboutDia { ui->setupUi(this); - QString hash = QString::fromLatin1(GitVersionHash); + QString hash = QString::fromLatin1(RENDERDOC_GetCommitHash()); if(hash[0] == QLatin1Char('N') && hash[1] == QLatin1Char('O')) { diff --git a/qrenderdoc/Windows/MainWindow.cpp b/qrenderdoc/Windows/MainWindow.cpp index 8c1d3430c..73f8b79c2 100644 --- a/qrenderdoc/Windows/MainWindow.cpp +++ b/qrenderdoc/Windows/MainWindow.cpp @@ -1015,7 +1015,7 @@ void MainWindow::SetTitle(const QString &filename) else text += tr("Unstable release (%1 - %2)") .arg(lit(FULL_VERSION_STRING)) - .arg(QString::fromLatin1(GitVersionHash)); + .arg(QString::fromLatin1(RENDERDOC_GetCommitHash())); if(IsRunningAsAdmin()) text += tr(" (Administrator)"); @@ -2631,7 +2631,7 @@ void MainWindow::on_action_Send_Error_Report_triggered() QVariantMap json; json[lit("version")] = lit(FULL_VERSION_STRING); - json[lit("gitcommit")] = QString::fromLatin1(GitVersionHash); + json[lit("gitcommit")] = QString::fromLatin1(RENDERDOC_GetCommitHash()); json[lit("replaycrash")] = 1; json[lit("report")] = (QString)report; diff --git a/qrenderdoc/qrenderdoc.pro b/qrenderdoc/qrenderdoc.pro index 5c648f569..df276811f 100644 --- a/qrenderdoc/qrenderdoc.pro +++ b/qrenderdoc/qrenderdoc.pro @@ -124,8 +124,6 @@ win32 { SOURCES += $$CMAKE_DIR/qrenderdoc/qrenderdoc_python.cxx SOURCES += $$CMAKE_DIR/qrenderdoc/qrenderdoc.py.c - SOURCES += $$_PRO_FILE_PWD_/../renderdoc/api/replay/version.cpp - CONFIG += warn_off CONFIG += c++14 QMAKE_CFLAGS_WARN_OFF -= -w diff --git a/renderdoc/CMakeLists.txt b/renderdoc/CMakeLists.txt index 42da09054..9dad4e0d2 100644 --- a/renderdoc/CMakeLists.txt +++ b/renderdoc/CMakeLists.txt @@ -93,7 +93,6 @@ set(sources api/replay/vk_pipestate.h api/replay/version.h api/replay/renderdoc_tostr.inl - api/replay/version.cpp common/common.cpp common/common.h common/custom_assert.h @@ -473,6 +472,12 @@ list(APPEND renderdoc_objects $ ${data_objects}) +add_library(rdoc_version OBJECT replay/version.cpp) +target_compile_definitions(rdoc_version PRIVATE -DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}") + +list(APPEND renderdoc_objects + $) + # posix_libentry must be the last so that library_loaded is called after # static objects are constructed. We guarantee this happens after even # any other static libraries that we link by making it its own static diff --git a/renderdoc/api/replay/version.h b/renderdoc/api/replay/version.h index 40927e591..c6c2673e0 100644 --- a/renderdoc/api/replay/version.h +++ b/renderdoc/api/replay/version.h @@ -41,12 +41,14 @@ // // To prevent a project rebuild cascading when the git commit changes, we declare a char array -// that's implemented in version.inl, which can be included in each module that uses the version. +// that's implemented in version.cpp, which is linked into the core module. // It's 41 characters to allow 40 characters of commit hash plus trailing NULL. -// Then the .cpp that includes version.inl is the only one that actually needs to have the hash -// defined properly. -// This replaces the previous GIT_COMMIT_HASH define here. +// Then version.cpp is the only thing that needs to be rebuilt when the git commit changes +// +// Only available internally, external users should use RENDERDOC_GetCommitHash() +#if defined(RENDERDOC_EXPORTS) extern "C" const char GitVersionHash[41]; +#endif // If this variable is set to 1, then this build is considered a stable version - based on a tagged // version number upstream, possibly with some patches applied as necessary. diff --git a/renderdoc/renderdoc_version.vcxproj b/renderdoc/renderdoc_version.vcxproj index ef72074f8..35f0bb903 100644 --- a/renderdoc/renderdoc_version.vcxproj +++ b/renderdoc/renderdoc_version.vcxproj @@ -122,6 +122,6 @@ namespace GitIntrospection { - + \ No newline at end of file diff --git a/renderdoc/renderdoc_version.vcxproj.filters b/renderdoc/renderdoc_version.vcxproj.filters index fe080078f..5a13c09a5 100644 --- a/renderdoc/renderdoc_version.vcxproj.filters +++ b/renderdoc/renderdoc_version.vcxproj.filters @@ -1,6 +1,6 @@  - + \ No newline at end of file diff --git a/renderdoc/api/replay/version.cpp b/renderdoc/replay/version.cpp similarity index 100% rename from renderdoc/api/replay/version.cpp rename to renderdoc/replay/version.cpp diff --git a/renderdoccmd/CMakeLists.txt b/renderdoccmd/CMakeLists.txt index 1e52a77eb..2db4a1490 100644 --- a/renderdoccmd/CMakeLists.txt +++ b/renderdoccmd/CMakeLists.txt @@ -1,4 +1,4 @@ -set(sources renderdoccmd.cpp ${CMAKE_SOURCE_DIR}/renderdoc/api/replay/version.cpp) +set(sources renderdoccmd.cpp) set(includes PRIVATE ${CMAKE_SOURCE_DIR}/renderdoc/api) set(libraries PRIVATE renderdoc) diff --git a/renderdoccmd/renderdoccmd.cpp b/renderdoccmd/renderdoccmd.cpp index 774eca1da..2fec0181a 100644 --- a/renderdoccmd/renderdoccmd.cpp +++ b/renderdoccmd/renderdoccmd.cpp @@ -171,7 +171,8 @@ struct VersionCommand : public Command virtual int Execute(cmdline::parser &parser, const CaptureOptions &) { std::cout << "renderdoccmd " << (sizeof(uintptr_t) == sizeof(uint64_t) ? "x64" : "x86") - << " v" MAJOR_MINOR_VERSION_STRING << " built from " << GitVersionHash << std::endl; + << " v" MAJOR_MINOR_VERSION_STRING << " built from " << RENDERDOC_GetCommitHash() + << std::endl; #if defined(DISTRIBUTION_VERSION) std::cout << "Packaged for " << DISTRIBUTION_NAME << " (" << DISTRIBUTION_VERSION << ") - "