Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / build / autoconf / compiler-opts.m4
blob68960acbc136059894a8aa87e90be9ad931b3777
1 dnl This Source Code Form is subject to the terms of the Mozilla Public
2 dnl License, v. 2.0. If a copy of the MPL was not distributed with this
3 dnl file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 dnl Add compiler specific options
7 dnl ============================================================================
8 dnl C++ rtti
9 dnl We don't use it in the code, but it can be usefull for debugging, so give
10 dnl the user the option of enabling it.
11 dnl ============================================================================
12 AC_DEFUN([MOZ_RTTI],
14 if test -z "$_MOZ_USE_RTTI"; then
15     if test "$GNU_CC"; then
16         CXXFLAGS="$CXXFLAGS -fno-rtti"
17     else
18         case "$target" in
19         *-mingw*)
20             CXXFLAGS="$CXXFLAGS -GR-"
21         esac
22     fi
26 dnl ========================================================
27 dnl =
28 dnl = Debugging Options
29 dnl =
30 dnl ========================================================
31 AC_DEFUN([MOZ_DEBUGGING_OPTS],
34 if test -z "$MOZ_DEBUG" -o -n "$MOZ_ASAN"; then
35     MOZ_NO_DEBUG_RTL=1
38 AC_SUBST(MOZ_NO_DEBUG_RTL)
40 if test -n "$MOZ_DEBUG"; then
41     if test -n "$COMPILE_ENVIRONMENT"; then
42         AC_MSG_CHECKING([for valid debug flags])
43         _SAVE_CFLAGS=$CFLAGS
44         CFLAGS="$CFLAGS $MOZ_DEBUG_FLAGS"
45         AC_TRY_COMPILE([#include <stdio.h>],
46             [printf("Hello World\n");],
47             _results=yes,
48             _results=no)
49         AC_MSG_RESULT([$_results])
50         if test "$_results" = "no"; then
51             AC_MSG_ERROR([These compiler flags are invalid: $MOZ_DEBUG_FLAGS])
52         fi
53         CFLAGS=$_SAVE_CFLAGS
54     fi
58 dnl A high level macro for selecting compiler options.
59 AC_DEFUN([MOZ_COMPILER_OPTS],
61   MOZ_DEBUGGING_OPTS
62   MOZ_RTTI
64 if test "$GNU_CC"; then
65     if test -z "$DEVELOPER_OPTIONS"; then
66         CFLAGS="$CFLAGS -ffunction-sections -fdata-sections"
67         CXXFLAGS="$CXXFLAGS -ffunction-sections -fdata-sections"
68     fi
70     CFLAGS="$CFLAGS -fno-math-errno"
71     CXXFLAGS="$CXXFLAGS -fno-exceptions -fno-math-errno"
74 dnl ========================================================
75 dnl = Identical Code Folding
76 dnl ========================================================
78 if test "$GNU_CC" -a "$GCC_USE_GNU_LD" -a -z "$MOZ_DISABLE_ICF" -a -z "$DEVELOPER_OPTIONS"; then
79     AC_CACHE_CHECK([whether the linker supports Identical Code Folding],
80         moz_cv_opt_ld_supports_icf,
81         [echo 'int foo() {return 42;}' \
82               'int bar() {return 42;}' \
83               'int main() {return foo() - bar();}' > conftest.${ac_ext}
84         # If the linker supports ICF, foo and bar symbols will have
85         # the same address
86         if AC_TRY_COMMAND([${CC-cc} -o conftest${ac_exeext} $LDFLAGS -Wl,--icf=safe -ffunction-sections conftest.${ac_ext} $LIBS 1>&2]) &&
87            test -s conftest${ac_exeext} &&
88            $LLVM_OBJDUMP -t conftest${ac_exeext} | awk changequote(<<, >>)'{a[<<$>>6] = <<$>>1} END {if (a["foo"] && (a["foo"] != a["bar"])) { exit 1 }}'changequote([, ]); then
89             moz_cv_opt_ld_supports_icf=yes
90         else
91             moz_cv_opt_ld_supports_icf=no
92         fi
93         rm -rf conftest*])
94     if test "$moz_cv_opt_ld_supports_icf" = yes; then
95         _SAVE_LDFLAGS="$LDFLAGS -Wl,--icf=safe"
96         LDFLAGS="$LDFLAGS -Wl,--icf=safe -Wl,--print-icf-sections"
97         AC_TRY_LINK([], [],
98                     [LD_PRINT_ICF_SECTIONS=-Wl,--print-icf-sections],
99                     [LD_PRINT_ICF_SECTIONS=])
100         AC_SUBST([LD_PRINT_ICF_SECTIONS])
101         LDFLAGS="$_SAVE_LDFLAGS"
102     fi
105 dnl ========================================================
106 dnl = Detect static linkage of libstdc++
107 dnl ========================================================
109 if test "$OS_TARGET" = Linux; then
111 AC_CACHE_CHECK([whether we're trying to statically link with libstdc++],
112     moz_cv_opt_static_libstdcxx,
113     [moz_cv_opt_static_libstdcxx=no
114      AC_LANG_SAVE
115      AC_LANG_CPLUSPLUS
116      cat > conftest.$ac_ext <<EOF
117 #include <iostream>
118 int main() { std::cout << 1; }
120      dnl This test is quite conservative: it assumes dynamic linkage if the compilation step fails or if
121      dnl the binary format is not supported. But it still detects basic issues.
122      if AC_TRY_EVAL([ac_link]) && test -s conftest${ac_exeext} && $LLVM_OBJDUMP --private-headers conftest${ac_exeext} 2> conftest.err 1> conftest.out
123      then
124          if test -s conftest.err
125          then :
126          elif grep -q -E 'NEEDED.*lib(std)?c\+\+' conftest.out
127          then :
128          else moz_cv_opt_static_libstdcxx=yes
129          fi
130      fi
131      AC_LANG_RESTORE
132      rm -f conftest*
134 if test "$moz_cv_opt_static_libstdcxx" = "yes"; then
135     AC_MSG_ERROR([Firefox does not support linking statically with libstdc++])
140 dnl ========================================================
141 dnl = Automatically remove dead symbols
142 dnl ========================================================
144 SANCOV=
145 if test -n "$LIBFUZZER"; then
146     case "$LIBFUZZER_FLAGS" in
147     *-fsanitize-coverage*|*-fsanitize=fuzzer*)
148         SANCOV=1
149         ;;
150     esac
153 if test "$GNU_CC" -a "$GCC_USE_GNU_LD" -a -z "$DEVELOPER_OPTIONS" -a -z "$MOZ_PROFILE_GENERATE" -a -z "$SANCOV"; then
154     if test -n "$MOZ_DEBUG_FLAGS"; then
155         dnl See bug 670659
156         AC_CACHE_CHECK([whether removing dead symbols breaks debugging],
157             moz_cv_opt_gc_sections_breaks_debug_ranges,
158             [echo 'int foo() {return 42;}' \
159                   'int bar() {return 1;}' \
160                   'int main() {return foo();}' > conftest.${ac_ext}
161             if AC_TRY_COMMAND([${CC-cc} -o conftest.${ac_objext} $CFLAGS $MOZ_DEBUG_FLAGS -c conftest.${ac_ext} 1>&2]) &&
162                 AC_TRY_COMMAND([${CC-cc} -o conftest${ac_exeext} $LDFLAGS $MOZ_DEBUG_FLAGS -Wl,--gc-sections conftest.${ac_objext} $LIBS 1>&2]) &&
163                 test -s conftest${ac_exeext} -a -s conftest.${ac_objext}; then
164                  if test "`$PYTHON3 -m mozbuild.configure.check_debug_ranges conftest.${ac_objext} conftest.${ac_ext}`" = \
165                          "`$PYTHON3 -m mozbuild.configure.check_debug_ranges conftest${ac_exeext} conftest.${ac_ext}`"; then
166                      moz_cv_opt_gc_sections_breaks_debug_ranges=no
167                  else
168                      moz_cv_opt_gc_sections_breaks_debug_ranges=yes
169                  fi
170              else
171                   dnl We really don't expect to get here, but just in case
172                   moz_cv_opt_gc_sections_breaks_debug_ranges="no, but it's broken in some other way"
173              fi
174              rm -rf conftest*])
175          if test "$moz_cv_opt_gc_sections_breaks_debug_ranges" = no; then
176              DSO_LDOPTS="$DSO_LDOPTS -Wl,--gc-sections"
177          fi
178     else
179         DSO_LDOPTS="$DSO_LDOPTS -Wl,--gc-sections"
180     fi
183 if test "$GNU_CC$CLANG_CC"; then
184     case "${OS_TARGET}" in
185     Darwin|WASI)
186         # It's the default on those targets, and clang complains about -pie
187         # being unused if passed.
188         ;;
189     *)
190         MOZ_PROGRAM_LDFLAGS="$MOZ_PROGRAM_LDFLAGS -pie"
191         ;;
192     esac
195 AC_SUBST(MOZ_PROGRAM_LDFLAGS)