Luc*_*lia 7 variables bash makefile autocomplete
让我们说我的Makefile是这样的:
DIR :=#
foobar:
ls ${DIR}
Run Code Online (Sandbox Code Playgroud)
当我打字
mak[tab] f[tab]
Run Code Online (Sandbox Code Playgroud)
它给出了正确的
make foobar
Run Code Online (Sandbox Code Playgroud)
但
make foobar D[tab]
Run Code Online (Sandbox Code Playgroud)
不做魔术
make foobar DIR=
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:有没有办法在bash中自动完成Makefile变量(除了目标)?
这个答案还远未完成。要 grep Makefile 中的所有变量,我们使用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 :=
Run Code Online (Sandbox Code Playgroud)
我们正在寻找以# makefile (from 'Makefile', line xy)以下变量名称开头的行并提取以下变量的名称:
$ make -p | sed -n '/# makefile (from/ {n; p;}'
MAKEFILE_LIST := Makefile
DIR :=
Run Code Online (Sandbox Code Playgroud)
在下一步中,我们删除除变量名称之外的所有内容( 后面的所有内容:=):
$ make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1/p;}'
MAKEFILE_LIST
DIR
Run Code Online (Sandbox Code Playgroud)
以下几行演示了如何完成此操作:
_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
Run Code Online (Sandbox Code Playgroud)
现在make D[tab]结果为make DIR=.
遗憾的是,使用这种方法您将丢失所有文件和目标完成。MAKEFILE_LIST此外,从完成输出中删除更多变量(例如 )也会很有用。
也许值得针对bash-completion 项目填写愿望/错误报告以添加此功能。