假设我的Makefile
是这样的:
DIR :=#
foobar:
ls ${DIR}
当我输入时
mak[tab] f[tab]
它给出了正确的结果。
make foobar
但是
make foobar D[tab]
不能产生神奇效果
make foobar DIR=
所以我的问题是:在bash中是否有一种方法可以自动完成Makefile变量(除目标外)?
假设我的Makefile
是这样的:
DIR :=#
foobar:
ls ${DIR}
当我输入时
mak[tab] f[tab]
它给出了正确的结果。
make foobar
但是
make foobar D[tab]
不能产生神奇效果
make foobar DIR=
所以我的问题是:在bash中是否有一种方法可以自动完成Makefile变量(除目标外)?
这个回答远非完整。要在Makefile中grep所有变量,我们使用make -p
打印Makefile数据库:
# GNU Make 3.81
# Copyright (C) 2006 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
# This program built for x86_64-pc-linux-gnu
# Make data base, printed on Mon Oct 13 13:36:12 2014
# Variables
# automatic
<D = $(patsubst %/,%,$(dir $<))
# automatic
?F = $(notdir $?)
# environment
DESKTOP_SESSION = kde-plasma
# ...
# makefile (from `Makefile', line 1)
DIR :=
# makefile (from 'Makefile', line xy)
开头的行,并提取以下变量的名称:$ make -p | sed -n '/# makefile (from/ {n; p;}'
MAKEFILE_LIST := Makefile
DIR :=
:=
之后的所有内容):$ make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1/p;}'
MAKEFILE_LIST
DIR
_make_variables()
{
# get current completion
local cur=${COMP_WORDS[COMP_CWORD]}
# get list of possible makefile variables
local var=$(make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1=/p;}')
# don't add a space after completion
compopt -o nospace
# find possible matches
COMPREPLY=( $(compgen -W "${var}" -- ${cur}) )
}
# use _make_variables to complete make arguments
complete -F _make_variables make
make D[tab]
将得到make DIR=
。但是,这种方法会导致失去所有文件和目标完成的功能。另外,从完成输出中删除更多变量(例如MAKEFILE_LIST
)也会很有用。也许值得向bash-completion项目提交意见/错误报告以添加此功能。
foobar
的文件。 - Beta