1 # ============================================================================
2 # https://www.gnu.org/software/autoconf-archive/ax_compile_check_sizeof.html
3 # ============================================================================
7 # AX_COMPILE_CHECK_SIZEOF(TYPE [, HEADERS [, EXTRA_SIZES...]])
11 # This macro checks for the size of TYPE using compile checks, not run
12 # checks. You can supply extra HEADERS to look into. the check will cycle
13 # through 1 2 4 8 16 and any EXTRA_SIZES the user supplies. If a match is
14 # found, it will #define SIZEOF_`TYPE' to that value. Otherwise it will
15 # emit a configure time error indicating the size of the type could not be
18 # The trick is that C will not allow duplicate case labels. While this is
21 # switch (0) case 0: case 1:;
23 # The following is not:
25 # switch (0) case 0: case 0:;
27 # Thus, the AC_COMPILE_IFELSE will fail if the currently tried size does
30 # Here is an example skeleton configure.in script, demonstrating the
34 # AC_CHECK_HEADERS(stddef.h unistd.h)
36 # AC_CHECK_TYPE(ssize_t, int)
38 # headers='#ifdef HAVE_STDDEF_H
41 # #ifdef HAVE_UNISTD_H
46 # AX_COMPILE_CHECK_SIZEOF(char)
47 # AX_COMPILE_CHECK_SIZEOF(short)
48 # AX_COMPILE_CHECK_SIZEOF(int)
49 # AX_COMPILE_CHECK_SIZEOF(long)
50 # AX_COMPILE_CHECK_SIZEOF(unsigned char *)
51 # AX_COMPILE_CHECK_SIZEOF(void *)
52 # AX_COMPILE_CHECK_SIZEOF(size_t, $headers)
53 # AX_COMPILE_CHECK_SIZEOF(ssize_t, $headers)
54 # AX_COMPILE_CHECK_SIZEOF(ptrdiff_t, $headers)
55 # AX_COMPILE_CHECK_SIZEOF(off_t, $headers)
59 # Copyright (c) 2008 Kaveh Ghazi <ghazi@caip.rutgers.edu>
60 # Copyright (c) 2017 Reini Urban <rurban@cpan.org>
62 # This program is free software: you can redistribute it and/or modify it
63 # under the terms of the GNU General Public License as published by the
64 # Free Software Foundation, either version 3 of the License, or (at your
65 # option) any later version.
67 # This program is distributed in the hope that it will be useful, but
68 # WITHOUT ANY WARRANTY; without even the implied warranty of
69 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
70 # Public License for more details.
72 # You should have received a copy of the GNU General Public License along
73 # with this program. If not, see <https://www.gnu.org/licenses/>.
75 # As a special exception, the respective Autoconf Macro's copyright owner
76 # gives unlimited permission to copy, distribute and modify the configure
77 # scripts that are the output of Autoconf when processing the Macro. You
78 # need not follow the terms of the GNU General Public License when using
79 # or distributing such scripts, even though portions of the text of the
80 # Macro appear in them. The GNU General Public License (GPL) does govern
81 # all other use of the material that constitutes the Autoconf Macro.
83 # This special exception to the GPL applies to versions of the Autoconf
84 # Macro released by the Autoconf Archive. When you make and distribute a
85 # modified version of the Autoconf Macro, you may extend this special
86 # exception to the GPL to apply to your modified version as well.
90 AU_ALIAS([AC_COMPILE_CHECK_SIZEOF], [AX_COMPILE_CHECK_SIZEOF])
91 AC_DEFUN([AX_COMPILE_CHECK_SIZEOF],
92 [changequote(<<, >>)dnl
93 dnl The name to #define.
94 define(<<AC_TYPE_NAME>>, translit(sizeof_$1, [a-z *], [A-Z_P]))dnl
95 dnl The cache variable name.
96 define(<<AC_CV_NAME>>, translit(ac_cv_sizeof_$1, [ *], [_p]))dnl
98 AC_MSG_CHECKING(size of $1)
99 AC_CACHE_VAL(AC_CV_NAME,
100 [for ac_size in 4 8 1 2 16 $3 ; do # List sizes in rough order of prevalence.
101 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
102 #include <sys/types.h>
104 ]], [[switch (0) case 0: case (sizeof ($1) == $ac_size):;]])], [AC_CV_NAME=$ac_size])
105 if test x$AC_CV_NAME != x ; then break; fi
108 if test x$AC_CV_NAME = x ; then
109 AC_MSG_ERROR([cannot determine a size for $1])
111 AC_MSG_RESULT($AC_CV_NAME)
112 AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME, [The number of bytes in type $1])
113 undefine([AC_TYPE_NAME])dnl
114 undefine([AC_CV_NAME])dnl