public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: rindeal <dev.rindeal@gmail.com>
To: Ulrich Mueller <ulm@gentoo.org>
Cc: gentoo-dev@lists.gentoo.org, "Michał Górny" <mgorny@gentoo.org>,
	"Jan Chren" <dev.rindeal@gmail.com>
Subject: Re: [gentoo-dev] [PATCH] flag-o-matic.eclass: bugfix for get-flag()
Date: Mon, 16 May 2016 14:17:46 +0200	[thread overview]
Message-ID: <CANgLvuBucqDDXQutxbBSoUErpo1r7pCJcwXmCY2f2c46hGLnOg@mail.gmail.com> (raw)
In-Reply-To: <22329.1243.790672.211591@a1i15.kph.uni-mainz.de>

> [Looks like your mailer is broken. All the tabs in your patch have
> been mangled and appear as spaces.]
>
>> +       # reverse loop
>> +       set -- ${!var}
>> +       local i=${#}
>> +       while [[ ${i} > 0 ]] ; do
>> +           local arg="${!i}"
>
> Using the positional parameters looks needlessly complicated here.
> Why not use an array, like this (untested):
>
>         local -a flags=(${!var})
>         for (( i=${#flags[@]}-1; i>=0; i-- )); do
>
> Below you can use ${flags[i]} instead of ${arg} then.

Done.

I've also changed comments and added examples section to docs.

So this is what it looks like now:

diff --git a/eclass/flag-o-matic.eclass b/eclass/flag-o-matic.eclass
index e0b19e9..217d33b 100644
--- a/eclass/flag-o-matic.eclass
+++ b/eclass/flag-o-matic.eclass
@@ -534,18 +534,26 @@ strip-unsupported-flags() {
 # @USAGE: <flag>
 # @DESCRIPTION:
 # Find and echo the value for a particular flag.  Accepts shell globs.
+#
+# Example:
+# @CODE
+# CFLAGS="-march=i686 -O1"
+# get-flag -march # outputs "-march=i686"
+# get-flag march  # outputs "i686"
+# get-flag '-O*'  # outputs "-O1"
+# @CODE
 get-flag() {
-   local f var findflag="$1"
-
-   # this code looks a little flaky but seems to work for
-   # everything we want ...
-   # for example, if CFLAGS="-march=i686":
-   # `get-flag -march` == "-march=i686"
-   # `get-flag march` == "i686"
+   local var pattern="${1}"
    for var in $(all-flag-vars) ; do
-       for f in ${!var} ; do
-           if [ "${f/${findflag}}" != "${f}" ] ; then
-               printf "%s\n" "${f/-${findflag}=}"
+       local i flags=( ${!var} )
+       for (( i=${#flags[@]}-1; i>=0; i-- )) ; do
+           local needle="-${pattern#-}" # force dash on
+           local haystack="${flags[i]%%=*}" # we're comparing flags, not values
+           if [[ ${haystack##${needle}} == '' ]] ; then
+               # preserve only value if only flag name was provided
+               local ret="${flags[i]#-${pattern}=}"
+               # ${ret} might contain `-e` or `-n` which confuses echo
+               printf '%s\n' "${ret}"
                return 0
            fi
        done


  reply	other threads:[~2016-05-16 12:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-15 13:31 [gentoo-dev] [PATCH] flag-o-matic.eclass: bugfix for get-flag() Jan Chren
2016-05-15 15:59 ` Michał Górny
2016-05-15 19:35   ` rindeal
2016-05-15 19:41     ` Michał Górny
2016-05-15 20:19       ` rindeal
2016-05-15 23:23     ` Ulrich Mueller
2016-05-16 12:17       ` rindeal [this message]
2016-05-20  3:15         ` Mike Frysinger
2016-06-05 12:13         ` rindeal
2016-05-16  6:23     ` [gentoo-dev] " Ryan Hill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CANgLvuBucqDDXQutxbBSoUErpo1r7pCJcwXmCY2f2c46hGLnOg@mail.gmail.com \
    --to=dev.rindeal@gmail.com \
    --cc=gentoo-dev@lists.gentoo.org \
    --cc=mgorny@gentoo.org \
    --cc=ulm@gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox