From fd2a6918c28227da649d5ff7122e637e96590c06 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Mon, 10 Jun 2024 13:43:08 +0200 Subject: [PATCH] ci: Add a cpp file for version information This should allow us to update the version information on each commit, but still cache most of the build. The single compilation unit for version.cpp won't be cached, but everything else can be cached, as the compiler arguments won't change on every commit. --- CMakeLists.txt | 33 ++++++++++++++++----------------- src/core/CMakeLists.txt | 4 ++++ src/core/version.cpp.in | 12 ++++++++++++ src/core/version.h | 8 ++++++++ src/gui/mainwindow.cpp | 3 ++- src/main.cpp | 3 ++- 6 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 src/core/version.cpp.in create mode 100644 src/core/version.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f8cf98d..67337dcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ option(USE_ASAN "use Address Sanitizer" OFF) # We enable this option in the appropriate CMakePresets. # If you just want to have a working build of knut, -Werror can be very annoying, so keep it off by default. option(KNUT_ERROR_ON_WARN "Issue a compiler error if the compiler encounters a warning" OFF) +option(KNUT_REV_IN_VERSION "Include the commit revision hash in the Knut version" ON) if(USE_ASAN) # /MD will be used implicitly @@ -52,28 +53,26 @@ endif() add_definitions(-DKNUT_VERSION="${PROJECT_VERSION}") set(KNUT_VERSION_STRING "${PROJECT_VERSION}") -# if(EXISTS "${CMAKE_SOURCE_DIR}/.git") -# find_package(Git) -# if(GIT_FOUND) -# execute_process( -# COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD -# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} -# OUTPUT_VARIABLE GIT_REVISION) -# string(REGEX REPLACE "\n" "" GIT_REVISION "${GIT_REVISION}") -# message(STATUS "Git revision ${GIT_REVISION} with exe ${GIT_EXECUTABLE}") -# set(KNUT_VERSION_STRING -# "${KNUT_VERSION_STRING} (revision: ${GIT_REVISION})") -# else() -# message(STATUS "GIT not found!?") -# endif() -# endif() -add_definitions(-DKNUT_VERSION_STRING="${KNUT_VERSION_STRING}") +if(EXISTS "${CMAKE_SOURCE_DIR}/.git") + find_package(Git) + if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_REVISION) + string(REGEX REPLACE "\n" "" GIT_REVISION "${GIT_REVISION}") + message(STATUS "Git revision ${GIT_REVISION} with exe ${GIT_EXECUTABLE}") + set(KNUT_VERSION_STRING + "${KNUT_VERSION_STRING} (revision: ${GIT_REVISION})") + else() + message(STATUS "GIT not found!?") + endif() +endif() # Generate build date for the about dialog Note: This is the simplest way to get # a build date out of CMake Drawbacks: The timestamp is only updated when CMake # is rerun. But this should sufficient for our use-case. string(TIMESTAMP KNUT_BUILDDATE "%Y-%m-%d") -add_definitions(-DKNUT_BUILDDATE="${KNUT_BUILDDATE}") # Qt # ############################################################################## diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index eee09984..5fb293a0 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -10,6 +10,8 @@ project(knut-core LANGUAGES CXX) +configure_file(version.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/version.cpp) + set(PROJECT_SOURCES astnode.h astnode.cpp @@ -104,6 +106,8 @@ set(PROJECT_SOURCES userdialog.cpp utils.h utils.cpp + version.h + ${CMAKE_CURRENT_BINARY_DIR}/version.cpp core.qrc) add_library(${PROJECT_NAME} STATIC ${PROJECT_SOURCES}) diff --git a/src/core/version.cpp.in b/src/core/version.cpp.in new file mode 100644 index 00000000..15c04a8a --- /dev/null +++ b/src/core/version.cpp.in @@ -0,0 +1,12 @@ +#include "core/version.h" + +namespace core { + QString knut_version() + { + return "${KNUT_VERSION_STRING}"; + } + + QString knut_build_date() { + return "${KNUT_BUILDDATE}"; + } +} diff --git a/src/core/version.h b/src/core/version.h new file mode 100644 index 00000000..ff16cf50 --- /dev/null +++ b/src/core/version.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace core { +QString knut_version(); +QString knut_build_date(); +} diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index e242aaab..3e1bc107 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -20,6 +20,7 @@ #include "core/slintdocument.h" #include "core/textdocument.h" #include "core/uidocument.h" +#include "core/version.h" #include "documentpalette.h" #include "guisettings.h" #include "historypanel.h" @@ -572,7 +573,7 @@ Build date: %2

Knut name has nothing to do with Knut Irvin, nor with Knut the polar bear.
The name Knut is coming from St Knut, which marks the end of the Christmas and holiday season in Sweden.
See Wikipedia article: Saint Knut's Day.)") - .arg(KNUT_VERSION_STRING, KNUT_BUILDDATE); + .arg(core::knut_version(), core::knut_build_date()); QMessageBox dialog(QMessageBox::Information, tr("About Knut"), text, QMessageBox::Ok, this); dialog.setIconPixmap(QPixmap(":/gui/icons/knut-64.png")); dialog.exec(); diff --git a/src/main.cpp b/src/main.cpp index 75624e47..bb2116ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ Contact KDAB at for commercial licensing options. */ +#include "core/version.h" #include "gui/knutmain.h" #include @@ -19,7 +20,7 @@ int main(int argc, char *argv[]) QApplication::setOrganizationName("KDAB"); QApplication::setApplicationName("knut"); - QApplication::setApplicationVersion(KNUT_VERSION_STRING); + QApplication::setApplicationVersion(core::knut_version()); QApplication::setWindowIcon(QIcon(":/gui/icons/knut-64.png")); Q_INIT_RESOURCE(core);