Gentoo Archives: gentoo-dev

From: rindeal <dev.rindeal@×××××.com>
To: "Michał Górny" <mgorny@g.o>
Cc: gentoo-dev@l.g.o, Jan Chren <dev.rindeal@×××××.com>
Subject: Re: [gentoo-dev] [PATCH] flag-o-matic.eclass: bugfix for get-flag()
Date: Sun, 15 May 2016 19:36:31
Message-Id: CANgLvuASukUCiFOMD5=SkiO7ZwSgkqrPK8fLrT2Ddb5rutXPZw@mail.gmail.com
In Reply to: Re: [gentoo-dev] [PATCH] flag-o-matic.eclass: bugfix for get-flag() by "Michał Górny"
1 > Dnia 15 maja 2016 15:31:29 CEST, Jan Chren <dev.rindeal@×××××.com> napisał(a):
2 >>- fix case:
3 >> - `CFLAGS='-O1 -O2'`
4 >> - `get-flag '-O*'`
5 >> - before `-O1`
6 >> - now `-O2`
7 >>- fix case:
8 >> - `CFLAGS='-W1,-O1'`
9 >> - `get-flag '-O*'`
10 >> - before `-W1,O1`
11 >> - now return 1
12 >>
13 >>`get-flag march` == "i686" syntax still works.
14 >
15 > Could you add appropriate test cases, in the tests subdirectory?
16
17 Done
18
19 >
20 >>---
21 >> eclass/flag-o-matic.eclass | 13 +++++++++----
22 >> 1 file changed, 9 insertions(+), 4 deletions(-)
23 >>
24 >>diff --git a/eclass/flag-o-matic.eclass b/eclass/flag-o-matic.eclass
25 >>index e0b19e9..f670320 100644
26 >>--- a/eclass/flag-o-matic.eclass
27 >>+++ b/eclass/flag-o-matic.eclass
28 >>@@ -535,7 +535,7 @@ strip-unsupported-flags() {
29 >> # @DESCRIPTION:
30 >> # Find and echo the value for a particular flag. Accepts shell globs.
31 >> get-flag() {
32 >>- local f var findflag="$1"
33 >>+ local var findflag="${1}"
34 >>
35 >> # this code looks a little flaky but seems to work for
36 >> # everything we want ...
37 >>@@ -543,11 +543,16 @@ get-flag() {
38 >> # `get-flag -march` == "-march=i686"
39 >> # `get-flag march` == "i686"
40 >> for var in $(all-flag-vars) ; do
41 >>- for f in ${!var} ; do
42 >>- if [ "${f/${findflag}}" != "${f}" ] ; then
43 >>- printf "%s\n" "${f/-${findflag}=}"
44 >>+ # reverse loop
45 >>+ set -- ${!var}
46 >>+ local i=$#
47 >
48 > You are using $ with and without braces inconsistently. Please stick to one form.
49
50 Done
51
52 >
53 >>+ while [ $i -gt 0 ] ; do
54 >
55 > Please use [[ ]] for conditionals. It has some nice bash magic that makes them whitespace-safe.
56
57 Although always a number, but done
58
59 >
60 >>+ local f="${!i}"
61 >>+ if [ "${f#-${findflag#-}}" != "${f}" ] ; then
62 >
63 > I know the original code sucked as well but could you replace this with more readable [[ ${f} == -${findflag#-}* ]] or alike (note: not tested).
64
65 This is just as buggy as my original implementation, I've reworked it
66 and thanks to the tests I hope it's now correct.
67
68 >
69 >>+ printf "%s\n" "${f#-${findflag}=}"
70 >
71 > It may be a good idea to add a short explanation why you can't use echo here, as a comment.
72
73 I've just copied what was there before, `echo` in bash is notoriously
74 wild, but with this simple string I guess it's ok, so done.
75
76 >
77 >> return 0
78 >> fi
79 >>+ ((i--))
80 >> done
81 >> done
82 >> return 1
83 >
84 >
85
86 apart from the tests, the patch now looks like this:
87
88 eclass/flag-o-matic.eclass | 18 ++++++++++++++----
89 1 file changed, 14 insertions(+), 4 deletions(-)
90
91 diff --git a/eclass/flag-o-matic.eclass b/eclass/flag-o-matic.eclass
92 index e0b19e9..a8a792e 100644
93 --- a/eclass/flag-o-matic.eclass
94 +++ b/eclass/flag-o-matic.eclass
95 @@ -535,7 +535,7 @@ strip-unsupported-flags() {
96 # @DESCRIPTION:
97 # Find and echo the value for a particular flag. Accepts shell globs.
98 get-flag() {
99 - local f var findflag="$1"
100 + local var pattern="${1}"
101
102 # this code looks a little flaky but seems to work for
103 # everything we want ...
104 @@ -543,11 +543,21 @@ get-flag() {
105 # `get-flag -march` == "-march=i686"
106 # `get-flag march` == "i686"
107 for var in $(all-flag-vars) ; do
108 - for f in ${!var} ; do
109 - if [ "${f/${findflag}}" != "${f}" ] ; then
110 - printf "%s\n" "${f/-${findflag}=}"
111 + # reverse loop
112 + set -- ${!var}
113 + local i=${#}
114 + while [[ ${i} > 0 ]] ; do
115 + local arg="${!i}"
116 + local needle="-${pattern#-}" # force dash on
117 + local haystack="${arg%%=*}" # we're comparing flags, not values
118 + if [[ ${haystack##${needle}} == '' ]] ; then
119 + local ret
120 + # preserve only value if only flag name was provided
121 + ret="${arg#-${pattern}=}"
122 + echo "${ret}"
123 return 0
124 fi
125 + ((i--))
126 done
127 done
128 return 1
129 --
130 2.7.3

Replies