summaryrefslogtreecommitdiff
path: root/completions/make
blob: 1aec348997fb4e4eed96721608217de3894c6ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# bash completion for GNU make                             -*- shell-script -*-

_make()
{
    local cur prev words cword split
    _init_completion -s || return

    local file makef makef_dir="." makef_inc i

    case $prev in
        -f|--file|--makefile|-o|--old-file|--assume-old|-W|--what-if|\
        --new-file|--assume-new)
            _filedir
            return 0
            ;;
        -I|--include-dir|-C|--directory|-m)
            _filedir -d
            return 0
            ;;
        -E)
            COMPREPLY=( $( compgen -v -- "$cur" ) )
            return 0
            ;;
        --eval|-D|-V|-x)
            return 0
            ;;
        --jobs|-j)
            COMPREPLY=( $( compgen -W "{1..$(( $(_ncpus)*2 ))}" -- "$cur" ) )
            return 0
            ;;
    esac

    $split && return 0

    if [[ "$cur" == -* ]]; then
        local opts="$( _parse_help "$1" )"
        [[ $opts ]] || opts="$( _parse_usage "$1" )"
        COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
        [[ $COMPREPLY == *= ]] && compopt -o nospace
    elif [[ $cur == *=* ]]; then
        prev=${cur%%=*}
        cur=${cur#*=}
        local diropt
        [[ ${prev,,} == *dir?(ectory) ]] && diropt=-d
        _filedir $diropt
    else
        # before we check for makefiles, see if a path was specified
        # with -C/--directory
        for (( i=0; i < ${#words[@]}; i++ )); do
            if [[ ${words[i]} == -@(C|-directory) ]]; then
                # eval for tilde expansion
                eval makef_dir=${words[i+1]}
                break
            fi
        done

        # before we scan for targets, see if a Makefile name was
        # specified with -f/--file/--makefile
        for (( i=0; i < ${#words[@]}; i++ )); do
            if [[ ${words[i]} == -@(f|-?(make)file) ]]; then
                # eval for tilde expansion
                eval makef=${words[i+1]}
                break
            fi
        done

        [[ -n $makef ]] && makef="-f ${makef}"
        [[ -n $makef_dir ]] && makef_dir="-C ${makef_dir}"

        COMPREPLY=( $( compgen -W "$( make -qp $makef $makef_dir 2>/dev/null | \
            awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ \
            {split($1,A,/ /);for(i in A)print A[i]}' )" \
            -- "$cur" ) )

    fi
} &&
complete -F _make make gmake gnumake pmake

# ex: ts=4 sw=4 et filetype=sh