Gentoo Archives: gentoo-dev

From: Sergei Trofimovich <slyfox@g.o>
To: gentoo-dev@l.g.o
Cc: Sergei Trofimovich <slyfox@g.o>
Subject: [gentoo-dev] [PATCH] flag-o-matic.eclass: add LDFLAGS testing against linker
Date: Mon, 23 Dec 2019 11:50:56
Message-Id: 20191223115043.3160866-1-slyfox@gentoo.org
1 Before the change we tested only compiler driver (gcc flag parser)
2 for LDFLAGS.
3
4 This does not cover cases when we would really like to filter out
5 unsupported linker flags like -Wl,--hash-style=gnu passed to non-ELF
6 targets.
7
8 The change adds test-flag-CCLD() helper to perform all of assembly,
9 compilation and linking steps. Helper is used to filter LDFLAGS variable
10 in strip-unsupported-flags().
11
12 Closes: https://bugs.gentoo.org/333763
13 Signed-off-by: Sergei Trofimovich <slyfox@g.o>
14 ---
15 eclass/flag-o-matic.eclass | 72 +++++++++++++++++++++++++++++-------
16 eclass/tests/flag-o-matic.sh | 2 +-
17 2 files changed, 59 insertions(+), 15 deletions(-)
18
19 diff --git a/eclass/flag-o-matic.eclass b/eclass/flag-o-matic.eclass
20 index f882b09d621..0aec22c83f2 100644
21 --- a/eclass/flag-o-matic.eclass
22 +++ b/eclass/flag-o-matic.eclass
23 @@ -441,29 +441,63 @@ test-flag-PROG() {
24 # 'type' needs a binary name
25 type -p ${comp[0]} >/dev/null || return 1
26
27 + # Set up test file.
28 + local in_src in_ext cmdline_extra=()
29 + case "${lang}" in
30 + # compiler/assembler only
31 + c)
32 + in_ext='.c'
33 + in_src='int main(void) { return 0; }'
34 + cmdline_extra+=(-xc -c)
35 + ;;
36 + c++)
37 + in_ext='.cc'
38 + in_src='int main(void) { return 0; }'
39 + cmdline_extra+=(-xc++ -c)
40 + ;;
41 + f77)
42 + in_ext='.f'
43 + # fixed source form
44 + in_src=' end'
45 + cmdline_extra+=(-xf77 -c)
46 + ;;
47 + f95)
48 + in_ext='.f90'
49 + in_src='end'
50 + cmdline_extra+=(-xf95 -c)
51 + ;;
52 +
53 + # C compiler/assembler/linker
54 + c+ld)
55 + in_ext='.c'
56 + in_src='int main(void) { return 0; }'
57 + cmdline_extra+=(-xc)
58 + ;;
59 + esac
60 + local test_in=${T}/test-flag-${comp}.${lang}
61 + local test_out=${T}/test-flag-${comp}.exe
62 +
63 + printf "%s\n" "${in_src}" > "${test_in}" || return 1
64 +
65 local cmdline=(
66 "${comp[@]}"
67 # Clang will warn about unknown gcc flags but exit 0.
68 # Need -Werror to force it to exit non-zero.
69 -Werror
70 - # Use -c so we can test the assembler as well.
71 - -c -o /dev/null
72 + "$@"
73 + # -x<lang> options need to go before first source file
74 + "${cmdline_extra[@]}"
75 +
76 + "${test_in}" -o "${test_out}"
77 )
78 - if "${cmdline[@]}" -x${lang} - </dev/null &>/dev/null ; then
79 - cmdline+=( "$@" -x${lang} - )
80 - else
81 - # XXX: what's the purpose of this? does it even work with
82 - # any compiler?
83 - cmdline+=( "$@" -c -o /dev/null /dev/null )
84 - fi
85
86 - if ! "${cmdline[@]}" </dev/null &>/dev/null; then
87 + if ! "${cmdline[@]}" &>/dev/null; then
88 # -Werror makes clang bail out on unused arguments as well;
89 # try to add -Qunused-arguments to work-around that
90 # other compilers don't support it but then, it's failure like
91 # any other
92 cmdline+=( -Qunused-arguments )
93 - "${cmdline[@]}" </dev/null &>/dev/null
94 + "${cmdline[@]}" &>/dev/null
95 fi
96 }
97
98 @@ -491,6 +525,12 @@ test-flag-F77() { test-flag-PROG "F77" f77 "$@"; }
99 # Returns shell true if <flag> is supported by the Fortran 90 compiler, else returns shell false.
100 test-flag-FC() { test-flag-PROG "FC" f95 "$@"; }
101
102 +# @FUNCTION: test-flag-CCLD
103 +# @USAGE: <flag>
104 +# @DESCRIPTION:
105 +# Returns shell true if <flag> is supported by the C compiler and linker, else returns shell false.
106 +test-flag-CCLD() { test-flag-PROG "CC" c+ld "$@"; }
107 +
108 test-flags-PROG() {
109 local comp=$1
110 local flags=()
111 @@ -548,6 +588,12 @@ test-flags-F77() { test-flags-PROG "F77" "$@"; }
112 # Returns shell true if <flags> are supported by the Fortran 90 compiler, else returns shell false.
113 test-flags-FC() { test-flags-PROG "FC" "$@"; }
114
115 +# @FUNCTION: test-flags-CCLD
116 +# @USAGE: <flags>
117 +# @DESCRIPTION:
118 +# Returns shell true if <flags> are supported by the C compiler and default linker, else returns shell false.
119 +test-flags-CCLD() { test-flags-PROG "CCLD" "$@"; }
120 +
121 # @FUNCTION: test-flags
122 # @USAGE: <flags>
123 # @DESCRIPTION:
124 @@ -576,9 +622,7 @@ strip-unsupported-flags() {
125 export CXXFLAGS=$(test-flags-CXX ${CXXFLAGS})
126 export FFLAGS=$(test-flags-F77 ${FFLAGS})
127 export FCFLAGS=$(test-flags-FC ${FCFLAGS})
128 - # note: this does not verify the linker flags but it is enough
129 - # to strip invalid C flags which are much more likely, #621274
130 - export LDFLAGS=$(test-flags-CC ${LDFLAGS})
131 + export LDFLAGS=$(test-flags-CCLD ${LDFLAGS})
132 }
133
134 # @FUNCTION: get-flag
135 diff --git a/eclass/tests/flag-o-matic.sh b/eclass/tests/flag-o-matic.sh
136 index 7c078499d70..90eaf3a6ffb 100755
137 --- a/eclass/tests/flag-o-matic.sh
138 +++ b/eclass/tests/flag-o-matic.sh
139 @@ -8,7 +8,7 @@ inherit flag-o-matic
140
141 CFLAGS="-a -b -c=1 --param l1-cache-size=32"
142 CXXFLAGS="-x -y -z=2"
143 -LDFLAGS="-l -m -n=3"
144 +LDFLAGS="-l -m -n=3 -Wl,--remove-me"
145 ftend() {
146 local ret=$?
147 local msg="Failed; flags are:"
148 --
149 2.24.1

Replies