Skip to content

Commit

Permalink
refactor: use configure_file for build time string data
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying authored Feb 8, 2025
1 parent 3953e35 commit 05a19b1
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ hotkeys.pro.user.*
object_script.goldendict.Debug
object_script.goldendict.Release

version.txt
src/build_config.hh

*.dmg

Expand Down Expand Up @@ -55,4 +55,4 @@ GoldenDict_resource.rc
*.TMP
*.orig

node_modules
node_modules
11 changes: 4 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ qt_standard_project_setup()
set(CMAKE_AUTORCC ON) # not included in the qt_standard_project_setup

#### Things required during configuration
block() # generate version.txt
block(PROPAGATE GD_VERSION_FULL)
string(TIMESTAMP build_time UTC)
find_package(Git)
if (EXISTS "${CMAKE_SOURCE_DIR}/.git" AND GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} -C "${CMAKE_SOURCE_DIR}" rev-parse --short HEAD
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE GIT_HASH)
file(WRITE "${CMAKE_SOURCE_DIR}/version.txt" "${PROJECT_VERSION}.${GIT_HASH} at ${build_time}")
set(GD_VERSION_FULL "${PROJECT_VERSION}.${GIT_HASH} at ${build_time}")
else () # not built in a git repo
file(WRITE "${CMAKE_SOURCE_DIR}/version.txt" "${PROJECT_VERSION} at ${build_time}")
set(GD_VERSION_FULL "${PROJECT_VERSION} at ${build_time}")
endif ()
endblock()

Expand Down Expand Up @@ -138,10 +138,6 @@ if (NOT USE_SYSTEM_FMT)
endif ()

### Common parts amount all platforms

# Note: used as c++ string thus need surrounding " "
add_compile_definitions(PROGRAM_VERSION="${PROJECT_VERSION}")

target_link_libraries(${GOLDENDICT} PRIVATE
Qt6::Xml
Qt6::Concurrent
Expand Down Expand Up @@ -226,4 +222,5 @@ elseif (WIN32)
include(cmake/Package_Windows.cmake)
endif ()

configure_file("${CMAKE_SOURCE_DIR}/src/build_config.hh.in" "${CMAKE_SOURCE_DIR}/src/build_config.hh")
feature_summary(WHAT ALL DESCRIPTION "Build configuration:")
4 changes: 2 additions & 2 deletions cmake/Package_Linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ install(FILES ${CMAKE_SOURCE_DIR}/redist/io.github.xiaoyifang.goldendict_ng.desk
install(FILES ${CMAKE_SOURCE_DIR}/redist/io.github.xiaoyifang.goldendict_ng.metainfo.xml DESTINATION share/metainfo)

if (NOT USE_ALTERNATIVE_NAME)
add_compile_definitions(PROGRAM_DATA_DIR="${CMAKE_INSTALL_PREFIX}/share/goldendict")
set(GD_PROGRAM_DATA_DIR "\"${CMAKE_INSTALL_PREFIX}/share/goldendict\"")
install(FILES ${CMAKE_SOURCE_DIR}/redist/icons/goldendict.png DESTINATION share/pixmaps)
install(FILES ${qm_files} DESTINATION share/goldendict/locale)
else ()
add_compile_definitions(PROGRAM_DATA_DIR="${CMAKE_INSTALL_PREFIX}/share/goldendict-ng")
set(GD_PROGRAM_DATA_DIR "\"${CMAKE_INSTALL_PREFIX}/share/goldendict-ng\"")
install(FILES ${CMAKE_SOURCE_DIR}/redist/icons/goldendict.png DESTINATION share/pixmaps
RENAME goldendict-ng.png)
install(FILES ${qm_files} DESTINATION share/goldendict-ng/locale)
Expand Down
1 change: 0 additions & 1 deletion resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<file>icons/add-anki-icon.svg</file>
<file>icons/document.png</file>
<file>icons/audio.png</file>
<file>version.txt</file>
<file>icons/arrow.png</file>
<file>icons/prefix.png</file>
<file>icons/playsound.png</file>
Expand Down
7 changes: 7 additions & 0 deletions src/build_config.hh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <QtGlobal>
constexpr auto GD_VERSION_NUMBER = "@PROJECT_VERSION@";
constexpr auto GD_VERSION_STRING = "@GD_VERSION_FULL@";
#if !defined( Q_OS_WIN ) && !defined( Q_OS_MACOS )
constexpr auto GD_PROGRAM_DATA_DIR = @GD_PROGRAM_DATA_DIR@;
#endif
3 changes: 2 additions & 1 deletion src/config.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */

#include "build_config.hh"
#include "config.hh"
#include "folding.hh"
#include <QSaveFile>
Expand Down Expand Up @@ -2319,7 +2320,7 @@ QString getProgramDataDir() noexcept
#else
// Hardcode a `$PREFIX/share/goldendict` instead of QStandardPaths::AppDataLocation
// to avoid unnecessary downstream packaging changes
return PROGRAM_DATA_DIR;
return GD_PROGRAM_DATA_DIR;
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mruqmenu.hh"
#include "gestures.hh"
#include "dictheadwords.hh"
#include "build_config.hh"
#include <QTextStream>
#include <QDir>
#include <QUrl>
Expand Down Expand Up @@ -3034,7 +3035,7 @@ void MainWindow::checkNewRelease()
QString latestVersion = tag_name.toString().mid( 1, 8 );
QString downloadUrl = html_url.toString();

if ( latestVersion > PROGRAM_VERSION && latestVersion != cfg.skippedRelease ) {
if ( latestVersion > GD_VERSION_NUMBER && latestVersion != cfg.skippedRelease ) {
QMessageBox msg( QMessageBox::Information,
tr( "New Release Available" ),
tr( "Version <b>%1</b> of GoldenDict is now available for download.<br>"
Expand Down
14 changes: 2 additions & 12 deletions src/version.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
#include "build_config.hh"
#include "version.hh"
#include <QFile>

namespace Version {
QString version()
{
QFile versionFile( ":/version.txt" );

if ( !versionFile.open( QFile::ReadOnly ) ) {
return QStringLiteral( "[Unknown Version]" );
}
else {
return versionFile.readAll().trimmed();
}
}

QString everything()
{
return QStringLiteral( "Version: " ) + Version::version() + "\n" + "Qt " + QLatin1String( qVersion() ) + " "
return QStringLiteral( "Version: " ) + GD_VERSION_STRING + "\n" + "Qt " + QLatin1String( qVersion() ) + " "
+ Version::compiler + "\n" + QSysInfo::productType() + " " + QSysInfo::kernelType() + " "
+ QSysInfo::kernelVersion() + " " + QSysInfo::buildAbi() + "\nFlags: " + flags;
}
Expand Down
3 changes: 0 additions & 3 deletions src/version.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ const QLatin1String compiler = QLatin1String(
"Unknown complier"
#endif
);
/// Version string from the version.txt
QString version();

/// Full report of version & various information
QString everything();

} // namespace Version

0 comments on commit 05a19b1

Please sign in to comment.