それMakefile
は:
%.pdf: %.tex
rubber -d $<
doc.tex
ディレクトリにがある場合、 がmake doc.pdf
構築されますdoc.pdf
。問題は、 と入力してmake
も自動補完が何も返さないことです。 への自動補完すら許可されませんmake doc.tex
。どうすればよいでしょうか?
答え1
パッケージbash-completion
はこれを行わず、コマンドラインオプションを処理してMakefile
ターゲットのリストを抽出するためにいくつかのアクロバットを行いますが、ワイルドカードを適用したり、その他の処理を行って一致を生成しようとはしません。パターンルール。
ただし、これは実行可能です。ここでは、いくつかの注意点がある簡単なバージョンを示します。
function _mkcache() {
local _file="$1"
# add "-r" to omit defaults (60+ rules)
${MAKE:-make} ${_file:+-f "$_file"} -qp 2>/dev/null |
gawk '/^# *Make data base/,/^# *Finished Make data base/{
if (/^# Not a target/) { getline; next }
## handle "target: ..."
if (match($0,/^([^.#% ][^:%=]+) *:($|[^=])(.*)/,bits)) {
#if (bits[3]=="") next # OPT: skip phony
printf("%s\n",bits[1])
}
## handle "%.x [...]: %.y [| x]", split into distinct targets/prereqs
else if (match($0,/^([^:]*%[^:]*) *(::?) *(.*%.*) *(\| *(.*))?/,bits)) {
#if (bits[3]=="%") next # OPT: skip wildcard ones
nb1=split(bits[1],bb1)
nb3=split(bits[3],bb3)
for (nn=1; nn<=nb1; nn++)
for (mm=1; mm<=nb3; mm++)
printf("%s : %s\n",bb1[nn],bb3[mm])
}
## handle fixed (no %) deps
else if (match($0,/^([^:]*%[^:]*) *(::?) *([^%]*)$/,bits)) {
if (bits[3]=="") next # phony
printf("%s : %s\n",bits[1],bits[3])
}
## handle old form ".c.o:" rewrite to new form "%.o: %.c"
else if (match($0,/^\.([^.]+)\.([^.]+): *(.*)/,bits)) {
printf("%%.%s : %%.%s\n", bits[2],bits[1])
}
}' > ".${_file:-Makefile}.targets"
}
function _bc_make() {
local ctok=${COMP_WORDS[COMP_CWORD]} # curr token
local ptok=${COMP_WORDS[COMP_CWORD-1]} # prev token
local -a mkrule maybe
local try rr lhs rhs rdir pat makefile=Makefile
## check we're not doing any make options
[[ ${ctok:0:1} != "-" && ! $ptok =~ ^-[fCIjloW] ]] && {
COMPREPLY=()
[[ "$makefile" -nt .${makefile}.targets ]] &&
_mkcache "$makefile"
mapfile -t mkrule < ".${makefile}.targets"
# mkrule+=( "%.o : %.c" ) # stuff in extra rules
for rr in "${mkrule[@]}"; do
IFS=": " read lhs rhs <<< $rr
## special "archive(member):"
[[ "$lhs" =~ ^(.*)?\((.+)\) ]] && {
continue # not handled
}
## handle simple targets
[[ "$rhs" == "" ]] && {
COMPREPLY+=( $(compgen -W "$lhs" -- "$ctok" ) )
continue
}
## rules with a path, like "% : RCS/%,v"
rdir=""
[[ "$rhs" == */* ]] && rdir="${rhs/%\/*/}/"
rhs=${rhs/#*\//}
## expand (glob) that matches RHS
## if current token already ends in a "." strip it
## match by replacing "%" stem with "*"
[[ $ctok == *. ]] && try="${rdir}${rhs/\%./$ctok*}" \
|| try="${rdir}${rhs/\%/$ctok*}"
maybe=( $(compgen -G "$try") ) # try must be quoted
## maybe[] is an array of filenames from expanded prereq globs
(( ${#maybe[*]} )) && {
[[ "$rhs" =~ % ]] && {
## promote rhs glob to a regex: % -> (.*)
rhs="${rhs/./\\.}"
pat="${rdir}${rhs/\%/(.*)}"
## use regex to extract stem from RHS, sub "%" on LHS
for nn in "${maybe[@]}"; do
[[ $nn =~ $pat ]] && {
COMPREPLY+=( "${lhs/\%/${BASH_REMATCH[1]}}" )
}
done
} || {
# fixed prereqs (no % on RHS)
COMPREPLY+=( "${lhs/\%/$ctok}" )
}
}
done
return
}
COMPREPLY=() #default
}
complete -F _bc_make ${MAKE:-make}
2 つの部分があり、関数_mkcache
は からすべてのルールとターゲットを抽出し、Makefile
これらをキャッシュします。また、少し処理を行って、target : pre-req
そのキャッシュ内でルールを単一の " " 形式に簡略化します。
次に、補完関数は_bc_make
、補完を試みたトークンを受け取り、ターゲットとの一致を試み、パターン ルールを使用して、前提条件と補完の単語に基づいて glob を展開します。1 つ以上の一致が見つかった場合、パターン ルールに基づいてターゲットのリストが作成されます。
GNUmake
が想定されています。正しく処理されるはずです:
- ターゲットとパターンルール(ただし、すべてではありません。以下を参照してください)
- 新旧形式
.c.o
→%.o : %.c
- 前提条件のパス(例
RCS/
) - すべてのデフォルトルールの有無(必要
-r
に応じて追加)make
注意、およびサポートされていないもの:
- 中間依存関係や連鎖依存関係の場合、
make
VPATH
またはvpath
.SUFFIXES
make -C dir
- 「アーカイブ(メンバー)」ターゲット、明示的または暗黙的
make
オプション拡張- Makefile の解析に問題を引き起こす可能性のある環境内の病的なジャンク (
TERMCAP
例) - 以外の名前のMakefile
Makefile
上記のいくつかは比較的簡単に追加できますが、アーカイブ処理などの他のものはそれほど簡単ではありません。