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 ============================================================================
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 ============================================================================
14 if test -z "$_MOZ_USE_RTTI"; then
15 if test "$GNU_CC"; then
16 CXXFLAGS="$CXXFLAGS -fno-rtti"
20 CXXFLAGS="$CXXFLAGS -GR-"
26 dnl ========================================================
28 dnl = Debugging Options
30 dnl ========================================================
31 AC_DEFUN([MOZ_DEBUGGING_OPTS],
34 if test -z "$MOZ_DEBUG" -o -n "$MOZ_ASAN"; then
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])
44 CFLAGS="$CFLAGS $MOZ_DEBUG_FLAGS"
45 AC_TRY_COMPILE([#include <stdio.h>],
46 [printf("Hello World\n");],
49 AC_MSG_RESULT([$_results])
50 if test "$_results" = "no"; then
51 AC_MSG_ERROR([These compiler flags are invalid: $MOZ_DEBUG_FLAGS])
58 dnl A high level macro for selecting compiler options.
59 AC_DEFUN([MOZ_COMPILER_OPTS],
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"
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
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
91 moz_cv_opt_ld_supports_icf=no
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"
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"
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
116 cat > conftest.$ac_ext <<EOF
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
124 if test -s conftest.err
126 elif grep -q -E 'NEEDED.*lib(std)?c\+\+' conftest.out
128 else moz_cv_opt_static_libstdcxx=yes
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 ========================================================
145 if test -n "$LIBFUZZER"; then
146 case "$LIBFUZZER_FLAGS" in
147 *-fsanitize-coverage*|*-fsanitize=fuzzer*)
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
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
168 moz_cv_opt_gc_sections_breaks_debug_ranges=yes
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"
175 if test "$moz_cv_opt_gc_sections_breaks_debug_ranges" = no; then
176 DSO_LDOPTS="$DSO_LDOPTS -Wl,--gc-sections"
179 DSO_LDOPTS="$DSO_LDOPTS -Wl,--gc-sections"
183 if test "$GNU_CC$CLANG_CC"; then
184 case "${OS_TARGET}" in
186 # It's the default on those targets, and clang complains about -pie
187 # being unused if passed.
190 MOZ_PROGRAM_LDFLAGS="$MOZ_PROGRAM_LDFLAGS -pie"
195 AC_SUBST(MOZ_PROGRAM_LDFLAGS)