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], [DPKG_COMPILER_CFLAGS])
16 m4_pattern_allow([DPKG_COMPILER_CFLAGS])
17 AS_VAR_PUSHDEF([dpkg_varname_cache], [dpkg_cv_cflags_$1])
20 m4_define([dpkg_compiler], [$CXX])
21 m4_define([dpkg_varname], [CXXFLAGS])
22 m4_define([dpkg_varname_save], [dpkg_save_CXXFLAGS])
23 m4_define([dpkg_varname_export], [DPKG_COMPILER_CXXFLAGS])
24 m4_pattern_allow([DPKG_COMPILER_CXXFLAGS])
25 AS_VAR_PUSHDEF([dpkg_varname_cache], [dpkg_cv_cxxflags_$1])
27 AC_CACHE_CHECK([whether ]dpkg_compiler[ accepts $1], [dpkg_varname_cache], [
28 AS_VAR_COPY([dpkg_varname_save], [dpkg_varname])
29 AS_VAR_SET([dpkg_varname], ["-Werror dpkg_check_flag"])
33 AS_VAR_SET([dpkg_varname_cache], [yes])
35 AS_VAR_SET([dpkg_varname_cache], [no])
37 AS_VAR_COPY([dpkg_varname], [dpkg_varname_save])
39 AS_VAR_IF([dpkg_varname_cache], [yes], [
40 AS_VAR_APPEND([dpkg_varname_export], [" $1"])
42 AS_VAR_POPDEF([dpkg_varname_cache])
45 # DPKG_CHECK_COMPILER_WARNINGS
46 # ----------------------------
47 # Add configure option to disable additional compiler warnings.
48 AC_DEFUN([DPKG_CHECK_COMPILER_WARNINGS], [
49 DPKG_CHECK_COMPILER_FLAG([-Wall])
50 DPKG_CHECK_COMPILER_FLAG([-Wextra])
52 DPKG_CHECK_COMPILER_FLAG([-Walloca])
53 DPKG_CHECK_COMPILER_FLAG([-Walloc-zero])
54 DPKG_CHECK_COMPILER_FLAG([-Warray-bounds-pointer-arithmetic])
55 DPKG_CHECK_COMPILER_FLAG([-Wassign-enum])
56 DPKG_CHECK_COMPILER_FLAG([-Wbitfield-enum-conversion])
57 DPKG_CHECK_COMPILER_FLAG([-Wcast-align])
58 DPKG_CHECK_COMPILER_FLAG([-Wconditional-uninitialized])
59 DPKG_CHECK_COMPILER_FLAG([-Wdate-time])
60 DPKG_CHECK_COMPILER_FLAG([-Wdocumentation])
61 DPKG_CHECK_COMPILER_FLAG([-Wduplicate-enum])
62 DPKG_CHECK_COMPILER_FLAG([-Wduplicated-branches])
63 DPKG_CHECK_COMPILER_FLAG([-Wduplicated-cond])
64 DPKG_CHECK_COMPILER_FLAG([-Wextra-semi])
65 DPKG_CHECK_COMPILER_FLAG([-Wflexible-array-extensions])
66 DPKG_CHECK_COMPILER_FLAG([-Wfloat-conversion])
67 DPKG_CHECK_COMPILER_FLAG([-Wfloat-equal])
68 DPKG_CHECK_COMPILER_FLAG([-Wformat -Wformat-security])
69 DPKG_CHECK_COMPILER_FLAG([-Wformat=2])
70 DPKG_CHECK_COMPILER_FLAG([-Winit-self])
71 DPKG_CHECK_COMPILER_FLAG([-Wlogical-not-parentheses])
72 DPKG_CHECK_COMPILER_FLAG([-Wlogical-op])
73 DPKG_CHECK_COMPILER_FLAG([-Wmissing-declarations])
74 DPKG_CHECK_COMPILER_FLAG([-Wmissing-format-attribute])
75 DPKG_CHECK_COMPILER_FLAG([-Wmissing-variable-declarations])
76 DPKG_CHECK_COMPILER_FLAG([-Wnewline-eof])
77 DPKG_CHECK_COMPILER_FLAG([-Wno-missing-field-initializers])
78 DPKG_CHECK_COMPILER_FLAG([-Wno-nonnull-compare])
79 DPKG_CHECK_COMPILER_FLAG([-Wno-tautological-constant-out-of-range-compare])
80 DPKG_CHECK_COMPILER_FLAG([-Wno-unused-parameter])
81 DPKG_CHECK_COMPILER_FLAG([-Wnull-dereference])
82 DPKG_CHECK_COMPILER_FLAG([-Wpointer-arith])
83 DPKG_CHECK_COMPILER_FLAG([-Wredundant-decls])
84 DPKG_CHECK_COMPILER_FLAG([-Wregister])
85 DPKG_CHECK_COMPILER_FLAG([-Wrestrict])
86 DPKG_CHECK_COMPILER_FLAG([-Wshadow])
87 DPKG_CHECK_COMPILER_FLAG([-Wshift-negative-value])
88 DPKG_CHECK_COMPILER_FLAG([-Wshift-overflow=2])
89 DPKG_CHECK_COMPILER_FLAG([-Wshift-sign-overflow])
90 DPKG_CHECK_COMPILER_FLAG([-Wsizeof-array-argument])
91 DPKG_CHECK_COMPILER_FLAG([-Wstrict-overflow=2])
92 DPKG_CHECK_COMPILER_FLAG([-Wswitch-bool])
93 DPKG_CHECK_COMPILER_FLAG([-Wvla])
94 DPKG_CHECK_COMPILER_FLAG([-Wwrite-strings])
95 DPKG_CHECK_COMPILER_FLAG([-Wxor-used-as-pow])
99 DPKG_CHECK_COMPILER_FLAG([-Wbad-function-cast])
100 DPKG_CHECK_COMPILER_FLAG([-Wc99-c11-compat])
101 DPKG_CHECK_COMPILER_FLAG([-Wc99-compat])
102 DPKG_CHECK_COMPILER_FLAG([-Wc11-extensions])
103 DPKG_CHECK_COMPILER_FLAG([-Wc2x-compat])
104 DPKG_CHECK_COMPILER_FLAG([-Wc2x-extensions])
105 DPKG_CHECK_COMPILER_FLAG([-Wpre-c2x-compat])
106 DPKG_CHECK_COMPILER_FLAG([-Wdeclaration-after-statement])
107 DPKG_CHECK_COMPILER_FLAG([-Wenum-int-mismatch])
108 DPKG_CHECK_COMPILER_FLAG([-Wmissing-prototypes])
109 DPKG_CHECK_COMPILER_FLAG([-Wnested-externs])
110 DPKG_CHECK_COMPILER_FLAG([-Wold-style-definition])
111 DPKG_CHECK_COMPILER_FLAG([-Wstrict-prototypes])
114 DPKG_CHECK_COMPILER_FLAG([-Wc++11-compat])
115 DPKG_CHECK_COMPILER_FLAG([-Wc++11-compat-pedantic])
116 DPKG_CHECK_COMPILER_FLAG([-Wc++11-extensions])
117 DPKG_CHECK_COMPILER_FLAG([-Wcast-qual])
118 DPKG_CHECK_COMPILER_FLAG([-Wold-style-cast])
119 AS_IF([test "x$dpkg_cv_cxx11" = "xyes"], [
120 DPKG_CHECK_COMPILER_FLAG([-Wzero-as-null-pointer-constant])
125 # DPKG_COMPILER_WARNINGS
126 # ----------------------
127 # Add configure option to disable additional compiler warnings.
128 AC_DEFUN([DPKG_COMPILER_WARNINGS], [
129 AC_ARG_ENABLE([compiler-warnings],
130 [AS_HELP_STRING([--disable-compiler-warnings],
131 [Disable (detected) additional compiler warnings])],
132 [], [enable_compiler_warnings=yes])
134 AS_IF([test "x$enable_compiler_warnings" = "xyes"], [
135 DPKG_CHECK_COMPILER_WARNINGS
137 DPKG_CHECK_COMPILER_WARNINGS
140 CFLAGS="$DPKG_COMPILER_CFLAGS $CFLAGS"
141 CXXFLAGS="$DPKG_COMPILER_CXXFLAGS $CXXFLAGS"
145 # DPKG_CHECK_COMPILER_SANITIZER
146 # -----------------------------
147 # Check whether the compiler sanitizer options are supported.
148 AC_DEFUN([DPKG_CHECK_COMPILER_SANITIZER], [
149 DPKG_CHECK_COMPILER_FLAG([-fno-omit-frame-pointer])
150 DPKG_CHECK_COMPILER_FLAG([-fsanitize=address])
151 DPKG_CHECK_COMPILER_FLAG([-fsanitize=undefined])
154 # DPKG_COMPILER_SANITIZER
155 # -----------------------
156 # Add configure option to enable compiler sanitizer support options.
157 AC_DEFUN([DPKG_COMPILER_SANITIZER], [
158 AC_ARG_ENABLE([compiler-sanitizer],
159 [AS_HELP_STRING([--enable-compiler-sanitizer],
160 [Enable compiler sanitizer support])],
161 [], [enable_compiler_sanitizer=no])
163 AS_IF([test "x$enable_compiler_sanitizer" = "xyes"], [
164 DPKG_CHECK_COMPILER_SANITIZER
166 DPKG_CHECK_COMPILER_SANITIZER
169 LDFLAGS="$DPKG_COMPILER_CFLAGS $LDFLAGS"
170 CFLAGS="$DPKG_COMPILER_CFLAGS $CFLAGS"
171 CXXFLAGS="$DPKG_COMPILER_CXXFLAGS $CXXFLAGS"
175 # DPKG_COMPILER_ANALYZER
176 # ----------------------
177 # Add configure option to enable compiler analyzer support options.
178 # Note: This is only intended for development use, as enabling this option
179 # unconditionally can generate large amounts of false positives that
180 # require sentient triage intervention.
181 AC_DEFUN([DPKG_COMPILER_ANALYZER], [
182 AC_ARG_ENABLE([compiler-analyzer],
183 [AS_HELP_STRING([--enable-compiler-analyzer],
184 [Enable compiler analyzer support])],
185 [], [enable_compiler_analyzer=no])
187 AS_IF([test "x$enable_compiler_analyzer" = "xyes"], [
188 DPKG_CHECK_COMPILER_FLAG([-fanalyzer])
190 DPKG_CHECK_COMPILER_FLAG([-fanalyzer])
193 LDFLAGS="$COMPILER_CFLAGS $LDFLAGS"
194 CFLAGS="$COMPILER_CFLAGS $CFLAGS"
195 CXXFLAGS="$COMPILER_CXXFLAGS $CXXFLAGS"
199 # DPKG_COMPILER_OPTIMIZATIONS
200 # ---------------------------
201 # Add configure option to disable optimizations.
202 AC_DEFUN([DPKG_COMPILER_OPTIMIZATIONS], [
203 AC_ARG_ENABLE([compiler-optimizations],
204 [AS_HELP_STRING([--disable-compiler-optimizations],
205 [Disable (detected) compiler optimizations])],
206 [], [enable_compiler_optimizations=yes])
208 AS_IF([test "x$enable_compiler_optimizations" = "xno"], [
209 CFLAGS=$(echo "$CFLAGS" | $SED -e "s/ -O[[1-9]]*\b/ -O0/g")
213 # DPKG_TRY_C99([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
214 # ------------------------------------------------------
215 # Try compiling some C99 code to see whether it works
216 AC_DEFUN([DPKG_TRY_C99], [
219 #include <inttypes.h>
223 /* Variadic macro arguments. */
224 #define variadic_macro(foo, ...) printf(foo, __VA_ARGS__)
228 /* Designated initializers. */
229 struct { int a, b; } foo = { .a = 1, .b = 2 };
231 /* Compound literals. */
232 struct point { int x, y; } p = (struct point){ .x = 0, .y = 1 };
233 p = (struct point){ .x = 2, .y = 4 };
235 /* Trailing comma in enum. */
236 enum { FIRST, SECOND, } quux;
241 /* Specific size type. */
243 size_t size = SIZE_MAX;
244 intmax_t imax = INTMAX_MAX;
246 /* Format modifiers. */
247 rc = printf("%jd", imax);
250 rc = printf("%zu", size);
254 /* Magic __func__ variable. */
255 printf("%s", __func__);
262 # Check whether the compiler can do C99
263 AC_DEFUN([DPKG_C_C99], [
264 AC_CACHE_CHECK([whether $CC supports C99 features], [dpkg_cv_c99], [
265 DPKG_TRY_C99([dpkg_cv_c99=yes], [dpkg_cv_c99=no])
267 AS_IF([test "x$dpkg_cv_c99" != "xyes"], [
268 AC_CACHE_CHECK([for $CC option to accept C99 features], [dpkg_cv_c99_arg], [
271 for arg in "-std=gnu99" "-std=c99" "-c99" "-AC99" "-xc99=all" \
272 "-qlanglvl=extc99"; do
273 CC="$dpkg_save_CC $arg"
274 DPKG_TRY_C99([dpkg_arg_worked=yes], [dpkg_arg_worked=no])
277 AS_IF([test "x$dpkg_arg_worked" = "xyes"], [
278 dpkg_cv_c99_arg="$arg"
283 AS_IF([test "x$dpkg_cv_c99_arg" != "xnone"], [
284 CC="$CC $dpkg_cv_c99_arg"
288 AS_IF([test "x$dpkg_cv_c99" = "xyes"], [
289 AC_DEFINE([HAVE_C99], 1, [Define to 1 if the compiler supports C99.])
291 AC_MSG_ERROR([unsupported required C99 extensions])
295 # DPKG_TRY_CXX11([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
297 # Try compiling some C++11 code to see whether it works.
298 AC_DEFUN([DPKG_TRY_CXX11], [
303 // Null pointer keyword.
307 AC_LANG_POP([C++])dnl
312 # Check whether the compiler can do C++11.
313 AC_DEFUN([DPKG_CXX_CXX11], [
314 AC_CACHE_CHECK([whether $CXX supports C++11], [dpkg_cv_cxx11], [
315 DPKG_TRY_CXX11([dpkg_cv_cxx11=yes], [dpkg_cv_cxx11=no])
317 AS_IF([test "x$dpkg_cv_cxx11" != "xyes"], [
318 AC_CACHE_CHECK([for $CXX option to accept C++11], [dpkg_cv_cxx11_arg], [
319 dpkg_cv_cxx11_arg=none
321 for arg in "-std=gnu++11" "-std=c++11"; do
322 CXX="$dpkg_save_CXX $arg"
323 DPKG_TRY_CXX11([dpkg_arg_worked=yes], [dpkg_arg_worked=no])
326 AS_IF([test "x$dpkg_arg_worked" = "xyes"], [
327 dpkg_cv_cxx11_arg="$arg"; break
331 AS_IF([test "x$dpkg_cv_cxx11_arg" != "xnone"], [
332 CXX="$CXX $dpkg_cv_cxx11_arg"
336 AS_IF([test "x$dpkg_cv_cxx11" = "xyes"], [
337 AC_DEFINE([HAVE_CXX11], 1, [Define to 1 if the compiler supports C++11.])