Define application name and version in CMakeLists

Mon, 27 Jun 2022 01:49:20 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 27 Jun 2022 01:49:20 +0300
changeset 268
fb319526ba6c
parent 267
9a482f506747
child 269
593545977c5e

Define application name and version in CMakeLists

CMakeLists.txt file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/version.cpp file | annotate | diff | comparison | revisions
src/version.h file | annotate | diff | comparison | revisions
widgets/CMakeLists.txt file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Mon Jun 27 01:29:03 2022 +0300
+++ b/CMakeLists.txt	Mon Jun 27 01:49:20 2022 +0300
@@ -1,4 +1,14 @@
-project(ldforge)
+project(LDForge)
+set(VERSION_MAJOR 1)
+set(VERSION_MINOR 0)
+set(VERSION_PATCH 0)
+
+set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}")
+if (NOT ${VERSION_PATCH} EQUAL 0)
+	set(VERSION_STRING "${VERSION_STRING}.${VERSION_PATCH}")
+endif()
+
+string(TOLOWER ${PROJECT_NAME} TARGET_NAME)
 cmake_minimum_required(VERSION 2.8.12)
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
 set(OpenGL_GL_PREFERENCE GLVND)
@@ -123,6 +133,12 @@
 include_directories("${PROJECT_BINARY_DIR}/src")
 include_directories("${PROJECT_SOURCE_DIR}")
 
+add_definitions(-DVERSION_MAJOR=${VERSION_MAJOR})
+add_definitions(-DVERSION_MINOR=${VERSION_MINOR})
+add_definitions(-DVERSION_PATCH=${VERSION_PATCH})
+add_definitions(-DAPPNAME="${PROJECT_NAME}")
+add_definitions(-DVERSION_STRING="${VERSION_STRING}")
+
 if (NOT MSVC)
 	if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
 		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG")
@@ -145,7 +161,7 @@
 qt_add_resources(QRC_SOURCE ${QRC_FILE})
 qt_wrap_ui(FORMS_HEADERS ${FORM_FILES})
 
-add_executable(${PROJECT_NAME} WIN32
+add_executable(${TARGET_NAME} WIN32
 	${SOURCE_FILES}
 	${HEADER_FILES}
 	${QRC_FILE}
@@ -157,30 +173,30 @@
 set_source_files_properties(${HEADER_FILES} PROPERTIES HEADER_FILE_ONLY TRUE)
 set_source_files_properties(${QRC_FILE} PROPERTIES HEADER_FILE_ONLY TRUE)
 set_source_files_properties(${OTHER_FILES} PROPERTIES HEADER_FILE_ONLY TRUE)
-set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC 1)
-target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Core)
-target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Widgets)
-target_link_libraries(${PROJECT_NAME} PRIVATE Qt::OpenGL)
-target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENGL_LIBRARIES})
-target_link_libraries(${PROJECT_NAME} PRIVATE ${GLEW_LIBRARIES})
-target_link_libraries(${PROJECT_NAME} PRIVATE ${WIDGETLIB})
+set_target_properties(${TARGET_NAME} PROPERTIES AUTOMOC 1)
+target_link_libraries(${TARGET_NAME} PRIVATE Qt::Core)
+target_link_libraries(${TARGET_NAME} PRIVATE Qt::Widgets)
+target_link_libraries(${TARGET_NAME} PRIVATE Qt::OpenGL)
+target_link_libraries(${TARGET_NAME} PRIVATE ${OPENGL_LIBRARIES})
+target_link_libraries(${TARGET_NAME} PRIVATE ${GLEW_LIBRARIES})
+target_link_libraries(${TARGET_NAME} PRIVATE ${WIDGETLIB})
 if (Qt6_FOUND)
