Before I knew about it, I had written a similar thing:
# Autocomplete make rules
_make () {
local cur prev words cword cmdline
_init_completion || return
# command to generate the rules
words="$(make -pRrq |\
awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($1 !~ "^[#.]") {print $1}}' |\
sort | egrep -v -e '^[^[:alnum:]]' | xargs)"
COMPREPLY+=( $( compgen -W "$words" -- $cur) )
}
if declare -f _init_completion > /dev/null; then
complete -F _make make
fi
It relies on `make -pRrq`, that is a very useful command to debug your makefiles, especially in a big project. The option `-p` prints the make data-base, -R and -r removes the implicit rules and variables, and -q indicates only asking make a question about current state, avoid executing anything.Well, it worked for a time, but the bash-completion one is nicer (though much, much more complex). Very nice however to know about `-pRrq`, definitely helps when working on big projects.