From 5da27c84bb8ccc64d20d77ccb2353035086b3387 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Wed, 21 Aug 2024 17:41:08 +0000 Subject: [PATCH] GitInfo system with new -v switch --- CMakeLists.txt | 8 ++ cmake/gitinfo.cmake | 220 ++++++++++++++++++++++++++++++++++++++++++++ src/cm-gitinfo.h.in | 6 ++ src/gitinfo.cc | 37 ++++++++ src/gitinfo.h | 35 +++++++ src/main.cc | 19 +++- 6 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 cmake/gitinfo.cmake create mode 100644 src/cm-gitinfo.h.in create mode 100644 src/gitinfo.cc create mode 100644 src/gitinfo.h diff --git a/CMakeLists.txt b/CMakeLists.txt index aa2c7b9..6b06dbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ endif() set (VOMP_OBJ_COMMON + src/gitinfo.cc src/util.cc src/control.cc src/thread.cc @@ -190,6 +191,12 @@ add_executable(vomp ${VOMP_OBJ_COMMON} ${VOMP_OBJ_RASPBERRY} pngs.o) # set(CMAKE_CXX_COMPILER_LAUNCHER "distcc") - not until cmake 3.17! +list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) +include(gitinfo) +GitInfo(${CMAKE_SOURCE_DIR}) +GitInfoReport() + +configure_file(${CMAKE_SOURCE_DIR}/src/cm-gitinfo.h.in ${CMAKE_BINARY_DIR}/src/cm-gitinfo.h) if (${ENABLE_MOLD}) @@ -207,6 +214,7 @@ find_path(INC_MAGICK Magick++.h PATHS /usr/include/ImageMagick-6) find_path(INC_MAGICK2 magick/magick-baseconfig.h PATHS /usr/include/arm-linux-gnueabihf/ImageMagick-6) target_include_directories(vomp SYSTEM PRIVATE ${INC_VC} ${INC_FT2} ${INC_MAGICK} ${INC_MAGICK2}) +target_include_directories(vomp PRIVATE "${CMAKE_BINARY_DIR}/src") target_compile_options(vomp PRIVATE -Werror=return-type diff --git a/cmake/gitinfo.cmake b/cmake/gitinfo.cmake new file mode 100644 index 0000000..e802bbd --- /dev/null +++ b/cmake/gitinfo.cmake @@ -0,0 +1,220 @@ +# MIT License +# +# Copyright (c) 2020 C++ Modern Framework +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +# ---------------------------------------------------------------------------- # +# +# Copyright (c) 2020 C++ Modern Framework +# +# ---------------------------------------------------------------------------- # + +# ---------------------------------------------------------------------------- # +# +# Following variables will be set when calling GitInfo +# +# GIT_DIR: path to the project .git directory +# GIT_IS_DIRTY: whether or not the working tree is dirty +# GIT_HEAD_BRANCH : name of the branch associated te HEAD +# GIT_REVISION_HASH: current HEAD sha hash +# GIT_REVISION: shorten version of GIT_REVISION_HASH +# GIT_REVISION_NAME: name associated to GIT_REVISION_HASH +# GIT_AUTHOR_NAME : author name +# GIT_AUTHOR_EMAIL : author email +# GIT_AUTHOR_DATE : author date +# GIT_AUTHOR_DATE_ISO : author date, strict ISO 8601 format +# GIT_COMMITTER_NAME : committer name +# GIT_COMMITTER_EMAIL : committer email +# GIT_COMMITTER_DATE : committer date +# GIT_COMMITTER_DATE_ISO : committer date, strict ISO 8601 format +# GIT_REMOTE_ORIGIN_URL : origin remote url +# GIT_LATEST_TAG_LONG : most recent tag of the current branch +# GIT_LATEST_TAG : most recent tagname of the current branch +# +# ---------------------------------------------------------------------------- # + +# This is the main function to call in project CMakeLists.txt +# source should point to the root project directory +function(GitInfo source) + + # Check is source is a valid path + if(NOT EXISTS ${source}) + message(FATAL_ERROR "'${source}' is not a valid path") + endif() + + # Define the possible location of the .git directory + set(GIT_DIR "${source}/.git") + + # Check if .git folder exist + if(EXISTS ${GIT_DIR}) + + # + set(GIT_DIR "${GIT_DIR}" CACHE PATH "Project .git directory") + + # Check if git is installed + if(NOT GIT_FOUND) + find_package(Git QUIET) + endif() + if(NOT GIT_FOUND) + message(AUTHOR_WARNING "Git not found, cannot get git informations") + return() + endif() + + # whether or not the working tree is dirty + execute_process(COMMAND ${GIT_EXECUTABLE} status --porcelain + WORKING_DIRECTORY ${source} + RESULT_VARIABLE exit_code + OUTPUT_VARIABLE GIT_IS_DIRTY OUTPUT_STRIP_TRAILING_WHITESPACE) + # the working tree is dirty when the error code is different from 0 + # or if the output is not empty + if(NOT exit_code EQUAL 0 OR NOT ${GIT_IS_DIRTY} STREQUAL "") + unset(GIT_IS_DIRTY) + set(GIT_IS_DIRTY ON CACHE BOOL "Indicate if current branch is dirty") + else() + unset(GIT_IS_DIRTY) + set(GIT_IS_DIRTY OFF CACHE BOOL "Indicate if current branch is dirty") + endif() + + # name of the brack associated te HEAD + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_HEAD_BRANCH OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_HEAD_BRANCH "${GIT_HEAD_BRANCH}" CACHE INTERNAL "name of the brack associated te HEAD") + + # git revision full hash + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%H" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_REVISION_HASH OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_REVISION_HASH "${GIT_REVISION_HASH}" CACHE INTERNAL "git revision full hash") + + # shorten version of git revision + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%h" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_REVISION OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_REVISION "${GIT_REVISION}" CACHE INTERNAL "shorten version of git revision") + + # shorten version of git revision name + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%s" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_REVISION_NAME OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_REVISION_NAME "${GIT_REVISION_NAME}" CACHE INTERNAL "shorten version of git revision name") + + # author name + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%an" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_AUTHOR_NAME OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_AUTHOR_NAME "${GIT_AUTHOR_NAME}" CACHE INTERNAL "git author name") + + # author email + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%ae" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_AUTHOR_EMAIL OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_AUTHOR_EMAIL "${GIT_AUTHOR_EMAIL}" CACHE INTERNAL "git author email") + + # author date + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%ad" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_AUTHOR_DATE OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_AUTHOR_DATE "${GIT_AUTHOR_DATE}" CACHE INTERNAL "git author date") + + # author date, strict ISO 8601 format + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%aI" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_AUTHOR_DATE_ISO OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_AUTHOR_DATE_ISO "${GIT_AUTHOR_DATE_ISO}" CACHE INTERNAL "git author date, strict ISO 8601 format") + + # committer name + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%cn" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_COMMITTER_NAME OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_COMMITTER_NAME "${GIT_COMMITTER_NAME}" CACHE INTERNAL "git committer name") + + # committer email + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%ce" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_COMMITTER_EMAIL OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_COMMITTER_EMAIL "${GIT_COMMITTER_EMAIL}" CACHE INTERNAL "git committer email") + + # committer date + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%cd" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_COMMITTER_DATE OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_COMMITTER_DATE "${GIT_COMMITTER_DATE}" CACHE INTERNAL "git committer date") + + # committer date, strict ISO 8601 format + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%cI" HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_COMMITTER_DATE_ISO OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_COMMITTER_DATE_ISO "${GIT_COMMITTER_DATE_ISO}" CACHE INTERNAL "git committer date, strict ISO 8601 format") + + # origin remote url + execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_REMOTE_ORIGIN_URL OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_REMOTE_ORIGIN_URL "${GIT_REMOTE_ORIGIN_URL}" CACHE INTERNAL "git origin remote url") + + # most recent tag of the current branch + execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_LATEST_TAG_LONG OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_LATEST_TAG_LONG "${GIT_LATEST_TAG_LONG}" CACHE INTERNAL "git most recent tag of the current branch") + + # most recent tagname of the current branch + execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags HEAD + WORKING_DIRECTORY ${source} + OUTPUT_VARIABLE GIT_LATEST_TAG OUTPUT_STRIP_TRAILING_WHITESPACE) + set(GIT_LATEST_TAG "${GIT_LATEST_TAG}" CACHE INTERNAL "git most recent tagname of the current branch") + + endif() + +endfunction() + + +# Report git information +function(GitInfoReport) + message(STATUS "") + message(STATUS "----------------------------------------------------") + message(STATUS " GitInfo.cmake") + message(STATUS "") + message(STATUS "GIT_DIR : ${GIT_DIR}") + message(STATUS "") + message(STATUS "GIT_IS_DIRTY : ${GIT_IS_DIRTY}") + message(STATUS "GIT_HEAD_BRANCH : ${GIT_HEAD_BRANCH}") + message(STATUS "GIT_REVISION : ${GIT_REVISION}") + message(STATUS "GIT_REVISION_HASH : ${GIT_REVISION_HASH}") + message(STATUS "GIT_REVISION_NAME : ${GIT_REVISION_NAME}") + message(STATUS "") + message(STATUS "GIT_AUTHOR_NAME : ${GIT_AUTHOR_NAME}") + message(STATUS "GIT_AUTHOR_EMAIL : ${GIT_AUTHOR_EMAIL}") + message(STATUS "GIT_AUTHOR_DATE : ${GIT_AUTHOR_DATE}") + message(STATUS "GIT_AUTHOR_DATE_ISO : ${GIT_AUTHOR_DATE_ISO}") + message(STATUS "") + message(STATUS "GIT_COMMITTER_NAME : ${GIT_COMMITTER_NAME}") + message(STATUS "GIT_COMMITTER_EMAIL : ${GIT_COMMITTER_EMAIL}") + message(STATUS "GIT_COMMITTER_DATE : ${GIT_COMMITTER_DATE}") + message(STATUS "GIT_COMMITTER_DATE_ISO : ${GIT_COMMITTER_DATE_ISO}") + message(STATUS "") + message(STATUS "GIT_REMOTE_ORIGIN_URL : ${GIT_REMOTE_ORIGIN_URL}") + message(STATUS "GIT_LATEST_TAG_LONG : ${GIT_LATEST_TAG_LONG}") + message(STATUS "GIT_LATEST_TAG : ${GIT_LATEST_TAG}") + message(STATUS "") + message(STATUS "----------------------------------------------------") + message(STATUS "") +endfunction() diff --git a/src/cm-gitinfo.h.in b/src/cm-gitinfo.h.in new file mode 100644 index 0000000..2a40236 --- /dev/null +++ b/src/cm-gitinfo.h.in @@ -0,0 +1,6 @@ +#define GIT_HEAD_BRANCH "@GIT_HEAD_BRANCH@" +#define GIT_REVISION "@GIT_REVISION@" +#define GIT_REVISION_NAME "@GIT_REVISION_NAME@" +#define GIT_COMMITTER_DATE "@GIT_COMMITTER_DATE@" +#define GIT_LATEST_TAG_LONG "@GIT_LATEST_TAG_LONG@" +#cmakedefine GIT_IS_DIRTY diff --git a/src/gitinfo.cc b/src/gitinfo.cc new file mode 100644 index 0000000..31e9cf0 --- /dev/null +++ b/src/gitinfo.cc @@ -0,0 +1,37 @@ +/* + Copyright 2004-2005 Chris Tallon + + This file is part of VOMP. + + VOMP is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + VOMP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with VOMP. If not, see . +*/ + +#include "gitinfo.h" + +#include "cm-gitinfo.h" + +const char* GitInfo::GitHeadBranch() { return GIT_HEAD_BRANCH; } +const char* GitInfo::GitRevision() { return GIT_REVISION; } +const char* GitInfo::GitRevisionName() { return GIT_REVISION_NAME; } +const char* GitInfo::GitCommitterDate() { return GIT_COMMITTER_DATE; } +const char* GitInfo::GitLatestTag() { return GIT_LATEST_TAG_LONG; } + +bool GitInfo::GitIsDirty() +{ +#ifdef GIT_IS_DIRTY + return true; +#else + return false; +#endif +} diff --git a/src/gitinfo.h b/src/gitinfo.h new file mode 100644 index 0000000..3bc49fd --- /dev/null +++ b/src/gitinfo.h @@ -0,0 +1,35 @@ +/* + Copyright 2004-2005 Chris Tallon + + This file is part of VOMP. + + VOMP is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + VOMP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with VOMP. If not, see . +*/ + +#ifndef GITINFO_H +#define GITINFO_H + +class GitInfo +{ + public: + static const char* GitHeadBranch(); + static const char* GitRevision(); + static const char* GitRevisionName(); + static const char* GitCommitterDate(); + static const char* GitLatestTag(); + static bool GitIsDirty(); + +}; + +#endif diff --git a/src/main.cc b/src/main.cc index b3d9b26..e44a31f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -44,6 +44,7 @@ #include #endif +#include "gitinfo.h" #include "config.h" #include "log.h" #include "util.h" @@ -61,6 +62,8 @@ void threadSignalReceiverFunction(); #endif + +[[ noreturn ]] void printVersion(); [[ noreturn ]] void shutdown(int code); Config* config; @@ -88,7 +91,7 @@ int main(int argc, char** argv) } int c; - while ((c = getopt(argc, argv, "hclf:ns:p:")) != -1) + while ((c = getopt(argc, argv, "hclvf:ns:p:")) != -1) { switch (c) { @@ -110,6 +113,8 @@ int main(int argc, char** argv) case 'p': config->set("server", "port", std::atoi(optarg)); break; + case 'v': + printVersion(); case '?': case 'h': printf("Vompclient\n\n"); @@ -279,6 +284,18 @@ void threadSignalReceiverFunction() // ------------------------------------------------------------------------------------------------------------------- +[[ noreturn ]] void printVersion() +{ + printf("Vomp Client\n"); + printf("Head branch: %s\n", GitInfo::GitHeadBranch()); + printf("Revision: %s\n", GitInfo::GitRevision()); + printf("Revision Name: %s\n", GitInfo::GitRevisionName()); + printf("Comitter date: %s\n", GitInfo::GitCommitterDate()); + printf("Latest tag: %s\n", GitInfo::GitLatestTag()); + printf("Git wd dirty: %i\n", GitInfo::GitIsDirty()); + exit(0); +} + [[ noreturn ]] void shutdown(int code) { // FIXME, send a del all to boxstack first, then get rid of it after control? -- 2.39.5