1 # wm_library_constructors.m4 - Macros to see if compiler supports attributes "constructors" for libraries
3 # Copyright (c) 2021 Christophe CURIS
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 # WM_LIBRARY_CONSTRUCTORS
21 # -----------------------
23 # GCC introduced the attribute 'constructor' which says that a function must be
24 # called when a library is loaded (needed for C++ support). However this may not
25 # be totally portable, so we may use the old method "_init" as a fall-back
26 # solution, which is considered Obsolete/Dangerous.
28 # This is explained here:
29 # https://tldp.org/HOWTO/Program-Library-HOWTO/miscellaneous.html#INIT-AND-CLEANUP
31 # This macro is making sure that the compiler supports the attribute, because we
32 # need it for the WRaster library (see RStartup in wrlib/misc.c to see why).
33 AC_DEFUN_ONCE([WM_LIBRARY_CONSTRUCTORS],
34 [AC_CACHE_CHECK([how to declare a library constructor], [wm_cv_library_constructors],
35 [save_CFLAGS="$CFLAGS"
36 dnl We need this to cause a detectable failure in case of unsupported attribute name
37 dnl if we don't do this, we just get a warning and AC_COMPILE suppose it was ok.
38 CFLAGS="$CFLAGS -Werror"
41 static int is_initialised = 0;
43 void __attribute__ ((constructor)) special_function(void)
48 int main(int argc, char **argv)
50 /* To avoid warning on unused parameters, they could cause a false failure */
53 return (is_initialised)?0:1;
56 [wm_cv_library_constructors=attribute],
57 [wm_cv_library_constructors=legacy])
58 CFLAGS="$save_CFLAGS"])
59 dnl In a perfect world we should also make sure that the feature works, but that implies
60 dnl to be able to actually build and run a program, which is not compatible with a
61 dnl cross-compiling user setup
62 AS_IF([test "X$wm_cv_library_constructors" = "Xattribute"],
63 [AC_DEFINE([WLIB_CONSTRUCTOR(func_name)], [__attribute__ ((constructor)) func_name],
64 [Method for defining a library initialisation function])],
65 [AC_DEFINE([WLIB_CONSTRUCTOR(func_name)], [_init],
66 [Method for defining a library initialisation function])] )