diff --git a/CMakeLists.txt b/CMakeLists.txt index e66cc364..97eb7da3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,7 +60,15 @@ set_target_properties(jwt_static PROPERTIES OUTPUT_NAME jwt) target_sources(jwt PRIVATE libjwt/jwt.c libjwt/jwks.c libjwt/base64.c) -generate_export_header(jwt) +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") +endif() +generate_export_header(jwt + CUSTOM_CONTENT_FROM_VARIABLE JWT_CUSTOM_CONTENT) include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}) diff --git a/Doxyfile b/Doxyfile index bd729f3a..6867da64 100644 --- a/Doxyfile +++ b/Doxyfile @@ -2397,8 +2397,11 @@ INCLUDE_FILE_PATTERNS = PREDEFINED = _DOXYGEN \ JWT_EXPORT= \ + JWT_NO_EXPORT= \ JWT_DEPRECATED= \ JWT_DEPRECATED_EXPORT= \ + JWT_DEPRECATED_NO_EXPORT= \ + JWT_CONSTRUCTOR= \ __attribute__(x)= \ __declspec(x)= diff --git a/configure.ac b/configure.ac index 09581c67..076f8c4b 100644 --- a/configure.ac +++ b/configure.ac @@ -117,6 +117,7 @@ dnl AX_GCC_FUNC_ATTRIBUTE([deprecated]) dnl AX_GCC_FUNC_ATTRIBUTE([visibility]) AC_SUBST([GCC_FUNC_DEPRECATED],["__attribute__((__deprecated__))"]) AC_SUBST([GCC_EXPORT],["__attribute__((visibility(\"default\")))"]) +AC_SUBST([GCC_CONSTRUCTOR],["__attribute__((constructor))"]) dnl Valgrind defaults AX_VALGRIND_DFLT([memcheck], [on]) diff --git a/include/jwt_export.h.in b/include/jwt_export.h.in index 89c2ceaf..c967382d 100644 --- a/include/jwt_export.h.in +++ b/include/jwt_export.h.in @@ -11,9 +11,11 @@ # define JWT_EXPORT __declspec(dllimport) # endif # endif +# define JWT_CONSTRUCTOR #else # define JWT_DEPRECATED @GCC_FUNC_DEPRECATED@ # define JWT_EXPORT @GCC_EXPORT@ +# define JWT_CONSTRUCTOR @GCC_CONSTRUCTOR@ #endif #ifndef JWT_DEPRECATED_EXPORT diff --git a/libjwt/jwt.c b/libjwt/jwt.c index c9c3dfe2..18c0d64c 100644 --- a/libjwt/jwt.c +++ b/libjwt/jwt.c @@ -95,7 +95,7 @@ int jwt_crypto_ops_supports_jwk(void) return jwt_ops->jwk_implemented ? 1 : 0; } -__attribute__((constructor)) +JWT_CONSTRUCTOR void jwt_init() { const char *opname = getenv("JWT_CRYPTO"); diff --git a/tests/jwt_dump.c b/tests/jwt_dump.c index 5441467d..23be5d22 100644 --- a/tests/jwt_dump.c +++ b/tests/jwt_dump.c @@ -28,6 +28,7 @@ static int test_set_alloc(void) return jwt_set_alloc(test_malloc, test_realloc, test_free); } +#ifdef JWT_CONSTRUCTOR START_TEST(test_jwt_crypto_ops) { const char *msg = getenv("JWT_CRYPTO"); @@ -35,6 +36,7 @@ START_TEST(test_jwt_crypto_ops) ck_assert_str_eq(msg, "NONEXISTENT"); } END_TEST +#endif START_TEST(test_alloc_funcs) { @@ -374,7 +376,9 @@ static Suite *libjwt_suite(const char *title) tc_core = tcase_create("jwt_dump"); +#ifdef JWT_CONSTRUCTOR tcase_add_test(tc_core, test_jwt_crypto_ops); +#endif tcase_add_loop_test(tc_core, test_alloc_funcs, 0, i); tcase_add_loop_test(tc_core, test_jwt_dump_fp, 0, i); tcase_add_loop_test(tc_core, test_jwt_dump_str, 0, i); diff --git a/tests/jwt_new.c b/tests/jwt_new.c index ba6381ba..295d8426 100644 --- a/tests/jwt_new.c +++ b/tests/jwt_new.c @@ -8,6 +8,7 @@ #include "jwt_tests.h" +#ifdef JWT_CONSTRUCTOR START_TEST(test_jwt_crypto_ops) { const char *msg = getenv("JWT_CRYPTO"); @@ -15,6 +16,7 @@ START_TEST(test_jwt_crypto_ops) ck_assert_str_eq(msg, "openssl"); } END_TEST +#endif START_TEST(test_jwt_new) { @@ -615,7 +617,9 @@ static Suite *libjwt_suite(const char *title) tc_core = tcase_create("jwt_new"); +#ifdef JWT_CONSTRUCTOR tcase_add_test(tc_core, test_jwt_crypto_ops); +#endif tcase_add_loop_test(tc_core, test_jwt_new, 0, i); tcase_add_loop_test(tc_core, test_jwt_dup, 0, i); tcase_add_loop_test(tc_core, test_jwt_dup_signed, 0, i);