Skip to content

Commit

Permalink
cmake: Fixup GEH for local setup
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Dec 25, 2024
1 parent bd34514 commit 626685d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
25 changes: 13 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,25 @@ option(WITH_TESTS "Whether to build and run the testsuite (default is ON)" ON)
# Need at least one of these
if (WITH_GNUTLS)
if (NOT GNUTLS_AUTO)
set(GNUTLS_REQUIRED "REQUIRED")
set(GNUTLS_REQUIRED REQUIRED)
endif()
pkg_check_modules(GNUTLS gnutls>=3.6.0 IMPORTED_TARGET
${GNUTLS_REQUIRED})
endif()

if (WITH_OPENSSL)
if (NOT OPENSSL_AUTO)
set(OPENSSL_REQUIRED "REQUIRED")
set(OPENSSL_REQUIRED REQUIRED)
endif()
pkg_check_modules(OPENSSL openssl>=1.1.0 IMPORTED_TARGET
${OPENSSL_REQUIRED})
endif()

add_library(jwt SHARED)
add_library(jwt_static STATIC)
set_target_properties(jwt_static PROPERTIES OUTPUT_NAME jwt)
set_target_properties(jwt_static PROPERTIES
OUTPUT_NAME jwt
COMPILE_FLAGS -DJWT_STATIC_DEFINE)

set(JWT_SOURCES libjwt/base64.c
libjwt/jwt-memory.c
Expand All @@ -70,15 +72,14 @@ set(JWT_SOURCES libjwt/base64.c
libjwt/jwt-encode.c
libjwt/jwt-verify.c)

set(COMPILER_CONSTRUCTOR "__attribute__((constructor))")
_check_c_compiler_attribute(${COMPILER_CONSTRUCTOR} COMPILER_HAS_CONSTRUCTOR)
if (COMPILER_HAS_CONSTRUCTOR)
set(JWT_CUSTOM_CONTENT "\n#define JWT_CONSTRUCTOR ${COMPILER_CONSTRUCTOR}\n")
else()
set(JWT_CUSTOM_CONTENT "\n#define JWT_CONSTRUCTOR\n")
# Allow building without deprecated functions (suggested)
option(EXCLUDE_DEPRECATED
"Exclude deprecated parts of the library (default included)" FALSE)
if (EXCLUDE_DEPRECATED)
set(NO_BUILD_DEPRECATED DEFINE_NO_DEPRECATED)
endif()
generate_export_header(jwt
CUSTOM_CONTENT_FROM_VARIABLE JWT_CUSTOM_CONTENT)

generate_export_header(jwt ${NO_BUILD_DEPRECATED})

include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/libjwt)
Expand Down Expand Up @@ -215,7 +216,7 @@ option(ENABLE_COVERAGE "Enable code coverage rules" OFF)
# Tests and coverage depend on this, but optional
if (WITH_TESTS)
if (ENABLE_COVERAGE)
set(CHECK_REQUIRED "REQUIRED")
set(CHECK_REQUIRED REQUIRED)
endif()
pkg_check_modules(CHECK check>=0.9.10 IMPORTED_TARGET ${CHECK_REQUIRED})
else()
Expand Down
58 changes: 55 additions & 3 deletions cmake/GenerateExportHeader.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,60 @@ macro(_test_compiler_has_deprecated)
endif()
endmacro()

macro(_test_compiler_has_constructor)
if(CMAKE_CXX_COMPILER_ID MATCHES Borland
OR CMAKE_CXX_COMPILER_ID MATCHES Embarcadero
OR CMAKE_CXX_COMPILER_ID MATCHES HP
OR GCC_TOO_OLD
OR CMAKE_CXX_COMPILER_ID MATCHES "^(PGI|NVHPC)$"
OR CMAKE_CXX_COMPILER_ID MATCHES Watcom)
set(COMPILER_HAS_CONSTRUCTOR "" CACHE INTERNAL
"Compiler support for a constructor attribute")
else()
if (CMAKE_CXX_COMPILER_LOADED)
_check_cxx_compiler_attribute("__attribute__((__constructor__))"
COMPILER_HAS_CONSTRUCTOR_ATTR)
if(COMPILER_HAS_CONSTRUCTOR_ATTR)
set(COMPILER_HAS_CONSTRUCTOR "${COMPILER_HAS_CONSTRUCTOR_ATTR}"
CACHE INTERNAL "Compiler support for a constructor attribute")
else()
_check_cxx_compiler_attribute("__declspec(constructor)"
COMPILER_HAS_CONSTRUCTOR)
endif()
else()
_check_c_compiler_attribute("__attribute__((__constructor__))"
COMPILER_HAS_CONSTRUCTOR_ATTR)
if(COMPILER_HAS_CONSTRUCTOR_ATTR)
set(COMPILER_HAS_CONSTRUCTOR "${COMPILER_HAS_CONSTRUCTOR_ATTR}"
CACHE INTERNAL "Compiler support for a constructor attribute")
else()
_check_c_compiler_attribute("__declspec(constructor)"
COMPILER_HAS_CONSTRUCTOR)
endif()

endif()
endif()
endmacro()

get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
"${CMAKE_CURRENT_LIST_FILE}" PATH)

macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
set(DEFINE_DEPRECATED)
set(DEFINE_CONSTRUCTOR)
set(DEFINE_EXPORT)
set(DEFINE_IMPORT)
set(DEFINE_NO_EXPORT)

