1 ## Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
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
13 AC_DEFUN([SQUID_CC_CHECK_ARGUMENT],[
14 AC_CACHE_CHECK([whether compiler accepts $2],[$1],
16 AC_REQUIRE([AC_PROG_CC])
17 SQUID_STATE_SAVE([ARGCHECK])
19 CXXFLAGS="$CXXFLAGS $2"
20 AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],[$1=yes],[$1=no])
21 SQUID_STATE_ROLLBACK([ARGCHECK])
25 # Check if the compiler requires a supplied flag to build a test program.
26 # When cross-compiling set flags explicitly.
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
34 AC_DEFUN([SQUID_CC_REQUIRE_ARGUMENT],[
35 AC_CACHE_CHECK([whether compiler requires $2],[$1],
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"],[
42 CXXFLAGS="$CXXFLAGS $2"
43 AC_COMPILE_IFELSE([AC_LANG_PROGRAM($3,$4)],[$1=yes],[$1=no],[$1=no])
45 SQUID_STATE_ROLLBACK([ARGREQ])
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
56 AC_DEFUN([SQUID_CC_GUESS_VARIANT], [
57 AC_CACHE_CHECK([what kind of compiler we're using],[squid_cv_compiler],
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
64 AS_IF([test -z "$squid_cv_compiler"],[
67 #if !defined(__SUNPRO_C) && !defined(__SUNPRO_CC)
70 ]])],[squid_cv_compiler="sunstudio"],[])
73 AS_IF([test -z "$squid_cv_compiler"],[
77 #error "not Intel(R) C++ Compiler"
79 ]])],[squid_cv_compiler="icc"],[])
82 AS_IF([test -z "$squid_cv_compiler"],[
85 #if !defined(__clang__)
88 ]])],[squid_cv_compiler="clang"],[])
90 dnl microsoft visual c++
91 AS_IF([test -z "$squid_cv_compiler"],[
94 #if !defined(_MSC_VER)
95 #error "not Microsoft VC++"
97 ]])],[squid_cv_compiler="msvc"],[])
99 dnl gcc. MUST BE LAST as many other compilers also define it for compatibility
100 AS_IF([test -z "$squid_cv_compiler"],[
103 #if !defined(__GNUC__)
106 ]])],[squid_cv_compiler="gcc"],[])
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
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],
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=]))
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])
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])
142 SQUID_STATE_ROLLBACK([CXXARGTEST])
143 AS_IF([test "x${$2}" = "xyes"],[SQUID_CXXFLAGS="$SQUID_CXXFLAGS $1"])
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_]],[_]))
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
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)
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],
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"
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=""
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=""
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=""
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=""
204 AC_MSG_RESULT([$squid_cv_compiler])