summary refs log tree commit diff
path: root/Completion/Unix/Command/_make
blob: 93d4b52daca26ed5b0bcef9ffa0f43fe1a8db174 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#compdef make gmake pmake dmake

local prev="$words[CURRENT-1]" file expl tmp is_gnu dir incl

expandVars() {
    local open close var val tmp=$2 ret=$2
    if (( $1 == 0 )); then
	return
    fi
    while :; do
	var=${tmp#*\$}
	if [[ $var != $tmp ]]; then
	    tmp=$var
	    case $var in
	    (\(*)
		open='('
		close=')'
		;;
	    ({*)
		open='{'
		close='}'
		;;
	    ([[:alpha:]]*)
		open=''
		close=''
		var=${(s::)var[1]}
		;;
	    (\$*)
		# avoid parsing second $ in $$
		tmp=${tmp#\$}
		;&
	    (*)
		continue
		;;
	    esac
	    if [[ $open != '' ]]; then
		var=${var#$open}
		var=${var%%$close*}
	    fi
	    case $var in
	    ([[:alnum:]_]#)
		val=${(P)var}
		val=$(expandVars $(($1 - 1)) $val)
		ret=${ret//\$$open$var$close/$val}
		;;
	    esac
	else
	    print -- ${ret//\$\$/\$}
	    return
	fi
    done
}

# parseMakefile only runs inside $(...), so it doesn't matter that
# it pollutes the global namespace, setting zsh variables to
# make variables.  The difficult case is where a make variable
# is special in zsh; we use local -h to hide those.  This
# isn't a complete solution since it means variables defined in
# included Makefiles are undefined before returning to the parent.
parseMakefile() {
    local input var val TAB=$'\t' dir=$1

    while read input; do
	case "$input " in
	([[:alnum:]][[:alnum:]_]#[ $TAB]#=*)
	    var=${input%%[ $TAB]#=*}
	    val=${input#*=}
	    val=${val##[ $TAB]#}
	    [[ ${(tP)var} = *special ]] && local -h $var
	    eval $var=\$val
	    ;;
	([[:alnum:]][[:alnum:]_]#[ $TAB]#:=*)
	    var=${input%%[ $TAB]#:=*}
	    val=${input#*=}
	    val=${val##[ $TAB]#}
	    val=$(expandVars 10 $val)
	    [[ ${(tP)var} = *special ]] && local -h $var
	    eval $var=\$val
	    ;;
	([[:alnum:]][^$TAB:=]#:[^=]*)
	    input=${input%%:*}
	    print $(expandVars 10 $input)
	    ;;
	($incl *)
	    local f=${input##$incl ##}
	    if [[ $incl = '.include' ]]; then
		f=${f#[\"<]}
		f=${f%[\">]}
	    fi
	    f=$(expandVars 10 $f)
	    case $f in
	    (/*) ;;
	    (*)  f=$dir/$f ;;
	    esac
	    if [ -r $f ]; then
		parseMakefile ${f%%/[^/]##} < $f
	    fi
	    ;;
	esac
    done
}

findBasedir () {
  local file index basedir
  basedir=$PWD
  for ((index=0; index<$#@; index++)); do
    if [[ $@[index] = -C ]]; then
      file=${~@[index+1]};
      if [[ -z $file ]]; then
	# make returns with an error if an empty arg is given
	# even if the concatenated path is a valid directory
	return
      elif [[ $file = /* ]]; then
	# Absolute path, replace base directory
	basedir=$file
      else
	# Relative, concatenate path
	basedir=$basedir/$file
      fi
    fi
  done
  print -- $basedir
}

_pick_variant -r is_gnu gnu=GNU unix -v -f

if [[ $is_gnu = gnu ]]; then
    incl=include
else
    incl=.include
fi
if [[ "$prev" = -[CI] ]]; then
  _files -W ${(q)$(findBasedir ${words[1,CURRENT-1]})} -/
elif [[ "$prev" = -[foW] ]]; then
  _files -W ${(q)$(findBasedir $words)}
else
  file="$words[(I)-f]"
  if (( file )); then
    file=${~words[file+1]}
    [[ $file = [^/]* ]] && file=${(q)$(findBasedir $words)}/$file
    [[ -r $file ]] || file=
  else
    local basedir
    basedir=${(q)$(findBasedir $words)}
    if [[ $is_gnu = gnu && -r $basedir/GNUmakefile ]]; then
      file=$basedir/GNUmakefile
    elif [[ -r $basedir/makefile ]]; then
      file=$basedir/makefile
    elif [[ -r $basedir/Makefile ]]; then
      file=$basedir/Makefile
    else
      file=''
    fi
  fi

  if [[ -n "$file" ]] && _tags targets; then
    if [[ $is_gnu = gnu ]] &&
       zstyle -t ":completion:${curcontext}:targets" call-command; then
       tmp=( $(_call_program targets "$words[1]" -nsp --no-print-directory -f "$file" .PHONY 2> /dev/null | parseMakefile $PWD) )
    else
       tmp=( $(parseMakefile $PWD < $file) )
    fi
    _wanted targets expl 'make target' compadd -a tmp && return 0
  fi
  compstate[parameter]="${PREFIX%%\=*}"
  compset -P 1 '*='
  _value "$@"
fi