Skip to content

Commit

Permalink
build: Add checks for compiler supporting constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Dec 23, 2024
1 parent 8254046 commit 59531a0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 2 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
3 changes: 3 additions & 0 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -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)=

Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 2 additions & 0 deletions include/jwt_export.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libjwt/jwt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions tests/jwt_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ 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");

ck_assert_str_eq(msg, "NONEXISTENT");
}
END_TEST
#endif

START_TEST(test_alloc_funcs)
{
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/jwt_new.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

#include "jwt_tests.h"

#ifdef JWT_CONSTRUCTOR
START_TEST(test_jwt_crypto_ops)
{
const char *msg = getenv("JWT_CRYPTO");

ck_assert_str_eq(msg, "openssl");
}
END_TEST
#endif

START_TEST(test_jwt_new)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 59531a0

Please sign in to comment.