From f2030c379b92b8a8537cd9236f318fc1d3b4d24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=9B=D1=83=D1=85?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2?= Date: Mon, 2 Sep 2024 15:32:42 +0300 Subject: [PATCH] Automate version generation Version is now generated automatically based on git tags. Both cases are handled: building from git tree and building tarball downloaded from github. Works for qmake and cmake --- .gitattributes | 1 + .gitignore | 5 ++-- CMakeLists.txt | 12 ++++------ cmake/modules/GenerateVersionHeader.cmake | 28 +++++++++++++++++++++++ common.pri | 7 ------ config.h.in | 7 ------ console/main.cpp | 1 + limereport/limereport.pri | 16 +++++++++++++ limereport/lraboutdialog.cpp | 4 +--- limereport/version.h.in | 9 ++++++++ 10 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 .gitattributes create mode 100644 cmake/modules/GenerateVersionHeader.cmake delete mode 100644 config.h.in create mode 100644 limereport/version.h.in diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..a848c3e6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +limereport/version.h.in export-subst diff --git a/.gitignore b/.gitignore index 5150452b..3b4c2bcd 100644 --- a/.gitignore +++ b/.gitignore @@ -128,7 +128,8 @@ modules.xml .LSOverride # Icon must end with two \r -Icon +Icon + # Thumbnails ._* @@ -288,6 +289,6 @@ $RECYCLE.BIN/ # End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,qtcreator,qt,macos,intellij+iml,cmake,linux,windows # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) - +limereport/version.h /build/ *.app diff --git a/CMakeLists.txt b/CMakeLists.txt index aff69f80..8e8e4805 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,10 @@ cmake_minimum_required(VERSION 3.14) project(limereport) +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" "${CMAKE_MODULE_PATH}") +include(GenerateVersionHeader) + set(DEFAULT_ITEM_PADDING 0) -set(LIMEREPORT_VERSION_MAJOR 1) -set(LIMEREPORT_VERSION_MINOR 7) -set(LIMEREPORT_VERSION_RELEASE 14) option(ENABLE_ZINT "Enable libzint build for barcode support" OFF) option(LIMEREPORT_STATIC "Build LimeReport as static library" OFF) @@ -349,14 +349,12 @@ set(EXTRA_FILES ${PROJECT_NAME}/lrpreparedpagesintf.h ) -configure_file(config.h.in config.h @ONLY) - set(GLOBAL_HEADERS ${PROJECT_NAME}/LimeReport ${PROJECT_NAME}/LRCallbackDS ${PROJECT_NAME}/LRDataManager ${PROJECT_NAME}/LRScriptManager - ${CMAKE_CURRENT_BINARY_DIR}/config.h + ${CMAKE_CURRENT_BINARY_DIR}/limereport/version.h ) set(PROJECT_NAME ${PROJECT_NAME}-qt${QT_VERSION_MAJOR}) @@ -371,8 +369,6 @@ else() target_compile_definitions( ${PROJECT_NAME} INTERFACE -DLIMEREPORT_IMPORTS) endif() -target_compile_definitions(${PROJECT_NAME} PUBLIC -DCMAKE_CONFIG) - if(Qt${QT_VERSION_MAJOR}UiTools_FOUND) target_compile_definitions( ${PROJECT_NAME} PRIVATE -DHAVE_UI_LOADER) target_link_libraries( ${PROJECT_NAME} PUBLIC diff --git a/cmake/modules/GenerateVersionHeader.cmake b/cmake/modules/GenerateVersionHeader.cmake new file mode 100644 index 00000000..3084a1e4 --- /dev/null +++ b/cmake/modules/GenerateVersionHeader.cmake @@ -0,0 +1,28 @@ +find_package(Git) + +if(GIT_EXECUTABLE) + # Generate a git-describe version string from Git repository tags + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --dirty + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_DESCRIBE_VERSION + RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT GIT_DESCRIBE_ERROR_CODE) + set(GIT_VERSION ${GIT_DESCRIBE_VERSION}) + endif() +endif() + +# Final fallback: Just use a bogus version string that is semantically older +# than anything else and spit out a warning to the developer. +if(NOT DEFINED GIT_VERSION) + set(GIT_VERSION 0.0.0-unknown) + message(WARNING "Failed to determine version from Git tags. Using default version \"${FOOBAR_VERSION}\".") +endif() + +configure_file( + ${CMAKE_SOURCE_DIR}/limereport/version.h.in + ${CMAKE_BINARY_DIR}/limereport/version.h + @ONLY) + diff --git a/common.pri b/common.pri index e87efa0c..53960ecf 100644 --- a/common.pri +++ b/common.pri @@ -139,13 +139,6 @@ UI_SOURCES_DIR = $${ARCH_DIR}/$${BUILD_TYPE}/ui OBJECTS_DIR = $${ARCH_DIR}/$${BUILD_TYPE}/obj RCC_DIR = $${ARCH_DIR}/$${BUILD_TYPE}/rcc -LIMEREPORT_VERSION_MAJOR = 1 -LIMEREPORT_VERSION_MINOR = 7 -LIMEREPORT_VERSION_RELEASE = 14 - -LIMEREPORT_VERSION = '$${LIMEREPORT_VERSION_MAJOR}.$${LIMEREPORT_VERSION_MINOR}.$${LIMEREPORT_VERSION_RELEASE}' -DEFINES *= LIMEREPORT_VERSION_STR=\\\"$${LIMEREPORT_VERSION}\\\" - QT *= xml sql REPORT_PATH = $$PWD/limereport diff --git a/config.h.in b/config.h.in deleted file mode 100644 index dcb5e741..00000000 --- a/config.h.in +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#cmakedefine LIMEREPORT_VERSION_MAJOR "@LIMEREPORT_VERSION_MAJOR@" -#cmakedefine LIMEREPORT_VERSION_MINOR "@LIMEREPORT_VERSION_MINOR@" -#cmakedefine LIMEREPORT_VERSION_RELEASE "@LIMEREPORT_VERSION_RELEASE@" - -#define LIMEREPORT_VERSION_STR LIMEREPORT_VERSION_MAJOR"." LIMEREPORT_VERSION_MINOR"." LIMEREPORT_VERSION_RELEASE diff --git a/console/main.cpp b/console/main.cpp index 7493e676..2d6e9a17 100644 --- a/console/main.cpp +++ b/console/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "../limereport/version.h" #ifdef _WIN32 #include diff --git a/limereport/limereport.pri b/limereport/limereport.pri index c86b5756..d13e5f30 100644 --- a/limereport/limereport.pri +++ b/limereport/limereport.pri @@ -213,3 +213,19 @@ FORMS += \ RESOURCES += \ $$REPORT_PATH/report.qrc \ $$REPORT_PATH/items/items.qrc + +system("git -v") { + LR_VERSION = $$system("git --git-dir=../.git describe --tags --dirty") +} else { + LR_VERSION = "0.0.0-unknown" +} + +VERSION_TEMPLATE = $$PWD/version.h.in + +generateversion.depends = FORCE +generateversion.input = VERSION_TEMPLATE +generateversion.output = $$OUT_PWD/version.h +generateversion.commands = $$QMAKE_STREAM_EDITOR \'s/@GIT_VERSION@/$$LR_VERSION/\' ${QMAKE_FILE_IN} > ${QMAKE_FILE_OUT} +generateversion.CONFIG = no_link target_predeps + +QMAKE_EXTRA_COMPILERS += generateversion diff --git a/limereport/lraboutdialog.cpp b/limereport/lraboutdialog.cpp index 57cddd65..3663e31d 100644 --- a/limereport/lraboutdialog.cpp +++ b/limereport/lraboutdialog.cpp @@ -27,9 +27,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * ****************************************************************************/ -#ifdef CMAKE_CONFIG -#include -#endif +#include "version.h" #include "lraboutdialog.h" #include "ui_lraboutdialog.h" diff --git a/limereport/version.h.in b/limereport/version.h.in new file mode 100644 index 00000000..85100a12 --- /dev/null +++ b/limereport/version.h.in @@ -0,0 +1,9 @@ +#pragma once + +// git will put "#define GIT_ARCHIVE 1" on the next line inside archives. $Format:%n#define GIT_ARCHIVE 1$ + +#ifdef GIT_ARCHIVE +#define LIMEREPORT_VERSION_STR "$Format:%(describe)$" +#else +#define LIMEREPORT_VERSION_STR "@GIT_VERSION@" +#endif