build: add support for conan and make vendored json optional

This commit is contained in:
Carlos Gomes Martinho 2020-02-24 10:29:57 +01:00
parent de69fd2133
commit 564e9f8d23
5 changed files with 50 additions and 7 deletions

View file

@ -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 $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
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)
# ##############################################################################

View file

@ -209,12 +209,27 @@ Tested with <strong>clang-5.0</strong> and <strong>g++-6.4</strong>.
With issue#12, <strong>VS2017</strong> 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

10
conanfile.txt Normal file
View file

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

View file

@ -1,7 +1,10 @@
#include <iostream>
#include <string>
#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()

View file

@ -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<std::chrono::system_clock>;