From 039effb3446bdc14f7153c6b73863303b6f0ff33 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 22 Feb 2023 02:38:51 +0100 Subject: [PATCH] add config binary --- CMakeLists.txt | 13 ++++++++++ src/CMakeLists.txt | 1 + src/bin/CMakeLists.txt | 6 +++++ src/bin/config.cc | 52 +++++++++++++++++++++++++++++++++++++++ src/include/base/base.h | 1 + src/include/base/config.h | 20 +++++++++++++++ src/lib/CMakeLists.txt | 1 + src/lib/base/config.cc | 23 +++++++++++++++++ 8 files changed, 117 insertions(+) create mode 100644 src/bin/CMakeLists.txt create mode 100644 src/bin/config.cc create mode 100644 src/include/base/config.h create mode 100644 src/lib/base/config.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 24e17db..1af5b25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,15 @@ cmake_minimum_required(VERSION 3.0) project(cnorxz) +execute_process(COMMAND bash "-c" "git rev-parse HEAD" OUTPUT_VARIABLE GIT_HASH OUTPUT_STRIP_TRAILING_WHITESPACE) +execute_process(COMMAND bash "-c" "git tag -l --sort=refname v* | tail -n1" OUTPUT_VARIABLE GIT_TAG OUTPUT_STRIP_TRAILING_WHITESPACE) +execute_process(COMMAND bash "-c" "git rev-parse ${GIT_TAG}" OUTPUT_VARIABLE GIT_TAG_HASH OUTPUT_STRIP_TRAILING_WHITESPACE) +string(SUBSTRING ${GIT_TAG} 1 -1 VERSION) +if(NOT ${GIT_HASH} EQUAL ${GIT_TAG_HASH}) + string(SUBSTRING ${GIT_HASH} 0 7 GIT_HASH_SHORT) + set(VERSION "${VERSION}-${GIT_HASH_SHORT}") +endif() + if(CMAKE_COMPILER_IS_GNUCXX) if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.0) message(FATAL_ERROR "require gcc version >= 7.0") @@ -42,4 +51,8 @@ else() set(ENABLE_hdf5 TRUE CACHE BOOL "enable hdf5") endif() +add_definitions(-DVERSION="${VERSION}") +add_definitions(-DGIT_COMMIT="${GIT_HASH}") +add_definitions(-DCXX_FLAGS="${CMAKE_CXX_FLAGS}") + add_subdirectory(src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d5dc42f..04789ca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ include_directories(${CMAKE_SOURCE_DIR}/src/include) add_subdirectory(tests) add_subdirectory(lib) +add_subdirectory(bin) if(ENABLE_hdf5) add_subdirectory(opt/hdf5) diff --git a/src/bin/CMakeLists.txt b/src/bin/CMakeLists.txt new file mode 100644 index 0000000..e033785 --- /dev/null +++ b/src/bin/CMakeLists.txt @@ -0,0 +1,6 @@ + +add_executable(cnorxz-config config.cc) +add_dependencies(cnorxz-config cnorxz) +target_link_libraries(cnorxz-config cnorxz) + +install(TARGETS cnorxz-config RUNTIME DESTINATION ${INSTALL_PATH}/bin) diff --git a/src/bin/config.cc b/src/bin/config.cc new file mode 100644 index 0000000..ad18459 --- /dev/null +++ b/src/bin/config.cc @@ -0,0 +1,52 @@ + +#include +#include +#include +#include +#include "base/config.h" + +using CNORXZ::String; +using CNORXZ::SizeT; +typedef std::map> CMapT; + +auto configMap() +{ + CMapT m; + m["--version"] = CNORXZ::Config::version; + m["--commit"] = CNORXZ::Config::commit; + m["--flags"] = CNORXZ::Config::flags; + return m; +} + +void printUsage(const String& prog, const CMapT& cm) +{ + std::cout << "usage: " << prog << " "; + for(const auto& x: cm){ + std::cout << "[" << x.first << "] "; + } + std::cout << std::endl; +} + +int main(int argc, char** argv) +{ + const String prog(argv[0]); + const auto cm = configMap(); + if(argc == 1){ + printUsage(prog, cm); + } + try { + for(SizeT i = 1; i != static_cast(argc); ++i){ + std::cout << cm.at( String(argv[i]) )() << std::endl; + } + } + catch(const std::out_of_range& e){ + std::cerr << "caught exception: " << e.what() << std::endl; + printUsage(prog, cm); + return 1; + } + catch(...){ + std::cerr << "caught generic exception" << std::endl; + } + + return 0; +} diff --git a/src/include/base/base.h b/src/include/base/base.h index f5630af..033801f 100644 --- a/src/include/base/base.h +++ b/src/include/base/base.h @@ -25,6 +25,7 @@ #include "isq.h" #include "iter.h" #include "uuid.h" +#include "config.h" #include "base.cc.h" diff --git a/src/include/base/config.h b/src/include/base/config.h new file mode 100644 index 0000000..58859ab --- /dev/null +++ b/src/include/base/config.h @@ -0,0 +1,20 @@ + +#ifndef __cxz_config_h__ +#define __cxz_config_h__ + +#include +#include "base/types.h" + +namespace CNORXZ +{ + namespace Config + { + String version(); + + String commit(); + + String flags(); + } +} + +#endif diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 453861a..6389903 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -2,6 +2,7 @@ set(libcnorxz_a_SOURCES ${CMAKE_SOURCE_DIR}/src/lib/base/to_string.cc ${CMAKE_SOURCE_DIR}/src/lib/base/uuid.cc + ${CMAKE_SOURCE_DIR}/src/lib/base/config.cc ${CMAKE_SOURCE_DIR}/src/lib/memory/memcount.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/yrange.cc diff --git a/src/lib/base/config.cc b/src/lib/base/config.cc new file mode 100644 index 0000000..f13756a --- /dev/null +++ b/src/lib/base/config.cc @@ -0,0 +1,23 @@ + +#include "base/config.h" + +namespace CNORXZ +{ + namespace Config + { + String version() + { + return String(VERSION); + } + + String commit() + { + return String(GIT_COMMIT); + } + + String flags() + { + return String(CXX_FLAGS); + } + } +}