Existing use implies a desire to:
- treat leading "-" as optional
- query the value of a "flag"
- query the entire arg of a "-flag" or "-flag=" or really anything other
  than "flag" where CFLAGS has "-flag=*"

But unfortunately we also do odd things like match `get-flag -g` to
retrieve -grecord-gcc-switches or -fno-gnu-keywords etc. It's a rough
substring match and really we want to match flag *names*. It is also a
bit awkward to do "-flag=" and then trim off the flag name by hand.

Update the algorithm to allow for this. Given it's a big change to
usability, require users to opt in to the new algorithm by migrating
their ebuilds to EAPI 9 and testing there. They could have been relying
on what I called a bug.

Some things that will definitely break, but I cannot conceive of why one
would do it:

- FLAGS="-fno-gnu-keywords"; get-flag -g
  Gross misuse.

- FLAGS="-flto-incremental=/path"; get-flag "-flto-inc*="
  omitting the = works fine, what is the goal here???

Signed-off-by: Eli Schwartz <[email protected]>
---

v2: add eclass tests

 eclass/flag-o-matic.eclass   | 21 ++++++++++++++++++---
 eclass/tests/flag-o-matic.sh | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/eclass/flag-o-matic.eclass b/eclass/flag-o-matic.eclass
index 830e42f1b9a4..a9c5264c7cf0 100644
--- a/eclass/flag-o-matic.eclass
+++ b/eclass/flag-o-matic.eclass
@@ -853,9 +853,24 @@ get-flag() {
        # `get-flag march` == "i686"
        for var in $(all-flag-vars) ; do
                for f in ${!var} ; do
-                       if [ "${f/${findflag}}" != "${f}" ] ; then
-                               printf "%s\n" "${f/-${findflag}=}"
-                               return 0
+                       if [[ ${EAPI} = [78] && -z 
"${_FLAG_O_MATIC_TESTS_FAKE_EAPI_NINE}" ]]; then
+                               if [[ "${f/${findflag}}" != "${f}" ]] ; then
+                                       printf "%s\n" "${f/-${findflag}=}"
+                                       return 0
+                               fi
+                       else
+                               # Print RHS for "flag" (no leading "-")
+                               if [[ "${f#-${findflag}=}" != "${f}" ]] ; then
+                                       printf "%s\n" "${f#-${findflag}=}"
+                                       return 0
+                               fi
+                               # Print full match for any of:
+                               #   "-flag" with leading "-"
+                               #   "flag" without leading "-" that has no 
unmatched succeeding =value
+                               if [[ ${f} = -${findflag#-} || ${f%=*} = 
${findflag} ]] ; then
+                                       printf "%s\n" "${f}"
+                                       return 0
+                               fi
                        fi
                done
        done
diff --git a/eclass/tests/flag-o-matic.sh b/eclass/tests/flag-o-matic.sh
index 4dc133776038..853b3ce57443 100755
--- a/eclass/tests/flag-o-matic.sh
+++ b/eclass/tests/flag-o-matic.sh
@@ -151,6 +151,40 @@ out=$(test-flags-CC -finvalid-flag)
 [[ $? -ne 0 && -z ${out} ]]
 ftend
 
+tbegin "get-flag (EAPI 7 substring args)"
+CFLAGS="-frecord-gcc-switches -g"
+CXXFLAGS=""
+LDFLAGS=""
+out=$(get-flag -g)
+[[ ${out} = -frecord-gcc-switches ]]
+ftend
+
+tbegin "get-flag (EAPI 9 exact match)"
+CFLAGS="-frecord-gcc-switches -g"
+CXXFLAGS=""
+LDFLAGS=""
+out=$(_FLAG_O_MATIC_TESTS_FAKE_EAPI_NINE=1 get-flag -g)
+[[ ${out} = -g ]]
+ftend
+
+tbegin "get-flag (EAPI 7 values)"
+CFLAGS="-flto-incremental=/path -flto=4"
+CXXFLAGS=""
+LDFLAGS=""
+out=$(get-flag flto)
+[[ ${out} = -flto-incremental=/path ]]
+ftend
+
+tbegin "get-flag (EAPI 9 values)"
+CFLAGS="-flto-incremental=/path -flto=4"
+CXXFLAGS=""
+LDFLAGS=""
+out=$(_FLAG_O_MATIC_TESTS_FAKE_EAPI_NINE=1 get-flag flto)
+[[ ${out} = 4 ]]
+ftend
+
+
+
 if type -P clang >/dev/null ; then
        tbegin "test-flags-CC (valid flags w/clang)"
        out=$(CC=clang test-flags-CC -O3)
-- 
2.52.0


Reply via email to