add config binary

This commit is contained in:
Christian Zimmermann 2023-02-22 02:38:51 +01:00
parent 229a913bcb
commit 039effb344
8 changed files with 117 additions and 0 deletions

View file

@ -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)

View file

@ -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)

6
src/bin/CMakeLists.txt Normal file
View file

@ -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)

52
src/bin/config.cc Normal file
View file

@ -0,0 +1,52 @@
#include <cstdlib>
#include <iostream>
#include <map>
#include <functional>
#include "base/config.h"
using CNORXZ::String;
using CNORXZ::SizeT;
typedef std::map<String,std::function<String(void)>> 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<SizeT>(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;
}

View file

@ -25,6 +25,7 @@
#include "isq.h"
#include "iter.h"
#include "uuid.h"
#include "config.h"
#include "base.cc.h"

20
src/include/base/config.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef __cxz_config_h__
#define __cxz_config_h__
#include <cstdlib>
#include "base/types.h"
namespace CNORXZ
{
namespace Config
{
String version();
String commit();
String flags();
}
}
#endif

View file

@ -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

23
src/lib/base/config.cc Normal file
View file

@ -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);
}
}
}