2 # Copyright © 2004 Scott James Remnant <scott@netsplit.com>
3 # Copyright © 2006, 2009-2011, 2013-2016 Guillem Jover <guillem@debian.org>
5 # DPKG_CHECK_COMPILER_FLAG
6 # ------------------------
7 AC_DEFUN([DPKG_CHECK_COMPILER_FLAG], [
8 m4_define([dpkg_check_flag], m4_bpatsubst([$1], [^-Wno-], [-W]))
12 m4_define([dpkg_compiler], [$CC])
13 m4_define([dpkg_varname], [CFLAGS])
14 m4_define([dpkg_varname_save], [dpkg_save_CFLAGS])
15 m4_define([dpkg_varname_export], [COMPILER_CFLAGS])
16 AS_VAR_PUSHDEF([dpkg_varname_cache], [dpkg_cv_cflags_$1])
19 m4_define([dpkg_compiler], [$CXX])
20 m4_define([dpkg_varname], [CXXFLAGS])
21 m4_define([dpkg_varname_save], [dpkg_save_CXXFLAGS])
22 m4_define([dpkg_varname_export], [COMPILER_CXXFLAGS])
23 AS_VAR_PUSHDEF([dpkg_varname_cache], [dpkg_cv_cxxflags_$1])
25 AC_CACHE_CHECK([whether ]dpkg_compiler[ accepts $1], [dpkg_varname_cache], [
26 AS_VAR_COPY([dpkg_varname_save], [dpkg_varname])
27 AS_VAR_SET([dpkg_varname], ["-Werror dpkg_check_flag"])
31 AS_VAR_SET([dpkg_varname_cache], [yes])
33 AS_VAR_SET([dpkg_varname_cache], [no])
35 AS_VAR_COPY([dpkg_varname], [dpkg_varname_save])
37 AS_VAR_IF([dpkg_varname_cache], [yes], [
38 AS_VAR_APPEND([dpkg_varname_export], [" $1"])
40 AS_VAR_POPDEF([dpkg_varname_cache])
43 # DPKG_CHECK_COMPILER_WARNINGS
44 # ----------------------------
45 # Add configure option to disable additional compiler warnings.
46 AC_DEFUN([DPKG_CHECK_COMPILER_WARNINGS], [
47 DPKG_CHECK_COMPILER_FLAG([-Wall])
48 DPKG_CHECK_COMPILER_FLAG([-Wextra])
50 DPKG_CHECK_COMPILER_FLAG([-Wcast-align])
51 DPKG_CHECK_COMPILER_FLAG([-Wdocumentation])
52 DPKG_CHECK_COMPILER_FLAG([-Wduplicated-branches])
53 DPKG_CHECK_COMPILER_FLAG([-Wduplicated-cond])
54 DPKG_CHECK_COMPILER_FLAG([-Wformat -Wformat-security])
55 DPKG_CHECK_COMPILER_FLAG([-Wformat=2])
56 DPKG_CHECK_COMPILER_FLAG([-Winit-self])
57 DPKG_CHECK_COMPILER_FLAG([-Wlogical-not-parentheses])
58 DPKG_CHECK_COMPILER_FLAG([-Wlogical-op])
59 DPKG_CHECK_COMPILER_FLAG([-Wmissing-declarations])
60 DPKG_CHECK_COMPILER_FLAG([-Wmissing-format-attribute])
61 DPKG_CHECK_COMPILER_FLAG([-Wno-missing-field-initializers])
62 DPKG_CHECK_COMPILER_FLAG([-Wno-nonnull-compare])
63 DPKG_CHECK_COMPILER_FLAG([-Wno-tautological-constant-out-of-range-compare])
64 DPKG_CHECK_COMPILER_FLAG([-Wno-unused-parameter])
65 DPKG_CHECK_COMPILER_FLAG([-Wnull-dereference])
66 DPKG_CHECK_COMPILER_FLAG([-Wpointer-arith])
67 DPKG_CHECK_COMPILER_FLAG([-Wredundant-decls])
68 DPKG_CHECK_COMPILER_FLAG([-Wregister])
69 DPKG_CHECK_COMPILER_FLAG([-Wrestrict])
70 DPKG_CHECK_COMPILER_FLAG([-Wshadow])
71 DPKG_CHECK_COMPILER_FLAG([-Wshift-negative-value])
72 DPKG_CHECK_COMPILER_FLAG([-Wsizeof-array-argument])
73 DPKG_CHECK_COMPILER_FLAG([-Wswitch-bool])
74 DPKG_CHECK_COMPILER_FLAG([-Wvla])
75 DPKG_CHECK_COMPILER_FLAG([-Wwrite-strings])
79 DPKG_CHECK_COMPILER_FLAG([-Wbad-function-cast])
80 DPKG_CHECK_COMPILER_FLAG([-Wc99-c11-compat])
81 DPKG_CHECK_COMPILER_FLAG([-Wdeclaration-after-statement])
82 DPKG_CHECK_COMPILER_FLAG([-Wmissing-prototypes])
83 DPKG_CHECK_COMPILER_FLAG([-Wnested-externs])
84 DPKG_CHECK_COMPILER_FLAG([-Wold-style-definition])
85 DPKG_CHECK_COMPILER_FLAG([-Wstrict-prototypes])
88 DPKG_CHECK_COMPILER_FLAG([-Wc++11-compat])
89 DPKG_CHECK_COMPILER_FLAG([-Wcast-qual])
90 DPKG_CHECK_COMPILER_FLAG([-Wold-style-cast])
91 AS_IF([test "x$dpkg_cv_cxx11" = "xyes"], [
92 DPKG_CHECK_COMPILER_FLAG([-Wzero-as-null-pointer-constant])
97 # DPKG_COMPILER_WARNINGS
98 # ----------------------
99 # Add configure option to disable additional compiler warnings.
100 AC_DEFUN([DPKG_COMPILER_WARNINGS], [
101 AC_ARG_ENABLE([compiler-warnings],
102 [AS_HELP_STRING([--disable-compiler-warnings],
103 [Disable (detected) additional compiler warnings])],
104 [], [enable_compiler_warnings=yes])
106 AS_IF([test "x$enable_compiler_warnings" = "xyes"], [
107 DPKG_CHECK_COMPILER_WARNINGS
109 DPKG_CHECK_COMPILER_WARNINGS
112 CFLAGS="$COMPILER_CFLAGS $CFLAGS"
113 CXXFLAGS="$COMPILER_CXXFLAGS $CXXFLAGS"
117 # DPKG_COMPILER_OPTIMIZATIONS
118 # ---------------------------
119 # Add configure option to disable optimizations.
120 AC_DEFUN([DPKG_COMPILER_OPTIMIZATIONS], [
121 AC_ARG_ENABLE([compiler-optimizations],
122 [AS_HELP_STRING([--disable-compiler-optimizations],
123 [Disable (detected) compiler optimizations])],
124 [], [enable_compiler_optimizations=yes])
126 AS_IF([test "x$enable_compiler_optimizations" = "xno"], [
127 CFLAGS=$(echo "$CFLAGS" | $SED -e "s/ -O[[1-9]]*\b/ -O0/g")
131 # DPKG_TRY_C99([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
132 # ------------------------------------------------------
133 # Try compiling some C99 code to see whether it works
134 AC_DEFUN([DPKG_TRY_C99], [
137 #include <inttypes.h>
141 /* Variadic macro arguments. */
142 #define variadic_macro(foo, ...) printf(foo, __VA_ARGS__)
146 /* Designated initializers. */
147 struct { int a, b; } foo = { .a = 1, .b = 2 };
149 /* Compound literals. */
150 struct point { int x, y; } p = (struct point){ .x = 0, .y = 1 };
151 p = (struct point){ .x = 2, .y = 4 };
153 /* Trailing comma in enum. */
154 enum { FIRST, SECOND, } quux;
159 /* Specific size type. */
161 size_t size = SIZE_MAX;
162 intmax_t imax = INTMAX_MAX;
164 /* Format modifiers. */
165 rc = printf("%jd", imax);
168 rc = printf("%zu", size);
172 /* Magic __func__ variable. */
173 printf("%s", __func__);
180 # Check whether the compiler can do C99
181 AC_DEFUN([DPKG_C_C99], [
182 AC_CACHE_CHECK([whether $CC supports C99 features], [dpkg_cv_c99], [
183 DPKG_TRY_C99([dpkg_cv_c99=yes], [dpkg_cv_c99=no])
185 AS_IF([test "x$dpkg_cv_c99" != "xyes"], [
186 AC_CACHE_CHECK([for $CC option to accept C99 features], [dpkg_cv_c99_arg], [
189 for arg in "-std=gnu99" "-std=c99" "-c99" "-AC99" "-xc99=all" \
190 "-qlanglvl=extc99"; do
191 CC="$dpkg_save_CC $arg"
192 DPKG_TRY_C99([dpkg_arg_worked=yes], [dpkg_arg_worked=no])
195 AS_IF([test "x$dpkg_arg_worked" = "xyes"], [
196 dpkg_cv_c99_arg="$arg"
201 AS_IF([test "x$dpkg_cv_c99_arg" != "xnone"], [
202 CC="$CC $dpkg_cv_c99_arg"
206 AS_IF([test "x$dpkg_cv_c99" = "xyes"], [
207 AC_DEFINE([HAVE_C99], 1, [Define to 1 if the compiler supports C99.])
209 AC_MSG_ERROR([unsupported required C99 extensions])
213 # DPKG_TRY_CXX11([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
215 # Try compiling some C++11 code to see whether it works.
216 AC_DEFUN([DPKG_TRY_CXX11], [
221 // Null pointer keyword.
225 AC_LANG_POP([C++])dnl
230 # Check whether the compiler can do C++11.
231 AC_DEFUN([DPKG_CXX_CXX11], [
232 AC_CACHE_CHECK([whether $CXX supports C++11], [dpkg_cv_cxx11], [
233 DPKG_TRY_CXX11([dpkg_cv_cxx11=yes], [dpkg_cv_cxx11=no])
235 AS_IF([test "x$dpkg_cv_cxx11" != "xyes"], [
236 AC_CACHE_CHECK([for $CXX option to accept C++11], [dpkg_cv_cxx11_arg], [
237 dpkg_cv_cxx11_arg=none
239 for arg in "-std=gnu++11" "-std=c++11"; do
240 CXX="$dpkg_save_CXX $arg"
241 DPKG_TRY_CXX11([dpkg_arg_worked=yes], [dpkg_arg_worked=no])
244 AS_IF([test "x$dpkg_arg_worked" = "xyes"], [
245 dpkg_cv_cxx11_arg="$arg"; break
249 AS_IF([test "x$dpkg_cv_cxx11_arg" != "xnone"], [
250 CXX="$CXX $dpkg_cv_cxx11_arg"
254 AS_IF([test "x$dpkg_cv_cxx11" = "xyes"], [
255 AC_DEFINE([HAVE_CXX11], 1, [Define to 1 if the compiler supports C++11.])