Skip to content

Commit 03da4c7

Browse files
theunifanquake
andcommitted
build: make linker checks more robust
Check for a flag to turn linker warnings into errors. When flags are passed to linkers via the compiler driver using a -Wl,-foo flag, linker warnings may be swallowed rather than bubbling up. Co-authored-by: fanquake <[email protected]>
1 parent 551dc7f commit 03da4c7

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

configure.ac

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,30 @@ AC_ARG_ENABLE([werror],
283283
[enable_werror=no])
284284

285285
AC_LANG_PUSH([C++])
286+
287+
dnl Check for a flag to turn compiler warnings into errors. This is helpful for checks which may
288+
dnl appear to succeed because by default they merely emit warnings when they fail.
289+
dnl
290+
dnl Note that this is not necessarily a check to see if -Werror is supported, but rather to see if
291+
dnl a compile with -Werror can succeed. This is important because the compiler may already be
292+
dnl warning about something unrelated, for example about some path issue. If that is the case,
293+
dnl -Werror cannot be used because all of those warnings would be turned into errors.
286294
AX_CHECK_COMPILE_FLAG([-Werror],[CXXFLAG_WERROR="-Werror"],[CXXFLAG_WERROR=""])
287295

296+
dnl Check for a flag to turn linker warnings into errors. When flags are passed to linkers via the
297+
dnl compiler driver using a -Wl,-foo flag, linker warnings may be swallowed rather than bubbling up.
298+
dnl See note above, the same applies here as well.
299+
dnl
300+
dnl LDFLAG_WERROR Should only be used when testing -Wl,*
301+
case $host in
302+
*darwin*)
303+
AX_CHECK_LINK_FLAG([-Wl,-fatal_warnings],[LDFLAG_WERROR="-Wl,-fatal_warnings"],[LDFLAG_WERROR=""])
304+
;;
305+
*)
306+
AX_CHECK_LINK_FLAG([-Wl,--fatal-warnings],[LDFLAG_WERROR="-Wl,--fatal-warnings"],[LDFLAG_WERROR=""])
307+
;;
308+
esac
309+
288310
if test "x$enable_debug" = xyes; then
289311
dnl Clear default -g -O2 flags
290312
if test "x$CXXFLAGS_overridden" = xno; then
@@ -611,7 +633,7 @@ case $host in
611633
esac
612634
fi
613635

614-
AX_CHECK_LINK_FLAG([[-Wl,-headerpad_max_install_names]], [LDFLAGS="$LDFLAGS -Wl,-headerpad_max_install_names"])
636+
AX_CHECK_LINK_FLAG([[-Wl,-headerpad_max_install_names]], [LDFLAGS="$LDFLAGS -Wl,-headerpad_max_install_names"],, [[$LDFLAG_WERROR]])
615637
CPPFLAGS="$CPPFLAGS -DMAC_OSX -DOBJC_OLD_DISPATCH_PROTOTYPES=0"
616638
OBJCXXFLAGS="$CXXFLAGS"
617639
;;
@@ -707,8 +729,8 @@ if test x$use_glibc_compat != xno; then
707729
[ fdelt_type="long int"])
708730
AC_MSG_RESULT($fdelt_type)
709731
AC_DEFINE_UNQUOTED(FDELT_TYPE, $fdelt_type,[parameter and return value type for __fdelt_chk])
710-
AX_CHECK_LINK_FLAG([[-Wl,--wrap=__divmoddi4]], [COMPAT_LDFLAGS="$COMPAT_LDFLAGS -Wl,--wrap=__divmoddi4"])
711-
AX_CHECK_LINK_FLAG([[-Wl,--wrap=log2f]], [COMPAT_LDFLAGS="$COMPAT_LDFLAGS -Wl,--wrap=log2f"])
732+
AX_CHECK_LINK_FLAG([[-Wl,--wrap=__divmoddi4]], [COMPAT_LDFLAGS="$COMPAT_LDFLAGS -Wl,--wrap=__divmoddi4"],, [[$LDFLAG_WERROR]])
733+
AX_CHECK_LINK_FLAG([[-Wl,--wrap=log2f]], [COMPAT_LDFLAGS="$COMPAT_LDFLAGS -Wl,--wrap=log2f"],, [[$LDFLAG_WERROR]])
712734
else
713735
AC_SEARCH_LIBS([clock_gettime],[rt])
714736
fi
@@ -756,11 +778,11 @@ if test x$use_hardening != xno; then
756778
])
757779
fi
758780

