From 4b408944a4509db93c32395f7119562647e4e7f4 Mon Sep 17 00:00:00 2001 From: Martin Valgur Date: Sat, 4 May 2024 16:03:59 +0300 Subject: [PATCH] Don't require git for version.h, autoformat, use project version from CMakeLists.txt --- CMakeLists.txt | 28 ++++++------------- cmake/GenerateVersionHeader.cmake | 4 +-- src/version.h.in | 45 +++++++++---------------------- 3 files changed, 22 insertions(+), 55 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fad60c92..a898859ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.14) -project(novatel-edie) +project(novatel-edie VERSION 3.3.7) enable_testing() @@ -69,25 +69,13 @@ set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}") FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip) FetchContent_MakeAvailable(googletest) -# Find Git package, if not need to install manually or through .yml file -find_package(Git) -if(NOT Git_FOUND) - message(FATAL_ERROR "Git was not found. Install Git and make sure it is in your PATH.") -endif() - -if(NOT DEFINED GIT_BRANCH) - set(GIT_BRANCH "main") -endif() - -# Build version of EDIE through cmake -if(GIT_EXECUTABLE) - execute_process(COMMAND ${CMAKE_COMMAND} - -D SRC=${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in - -D DST=${CMAKE_CURRENT_SOURCE_DIR}/src/version.h - -D GIT_EXECUTABLE=${GIT_EXECUTABLE} - -D GIT_BRANCH=${GIT_BRANCH} - -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GenerateVersionHeader.cmake) -endif() +# Create src/version.h based on CMake project and git versions +execute_process(COMMAND ${CMAKE_COMMAND} + -D SRC=${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in + -D DST=${CMAKE_CURRENT_SOURCE_DIR}/src/version.h + -D PROJECT_VERSION=${PROJECT_VERSION} + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GenerateVersionHeader.cmake +) include_directories( ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/cmake/GenerateVersionHeader.cmake b/cmake/GenerateVersionHeader.cmake index b1b98f676..c38488fcb 100644 --- a/cmake/GenerateVersionHeader.cmake +++ b/cmake/GenerateVersionHeader.cmake @@ -1,7 +1,7 @@ get_filename_component(SRC_DIR ${SRC} DIRECTORY) # Generate a git-describe version string from Git repository tags - +find_package(Git) if(GIT_EXECUTABLE AND NOT DEFINED VERSION) execute_process( @@ -97,7 +97,7 @@ if(NOT DEFINED GIT_BRANCH) endif() if(NOT DEFINED GIT_IS_DIRTY) - set(GIT_IS_DIRTY FALSE) + set(GIT_IS_DIRTY "false") message(WARNING "Failed to determine GIT_IS_DIRTY from repository tags. Using default version \"${GIT_IS_DIRTY}\".") endif() diff --git a/src/version.h.in b/src/version.h.in index a33020922..aaa497201 100644 --- a/src/version.h.in +++ b/src/version.h.in @@ -1,45 +1,24 @@ -#include -#include - -#define RELEASE_VERSION "3.3.7" +#define RELEASE_VERSION "@PROJECT_VERSION@" #define GIT_SHA "@GIT_SHA@" #define GIT_BRANCH "@GIT_BRANCH@" #define GIT_IS_DIRTY @GIT_IS_DIRTY@ #define BUILD_TIMESTAMP "@BUILD_TIMESTAMP@" -constexpr const char* get_version() -{ - return RELEASE_VERSION; -} +constexpr const char* get_version() { return RELEASE_VERSION; } -constexpr const char* get_git_sha() -{ - return GIT_SHA; -} +constexpr const char* get_git_sha() { return GIT_SHA; } -constexpr const char* get_git_branch() -{ - return GIT_BRANCH; -} +constexpr const char* get_git_branch() { return GIT_BRANCH; } -constexpr bool get_git_is_dirty() -{ - return GIT_IS_DIRTY; -} +constexpr bool get_git_is_dirty() { return GIT_IS_DIRTY; } -constexpr const char* get_build_timestamp() -{ - return BUILD_TIMESTAMP; -} +constexpr const char* get_build_timestamp() { return BUILD_TIMESTAMP; } -constexpr const char* caPrettyPrint = [] -{ - return GIT_IS_DIRTY - ? "Version: " RELEASE_VERSION "-Dirty\nBranch: " GIT_BRANCH "\nSHA: " GIT_SHA - : "Version: " RELEASE_VERSION "\nBranch: " GIT_BRANCH "\nSHA: " GIT_SHA; +constexpr const char* caPrettyPrint = [] { + bool git_is_available = get_git_branch()[0] != '\0'; + return git_is_available ? "Version: " RELEASE_VERSION + : GIT_IS_DIRTY ? "Version: " RELEASE_VERSION "-Dirty\nBranch: " GIT_BRANCH "\nSHA: " GIT_SHA + : "Version: " RELEASE_VERSION "\nBranch: " GIT_BRANCH "\nSHA: " GIT_SHA; }(); -constexpr const char* get_pretty_version() -{ - return caPrettyPrint; -} +constexpr const char* get_pretty_version() { return caPrettyPrint; }