From a4ead0c25f7451d24e0ac4b13f2c6e17d7d30ec7 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Thu, 10 Mar 2022 19:36:05 +0100 Subject: [PATCH] Freshen version number when building with CMake Fixes #979 --- CMakeLists.txt | 16 ++++++++++------ src/.gitignore | 2 ++ src/CMakeLists.txt | 4 +++- src/version.c | 7 +++++++ 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/.gitignore diff --git a/CMakeLists.txt b/CMakeLists.txt index 336a0e68..06010dff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,15 +66,19 @@ endif() # Use versioning consistent with Makefile # the git revision is used but uses the fallback in an archive -execute_process(COMMAND git describe --tags --dirty --always - OUTPUT_VARIABLE GIT_REV - ERROR_QUIET) -string(STRIP "${GIT_REV}" GIT_REV) +find_program(GIT git) +if(GIT) + execute_process(COMMAND ${GIT} describe --tags --dirty --always + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE GIT_REV OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) + message(STATUS "RGBDS version: ${GIT_REV}") +else(GIT) + message(STATUS "Cannot determine RGBDS version (Git not installed), falling back") +endif(GIT) include_directories("${PROJECT_SOURCE_DIR}/include") -add_definitions(-DBUILD_VERSION_STRING="${GIT_REV}") - set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED True) diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 00000000..eca8b32e --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,2 @@ +# Generated by CMake +/.version.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e669e48a..794cd032 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,10 +6,12 @@ # SPDX-License-Identifier: MIT # +configure_file(version.c _version.c ESCAPE_QUOTES) + set(common_src "error.c" "extern/getopt.c" - "version.c" + "_version.c" ) find_package(PkgConfig) diff --git a/src/version.c b/src/version.c index 5a856f37..d9d4401d 100644 --- a/src/version.c +++ b/src/version.c @@ -12,6 +12,13 @@ #include "helpers.h" #include "version.h" +// This variable is passed via `-D` from the Makefile, but not from CMake +// (in which `configure_file()` is used on this file to replace some syntax) +#ifndef BUILD_VERSION_STRING +// CMake-specific syntax here +#define BUILD_VERSION_STRING "@GIT_REV@" +#endif + char const *get_package_version_string(void) { // The following conditional should be simplified by the compiler.