Skip to content

Commit

Permalink
ci: Add a cpp file for version information
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LeonMatthesKDAB committed Jun 10, 2024
1 parent 66c0acc commit fd2a691
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
33 changes: 16 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
# ##############################################################################
Expand Down
4 changes: 4 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down
12 changes: 12 additions & 0 deletions src/core/version.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "core/version.h"

namespace core {
QString knut_version()
{
return "${KNUT_VERSION_STRING}";
}

QString knut_build_date() {
return "${KNUT_BUILDDATE}";
}
}
8 changes: 8 additions & 0 deletions src/core/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <QString>

namespace core {
QString knut_version();
QString knut_build_date();
}
3 changes: 2 additions & 1 deletion src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -572,7 +573,7 @@ Build date: %2<br/><br/>
Knut name has nothing to do with Knut Irvin, nor with Knut the polar bear.<br/>
The name Knut is coming from St Knut, which marks the end of the Christmas and holiday season in Sweden.<br/>
See Wikipedia article: <a href="https://en.wikipedia.org/wiki/Saint_Knut%27s_Day">Saint Knut's Day</a>.)")
.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();
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Contact KDAB at <[email protected]> for commercial licensing options.
*/

#include "core/version.h"
#include "gui/knutmain.h"

#include <QApplication>
Expand All @@ -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);
Expand Down

0 comments on commit fd2a691

Please sign in to comment.