Skip to content

Commit d53e013

Browse files
Find nlohmann_json dependency only when required (#342)
* only find nlohmann_json test dep when required * provide config-variable JWT_EXTERNAL_TEST_NLOHMANN_JSON * be a doll and make PICOJSON variables dependent * dont mention test in the option name I think in the longer term we'll probably switch to this since it;s maintained unlike the current default --------- Co-authored-by: Chris Mc <[email protected]>
1 parent bd682ed commit d53e013

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

CMakeLists.txt

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ option(JWT_BUILD_DOCS "Adds a target for building the doxygen documentation" OFF
2121
option(JWT_ENABLE_COVERAGE "Enable code coverage testing" OFF)
2222
option(JWT_ENABLE_FUZZING "Enable fuzz testing" OFF)
2323

24-
option(JWT_EXTERNAL_PICOJSON "Use find_package() to locate picojson, provided to integrate with package managers" OFF)
25-
option(JWT_DISABLE_BASE64 "Do not include the base64 implementation from this library" OFF)
2624
option(JWT_DISABLE_PICOJSON "Do not provide the picojson template specialiaze" OFF)
25+
option(JWT_DISABLE_BASE64 "Do not include the base64 implementation from this library" OFF)
26+
include(CMakeDependentOption)
27+
cmake_dependent_option(JWT_EXTERNAL_PICOJSON
28+
"Use find_package() to locate picojson, provided to integrate with package managers" OFF
29+
"NOT JWT_DISABLE_PICOJSON" OFF)
30+
cmake_dependent_option(JWT_EXTERNAL_NLOHMANN_JSON
31+
"Use find_package() to locate nlohman-json required for tests and examples" OFF
32+
"JWT_BUILD_EXAMPLES OR JWT_BUILD_TESTS" OFF)
2733

2834
set(JWT_SSL_LIBRARY_OPTIONS OpenSSL LibreSSL wolfSSL)
2935
set(JWT_SSL_LIBRARY OpenSSL CACHE STRING "Determines which SSL library to build with")
@@ -63,18 +69,22 @@ elseif(${JWT_SSL_LIBRARY} MATCHES "wolfSSL")
6369
list(TRANSFORM wolfssl_INCLUDE_DIRS APPEND "/wolfssl") # This is required to access OpenSSL compatibility API
6470
endif()
6571

66-
if(JWT_EXTERNAL_PICOJSON)
72+
if(NOT JWT_DISABLE_PICOJSON AND JWT_EXTERNAL_PICOJSON)
6773
find_package(picojson 1.3.0 REQUIRED)
6874
endif()
6975

70-
find_package(nlohmann_json CONFIG)
71-
72-
if(NOT nlohmann_json_FOUND)
73-
message(STATUS "jwt-cpp: using FetchContent for nlohmann json")
74-
include(FetchContent)
75-
fetchcontent_declare(nlohmann_json URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz
76-
URL_MD5 127794b2c82c0c5693805feaa2a703e2)
77-
fetchcontent_makeavailable(nlohmann_json)
76+
if(JWT_BUILD_EXAMPLES OR JWT_BUILD_TESTS)
77+
if(JWT_EXTERNAL_NLOHMANN_JSON)
78+
message(STATUS "jwt-cpp: using find_package for nlohmann-json required for tests")
79+
find_package(nlohmann_json CONFIG REQUIRED)
80+
else()
81+
message(STATUS "jwt-cpp: using FetchContent for nlohmann-json required for tests")
82+
include(FetchContent)
83+
fetchcontent_declare(nlohmann_json
84+
URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz
85+
URL_MD5 127794b2c82c0c5693805feaa2a703e2)
86+
fetchcontent_makeavailable(nlohmann_json)
87+
endif()
7888
endif()
7989

8090
set(JWT_INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include)
@@ -118,7 +128,7 @@ if(${JWT_SSL_LIBRARY} MATCHES "wolfSSL")
118128
target_compile_definitions(jwt-cpp INTERFACE OPENSSL_EXTRA OPENSSL_ALL)
119129
endif()
120130

121-
if(JWT_EXTERNAL_PICOJSON)
131+
if(NOT JWT_DISABLE_PICOJSON AND JWT_EXTERNAL_PICOJSON)
122132
target_link_libraries(jwt-cpp INTERFACE picojson::picojson>)
123133
endif()
124134

@@ -129,7 +139,7 @@ endif()
129139

130140
configure_package_config_file(
131141
${CMAKE_CURRENT_LIST_DIR}/cmake/jwt-cpp-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/jwt-cpp-config.cmake
132-
INSTALL_DESTINATION ${JWT_CMAKE_FILES_INSTALL_DIR} PATH_VARS JWT_EXTERNAL_PICOJSON JWT_SSL_LIBRARY)
142+
INSTALL_DESTINATION ${JWT_CMAKE_FILES_INSTALL_DIR})
133143
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/jwt-cpp-config-version.cmake VERSION 0.7.0
134144
COMPATIBILITY ExactVersion)
135145

cmake/jwt-cpp-config.cmake.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@PACKAGE_INIT@
22

3+
set(JWT_DISABLE_PICOJSON @JWT_DISABLE_PICOJSON@)
34
set(JWT_EXTERNAL_PICOJSON @JWT_EXTERNAL_PICOJSON@)
45
set(JWT_SSL_LIBRARY @JWT_SSL_LIBRARY@)
56

@@ -12,7 +13,7 @@ else()
1213
find_dependency(${JWT_SSL_LIBRARY} REQUIRED)
1314
endif()
1415

15-
if(JWT_EXTERNAL_PICOJSON)
16+
if(NOT JWT_DISABLE_PICOJSON AND JWT_EXTERNAL_PICOJSON)
1617
find_dependency(picojson REQUIRED)
1718
endif()
1819

0 commit comments

Comments
 (0)