From 4b40b9a310ec44e46edff05834e5c4b8d387f2f4 Mon Sep 17 00:00:00 2001 From: nerix Date: Sat, 17 Jun 2023 19:43:39 +0200 Subject: [PATCH] Define Git and date constants in executables only (#4681) --- CHANGELOG.md | 1 + CMakeLists.txt | 10 +++++ src/CMakeLists.txt | 43 +++++++++---------- src/common/NetworkRequest.cpp | 5 ++- src/common/Version.cpp | 21 ++------- src/common/Version.hpp | 34 +++++++++++++++ .../liveupdates/BasicPubSubManager.hpp | 7 ++- 7 files changed, 78 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbe09aee9..000f7a050 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - Dev: Fixed undefined behavior when loading non-existant credentials. (#4673) - Dev: Added support for compiling with `sccache`. (#4678) - Dev: Added `sccache` in Windows CI. (#4678) +- Dev: Moved preprocessor Git and date definitions to executables only. (#4681) ## 2.4.4 diff --git a/CMakeLists.txt b/CMakeLists.txt index 340e8a64d..ac4338044 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,6 +185,16 @@ if (BUILD_WITH_CRASHPAD) add_subdirectory("${CMAKE_SOURCE_DIR}/lib/crashpad" EXCLUDE_FROM_ALL) endif() +# Used to provide a date of build in the About page (for nightly builds). Getting the actual time of +# compilation in CMake is a more involved, as documented in https://stackoverflow.com/q/24292898. +# For CI runs, however, the date of build file generation should be consistent with the date of +# compilation so this approximation is "good enough" for our purpose. +if (DEFINED ENV{CHATTERINO_SKIP_DATE_GEN}) + set(cmake_gen_date "1970-01-01") +else () + string(TIMESTAMP cmake_gen_date "%Y-%m-%d") +endif () + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b7465ebdb..a1a00e490 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,5 @@ set(LIBRARY_PROJECT "${PROJECT_NAME}-lib") +set(VERSION_PROJECT "${LIBRARY_PROJECT}-version") set(EXECUTABLE_PROJECT "${PROJECT_NAME}") add_compile_definitions(QT_DISABLE_DEPRECATED_BEFORE=0x050F00) @@ -46,8 +47,6 @@ set(SOURCE_FILES common/NetworkResult.hpp common/QLogging.cpp common/QLogging.hpp - common/Version.cpp - common/Version.hpp common/WindowDescriptors.cpp common/WindowDescriptors.hpp @@ -799,15 +798,26 @@ set_target_properties(${LIBRARY_PROJECT} AUTOUIC ON ) -# Used to provide a date of build in the About page (for nightly builds). Getting the actual time of -# compilation in CMake is a more involved, as documented in https://stackoverflow.com/q/24292898. -# For CI runs, however, the date of build file generation should be consistent with the date of -# compilation so this approximation is "good enough" for our purpose. -if (DEFINED ENV{CHATTERINO_SKIP_DATE_GEN}) - set(cmake_gen_date "1970-01-01") -else () - string(TIMESTAMP cmake_gen_date "%Y-%m-%d") -endif () +# The version project has definitions about the build. +# To avoid recompilations because of changing preprocessor definitions, +# this is its own project. +set(VERSION_SOURCE_FILES common/Version.cpp common/Version.hpp) +add_library(${VERSION_PROJECT} STATIC ${VERSION_SOURCE_FILES}) + +# source group for IDEs +source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${VERSION_SOURCE_FILES}) +target_include_directories(${VERSION_PROJECT} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(${VERSION_PROJECT} PRIVATE Qt${MAJOR_QT_VERSION}::Core) +target_compile_definitions(${VERSION_PROJECT} PRIVATE + CHATTERINO_GIT_HASH=\"${GIT_HASH}\" + CHATTERINO_GIT_RELEASE=\"${GIT_RELEASE}\" + CHATTERINO_GIT_COMMIT=\"${GIT_COMMIT}\" + CHATTERINO_GIT_MODIFIED=${GIT_MODIFIED} + + CHATTERINO_CMAKE_GEN_DATE=\"${cmake_gen_date}\" +) + +target_link_libraries(${LIBRARY_PROJECT} PRIVATE ${VERSION_PROJECT}) target_compile_definitions(${LIBRARY_PROJECT} PUBLIC CHATTERINO @@ -815,18 +825,7 @@ target_compile_definitions(${LIBRARY_PROJECT} PUBLIC AB_CUSTOM_SETTINGS IRC_STATIC IRC_NAMESPACE=Communi - - CHATTERINO_GIT_HASH=\"${GIT_HASH}\" - CHATTERINO_GIT_RELEASE=\"${GIT_RELEASE}\" - CHATTERINO_GIT_COMMIT=\"${GIT_COMMIT}\" - - CHATTERINO_CMAKE_GEN_DATE=\"${cmake_gen_date}\" ) -if (GIT_MODIFIED) - target_compile_definitions(${LIBRARY_PROJECT} PUBLIC - CHATTERINO_GIT_MODIFIED - ) -endif () if (USE_SYSTEM_QTKEYCHAIN) target_compile_definitions(${LIBRARY_PROJECT} PUBLIC CMAKE_BUILD diff --git a/src/common/NetworkRequest.cpp b/src/common/NetworkRequest.cpp index d856faf1e..b4dc8cc46 100644 --- a/src/common/NetworkRequest.cpp +++ b/src/common/NetworkRequest.cpp @@ -184,8 +184,9 @@ void NetworkRequest::execute() void NetworkRequest::initializeDefaultValues() { - const auto userAgent = QString("chatterino/%1 (%2)") - .arg(CHATTERINO_VERSION, CHATTERINO_GIT_HASH) + const auto userAgent = QStringLiteral("chatterino/%1 (%2)") + .arg(Version::instance().version(), + Version::instance().commitHash()) .toUtf8(); this->data->request_.setRawHeader("User-Agent", userAgent); diff --git a/src/common/Version.cpp b/src/common/Version.cpp index bbd99e176..76ef549a0 100644 --- a/src/common/Version.cpp +++ b/src/common/Version.cpp @@ -4,27 +4,14 @@ #include -#define UGLYMACROHACK1(s) #s -#define FROM_EXTERNAL_DEFINE(s) UGLYMACROHACK1(s) - namespace chatterino { Version::Version() + : version_(CHATTERINO_VERSION) + , commitHash_(QStringLiteral(CHATTERINO_GIT_HASH)) + , isModified_(CHATTERINO_GIT_MODIFIED == 1) + , dateOfBuild_(QStringLiteral(CHATTERINO_CMAKE_GEN_DATE)) { - this->version_ = CHATTERINO_VERSION; - - this->commitHash_ = - QString(FROM_EXTERNAL_DEFINE(CHATTERINO_GIT_HASH)).remove('"'); - -#ifdef CHATTERINO_GIT_MODIFIED - this->isModified_ = true; -#endif - -#ifdef CHATTERINO_CMAKE_GEN_DATE - this->dateOfBuild_ = - QString(FROM_EXTERNAL_DEFINE(CHATTERINO_CMAKE_GEN_DATE)).remove('"'); -#endif - this->fullVersion_ = "Chatterino "; if (Modes::instance().isNightly) { diff --git a/src/common/Version.hpp b/src/common/Version.hpp index ab7764e1a..db3ce978f 100644 --- a/src/common/Version.hpp +++ b/src/common/Version.hpp @@ -38,8 +38,42 @@ # define CHATTERINO_OS "unknown" #endif +#define CHATTERINO_DECLARE_BUILD_CONSTANTS() \ + namespace chatterino::detail::version { \ + QString gitHash() \ + { \ + return QStringLiteral(CHATTERINO_GIT_HASH); \ + } \ + QString gitRelease() \ + { \ + return QStringLiteral(CHATTERINO_GIT_RELEASE); \ + } \ + QString gitCommit() \ + { \ + return QStringLiteral(CHATTERINO_GIT_COMMIT); \ + } \ + bool gitModified() \ + { \ + return CHATTERINO_GIT_MODIFIED == 1; \ + } \ + QString cmakeGenDate() \ + { \ + return QStringLiteral(CHATTERINO_CMAKE_GEN_DATE); \ + } \ + } // namespace chatterino::detail::version + namespace chatterino { +namespace detail::version { + + extern QString gitHash(); + extern QString gitRelease(); + extern QString gitCommit(); + extern bool gitModified(); + extern QString cmakeGenDate(); + +} // namespace detail::version + class Version { public: diff --git a/src/providers/liveupdates/BasicPubSubManager.hpp b/src/providers/liveupdates/BasicPubSubManager.hpp index d596866bc..f82f70363 100644 --- a/src/providers/liveupdates/BasicPubSubManager.hpp +++ b/src/providers/liveupdates/BasicPubSubManager.hpp @@ -87,8 +87,11 @@ public: this->websocketClient_.set_fail_handler([this](auto hdl) { this->onConnectionFail(hdl); }); - this->websocketClient_.set_user_agent("Chatterino/" CHATTERINO_VERSION - " (" CHATTERINO_GIT_HASH ")"); + this->websocketClient_.set_user_agent( + QStringLiteral("Chatterino/%1 (%2)") + .arg(Version::instance().version(), + Version::instance().commitHash()) + .toStdString()); } virtual ~BasicPubSubManager() = default;