From 6e3ae3175b34679cf1e84fc6dc5b9ebe6a9c0478 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 25 May 2018 09:51:34 +0200 Subject: Add getters for the library version CMake generates version.h from version.h.in and sets the major and minor version as specified in CMakeLists.txt and the current git version as returned by `git describe --always`. These values are also added to the C API as NK_get{_major,_minor,}_library_version. --- CMakeLists.txt | 8 +++++++- NK_C_API.cc | 13 +++++++++++++ NK_C_API.h | 23 ++++++++++++++++++++++- libnitrokey/version.h.in | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 libnitrokey/version.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dacb48..11e3f3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ IF (NOT CMAKE_BUILD_TYPE) ENDIF() MESSAGE("${PROJECT_NAME}: Build type: ${CMAKE_BUILD_TYPE}") +include_directories(${CMAKE_CURRENT_BINARY_DIR}/libnitrokey) include_directories(hidapi) include_directories(libnitrokey) set(SOURCE_FILES @@ -115,7 +116,12 @@ IF (LOG_VOLATILE_DATA) ENDIF() -file(GLOB LIB_INCLUDES "libnitrokey/*.h" "NK_C_API.h") +# generate version.h +exec_program("git" ${CMAKE_CURRENT_SOURCE_DIR} ARGS "describe --always" OUTPUT_VARIABLE PROJECT_VERSION_GIT) +configure_file("libnitrokey/version.h.in" "libnitrokey/version.h" @ONLY) + + +file(GLOB LIB_INCLUDES "libnitrokey/*.h" "NK_C_API.h" "${CMAKE_CURRENT_BINARY_DIR}/libnitrokey/version.h") install (FILES ${LIB_INCLUDES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) install (TARGETS nitrokey DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/NK_C_API.cc b/NK_C_API.cc index 8e005b8..a26b1da 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -25,6 +25,7 @@ #include #include "libnitrokey/LibraryException.h" #include "libnitrokey/cxx_semantics.h" +#include "version.h" #ifdef _MSC_VER #ifdef _WIN32 @@ -353,6 +354,18 @@ extern "C" { m->set_loglevel(level); } + NK_C_API unsigned int NK_get_major_library_version() { + return get_major_library_version(); + } + + NK_C_API unsigned int NK_get_minor_library_version() { + return get_minor_library_version(); + } + + NK_C_API const char* NK_get_library_version() { + return get_library_version(); + } + NK_C_API int NK_totp_set_time(uint64_t time) { auto m = NitrokeyManager::instance(); return get_without_result([&]() { diff --git a/NK_C_API.h b/NK_C_API.h index 5985c0b..1bcb65e 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -61,7 +61,28 @@ extern "C" { * Set debug level of messages written on stderr * @param level (int) 0-lowest verbosity, 5-highest verbosity */ - NK_C_API void NK_set_debug_level(const int level); + NK_C_API void NK_set_debug_level(const int level); + + /** + * Get the major library version, e. g. the 3 in v3.2. + * @return the major library version + */ + NK_C_API unsigned int NK_get_major_library_version(); + + /** + * Get the minor library version, e. g. the 2 in v3.2. + * @return the minor library version + */ + NK_C_API unsigned int NK_get_minor_library_version(); + + /** + * Get the library version as a string. This is the output of + * `git describe --always` at compile time, for example "v3.3" or + * "v3.3-19-gaee920b". + * The return value is a string literal and must not be freed. + * @return the library version as a string + */ + NK_C_API const char* NK_get_library_version(); /** * Connect to device of given model. Currently library can be connected only to one device at once. diff --git a/libnitrokey/version.h.in b/libnitrokey/version.h.in new file mode 100644 index 0000000..20c3179 --- /dev/null +++ b/libnitrokey/version.h.in @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 Nitrokey UG + * + * This file is part of libnitrokey. + * + * libnitrokey is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * libnitrokey 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 Lesser General Public License + * along with libnitrokey. If not, see . + * + * SPDX-License-Identifier: LGPL-3.0 + */ + +#ifndef LIBNITROKEY_VERSION_H +#define LIBNITROKEY_VERSION_H + +namespace nitrokey { +unsigned int get_major_library_version() { + return @PROJECT_VERSION_MAJOR@; +} + +unsigned int get_minor_library_version() { + return @PROJECT_VERSION_MINOR@; +} + +const char* get_library_version() { + return "@PROJECT_VERSION_GIT@"; +} +} + +#endif -- cgit v1.2.3 From 35ba2261ddeac3881dc7d4f14b737947a19a4b9b Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 19 Jun 2018 14:56:33 +0200 Subject: Move implementation from header back to compilation unit Signed-off-by: Szczepan Zalega --- CMakeLists.txt | 9 +++++---- libnitrokey/version.h | 33 +++++++++++++++++++++++++++++++++ libnitrokey/version.h.in | 39 --------------------------------------- version.cc.in | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 43 deletions(-) create mode 100644 libnitrokey/version.h delete mode 100644 libnitrokey/version.h.in create mode 100644 version.cc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 11e3f3c..06ab448 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,6 @@ IF (NOT CMAKE_BUILD_TYPE) ENDIF() MESSAGE("${PROJECT_NAME}: Build type: ${CMAKE_BUILD_TYPE}") -include_directories(${CMAKE_CURRENT_BINARY_DIR}/libnitrokey) include_directories(hidapi) include_directories(libnitrokey) set(SOURCE_FILES @@ -67,7 +66,9 @@ set(SOURCE_FILES NitrokeyManager.cc NK_C_API.h NK_C_API.cc - DeviceCommunicationExceptions.cpp) + DeviceCommunicationExceptions.cpp + ${CMAKE_CURRENT_BINARY_DIR}/version.cc + ) set(BUILD_SHARED_LIBS ON CACHE BOOL "Build all libraries as shared") add_library(nitrokey ${SOURCE_FILES}) @@ -118,10 +119,10 @@ ENDIF() # generate version.h exec_program("git" ${CMAKE_CURRENT_SOURCE_DIR} ARGS "describe --always" OUTPUT_VARIABLE PROJECT_VERSION_GIT) -configure_file("libnitrokey/version.h.in" "libnitrokey/version.h" @ONLY) +configure_file("version.cc.in" "version.cc" @ONLY) -file(GLOB LIB_INCLUDES "libnitrokey/*.h" "NK_C_API.h" "${CMAKE_CURRENT_BINARY_DIR}/libnitrokey/version.h") +file(GLOB LIB_INCLUDES "libnitrokey/*.h" "NK_C_API.h") install (FILES ${LIB_INCLUDES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) install (TARGETS nitrokey DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/libnitrokey/version.h b/libnitrokey/version.h new file mode 100644 index 0000000..6547af0 --- /dev/null +++ b/libnitrokey/version.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018 Nitrokey UG + * + * This file is part of libnitrokey. + * + * libnitrokey is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * libnitrokey 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 Lesser General Public License + * along with libnitrokey. If not, see . + * + * SPDX-License-Identifier: LGPL-3.0 + */ + +#ifndef LIBNITROKEY_VERSION_H +#define LIBNITROKEY_VERSION_H + +namespace nitrokey { + unsigned int get_major_library_version(); + + unsigned int get_minor_library_version(); + + const char* get_library_version(); +} + +#endif diff --git a/libnitrokey/version.h.in b/libnitrokey/version.h.in deleted file mode 100644 index 20c3179..0000000 --- a/libnitrokey/version.h.in +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2018 Nitrokey UG - * - * This file is part of libnitrokey. - * - * libnitrokey is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * libnitrokey 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 Lesser General Public License - * along with libnitrokey. If not, see . - * - * SPDX-License-Identifier: LGPL-3.0 - */ - -#ifndef LIBNITROKEY_VERSION_H -#define LIBNITROKEY_VERSION_H - -namespace nitrokey { -unsigned int get_major_library_version() { - return @PROJECT_VERSION_MAJOR@; -} - -unsigned int get_minor_library_version() { - return @PROJECT_VERSION_MINOR@; -} - -const char* get_library_version() { - return "@PROJECT_VERSION_GIT@"; -} -} - -#endif diff --git a/version.cc.in b/version.cc.in new file mode 100644 index 0000000..0eae647 --- /dev/null +++ b/version.cc.in @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018 Nitrokey UG + * + * This file is part of libnitrokey. + * + * libnitrokey is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * libnitrokey 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 Lesser General Public License + * along with libnitrokey. If not, see . + * + * SPDX-License-Identifier: LGPL-3.0 + */ + +#include "version.h" + +namespace nitrokey { + unsigned int get_major_library_version() { + return @PROJECT_VERSION_MAJOR@; + } + + unsigned int get_minor_library_version() { + return @PROJECT_VERSION_MINOR@; + } + + const char* get_library_version() { + return "@PROJECT_VERSION_GIT@"; + } +} + -- cgit v1.2.3 From 57c0b8c1235c5bff3640e85a8e17be20e8f422f3 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 19 Jun 2018 15:04:48 +0200 Subject: Add offline test for version getter Signed-off-by: Szczepan Zalega --- unittest/test_offline.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/unittest/test_offline.cc b/unittest/test_offline.cc index 468849e..aad875f 100644 --- a/unittest/test_offline.cc +++ b/unittest/test_offline.cc @@ -160,3 +160,14 @@ TEST_CASE("Test device commands ids", "[fast]") { REQUIRE(STICK20_CMD_CHANGE_UPDATE_PIN == static_cast(CommandID::CHANGE_UPDATE_PIN)); } + +#include "version.h" +TEST_CASE("Test version getter", "[fast]") { + REQUIRE(nitrokey::get_major_library_version() >= 3u); + REQUIRE(nitrokey::get_minor_library_version() >= 3u); + const char *library_version = nitrokey::get_library_version(); + REQUIRE(library_version != nullptr); + std::string s = library_version; + REQUIRE(s.length() >= 8); + REQUIRE(s.find("g") != std::string::npos); +} -- cgit v1.2.3