about summary refs log tree commit diff
path: root/Completion/Unix/Command/_make
blob: 510368e8b5c9f4076bb1420d8b1407e06abfd19b (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#compdef make gmake pmake dmake freebsd-make bmake

# TODO: Based on targets given on the command line, show only variables that
# are used in those targets and their dependencies.

_make-expandVars() {
  local open close var val front='' rest=$1

  while [[ $rest == (#b)[^$]#($)* ]]; do
    front=$front${rest[1,$mbegin[1]-1]}
    rest=${rest[$mbegin[1],-1]}

    case $rest[2] in
      ($)	    # '$$'. may not appear in target and variable's value
	front=$front\$\$
	rest=${rest[3,-1]}
	continue
	;;
      (\()	    # Variable of the form $(foobar)
	open='('
	close=')'
	;;
      ({)	    # ${foobar}
	open='{'
	close='}'
	;;
      ([[:alpha:]]) # $foobar. This is exactly $(f)oobar.
	open=''
	close=''
	var=$rest[2]
	;;
      (*)	    # bad parameter name
	print -- $front$rest
	return 1
	;;
    esac

    if [[ -n $open ]]; then
      if [[ $rest == \$$open(#b)([[:alnum:]_]##)(#B)$close* ]]; then
	var=$match
      else  # unmatched () or {}, or bad parameter name
	print -- $front$rest
	return 1
      fi
    fi

    val=''
    if [[ -n ${VAR_ARGS[(i)$var]} ]]; then
      val=${VAR_ARGS[$var]}
    else
      if [[ -n $opt_args[(I)(-e|--environment-overrides)] ]]; then
	if [[ $parameters[$var] == scalar-export* ]]; then
	  val=${(P)var}
	elif [[ -n ${VARIABLES[(i)$var]} ]]; then
	  val=${VARIABLES[$var]}
	fi
      else
	if [[ -n ${VARIABLES[(i)$var]} ]]; then
	  val=${VARIABLES[$var]}
	elif [[ $parameters[$var] == scalar-export* ]]; then
	  val=${(P)var}
	fi
      fi
    fi
    rest=${rest//\$$open$var$close/$val}
  done

  print -- ${front}${rest}
}

_make-parseMakefile () {
  local input var val target dep TAB=$'\t' tmp IFS=

  while read input
  do
    case "$input " in
      # VARIABLE = value OR VARIABLE ?= value
      ([[:alnum:]][[:alnum:]_]#[" "$TAB]#(\?|)=*)
      var=${input%%[ $TAB]#(\?|)=*}
      val=${input#*=}
      val=${val##[ $TAB]#}
      VARIABLES[$var]=$val
      ;;

      # VARIABLE := value OR VARIABLE ::= value
      # Evaluated immediately
      ([[:alnum:]][[:alnum:]_]#[" "$TAB]#:(:|)=*)
      var=${input%%[ $TAB]#:(:|)=*}
      val=${input#*=}
      val=${val##[ $TAB]#}
      val=$(_make-expandVars $val)
      VARIABLES[$var]=$val
      ;;

      # TARGET: dependencies
      # TARGET1 TARGET2 TARGET3: dependencies
      ([[*?[:alnum:]$][^$TAB:=%]#:[^=]*)
      target=$(_make-expandVars ${input%%:*})
      TARGETS+=( ${(z)target} )
      ;;

      # Include another makefile
      (${~incl}" "*)
      local f=${input##${~incl} ##}
      if [[ $incl == '.include' ]]
      then
        f=${f#[\"<]}
        f=${f%[\">]}
      fi
      f=$(_make-expandVars $f)

      if [[ -r $f ]]
      then
        _make-parseMakefile < $f
      fi
      ;;
    esac
  done
}

_make-parseDataBase () {
  local input var TAB=$'\t' IFS= skip=0

  while read input
  do
    if [[ $skip = 1 ]]; then
      skip=0
      continue
    fi
    case "$input " in
      (\# Not a target*|\# environment*)
        skip=1  # skip next line
        ;;
      ([[:alnum:]][[:alnum:]_]#[" "$TAB]#(\?|:|::|)=*)
        var=${input%%[ $TAB]#(\?|:|::|)=*}
        VARIABLES[$var]=1
        ;;
      ([[*?[:alnum:]$][^$TAB:=%]#:[^=]*)
        TARGETS+=( ${input%%:*} )
        ;;
    esac
  done
}

_make() {

  local prev="$words[CURRENT-1]" file expl tmp is_gnu incl match basedir nul=$'\0'
  local curcontext=$curcontext state state_descr line
  local -a option_specs
  local -A VARIABLES VAR_ARGS opt_args
  local -aU TARGETS keys
  local -i cdir=-1 ret=1

  # VAR=VAL on the current command line
  for tmp in $words; do
    if [[ $tmp == (#b)([[:alnum:]_]##)=(*) ]]; then
      VAR_ARGS[${tmp[$mbegin[1],$mend[1]]}]=${(e)tmp[$mbegin[2],$mend[2]]}
    fi
  done
  keys=( ${(k)VAR_ARGS} ) # to be used in 'compadd -F keys'

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

  if [[ $is_gnu == gnu ]]
  then
    incl="(-|)include"
    option_specs=(
      '(-B --always-make)'{-B,--always-make}'[unconditionally make all targets]'
      '*'{-C,--directory=}'[change directory first]:change to directory:->cdir'
      '-d[print lots of debug information]'
      '--debug=-[print various types of debug information]:debug options:->debug'
      '(-e --environment-overrides)'{-e,--environment-overrides}'[environment variables override makefiles]'
      \*{-E+,--eval=-}'[evaluate string as a makefile statement]:string'
      '(-f --file --makefile)'{-f,--file=,--makefile=}'[read specified file as a makefile]:makefile:->file'
      '(- *)'{-h,--help}'[print help message and exit]'
      '(-i --ignore-errors)'{-i,--ignore-errors}'[ignore errors from recipes]'
      '*'{-I,--include-dir=}'[search specified directory for included makefiles]:search path for included makefile:->dir'
      '(-j --jobs)'{-j+,--jobs=}'[allow specified number of parallel jobs; unlimited jobs with no arg]:: : _guard "[0-9]#" "number of jobs"'
      '(-k --keep-going)'{-k,--keep-going}"[keep going when some targets can't be made]"
      '(-l --load-average --max-load)'{-l,--load-average=,--max-load}"[don't start multiple jobs unless load is below specified value]:load"
      '(-L --check-symlink-times)'{-L,--check-symlink-times}'[use the latest mtime between symlinks and target]'
      '(-n --just-print --dry-run --recon)'{-n,--just-print,--dry-run,--recon}"[don't actually run any recipe; just print them]"
      '*'{-o,--old-file=,--assume-old=}"[consider specified file to be old and don't remake it]:file not to remake:->file"
      '(-O --output-sync)'{-O-,--output-sync=-}'[synchronize output of parallel jobs]::granularity for grouping output:compadd -E 0 none line target recurse'
      '(-p --print-data-base)'{-p,--print-data-base}'[print makes internal database]'
      '(-q --question)'{-q,--question}'[run no recipe; exit status says if up to date]'
      '(-r --no-builtin-rules)'{-r,--no-builtin-rules}'[disable the built-in implicit rules]'
      '(-R --no-builtin-variables)'{-R,--no-builtin-variables}'[disable the built-in variable settings]'
      '(-s --silent --quiet)'{-s,--silent,--quiet}"[don't echo recipes]"
      '--no-silent[echo recipes (disable --silent mode)]'
      '(-S --no-keep-going --stop)'{-S,--no-keep-going,--stop}'[turns off -k]'
      '(-t --touch)'{-t,--touch}'[touch targets instead of remaking them]'
      '(- *)'{-v,--version}'[print the version number of make and exit]'
      '(-w --print-directory)'{-w,--print-directory}'[print the current directory]'
      '--no-print-directory[turn off -w, even if it was turned on implicitly]'
      '*'{-W,--what-if=,--new-file=,--assume-new=}'[consider specified file to be infinitely new]:file to treat as modified:->file'
      '--warn-undefined-variables[warn when an undefined variable is referenced]'
      '--warn-undefined-functions[warn when an undefined user function is called]'
    )
  else
    # Basic make options only.
    incl=.include
    option_specs=(
      '-C[change directory first]:directory:->cdir'
      '-I[include directory for makefiles]:directory:->dir'
      '-f[specify makefile]:makefile:->file'
      '-o[specify file not to remake]:file not to remake:->file'
      '-W[pretend file was modified]:file to treat as modified:->file'
    )
  fi

  _arguments -C -S -s $option_specs \
    '*:make target:->target' && ret=0

  [[ $state = cdir ]] && cdir=-2
  basedir=${(j./.)${${~"${(@s.:.):-$PWD:${(Q)${opt_args[-C]:-$opt_args[--directory]}//\\:/$nul}}"}[(R)/*,cdir]}//$nul/:}
  VAR_ARGS[CURDIR]="${basedir}"

  case $state in
    (*dir)
    _description directories expl "$state_descr"
    _files "$expl[@]" -W $basedir -/ && ret=0
    ;;

    (file)
    _description files expl "$state_descr"
    _files "$expl[@]" -W $basedir && ret=0
    ;;

    (debug)
    _values -s , 'debug option' \
      '(b v i j m)a[all debugging output]' \
      'b[basic debugging output]' \
      '(b)v[one level above basic]' \
      '(b)i[describe implicit rule searches (implies b)]' \
      'j[show details on invocation of subcommands]' \
      'm[enable debugging while remaking makefiles]' && ret=0
    ;;

    (target)
    # target name starting with '-' is allowed only after '--'
    if [[ $words[CURRENT] = -* ]] && (( ${words[(i)--]} > CURRENT )); then
      return ret
    fi
    file=${(v)opt_args[(I)(-f|--file|--makefile)]}
    if [[ -n $file ]]
    then
      [[ $file == [^/]* ]] && file=$basedir/$file
      [[ -r $file ]] || file=
    else
      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" ]]
    then
      if [[ $is_gnu == gnu ]] 
      then
        if zstyle -t ":completion:${curcontext}:targets" call-command; then
          _make-parseDataBase < <(_call_program targets "$words[1]" -nqp --no-print-directory -f "$file" .DEFAULT 2> /dev/null)
        else
          _make-parseMakefile < $file
        fi
      else
        if [[ $OSTYPE == (freebsd|dragonfly|netbsd)* || /$words[1] == */bmake* ]]; then
          TARGETS+=(${=${(f)"$(_call_program targets "$words[1]" -s -f "$file" -V.ALLTARGETS 2> /dev/null)"}})
          _make-parseMakefile < <(_call_program targets "$words[1]" -nsdg1Fstdout -f "$file" .PHONY 2> /dev/null)
        else
          _make-parseMakefile < $file
        fi
      fi
    fi

    if [[ $PREFIX == *'='* ]]
    then
      # Complete make variable as if shell variable
      compstate[parameter]="${PREFIX%%\=*}"
      compset -P 1 '*='
      _value "$@" && ret=0
    else
      _alternative \
        'targets:make target:compadd -Q -a TARGETS' \
        'variables:make variable:compadd -S = -F keys -k VARIABLES' \
        '*:file:_files -W $basedir' && ret=0
    fi
  esac

  return ret
}

_make "$@"