From 8a0d16b8f59dd1c9d75045aa016d9ce235257311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Urs=20F=C3=A4ssler?= Date: Thu, 28 Dec 2023 17:28:24 +0100 Subject: [PATCH] add version info to cucumber-cpp-main --- cmake/modules/GitVersion.cmake | 29 +++++++++++++++++++++++++++++ src/CMakeLists.txt | 7 +++++++ src/main.cpp | 6 ++++++ 3 files changed, 42 insertions(+) create mode 100644 cmake/modules/GitVersion.cmake diff --git a/cmake/modules/GitVersion.cmake b/cmake/modules/GitVersion.cmake new file mode 100644 index 00000000..b5c4e756 --- /dev/null +++ b/cmake/modules/GitVersion.cmake @@ -0,0 +1,29 @@ +function(git_get_version VERSION_VARIABLE) + find_program(GIT_EXECUTABLE git) + + if(NOT GIT_EXECUTABLE) + message(FATAL_ERROR "Git not found. Please install Git and make sure it is in your system's PATH.") + endif() + + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --always --dirty + OUTPUT_VARIABLE VERSION_STRING + OUTPUT_STRIP_TRAILING_WHITESPACE + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + ERROR_VARIABLE GIT_DESCRIBE_ERROR + RESULT_VARIABLE GIT_DESCRIBE_RESULT + ) + + if(NOT GIT_DESCRIBE_RESULT EQUAL 0) + message(FATAL_ERROR "Error running 'git describe': ${GIT_DESCRIBE_ERROR}") + endif() + + string(LENGTH "${VERSION_STRING}" VERSION_STRING_LENGTH) + string(SUBSTRING "${VERSION_STRING}" 0 1 FIRST_CHARACTER) + + if("${FIRST_CHARACTER}" STREQUAL "v") + string(SUBSTRING "${VERSION_STRING}" 1 ${VERSION_STRING_LENGTH} VERSION_STRING) + endif() + + set(${VERSION_VARIABLE} ${VERSION_STRING} PARENT_SCOPE) +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 547a5078..3579e695 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ include(GenerateExportHeader) find_package(nlohmann_json 3.10.5 REQUIRED) +include(../cmake/modules/GitVersion.cmake) set(CUKE_SOURCES drivers/GenericDriver.cpp @@ -123,6 +124,12 @@ foreach(TARGET endif(MINGW) endforeach() +git_get_version(CUKE_VERSION) +message(STATUS "Version: ${CUKE_VERSION}") +target_compile_definitions(cucumber-cpp PRIVATE + CUKE_VERSION="${CUKE_VERSION}" +) + target_link_libraries(cucumber-cpp PRIVATE Boost::program_options diff --git a/src/main.cpp b/src/main.cpp index 18656715..44031d1f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,7 @@ int CUCUMBER_CPP_EXPORT main(int argc, char** argv) { boost::program_options::options_description optionDescription("Allowed options"); optionDescription.add_options()("help,h", "help for cucumber-cpp")( "verbose,v", "verbose output" + )("version", "version of cucumber-cpp" )("listen,l", value(), "listening address of wireserver" )("port,p", value(), @@ -68,6 +69,11 @@ int CUCUMBER_CPP_EXPORT main(int argc, char** argv) { exit(1); } + if (optionVariableMap.count("version")) { + std::cout << CUKE_VERSION << std::endl; + exit(0); + } + std::string listenHost("127.0.0.1"); if (optionVariableMap.count("listen")) { listenHost = optionVariableMap["listen"].as();