From 6e7445b1978e961bed7a709233043b414f82ee95 Mon Sep 17 00:00:00 2001 From: PixlOne <8843371+PixlOne@users.noreply.github.com> Date: Fri, 10 Apr 2020 20:53:13 -0400 Subject: [PATCH] Add versioning to logiops binaries --- CMakeLists.txt | 37 +++++++++++++++++++++++++++++++++++++ src/logid/logid.cpp | 25 ++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22658b3..a1d9fd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,4 +11,41 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -Wall -Wextra") set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) +find_package(Git) + +# Set version number +if(EXISTS ${CMAKE_SOURCE_DIR}/version.txt) + file(READ version.txt LOGIOPS_VERSION) + string(REGEX REPLACE "\n$" "" LOGIOPS_VERSION ${LOGIOPS_VERSION}) +endif() + +if(GIT_FOUND AND EXISTS ${CMAKE_SOURCE_DIR}/.git) + execute_process(COMMAND ${GIT_EXECUTABLE} + rev-parse --abbrev-ref HEAD + OUTPUT_VARIABLE LOGIOPS_GIT_BRANCH) + string(REGEX REPLACE "\n$" "" LOGIOPS_GIT_BRANCH ${LOGIOPS_GIT_BRANCH}) + if(LOGIOPS_GIT_BRANCH MATCHES "^tags/?") + STRING(REGEX REPLACE "^tags/" "" + LOGIOPS_VERSION ${LOGIOPS_GIT_BRANCH}) + else() + execute_process(COMMAND ${GIT_EXECUTABLE} + rev-parse --short HEAD + OUTPUT_VARIABLE LOGIOPS_COMMIT_HASH) + string(REGEX REPLACE "\n$" "" LOGIOPS_COMMIT_HASH ${LOGIOPS_COMMIT_HASH}) + if(LOGIOPS_VERSION) + string(APPEND LOGIOPS_VERSION -${LOGIOPS_COMMIT_HASH}) + else() + set(LOGIOPS_VERSION git-${LOGIOPS_COMMIT_HASH}) + endif() + endif() +endif() + +if(LOGIOPS_VERSION) + message("LogiOps Version Number: ${LOGIOPS_VERSION}") +else() + set(LOGIOPS_VERSION "null") +endif() + +add_definitions( -DLOGIOPS_VERSION="${LOGIOPS_VERSION}") + add_subdirectory(src/logid) diff --git a/src/logid/logid.cpp b/src/logid/logid.cpp index e112e50..ab63b8a 100644 --- a/src/logid/logid.cpp +++ b/src/logid/logid.cpp @@ -19,6 +19,11 @@ #define evdev_name "logid" #define DEFAULT_CONFIG_FILE "/etc/logid.cfg" +#ifndef LOGIOPS_VERSION +#define LOGIOPS_VERSION "null" +#warning Version is undefined! +#endif + using namespace logid; LogLevel logid::global_verbosity = INFO; @@ -31,7 +36,8 @@ enum class Option None, Verbose, Config, - Help + Help, + Version }; int main(int argc, char** argv) @@ -51,11 +57,15 @@ int main(int argc, char** argv) if (op_str == "--verbose") option = Option::Verbose; if (op_str == "--config") option = Option::Config; if (op_str == "--help") option = Option::Help; + if (op_str == "--version") option = Option::Version; break; } case 'v': // Verbosity option = Option::Verbose; break; + case 'V': //Version + option = Option::Version; + break; case 'c': // Config file path option = Option::Config; break; @@ -103,14 +113,23 @@ int main(int argc, char** argv) break; } case Option::Help: - printf(R"(Usage: %s [options] + { + printf(R"(logid version %s +Usage: %s [options] Possible options are: -v,--verbose [level] Set log level to debug/info/warn/error (leave blank for debug) + -V,--version Print version number -c,--config [file path] Change config file from default at %s -h,--help Print this message. -)", argv[0], DEFAULT_CONFIG_FILE); +)", LOGIOPS_VERSION, argv[0], DEFAULT_CONFIG_FILE); return EXIT_SUCCESS; + } + case Option::Version: + { + printf("%s\n", LOGIOPS_VERSION); + return EXIT_SUCCESS; + } case Option::None: break; }