From 45654cb51499a7762fe4a65aa5a4b1696120ec89 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Mon, 24 May 2021 15:34:59 +0200 Subject: [PATCH] Build: Make the JWT backend selectable Introduce the -DZM_JWT_BACKEND CMake option through which the JWT backend can be selected. Supported values: jwt_cpp (default; in-tree) and libjwt When libjwt is chosen ZM_CRYPTO_BACKEND is taken into account to select the corresponding flavour of libjwt. --- CMakeLists.txt | 27 +++++++--- cmake/Modules/FindLibJWT.cmake | 91 ++++++++++++++++++++++++++++------ src/CMakeLists.txt | 11 +++- 3 files changed, 105 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ba783857..36e821a54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,15 @@ if(NOT ZM_CRYPTO_BACKEND IN_LIST ZM_CRYPTO_BACKEND_OPTIONS) message(FATAL_ERROR "Invalid value for ZM_CRYPTO_BACKEND. Possible options: ${ZM_CRYPTO_BACKEND_OPTIONS}") endif() +# Supported JWT backends. Using jwt-cpp as default. +set(ZM_JWT_BACKEND_OPTIONS libjwt jwt_cpp) +set(ZM_JWT_BACKEND jwt_cpp CACHE STRING "Determines which JWT backend should be used.") +set_property(CACHE ZM_JWT_BACKEND PROPERTY STRINGS ${ZM_JWT_BACKEND_OPTIONS}) + +if(NOT ZM_JWT_BACKEND IN_LIST ZM_JWT_BACKEND_OPTIONS) + message(FATAL_ERROR "Invalid value for ZM_JWT_BACKEND. Possible options: ${ZM_JWT_BACKEND_OPTIONS}") +endif() + # Reassign some variables if a target distro has been specified if((ZM_TARGET_DISTRO MATCHES "^el") OR (ZM_TARGET_DISTRO MATCHES "^fc")) set(ZM_RUNDIR "/var/run/zoneminder") @@ -339,14 +348,15 @@ else() "ZoneMinder requires jpeg but it was not found on your system") endif() -# LIBJWT -find_package(LibJWT) -if(LIBJWT_FOUND) - set(HAVE_LIBJWT 1) - set(optlibsfound "${optlibsfound} LIBJWT") - list(APPEND ZM_BIN_LIBS "${LIBJWT_LIBRARY}") -else() - set(optlibsnotfound "${optlibsnotfound} LIBJWT") +# libjwt +if (${ZM_JWT_BACKEND} STREQUAL "libjwt") + find_package(LibJWT REQUIRED COMPONENTS ${ZM_CRYPTO_BACKEND}) + if(LIBJWT_FOUND) + set(HAVE_LIBJWT 1) + set(optlibsfound "${optlibsfound} LIBJWT") + else() + set(optlibsnotfound "${optlibsnotfound} LIBJWT") + endif() endif() # GnuTLS @@ -842,6 +852,7 @@ message(STATUS "Optional libraries found:${optlibsfound}") message(STATUS "Optional libraries not found:${optlibsnotfound}") message(STATUS "Enabled crypto backend: ${ZM_CRYPTO_BACKEND}") +message(STATUS "Enabled JWT backend: ${ZM_JWT_BACKEND}") # Run ZM configuration generator message(STATUS "Running ZoneMinder configuration generator") diff --git a/cmake/Modules/FindLibJWT.cmake b/cmake/Modules/FindLibJWT.cmake index e0c834609..95a3a91f2 100644 --- a/cmake/Modules/FindLibJWT.cmake +++ b/cmake/Modules/FindLibJWT.cmake @@ -1,28 +1,89 @@ -include(FindPackageHandleStandardArgs) +#[=======================================================================[.rst: +FindLibJWT +---------- +Find the JWT C Library (libjwt) + + +This module accepts optional COMPONENTS to select the crypto backend (these are mutually exclusive):: + + openssl (default) + gnutls + +IMPORTED Targets +^^^^^^^^^^^^^^^^ + +This module defines the following :prop_tgt:`IMPORTED` targets: + +``JWT::libjwt`` + The JWT library, if found with the specified crypto backend. + +Result Variables +^^^^^^^^^^^^^^^^ + +``LIBJWT_FOUND`` + System has libjwt +``LIBJWT_INCLUDE_DIR`` + The libjwt include directory +``LIBJWT_LIBRARIES`` + The libraries needed to use libjwt +#]=======================================================================] + +include(FindPackageHandleStandardArgs) find_package(PkgConfig QUIET) + +if(LibJWT_FIND_COMPONENTS) + set(LIBJWT_CRYPTO_BACKEND "") + foreach(component IN LISTS LibJWT_FIND_COMPONENTS) + if(component MATCHES "^(openssl|gnutls)") + if(LIBJWT_CRYPTO_BACKEND) + message(FATAL_ERROR "LibJWT: Only one crypto library can be selected.") + endif() + set(LIBJWT_CRYPTO_BACKEND ${component}) + else() + message(FATAL_ERROR "LibJWT: Wrong crypto backend specified.") + endif() + endforeach() +else() + set(LIBJWT_CRYPTO_BACKEND "openssl") +endif() + +set(LIBJWT_LIB_NAMES "") +if(LIBJWT_CRYPTO_BACKEND STREQUAL "openssl") + set(LIBJWT_LIB_NAMES "jwt" "libjwt") +elseif(LIBJWT_CRYPTO_BACKEND STREQUAL "gnutls") + set(LIBJWT_LIB_NAMES "jwt-gnutls" "libjwt-gnutls") +endif() + pkg_check_modules(PC_LIBJWT QUIET libjwt) find_path(LIBJWT_INCLUDE_DIR NAMES jwt.h - HINTS ${PC_LIBJWT_INCLUDEDIR} ${PC_LIBJWT_INCLUDE_DIRS} - ) + HINTS + ${PC_LIBJWT_INCLUDEDIR} + ${PC_LIBJWT_INCLUDE_DIRS}) +mark_as_advanced(LIBJWT_INCLUDE_DIR) find_library(LIBJWT_LIBRARY - NAMES jwt-gnutls libjwt-gnutls liblibjwt-gnutls - HINTS ${PC_LIBJWT_LIBDIR} ${PC_LIBJWT_LIBRARY_DIR} - ) + NAMES ${LIBJWT_LIB_NAMES} + HINTS + ${PC_LIBJWT_LIBDIR} + ${PC_LIBJWT_LIBRARY_DIR}) +mark_as_advanced(LIBJWT_LIBRARY) find_package_handle_standard_args(LibJWT - REQUIRED_VARS LIBJWT_INCLUDE_DIR LIBJWT_LIBRARY - ) + REQUIRED_VARS + LIBJWT_INCLUDE_DIR + LIBJWT_LIBRARY + FAIL_MESSAGE + "Could NOT find LibJWT with the crypto backend ${LIBJWT_CRYPTO_BACKEND}.") if(LIBJWT_FOUND) - add_library(libjwt STATIC IMPORTED GLOBAL) - set_target_properties(libjwt PROPERTIES - IMPORTED_LOCATION "${LIBJWT_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${LIBJWT_INCLUDE_DIR}" - ) -endif() + set(LIBJWT_LIBRARIES ${LIBJWT_LIBRARY}) + set(LIBJWT_INCLUDE_DIRS ${LIBJWT_INCLUDE_DIR}) -mark_as_advanced(LIBJWT_INCLUDE_DIR LIBJWT_LIBRARY) \ No newline at end of file + add_library(JWT::libjwt UNKNOWN IMPORTED) + set_target_properties(JWT::libjwt PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${LIBJWT_INCLUDE_DIRS}" + IMPORTED_LOCATION "${LIBJWT_LIBRARY}") +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fc01af04d..1eb24a7fc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -78,12 +78,21 @@ target_include_directories(zm target_link_libraries(zm PUBLIC libbcrypt::bcrypt - jwt-cpp::jwt-cpp RtspServer::RtspServer martinmoene::span-lite PRIVATE zm-core-interface) +if(${ZM_JWT_BACKEND} STREQUAL "jwt_cpp") + target_link_libraries(zm + PUBLIC + jwt-cpp::jwt-cpp) +elseif(${ZM_JWT_BACKEND} STREQUAL "libjwt") + target_link_libraries(zm + PUBLIC + JWT::libjwt) +endif() + add_executable(zmc zmc.cpp) add_executable(zms zms.cpp) add_executable(zmu zmu.cpp)