Search code examples
bashmakefilegnu-makebash-completion

Bash completion for make with generic targets in a Makefile


I have a Makefile where most of my targets are created generically through a canned sequence. It seems that bash completion only suggests completions for normal targets, e.g.

target_name:
        #$@

and not for generic targets. Is there any way to make bash completion complete all the targets, even though they are not made explicit as the example above? To be more spesific, lets say I define a list of target names, and do something like this:

list=target1 target2
$(list):
        #$@

Is there some way to make these targets available for bash completion? Or, even more advanced, say I have two lists and I want the targets to be made of all possible combinations of the elements of the two lists. Can I also have these targets available for bash completion?


Solution

  • $ make -qp | grep '^[^.#].*: '
    all: bin1 bin2 bin3
    bin1: obj1.o obj2.o obj3.o
    obj1.o: obj1.c obj1.h
    ...
    $ make -qp | sed -n -e 's/^\([^.#[:space:]][^:[:space:]]*\): .*/\1/p'
    all
    bin1
    obj1.o
    ...

    The -q prevents Make from actually running anything, and the -p asks it to dump its database.

    Then all you need to do is write and register a completion function (example).