Make versioning file generation header-based
Through CI it was found that the C-style extern version definition,
linkage failed on Windows. The following change should be more robust and
applicable to all platforms and build systems.
MODIFY
src/CMakeLists.txt
src/CMakeLists.txt
@@ -2,12 +2,12 @@ cmake_minimum_required(VERSION 3.9)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
-file(GLOB LIB_SOURCES *.cpp graphics/*.cpp)
-file(GLOB LIB_HEADERS *.h graphics/*.h version/*.h)
+file(GLOB LIB_SOURCES *.cpp graphics/*.cpp version/version.cpp)
+file(GLOB LIB_HEADERS *.h graphics/*.h)
file(GLOB LIB_UIS *.ui)
set(RIPES_LIB ripes_lib)
-add_library(${RIPES_LIB} ${LIB_SOURCES} ${LIB_HEADERS} ${LIB_UIS} ${CMAKE_CURRENT_BINARY_DIR}/version/version.cpp)
+add_library(${RIPES_LIB} ${LIB_SOURCES} ${LIB_HEADERS} ${LIB_UIS})
target_include_directories (${RIPES_LIB} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(${RIPES_LIB} PRIVATE cxx_std_17)
@@ -19,30 +19,26 @@ add_subdirectory(processors)
# -----------------------------------------------------------------------------
# Version control file
-# A file version.cpp is generated, containing information regarding the current
-# .git revision. This information is embedded into the binary for future
-# reference, ie. for issue reporting.
-#
-# Modified version of https://www.mattkeeter.com/blog/2018-01-06-versioning/
+# A file gen_versionnumber.h is generated by version.cmake, containing information
+# regarding the current .git revision.
+# This information is embedded into the binary for future reference, ie. for issue
+# reporting.
add_custom_target(
version_file ALL
# Depend on the dummy file (which is not generated by the versioning script)
# to ensure that the custom command is executed each time the project is built.
- DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/version/_version.cpp
+ DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/version/dummy.cpp
)
add_dependencies(${RIPES_LIB} version_file)
add_custom_command(
OUTPUT
- # Actual file generated by the versioning script
- ${CMAKE_CURRENT_BINARY_DIR}/version/version.cpp
# Dummy file not generated by the sript, but may be used as a dependency
- ${CMAKE_CURRENT_BINARY_DIR}/version/_version.cpp
+ ${CMAKE_CURRENT_BINARY_DIR}/version/dummy.cpp
COMMAND
${CMAKE_COMMAND}
-DRIPES_SRC_DIR=${CMAKE_SOURCE_DIR}
-P ${CMAKE_CURRENT_SOURCE_DIR}/version/version.cmake)
# Do not run AUTOMOC and AUTOUIC on the generated file (CMake warning)
-set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/version/version.cpp PROPERTY SKIP_AUTOGEN ON)
# -----------------------------------------------------------------------------

ADD
src/version/.gitignore
+# Ignore generated version number file
+gen_*

MODIFY
src/version/version.cmake
src/version/version.cmake
-# Modified version of:
-# https://www.mattkeeter.com/blog/2018-01-06-versioning/
-
set(ENV{GIT_DIR} ${RIPES_SRC_DIR}/.git)
execute_process(COMMAND git describe --tags
OUTPUT_VARIABLE GIT_REV
@@ -15,14 +12,18 @@ else()
string(STRIP "${GIT_REV}" RIPES_GIT_VERSION)
endif()
-set(VERSION "const char* RIPES_GIT_VERSION=\"${RIPES_GIT_VERSION}\";")
+set(VERSION_FILE ${RIPES_SRC_DIR}/src/version/gen_versionnumber.h)
+
+# New version file content
+set(VERSION "#define RIPES_GIT_VERSION \"${RIPES_GIT_VERSION}\"")
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp)
- file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp VERSION_)
+# Only rewrite the version file if a change is seen
+if(EXISTS ${VERSION_FILE})
+ file(READ ${VERSION_FILE} OLD_VERSION)
else()
- set(VERSION_ "")
+ set(OLD_VERSION "")
endif()
-if (NOT "${VERSION}" STREQUAL "${VERSION_}")
- file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/version/version.cpp "${VERSION}")
+if (NOT "${VERSION}" STREQUAL "${OLD_VERSION}")
+ file(WRITE ${VERSION_FILE} "${VERSION}")
endif()
\ No newline at end of file

ADD
src/version/version.cpp
+#include "version.h"
+
+// The following header file is autogenerated by version.cmake, and will export a definition
+// of the current git version for the build
+#include "gen_versionnumber.h"
+
+namespace Ripes {
+
+QString getRipesVersion() {
+ return QString(RIPES_GIT_VERSION);
+}
+
+} // namespace Ripes

MODIFY
src/version/version.h
src/version/version.h
#pragma once
#include <QString>
-#include <QStringList>
-// Variables autogenerated from the current git-revision upon building the project
-extern "C" {
-extern const char* RIPES_GIT_VERSION;
-}
-
-static inline QString getRipesVersion() {
- return QString(RIPES_GIT_VERSION);
+namespace Ripes {
+QString getRipesVersion();
}

Make versioning file generation header-based

Through CI it was found that the C-style extern version definition,
linkage failed on Windows. The following change should be more robust and
applicable to all platforms and build systems.