From 2ac810c84357337b7ff5e42c4552ce2b5fff9f19 Mon Sep 17 00:00:00 2001 From: Sven Fink Date: Tue, 8 Dec 2020 09:16:44 +0100 Subject: [PATCH] Add generated version info --- CMakeLists.txt | 10 ++++++- src/json-schema-version.cpp.in | 44 +++++++++++++++++++++++++++++++ src/json-schema-version.hpp.in | 43 ++++++++++++++++++++++++++++++ src/nlohmann/json-schema-decl.hpp | 16 +++++++++++ src/nlohmann/json-schema.hpp | 12 +-------- 5 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 src/json-schema-version.cpp.in create mode 100644 src/json-schema-version.hpp.in create mode 100644 src/nlohmann/json-schema-decl.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ac1dc4c..f6395bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,12 @@ project(nlohmann_json_schema_validator message(STATUS "Version: ${PROJECT_VERSION}") +set(LIB_VERSION_CPP_FILE ${CMAKE_CURRENT_BINARY_DIR}/json-schema-version.cpp) +set(LIB_VERSION_HPP_FILE ${CMAKE_CURRENT_BINARY_DIR}/include/nlohmann/json-schema-version.hpp) + +configure_file(src/json-schema-version.cpp.in ${LIB_VERSION_CPP_FILE}) +configure_file(src/json-schema-version.hpp.in ${LIB_VERSION_HPP_FILE}) + cmake_minimum_required(VERSION 3.2) option(BUILD_TESTS "Build tests" ON) @@ -35,12 +41,14 @@ add_library(nlohmann_json_schema_validator src/json-uri.cpp src/json-validator.cpp src/json-patch.cpp + ${LIB_VERSION_CPP_FILE} src/string-format-check.cpp) target_include_directories(nlohmann_json_schema_validator PUBLIC $ - $) + $ + $) target_compile_features(nlohmann_json_schema_validator PUBLIC diff --git a/src/json-schema-version.cpp.in b/src/json-schema-version.cpp.in new file mode 100644 index 0000000..9c62aa6 --- /dev/null +++ b/src/json-schema-version.cpp.in @@ -0,0 +1,44 @@ +#include + +namespace nlohmann +{ +namespace json_schema +{ +namespace about +{ + +const std::string &version_txt() +{ + // clang-format off + static std::string version{"${${PROJECT_NAME}_VERSION}"}; + // clang-format on + return version; +} + +// clang-format off +template <> +bool check_library_version<${${PROJECT_NAME}_VERSION_MAJOR}>() noexcept +{ + return true; +} +// clang-format on + +// clang-format off +template <> +bool check_library_version<${${PROJECT_NAME}_VERSION_MAJOR}, ${${PROJECT_NAME}_VERSION_MINOR}>() noexcept +{ + return true; +} +// clang-format on + +// clang-format off +template <> +bool check_library_version<${${PROJECT_NAME}_VERSION_MAJOR}, ${${PROJECT_NAME}_VERSION_MINOR}, ${${PROJECT_NAME}_VERSION_PATCH}>() noexcept +{ + return true; +} +// clang-format on + +} // namespace about +} // namespace json_schema +} // namespace nlohmann diff --git a/src/json-schema-version.hpp.in b/src/json-schema-version.hpp.in new file mode 100644 index 0000000..d082587 --- /dev/null +++ b/src/json-schema-version.hpp.in @@ -0,0 +1,43 @@ +#ifndef NLOHMANN_JSON_SCHEMA_VERSION_HPP__ +#define NLOHMANN_JSON_SCHEMA_VERSION_HPP__ + +#include +#include + +namespace nlohmann +{ +namespace json_schema +{ +namespace about +{ +// clang-format off +constexpr int VERSION_MAJOR=${${PROJECT_NAME}_VERSION_MAJOR}; +constexpr int VERSION_MINOR=${${PROJECT_NAME}_VERSION_MINOR}; +constexpr int VERSION_PATCH=${${PROJECT_NAME}_VERSION_PATCH}; +constexpr int VERSION_ID=((VERSION_MAJOR*10000) + (VERSION_MINOR*100) + (VERSION_PATCH)); +// clang-format on + +// Get library version as string +JSON_SCHEMA_VALIDATOR_API const std::string & +version_txt(); + +// Linker level library version check +// Check for library major version only +template +JSON_SCHEMA_VALIDATOR_API bool check_library_version() noexcept; + +// Linker level library version check +// Check for library major/minor version +template +JSON_SCHEMA_VALIDATOR_API bool check_library_version() noexcept; + +// Linker level library version check +// Check for library major/minor/patch version +template +JSON_SCHEMA_VALIDATOR_API bool check_library_version() noexcept; + +} // namespace about +} // namespace json_schema +} // namespace nlohmann + +#endif diff --git a/src/nlohmann/json-schema-decl.hpp b/src/nlohmann/json-schema-decl.hpp new file mode 100644 index 0000000..2ea2787 --- /dev/null +++ b/src/nlohmann/json-schema-decl.hpp @@ -0,0 +1,16 @@ +#ifndef NLOHMANN_JSON_SCHEMA_DECL_HPP__ +#define NLOHMANN_JSON_SCHEMA_DECL_HPP__ + +#ifdef _WIN32 +# if defined(JSON_SCHEMA_VALIDATOR_EXPORTS) +# define JSON_SCHEMA_VALIDATOR_API __declspec(dllexport) +# elif defined(JSON_SCHEMA_VALIDATOR_IMPORTS) +# define JSON_SCHEMA_VALIDATOR_API __declspec(dllimport) +# else +# define JSON_SCHEMA_VALIDATOR_API +# endif +#else +# define JSON_SCHEMA_VALIDATOR_API +#endif + +#endif diff --git a/src/nlohmann/json-schema.hpp b/src/nlohmann/json-schema.hpp index e99c400..ae7efff 100644 --- a/src/nlohmann/json-schema.hpp +++ b/src/nlohmann/json-schema.hpp @@ -9,17 +9,7 @@ #ifndef NLOHMANN_JSON_SCHEMA_HPP__ #define NLOHMANN_JSON_SCHEMA_HPP__ -#ifdef _WIN32 -# if defined(JSON_SCHEMA_VALIDATOR_EXPORTS) -# define JSON_SCHEMA_VALIDATOR_API __declspec(dllexport) -# elif defined(JSON_SCHEMA_VALIDATOR_IMPORTS) -# define JSON_SCHEMA_VALIDATOR_API __declspec(dllimport) -# else -# define JSON_SCHEMA_VALIDATOR_API -# endif -#else -# define JSON_SCHEMA_VALIDATOR_API -#endif +#include "json-schema-decl.hpp" #include