]> git.ipfire.org Git - thirdparty/squid.git/blob - acinclude/compiler-flags.m4
a9bd7ffc670a229e32757eba31b5afb6f19b80bf
[thirdparty/squid.git] / acinclude / compiler-flags.m4
1 ## Copyright (C) 1996-2023 The Squid Software Foundation and contributors
2 ##
3 ## Squid software is distributed under GPLv2+ license and includes
4 ## contributions from numerous individuals and organizations.
5 ## Please see the COPYING and CONTRIBUTORS files for details.
6 ##
7
8 # check if the compiler accepts a supplied flag
9 # first argument is the variable containing the result
10 # (will be set to "yes" or "no")
11 # second argument is the flag to be tested, verbatim
12 #
13 AC_DEFUN([SQUID_CC_CHECK_ARGUMENT],[
14 AC_CACHE_CHECK([whether compiler accepts $2],[$1],
15 [
16 AC_REQUIRE([AC_PROG_CC])
17 SQUID_STATE_SAVE([ARGCHECK])
18 CFLAGS="$CFLAGS $2"
19 CXXFLAGS="$CXXFLAGS $2"
20 AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],[$1=yes],[$1=no])
21 SQUID_STATE_ROLLBACK([ARGCHECK])
22 ])
23 ])
24
25 # Check if the compiler requires a supplied flag to build a test program.
26 # When cross-compiling set flags explicitly.
27 #
28 # first argument is the variable containing the result
29 # (will be set to "yes" or "no")
30 # second argument is the flag to be tested, verbatim
31 # third is the #include and global setup for test program, verbatim
32 # fourth is the test program to compile, verbatim
33 #
34 AC_DEFUN([SQUID_CC_REQUIRE_ARGUMENT],[
35 AC_CACHE_CHECK([whether compiler requires $2],[$1],
36 [{
37 AC_REQUIRE([AC_PROG_CC])
38 SQUID_STATE_SAVE([ARGREQ])
39 AC_COMPILE_IFELSE([AC_LANG_PROGRAM($3,$4)],[$1=no],[],[$1=no])
40 AS_IF([test "x$$1" != "xno"],[
41 CFLAGS="$CFLAGS $2"
42 CXXFLAGS="$CXXFLAGS $2"
43 AC_COMPILE_IFELSE([AC_LANG_PROGRAM($3,$4)],[$1=yes],[$1=no],[$1=no])
44 ])
45 SQUID_STATE_ROLLBACK([ARGREQ])
46 }])
47 ])
48
49 # detect what kind of compiler we're using, either by using hints from
50 # autoconf itself, or by using predefined preprocessor macros
51 # sets the variable squid_cv_compiler to one of
52 # - gcc
53 # - sunstudio
54 # - none (undetected)
55 #
56 AC_DEFUN([SQUID_CC_GUESS_VARIANT], [
57 AC_CACHE_CHECK([what kind of compiler we're using],[squid_cv_compiler],
58 [
59 AC_REQUIRE([AC_PROG_CC])
60 dnl repeat the next block for each compiler, changing the
61 dnl preprocessor definition so that it depends on platform-specific
62 dnl predefined macros
63 dnl SunPro CC
64 AS_IF([test -z "$squid_cv_compiler"],[
65 AC_COMPILE_IFELSE([
66 AC_LANG_PROGRAM([[
67 #if !defined(__SUNPRO_C) && !defined(__SUNPRO_CC)
68 #error "not sunpro c"
69 #endif
70 ]])],[squid_cv_compiler="sunstudio"],[])
71 ])
72 dnl Intel CC
73 AS_IF([test -z "$squid_cv_compiler"],[
74 AC_COMPILE_IFELSE([
75 AC_LANG_PROGRAM([[
76 #if !defined(__ICC)
77 #error "not Intel(R) C++ Compiler"
78 #endif
79 ]])],[squid_cv_compiler="icc"],[])
80 ])
81 dnl clang
82 AS_IF([test -z "$squid_cv_compiler"],[
83 AC_COMPILE_IFELSE([
84 AC_LANG_PROGRAM([[
85 #if !defined(__clang__)
86 #error "not clang"
87 #endif
88 ]])],[squid_cv_compiler="clang"],[])
89 ])
90 dnl microsoft visual c++
91 AS_IF([test -z "$squid_cv_compiler"],[
92 AC_COMPILE_IFELSE([
93 AC_LANG_PROGRAM([[
94 #if !defined(_MSC_VER)
95 #error "not Microsoft VC++"
96 #endif
97 ]])],[squid_cv_compiler="msvc"],[])
98 ])
99 dnl gcc. MUST BE LAST as many other compilers also define it for compatibility
100 AS_IF([test -z "$squid_cv_compiler"],[
101 AC_COMPILE_IFELSE([
102 AC_LANG_PROGRAM([[
103 #if !defined(__GNUC__)
104 #error "not gcc"
105 #endif
106 ]])],[squid_cv_compiler="gcc"],[])
107 ])
108 dnl end of block to be repeated
109 AS_IF([test -z "$squid_cv_compiler"],[squid_cv_compiler="none"])
110 ]) dnl AC_CACHE_CHECK
111 ]) dnl AC_DEFUN
112
113 dnl SQUID_CC_ADD_CXXFLAG_WARNING_IF_SUPPORTED helper
114 dnl $1 is a compiler warning option (e.g., -Wall).
115 dnl $2 is a "squid_cv_cc_arg<snake_case_warning_name_equivalent>" string.
116 AC_DEFUN([SQUID_CC_ADD_CXXFLAG_WARNING_IF_SUPPORTED_],[
117 AC_REQUIRE([SQUID_CC_GUESS_VARIANT])
118 SQUID_STATE_SAVE([CXXARGTEST])
119 CXXFLAGS="$CXXFLAGS $SQUID_CXXFLAGS"
120 AS_CASE([$squid_cv_compiler],
121 [gcc],[
122 # Testing with -Werror -Wfoobar does not work well because GCC ignores
123 # unsupported _negative_ options, so we test with -Werror=foobar instead
124 # (where "foobar" is a name of a warning that may be given to us in
125 # positive -Wfoobar or negative -Wno-foobar form).
126 SQUID_CC_CHECK_ARGUMENT([$2],m4_bpatsubst([$1],[^-W],[-Werror=]))
127 ],
128 [clang],[
129 # Testing with -Werror=foobar (as we do for GCC above) is useless
130 # because clang does not recognize that pattern as a -Werror
131 # specialization, so we test with -Werror -Wfoobar instead.
132 SQUID_CC_CHECK_ARGUMENT([$2],[-Werror $1])
133 ],
134 [
135 # We lack code to reliably test whether this compiler supports a given
136 # warning. Some compilers (e.g, icc) succeed with bogus warning names.
137 # If $squid_cv_cxx_option_werror is set, we add that option because it
138 # helps in some (but not all) known cases.
139 SQUID_CC_CHECK_ARGUMENT([$2],[$squid_cv_cxx_option_werror $1])
140 ]
141 )
142 SQUID_STATE_ROLLBACK([CXXARGTEST])
143 AS_IF([test "x${$2}" = "xyes"],[SQUID_CXXFLAGS="$SQUID_CXXFLAGS $1"])
144 ])
145
146 dnl The argument is a compiler warning option (e.g. -Wall). If linking a
147 dnl warning-free program while using the given warning succeeds, then the
148 dnl option is added to SQUID_CXXFLAGS in the same order as calls to the macro.
149 AC_DEFUN([SQUID_CC_ADD_CXXFLAG_WARNING_IF_SUPPORTED],[
150 SQUID_CC_ADD_CXXFLAG_WARNING_IF_SUPPORTED_($1,m4_bpatsubst(m4_tolower([squid_cv_cc_arg$1]),[[^a-zA-Z0-9_]],[_]))
151 ])
152
153 # define the flag to use to have the compiler treat warnings as errors
154 # requirs SQUID_CC_GUESS_VARIANT
155 # Sets a few variables to contain some compiler-dependent command line
156 # options, or to empty strings if the compiler doesn't support those
157 # options
158 # They are (with their GCC equivalent):
159 # squid_cv_cc_option_werror (-Werror)
160 # squid_cv_cxx_option_werror (-Werror)
161 # squid_cv_cc_option_wall (-Wall)
162 # squid_cv_cc_option_optimize (-O3)
163 #
164 AC_DEFUN([SQUID_CC_GUESS_OPTIONS], [
165 AC_REQUIRE([SQUID_CC_GUESS_VARIANT])
166 AC_MSG_CHECKING([for compiler variant])
167 AS_CASE([$squid_cv_compiler],
168 [gcc],[
169 squid_cv_cc_option_werror="-Werror"
170 squid_cv_cxx_option_werror="-Werror"
171 squid_cv_cc_option_wall="-Wall"
172 squid_cv_cc_option_optimize="-O3"
173 squid_cv_cc_arg_pipe="-pipe"
174 ],
175 [sunstudio],[
176 squid_cv_cc_option_werror="-errwarn=%all -errtags"
177 squid_cv_cxx_option_werror="-errwarn=%all,no%badargtype2w,no%wbadinit,no%wbadasg -errtags"
178 squid_cv_cc_option_wall="+w"
179 squid_cv_cc_option_optimize="-fast"
180 squid_cv_cc_arg_pipe=""
181 ],
182 [clang],[
183 squid_cv_cxx_option_werror="-Werror"
184 squid_cv_cc_option_werror="$squid_cv_cxx_option_werror"
185 squid_cv_cc_option_wall="-Wall"
186 squid_cv_cc_option_optimize="-O2"
187 squid_cv_cc_arg_pipe=""
188 ],
189 [icc],[
190 squid_cv_cxx_option_werror="-Werror"
191 squid_cv_cc_option_werror="$squid_cv_cxx_option_werror"
192 squid_cv_cc_option_wall="-Wall"
193 squid_cv_cc_option_optimize="-O2"
194 squid_cv_cc_arg_pipe=""
195 ],
196 [
197 squid_cv_cxx_option_werror=""
198 squid_cv_cc_option_werror=""
199 squid_cv_cc_option_wall=""
200 squid_cv_cc_option_optimize="-O"
201 squid_cv_cc_arg_pipe=""
202 ]
203 )
204 AC_MSG_RESULT([$squid_cv_compiler])
205 ])