Commit | Line | Data |
---|---|---|
02bb4fcc SM |
1 | # ============================================================================== |
2 | # https://www.gnu.org/software/autoconf-archive/ax_compiler_flags_ldflags.html | |
3 | # ============================================================================== | |
4 | # | |
5 | # SYNOPSIS | |
6 | # | |
7 | # AX_COMPILER_FLAGS_LDFLAGS([VARIABLE], [IS-RELEASE], [EXTRA-BASE-FLAGS], [EXTRA-YES-FLAGS]) | |
8 | # | |
9 | # DESCRIPTION | |
10 | # | |
11 | # Add warning flags for the linker to VARIABLE, which defaults to | |
12 | # WARN_LDFLAGS. VARIABLE is AC_SUBST-ed by this macro, but must be | |
13 | # manually added to the LDFLAGS variable for each target in the code base. | |
14 | # | |
15 | # This macro depends on the environment set up by AX_COMPILER_FLAGS. | |
16 | # Specifically, it uses the value of $ax_enable_compile_warnings to decide | |
17 | # which flags to enable. | |
18 | # | |
19 | # LICENSE | |
20 | # | |
21 | # Copyright (c) 2014, 2015 Philip Withnall <philip@tecnocode.co.uk> | |
22 | # Copyright (c) 2017, 2018 Reini Urban <rurban@cpan.org> | |
23 | # | |
24 | # Copying and distribution of this file, with or without modification, are | |
25 | # permitted in any medium without royalty provided the copyright notice | |
26 | # and this notice are preserved. This file is offered as-is, without any | |
27 | # warranty. | |
28 | ||
29 | #serial 9 | |
30 | ||
31 | AC_DEFUN([AX_COMPILER_FLAGS_LDFLAGS],[ | |
32 | AX_REQUIRE_DEFINED([AX_APPEND_LINK_FLAGS]) | |
33 | AX_REQUIRE_DEFINED([AX_APPEND_FLAG]) | |
34 | AX_REQUIRE_DEFINED([AX_CHECK_COMPILE_FLAG]) | |
35 | AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG]) | |
36 | ||
37 | # Variable names | |
38 | m4_define([ax_warn_ldflags_variable], | |
39 | [m4_normalize(ifelse([$1],,[WARN_LDFLAGS],[$1]))]) | |
40 | ||
41 | # Always pass -Werror=unknown-warning-option to get Clang to fail on bad | |
42 | # flags, otherwise they are always appended to the warn_ldflags variable, | |
43 | # and Clang warns on them for every compilation unit. | |
44 | # If this is passed to GCC, it will explode, so the flag must be enabled | |
45 | # conditionally. | |
46 | AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],[ | |
47 | ax_compiler_flags_test="-Werror=unknown-warning-option" | |
48 | ],[ | |
49 | ax_compiler_flags_test="" | |
50 | ]) | |
51 | ||
52 | AX_CHECK_LINK_FLAG([-Wl,--as-needed], [ | |
53 | AX_APPEND_LINK_FLAGS([-Wl,--as-needed], | |
54 | [AM_LDFLAGS],[$ax_compiler_flags_test]) | |
55 | ]) | |
56 | AX_CHECK_LINK_FLAG([-Wl,-z,relro], [ | |
57 | AX_APPEND_LINK_FLAGS([-Wl,-z,relro], | |
58 | [AM_LDFLAGS],[$ax_compiler_flags_test]) | |
59 | ]) | |
60 | AX_CHECK_LINK_FLAG([-Wl,-z,now], [ | |
61 | AX_APPEND_LINK_FLAGS([-Wl,-z,now], | |
62 | [AM_LDFLAGS],[$ax_compiler_flags_test]) | |
63 | ]) | |
64 | AX_CHECK_LINK_FLAG([-Wl,-z,noexecstack], [ | |
65 | AX_APPEND_LINK_FLAGS([-Wl,-z,noexecstack], | |
66 | [AM_LDFLAGS],[$ax_compiler_flags_test]) | |
67 | ]) | |
68 | # textonly, retpolineplt not yet | |
69 | ||
70 | # macOS and cygwin linker do not have --as-needed | |
71 | AX_CHECK_LINK_FLAG([-Wl,--no-as-needed], [ | |
72 | ax_compiler_flags_as_needed_option="-Wl,--no-as-needed" | |
73 | ], [ | |
74 | ax_compiler_flags_as_needed_option="" | |
75 | ]) | |
76 | ||
77 | # macOS linker speaks with a different accent | |
78 | ax_compiler_flags_fatal_warnings_option="" | |
79 | AX_CHECK_LINK_FLAG([-Wl,--fatal-warnings], [ | |
80 | ax_compiler_flags_fatal_warnings_option="-Wl,--fatal-warnings" | |
81 | ]) | |
82 | AX_CHECK_LINK_FLAG([-Wl,-fatal_warnings], [ | |
83 | ax_compiler_flags_fatal_warnings_option="-Wl,-fatal_warnings" | |
84 | ]) | |
85 | ||
86 | # Base flags | |
87 | AX_APPEND_LINK_FLAGS([ dnl | |
88 | $ax_compiler_flags_as_needed_option dnl | |
89 | $3 dnl | |
90 | ],ax_warn_ldflags_variable,[$ax_compiler_flags_test]) | |
91 | ||
92 | AS_IF([test "$ax_enable_compile_warnings" != "no"],[ | |
93 | # "yes" flags | |
94 | AX_APPEND_LINK_FLAGS([$4 $5 $6 $7], | |
95 | ax_warn_ldflags_variable, | |
96 | [$ax_compiler_flags_test]) | |
97 | ]) | |
98 | AS_IF([test "$ax_enable_compile_warnings" = "error"],[ | |
99 | # "error" flags; -Werror has to be appended unconditionally because | |
100 | # it's not possible to test for | |
101 | # | |
102 | # suggest-attribute=format is disabled because it gives too many false | |
103 | # positives | |
104 | AX_APPEND_LINK_FLAGS([ dnl | |
105 | $ax_compiler_flags_fatal_warnings_option dnl | |
106 | ],ax_warn_ldflags_variable,[$ax_compiler_flags_test]) | |
107 | ]) | |
108 | ||
109 | # Substitute the variables | |
110 | AC_SUBST(ax_warn_ldflags_variable) | |
111 | ])dnl AX_COMPILER_FLAGS |