Skip to content

Commit

Permalink
Check availability of constructor attribute and use it on Windows in …
Browse files Browse the repository at this point in the history
…favor of DllMain
  • Loading branch information
nikias committed Sep 13, 2021
1 parent 9035527 commit 042c1a2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
18 changes: 18 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ case ${host_os} in
esac
AM_CONDITIONAL(WIN32, test x$win32 = xtrue)

# 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

AC_CHECK_MEMBER(struct dirent.d_type, AC_DEFINE(HAVE_DIRENT_D_TYPE, 1, [define if struct dirent has member d_type]),, [#include <dirent.h>])

AS_COMPILER_FLAGS(GLOBAL_CFLAGS, "-Wall -Wextra -Wmissing-declarations -Wredundant-decls -Wshadow -Wpointer-arith -Wwrite-strings -Wswitch-default -Wno-unused-parameter -fsigned-char -fvisibility=hidden")
Expand Down
28 changes: 18 additions & 10 deletions src/glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,23 @@ static void internal_glue_deinit(void)
static thread_once_t init_once = THREAD_ONCE_INIT;
static thread_once_t deinit_once = THREAD_ONCE_INIT;

#ifdef WIN32
#ifndef HAVE_ATTRIBUTE_CONSTRUCTOR
#if defined(__llvm__) || defined(__GNUC__)
#define HAVE_ATTRIBUTE_CONSTRUCTOR
#endif
#endif

#ifdef HAVE_ATTRIBUTE_CONSTRUCTOR
static void __attribute__((constructor)) limd_glue_initialize(void)
{
thread_once(&init_once, internal_glue_init);
}

static void __attribute__((destructor)) limd_glue_deinitialize(void)
{
thread_once(&deinit_once, internal_glue_deinit);
}
#elif defined(WIN32)
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason) {
Expand All @@ -60,13 +76,5 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
return 1;
}
#else
static void __attribute__((constructor)) limd_glue_initialize(void)
{
thread_once(&init_once, internal_glue_init);
}

static void __attribute__((destructor)) limd_glue_deinitialize(void)
{
thread_once(&deinit_once, internal_glue_deinit);
}
#warning No compiler support for constructor/destructor attributes, some features might not be available.
#endif

0 comments on commit 042c1a2

Please sign in to comment.