Makefile variable autofill in bash

Say my Makefile looks something like this:

 DIR :=# foobar: ls ${DIR} 

When i type

 mak[tab] f[tab] 

he gives correctly

 make foobar 

But

 make foobar D[tab] 

doesn't perform magic

 make foobar DIR= 

So my question is: is there a way to autocomplete Makefile variables (towards goals) in bash?

+5
source share
1 answer

This answer is far from complete. To grep all the variables in the Makefile, we use make -p to print the Makefile database:

 # 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 := 

We look for lines starting with # makefile (from 'Makefile', line xy) and extract the name of the following variable:

 $ make -p | sed -n '/# makefile (from/ {n; p;}' MAKEFILE_LIST := Makefile DIR := 

In the next step, we will delete everything except the variable name (everything after := ):

 $ make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1/p;}' MAKEFILE_LIST DIR 

The following lines show how to do this:

 _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 

Now make D[tab] leads to make DIR= .

Unfortunately, you will lose the entire file and achieve the goal with this approach. It would also be useful to remove a few more variables (e.g. MAKEFILE_LIST ) from the completion output.

It might be worth adding a wish / bug report to the bash -completion project to add this feature.

+2
source

Source: https://habr.com/ru/post/1202240/


All Articles