if (COMPILER_HAS_DEPRECATED_ATTR AND NOT WIN32)
set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
elseif(COMPILER_HAS_DEPRECATED)
set(DEFINE_DEPRECATED "__declspec(deprecated)")
set(DEFINE_DEPRECATED "__declspec(deprecated")
endif()
if (COMPILER_HAS_CONSTRUCTOR_ATTR AND NOT WIN32)
set(DEFINE_CONSTRUCTOR "__attribute__ ((__constructor__))")
elseif(COMPILER_HAS_CONSTRUCTOR)
set(DEFINE_CONSTRUCTOR "__declspec(constructor)")
endif()

get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
Expand All @@ -317,7 +358,7 @@ macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
# Option overrides
set(options DEFINE_NO_DEPRECATED)
set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE CONSTRUCTOR_MACRO_NAME
NO_DEPRECATED_MACRO_NAME CUSTOM_CONTENT_FROM_VARIABLE INCLUDE_GUARD_NAME)
set(multiValueArgs)

Expand All @@ -338,9 +379,14 @@ macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
set(CONSTRUCTOR_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_CONSTRUCTOR")
set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
set(NO_DEPRECATED_MACRO_NAME
"${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
set(VERSION_MAJOR_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_VERSION_MAJOR")
set(VERSION_MINOR_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_VERSION_MINOR")
set(VERSION_MICRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_VERSION_MICRO")
set(VERSION_STRING_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_VERSION_STRING")

if(_GEH_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
Expand All @@ -361,6 +407,10 @@ macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
endif()
string(MAKE_C_IDENTIFIER ${DEPRECATED_MACRO_NAME} DEPRECATED_MACRO_NAME)
if(_GEH_CONSTRUCTOR_MACRO_NAME)
set(CONSTRUCTOR_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_CONSTRUCTOR_MACRO_NAME})
endif()
string(MAKE_C_IDENTIFIER ${CONSTRUCTOR_MACRO_NAME} CONSTRUCTOR_MACRO_NAME)
if(_GEH_NO_EXPORT_MACRO_NAME)
set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
endif()
Expand Down Expand Up @@ -403,7 +453,7 @@ macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
endif()
endif()

configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
configure_file("${CMAKE_SOURCE_DIR}/include/jwt_export.h.in"
"${EXPORT_FILE_NAME}" @ONLY)
endmacro()

Expand All @@ -418,6 +468,7 @@ function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
endif()
_test_compiler_hidden_visibility()
_test_compiler_has_deprecated()
_test_compiler_has_constructor()
_do_set_macro_values(${TARGET_LIBRARY})
_do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
endfunction()
Expand All @@ -429,6 +480,7 @@ function(add_compiler_export_flags)

_test_compiler_hidden_visibility()
_test_compiler_has_deprecated()
_test_compiler_has_constructor()

option(USE_COMPILER_HIDDEN_VISIBILITY
"Use HIDDEN visibility support if available." ON)
Expand Down
4 changes: 4 additions & 0 deletions cmake/LibJWTVersions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ set(LIBJWT_SO_CRA 13 1 11)
# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html

string(TOLOWER ${LIBJWT_PROJECT} LIBJWT_PROJECT_LOWER)
list(GET LIBJWT_VERSION_SET 0 DEFINE_MAJOR)
list(GET LIBJWT_VERSION_SET 1 DEFINE_MINOR)
list(GET LIBJWT_VERSION_SET 1 DEFINE_MICRO)
string(JOIN "." LIBJWT_VERSION ${LIBJWT_VERSION_SET})
set(DEFINE_VERSION "\"${LIBJWT_VERSION}\"")

list(GET LIBJWT_SO_CRA 0 LIBJWT_SO_CURRENT)
list(GET LIBJWT_SO_CRA 1 LIBJWT_SO_REVISION)
Expand Down
14 changes: 13 additions & 1 deletion cmake/exportheader.cmake.in → include/jwt_export.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@
#ifndef @INCLUDE_GUARD_NAME@
#define @INCLUDE_GUARD_NAME@

/* Version macros for LibJWT */
#define @VERSION_MAJOR_NAME@ @DEFINE_MAJOR@
#define @VERSION_MINOR_NAME@ @DEFINE_MINOR@
#define @VERSION_MICRO_NAME@ @DEFINE_MICRO@

#define @VERSION_STRING_NAME@ @DEFINE_VERSION@

#ifdef @STATIC_DEFINE@
# define @EXPORT_MACRO_NAME@
# define @NO_EXPORT_MACRO_NAME@
# define @CONSTRUCTOR_MACRO_NAME@
#else
# ifndef @EXPORT_MACRO_NAME@
# ifdef @EXPORT_IMPORT_CONDITION@
Expand All @@ -28,6 +36,10 @@
# endif
#endif

#ifndef @CONSTRUCTOR_MACRO_NAME@
# define @CONSTRUCTOR_MACRO_NAME@ @DEFINE_CONSTRUCTOR@
#endif

#ifndef @DEPRECATED_MACRO_NAME@
# define @DEPRECATED_MACRO_NAME@ @DEFINE_DEPRECATED@
#endif
Expand All @@ -46,5 +58,5 @@
# define @NO_DEPRECATED_MACRO_NAME@
# endif
#endif
@CUSTOM_CONTENT@

#endif /* @INCLUDE_GUARD_NAME@ */

0 comments on commit 626685d

Please sign in to comment.