Define Git and date constants in executables only (#4681)

This commit is contained in:
nerix 2023-06-17 19:43:39 +02:00 committed by GitHub
parent aff9342647
commit 4b40b9a310
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 43 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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);

View file

@ -4,27 +4,14 @@
#include <QFileInfo>
#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)
{

View file

@ -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:

View file

@ -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;