-	target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core5Compat)
-	target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::OpenGLWidgets)
+	target_link_libraries(${TARGET_NAME} PRIVATE Qt6::Core5Compat)
+	target_link_libraries(${TARGET_NAME} PRIVATE Qt6::OpenGLWidgets)
 endif()
 
 add_custom_target(linelength ALL
 	COMMAND python3
 		"${CMAKE_SOURCE_DIR}/tools/linelength.py"
 		${SOURCE_FILES}
-		${HEADER_FILES}
+		${HEADER_FILESl}
 	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
-add_dependencies(${PROJECT_NAME} linelength)
+add_dependencies(${TARGET_NAME} linelength)
 
 # Collect the current hg revision into hginfo.h
 add_custom_target(revision_check ALL
 	COMMAND python3 "${CMAKE_SOURCE_DIR}/tools/updaterevision.py" --cwd "$(CMAKE_SOURCE_DIR)" hginfo.h
 	WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
-add_dependencies(${PROJECT_NAME} revision_check)
+add_dependencies(${TARGET_NAME} revision_check)
 
-install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
+install(TARGETS ${TARGET_NAME} RUNTIME DESTINATION bin)
--- a/src/main.cpp	Mon Jun 27 01:29:03 2022 +0300
+++ b/src/main.cpp	Mon Jun 27 01:49:20 2022 +0300
@@ -709,7 +709,6 @@
 				}
 			}
 		});
-	mainWindow.setWindowTitle(title());
 	mainWindow.tabifyDockWidget(ui.messageLogDock, ui.toolOptionsDock);
 	mainWindow.restoreGeometry(setting<Setting::MainWindowGeometry>());
 	mainWindow.restoreState(setting<Setting::MainWindowState>());
@@ -720,6 +719,7 @@
 	}
 	restoreSettings();
 	updateRenderPreferences(&ui, &renderPreferences, &documents);
+	mainWindow.setWindowTitle(title());
 	mainWindow.show();
 	const int result = app.exec();
 	saveSettings();
--- a/src/version.cpp	Mon Jun 27 01:29:03 2022 +0300
+++ b/src/version.cpp	Mon Jun 27 01:49:20 2022 +0300
@@ -21,27 +21,14 @@
 #include <hginfo.h>
 #include "src/version.h"
 
-static QString makeVersionString(const Version &version)
+const char* fullVersionString()
 {
-	QString result = QString::number(version.major) + "." + QString::number(version.minor);
-	if (version.patch != 0) {
-		result += ".";
-		result += QString::number(version.patch);
+	if (::BUILD_TYPE != ReleaseBuild) {
+		return VERSION_STRING "-" HG_DATE_VERSION;
 	}
-	return result;
-}
-
-const QString& fullVersionString()
-{
-#ifdef HG_DATE_VERSION
-	if (::BUILD_TYPE != ReleaseBuild) {
-		static const QString result = makeVersionString(APPVERSION) + "-" HG_DATE_VERSION;
-		return result;
+	else {
+		return VERSION_STRING;
 	}
-#else
-	static const QString result = makeVersionString(::version);
-	return result;
-#endif
 }
 
 static QString makeCommitTimeString()
--- a/src/version.h	Mon Jun 27 01:29:03 2022 +0300
+++ b/src/version.h	Mon Jun 27 01:49:20 2022 +0300
@@ -19,18 +19,15 @@
 #pragma once
 #include <QString>
 
-const char appName[] = "LDForge";
-const char unixName[] = "ldforge";
+constexpr char appName[] = APPNAME;
 
-struct Version {
-	int major;
-	int minor;
-	int patch = 0;
-};
+constexpr struct {
+	int major = VERSION_MAJOR;
+	int minor = VERSION_MINOR;
+	int patch = VERSION_PATCH;
+} APPVERSION;
 
 enum BuildType {InternalBuild, ReleaseBuild};
-
-constexpr Version APPVERSION = {1, 0};
 constexpr BuildType BUILD_TYPE = InternalBuild;
 
 #ifdef DEBUG
@@ -41,5 +38,5 @@
 # undef DEBUG
 #endif // RELEASE
 
-const QString& fullVersionString();
+const char *fullVersionString();
 const QString& commitTimeString();
--- a/widgets/CMakeLists.txt	Mon Jun 27 01:29:03 2022 +0300
+++ b/widgets/CMakeLists.txt	Mon Jun 27 01:49:20 2022 +0300
@@ -1,5 +1,5 @@
-set(WIDGETLIB "${PROJECT_NAME}widgets" PARENT_SCOPE)
-set(WIDGETLIB "${PROJECT_NAME}widgets")
+set(WIDGETLIB "${TARGET_NAME}widgets" PARENT_SCOPE)
+set(WIDGETLIB "${TARGET_NAME}widgets")
 qt_wrap_ui(WIDGETS_FORMS
 	vec3editor.ui
 	multiplyfactordialog.ui

mercurial