Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define Git and date constants in executables only #4681

Merged
merged 8 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
43 changes: 21 additions & 22 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -799,34 +798,34 @@ 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
UNICODE
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
Expand Down
5 changes: 3 additions & 2 deletions src/common/NetworkRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 4 additions & 17 deletions src/common/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
34 changes: 34 additions & 0 deletions src/common/Version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,42 @@
# define CHATTERINO_OS "unknown"
#endif

#define CHATTERINO_DECLARE_BUILD_CONSTANTS() \
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
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:
Expand Down
7 changes: 5 additions & 2 deletions src/providers/liveupdates/BasicPubSubManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ class BasicPubSubManager
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;
Expand Down