759-
AX_CHECK_LINK_FLAG([[-Wl,--dynamicbase]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--dynamicbase"])
760-
AX_CHECK_LINK_FLAG([[-Wl,--nxcompat]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--nxcompat"])
761-
AX_CHECK_LINK_FLAG([[-Wl,--high-entropy-va]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--high-entropy-va"])
762-
AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"])
763-
AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"])
781+
AX_CHECK_LINK_FLAG([[-Wl,--dynamicbase]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--dynamicbase"],, [[$LDFLAG_WERROR]])
782+
AX_CHECK_LINK_FLAG([[-Wl,--nxcompat]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--nxcompat"],, [[$LDFLAG_WERROR]])
783+
AX_CHECK_LINK_FLAG([[-Wl,--high-entropy-va]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--high-entropy-va"],, [[$LDFLAG_WERROR]])
784+
AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"],, [[$LDFLAG_WERROR]])
785+
AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"],, [[$LDFLAG_WERROR]])
764786
AX_CHECK_LINK_FLAG([[-fPIE -pie]], [PIE_FLAGS="-fPIE"; HARDENED_LDFLAGS="$HARDENED_LDFLAGS -pie"],, [[$CXXFLAG_WERROR]])
765787

766788
case $host in
@@ -774,14 +796,14 @@ dnl These flags are specific to ld64, and may cause issues with other linkers.
774796
dnl For example: GNU ld will intepret -dead_strip as -de and then try and use
775797
dnl "ad_strip" as the symbol for the entry point.
776798
if test x$TARGET_OS = xdarwin; then
777-
AX_CHECK_LINK_FLAG([[-Wl,-dead_strip]], [LDFLAGS="$LDFLAGS -Wl,-dead_strip"])
778-
AX_CHECK_LINK_FLAG([[-Wl,-dead_strip_dylibs]], [LDFLAGS="$LDFLAGS -Wl,-dead_strip_dylibs"])
779-
AX_CHECK_LINK_FLAG([[-Wl,-bind_at_load]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-bind_at_load"])
799+
AX_CHECK_LINK_FLAG([[-Wl,-dead_strip]], [LDFLAGS="$LDFLAGS -Wl,-dead_strip"],, [[$LDFLAG_WERROR]])
800+
AX_CHECK_LINK_FLAG([[-Wl,-dead_strip_dylibs]], [LDFLAGS="$LDFLAGS -Wl,-dead_strip_dylibs"],, [[$LDFLAG_WERROR]])
801+
AX_CHECK_LINK_FLAG([[-Wl,-bind_at_load]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-bind_at_load"],, [[$LDFLAG_WERROR]])
780802
fi
781803

782804
if test x$enable_determinism = xyes; then
783805
if test x$TARGET_OS = xwindows; then
784-
AX_CHECK_LINK_FLAG([[-Wl,--no-insert-timestamp]], [LDFLAGS="$LDFLAGS -Wl,--no-insert-timestamp"])
806+
AX_CHECK_LINK_FLAG([[-Wl,--no-insert-timestamp]], [LDFLAGS="$LDFLAGS -Wl,--no-insert-timestamp"],, [[$LDFLAG_WERROR]])
785807
fi
786808
fi
787809

@@ -1213,7 +1235,7 @@ fi
12131235

12141236
if test x$use_reduce_exports = xyes; then
12151237
CXXFLAGS="$CXXFLAGS $RE_CXXFLAGS"
1216-
AX_CHECK_LINK_FLAG([[-Wl,--exclude-libs,ALL]], [RELDFLAGS="-Wl,--exclude-libs,ALL"])
1238+
AX_CHECK_LINK_FLAG([[-Wl,--exclude-libs,ALL]], [RELDFLAGS="-Wl,--exclude-libs,ALL"],, [[$LDFLAG_WERROR]])
12171239
fi
12181240

12191241
if test x$use_tests = xyes; then

0 commit comments

Comments
 (0)