From 564e9f8d23f5bd9b422ca141a5edaab75574e293 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 10:29:57 +0100 Subject: [PATCH] build: add support for conan and make vendored json optional --- CMakeLists.txt | 14 +++++++++++++- README.md | 23 +++++++++++++++++++---- conanfile.txt | 10 ++++++++++ include/jwt/json/test_json.cc | 5 ++++- include/jwt/jwt.hpp | 5 ++++- 5 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 conanfile.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f1a58a..bffe31d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,11 @@ cmake_minimum_required(VERSION 3.5.0) project(cpp-jwt) option(CPP_JWT_BUILD_EXAMPLES "build examples" ON) -option(CPP_JWT_BUILD_TESTS "build examples" ON) +option(CPP_JWT_BUILD_TESTS "build tests" ON) +option(CPP_JWT_USE_VENDORED_NLOHMANN_JSON "use vendored json header" ON) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR}) # only set compiler flags if we are the main project, otherwise let the main # project decide on the flags @@ -14,6 +18,10 @@ endif() find_package(OpenSSL REQUIRED) +if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON) + find_package(nlohmann_json REQUIRED) +endif() + # ############################################################################## # LIBRARY # ############################################################################## @@ -24,6 +32,10 @@ target_include_directories( INTERFACE $ $) target_link_libraries(${PROJECT_NAME} INTERFACE OpenSSL::SSL) +if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON) + target_link_libraries(${PROJECT_NAME} INTERFACE nlohmann_json::nlohmann_json) + add_definitions(-DCPP_JWT_USE_VENDORED_NLOHMANN_JSON) +endif() target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_14) # ############################################################################## diff --git a/README.md b/README.md index 682789d..1cdff6e 100644 --- a/README.md +++ b/README.md @@ -209,12 +209,27 @@ Tested with clang-5.0 and g++-6.4. With issue#12, VS2017 is also supported. ## Installation -Use the C++ package manager..... just kidding :) -This is a header only library, so you can just add it to your include path and start using it. The only somewhat tricky part is to link it with openssl library. Check out the cmake file for building it properly. -For example one can run cmake like: +### using conan + +```shell +mkdir build +cd build +conan install .. --build missing +cmake .. +cmake --build . -j ``` -cmake -DOPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2j -DGTEST_ROOT=$HOME/googletest + +### using debian + +```shell +sudo apt install nlohmann-json3-dev +sudo apt install libgtest-dev +sudo apt install libssl-dev +mkdir build +cd build +cmake .. +cmake --build . -j ``` ## Parameters diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..b7af822 --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,10 @@ +[requires] +gtest/1.10.0 +nlohmann_json/3.7.0 +openssl/1.1.1d + +[generators] +cmake_find_package +cmake_paths + +[options] diff --git a/include/jwt/json/test_json.cc b/include/jwt/json/test_json.cc index 869d24f..d1ea502 100644 --- a/include/jwt/json/test_json.cc +++ b/include/jwt/json/test_json.cc @@ -1,7 +1,10 @@ #include #include +#if defined( CPP_JWT_USE_VENDORED_NLOHMANN_JSON) #include "./json.hpp" - +#else +#include "nlohmann/json.hpp" +#endif using json = nlohmann::json; void basic_json_test() diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 3c90549..3d00162 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -38,8 +38,11 @@ SOFTWARE. #include "jwt/string_view.hpp" #include "jwt/parameters.hpp" #include "jwt/exceptions.hpp" +#if defined(CPP_JWT_USE_VENDORED_NLOHMANN_JSON) #include "jwt/json/json.hpp" - +#else +#include "nlohmann/json.hpp" +#endif // For convenience using json_t = nlohmann::json; using system_time_t = std::chrono::time_point;