From 677b0c03f8fca937f6b16da3341602637d092179 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 13 Sep 2021 18:41:59 +0200 Subject: Check availability of constructor attribute and use it on Windows in favor of DllMain --- configure.ac | 26 ++++++++++++++++++++++---- src/plist.c | 39 +++++++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/configure.ac b/configure.ac index 9300f31..0d13e04 100644 --- a/configure.ac +++ b/configure.ac @@ -55,17 +55,17 @@ AC_C_BIGENDIAN([AC_DEFINE([__BIG_ENDIAN__], [1], [big endian])], # Check for operating system -AC_MSG_CHECKING([wether we need platform-specific build settings for ${host_os}]) +AC_MSG_CHECKING([for platform-specific build settings]) case ${host_os} in *mingw32*|*cygwin*) - AC_MSG_RESULT([yes]) + AC_MSG_RESULT([${host_os}]) win32=true ;; darwin*|*android*) - AC_MSG_RESULT([no]) + AC_MSG_RESULT([${host_os}]) ;; *) - AC_MSG_RESULT([yes]) + AC_MSG_RESULT([${host_os}]) AX_PTHREAD([], [AC_MSG_ERROR([pthread is required to build libplist])]) AC_CHECK_LIB(pthread, [pthread_once], [], [AC_MSG_ERROR([pthread with pthread_once required to build libplist])]) ;; @@ -74,6 +74,24 @@ AM_CONDITIONAL(WIN32, test x$win32 = xtrue) AC_SEARCH_LIBS([fmin],[m]) +# Check if the C compiler supports __attribute__((constructor)) +AC_CACHE_CHECK([wether the C compiler supports constructor/destructor attributes], + ac_cv_attribute_constructor, [ + ac_cv_attribute_constructor=no + AC_COMPILE_IFELSE([AC_LANG_PROGRAM( + [[ + static void __attribute__((constructor)) test_constructor(void) { + } + static void __attribute__((destructor)) test_destructor(void) { + } + ]], [])], + [ac_cv_attribute_constructor=yes] + )] +) +if test "$ac_cv_attribute_constructor" = "yes"; then + AC_DEFINE(HAVE_ATTRIBUTE_CONSTRUCTOR, 1, [Define if the C compiler supports constructor/destructor attributes]) +fi + # Check if struct tm has a tm_gmtoff member AC_CACHE_CHECK(for tm_gmtoff in struct tm, ac_cv_struct_tm_gmtoff, AC_COMPILE_IFELSE([AC_LANG_PROGRAM([ diff --git a/src/plist.c b/src/plist.c index 454d08c..d0e6c77 100644 --- a/src/plist.c +++ b/src/plist.c @@ -63,7 +63,6 @@ static void internal_plist_deinit(void) } #ifdef WIN32 - typedef volatile struct { LONG lock; int state; @@ -83,7 +82,29 @@ static void thread_once(thread_once_t *once_control, void (*init_routine)(void)) } InterlockedExchange(&(once_control->lock), 0); } +#else +static pthread_once_t init_once = PTHREAD_ONCE_INIT; +static pthread_once_t deinit_once = PTHREAD_ONCE_INIT; +#define thread_once pthread_once +#endif + +#ifndef HAVE_ATTRIBUTE_CONSTRUCTOR + #if defined(__llvm__) || defined(__GNUC__) + #define HAVE_ATTRIBUTE_CONSTRUCTOR + #endif +#endif + +#ifdef HAVE_ATTRIBUTE_CONSTRUCTOR +static void __attribute__((constructor)) libplist_initialize(void) +{ + thread_once(&init_once, internal_plist_init); +} +static void __attribute__((destructor)) libplist_deinitialize(void) +{ + thread_once(&deinit_once, internal_plist_deinit); +} +#elif defined(WIN32) BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) { switch (dwReason) { @@ -98,22 +119,8 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) } return 1; } - #else - -static pthread_once_t init_once = PTHREAD_ONCE_INIT; -static pthread_once_t deinit_once = PTHREAD_ONCE_INIT; - -static void __attribute__((constructor)) libplist_initialize(void) -{ - pthread_once(&init_once, internal_plist_init); -} - -static void __attribute__((destructor)) libplist_deinitialize(void) -{ - pthread_once(&deinit_once, internal_plist_deinit); -} - +#warning No compiler support for constructor/destructor attributes, some features might not be available. #endif #ifndef HAVE_MEMMEM -- cgit v1.1-32-gdbae