Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flag-o-matic.eclass: a complete rewrite of get-flag() #1425

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 28 additions & 9 deletions eclass/flag-o-matic.eclass
Original file line number Diff line number Diff line change
Expand Up @@ -820,23 +820,42 @@ 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() {
[[ $# -ne 1 ]] && die "usage: <flag>"
local f var findflag="$1"
local var pattern="${1}"
# ensure ${needle} starts with a single dash
local needle="-${pattern#-}"

# 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"
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} )

# reverse loop because last flag wins
for (( i = ${#flags[@]} - 1 ; i >= 0 ; i-- )) ; do
local flag="${flags[i]}"
# strip value as it's not needed for comparison
local haystack="${flag%%=*}"

# as long as ${needle} remains unquoted, wildcards will work
if [[ "${haystack}" == ${needle} ]] ; then
# preserve only value if only flag name was provided
local ret="${flag#-${pattern}=}"

# ${ret} might contain `-e` or `-n` which confuses echo
printf '%s\n' "${ret}"

return 0
fi
done
done

return 1
}

Expand Down
70 changes: 70 additions & 0 deletions eclass/tests/flag-o-matic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,74 @@ out=$(CC=clang test-flags-CC -Wl,-O1)
ftend
fi

clear-env() {
unset $(all-flag-vars)
}

fom-tbegin() {
clear-env
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just clear-env once and limit the scope of CFLAGS= being set for calls?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(like `CFLAGS='-foo' fom-run ...)

Copy link
Contributor Author

@rindeal rindeal May 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this idea as well, unfortunately after I wrote all of it and haven't considered it to be worth the effort to change it.

tbegin "${@}"
}

fom-run() {
out="$("$@")"
ret=${?}
}

fom-tend() {
local ret="${1:-"${?}"}" msg="${2:-"ret='${ret}' out='${out}'"}"
shift 2
tend ${ret} "${msg}" "${@}"
clear-env
}

fom-tbegin "get-flag() - match first from the end"
CFLAGS='-O1 -O2'
fom-run get-flag '-O*'
[[ ${ret} == 0 && ${out} == '-O2' ]]
fom-tend

fom-tbegin "get-flag() - don't match substrings"
CFLAGS='-W1,-O1'
fom-run get-flag '-O*'
[[ ${ret} != 0 ]]
fom-tend

fom-tbegin "get-flag() - check full string comparison"
CFLAGS='-ggdb'
fom-run get-flag '-g'
[[ ${ret} != 0 ]]
fom-tend

fom-tbegin "get-flag() - return all"
CFLAGS='-march=native'
fom-run get-flag '-march'
[[ ${ret} == 0 && ${out} == '-march=native' ]]
fom-tend

fom-tbegin "get-flag() - return value only"
CFLAGS='-march=native'
fom-run get-flag 'march'
[[ ${ret} == 0 && ${out} == 'native' ]]
fom-tend

fom-tbegin "get-flag() - echo -e"
CFLAGS='-n -e'
fom-run get-flag '-e'
[[ ${ret} == 0 && ${out} == '-e' ]]
fom-tend

fom-tbegin "get-flag() - echo -n"
CFLAGS='-e -n'
fom-run get-flag '-n'
[[ ${ret} == 0 && ${out} == '-n' ]]
fom-tend

fom-tbegin "filter-mfpmath() - example"
CFLAGS='-mfpmath=sse,386'
filter-mfpmath 'sse'
fom-run get-flag '-mfpmath'
[[ ${ret} == 0 && ${out} == '-mfpmath=386' ]]
fom-tend

texit