加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash中的Makefile变量自动完成

发布时间:2020-12-15 22:57:45 所属栏目:安全 来源:网络整理
导读:假设我的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开头的行(来自’Makefile’,行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

现在让D [tab]结果为make DIR =.

遗憾的是,您将使用此方法完成所有文件和目标的完成.另外,从完成输出中删除一些更多变量(例如MAKEFILE_LIST)会很有用.

也许值得填写针对bash-completion project的愿望/错误报告来添加此功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读