Skip to content

Instantly share code, notes, and snippets.

@ShenTengTu
Last active August 14, 2024 08:30
Show Gist options
  • Save ShenTengTu/6a0bb6a9e70a7a5e7c411218e36735fa to your computer and use it in GitHub Desktop.
Save ShenTengTu/6a0bb6a9e70a7a5e7c411218e36735fa to your computer and use it in GitHub Desktop.
shell script provides advanced output of `pkg-config`
#!/bin/bash
action=$1
shift 1
no_flag=0
quote=0
while getopts "nqf:" opt; do
case ${opt} in
n)
no_flag=1
;;
q)
quote=1
;;
*)
;;
esac
done
shift $((OPTIND -1))
function _cmd() {
local flag=$1
shift 1
local base=""
local array=()
for pkg in "$@"; do
IFS=' ' read -r -a arr <<< "$(pkg-config "$flag" "$pkg")"
for i in "${arr[@]}"; do
array+=("$i")
done
done
base="$(echo "${array[@]}" | tr ' ' '\n' | sort -u)"
if [ $quote -eq 1 ]; then
base="$(echo "$base" | xargs -d '\n' -I {} echo '"{}",')"
fi
echo "$base"
}
ret=""
case $action in
cflags)
ret="$(_cmd --cflags "$@")"
;;
libs)
ret="$(_cmd --libs "$@")"
;;
static)
ret="$(_cmd --static "$@")"
;;
include)
ret="$(_cmd --cflags-only-I "$@")"
if [ $no_flag -eq 1 ]; then
ret="${ret//-I/}"
fi
;;
*)
;;
esac
echo "$ret"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment