cmake 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # bash completion for cmake(1) -*- shell-script -*-
  2. _cmake()
  3. {
  4. local is_old_completion=false
  5. local is_init_completion=false
  6. local cur prev words cword split was_split
  7. if type -t _comp_initialize >/dev/null; then
  8. _comp_initialize -s || return
  9. elif type -t _init_completion >/dev/null; then
  10. _init_completion -s || return
  11. is_init_completion=true
  12. else
  13. # manual initialization for older bash completion versions
  14. COMPREPLY=()
  15. cur="${COMP_WORDS[COMP_CWORD]}"
  16. prev="${COMP_WORDS[COMP_CWORD-1]}"
  17. is_old_completion=true
  18. split=false
  19. fi
  20. # Workaround for options like -DCMAKE_BUILD_TYPE=Release
  21. local prefix=
  22. if [[ $cur == -D* ]]; then
  23. prev=-D
  24. prefix=-D
  25. cur="${cur#-D}"
  26. elif [[ $cur == -U* ]]; then
  27. prev=-U
  28. prefix=-U
  29. cur="${cur#-U}"
  30. fi
  31. case "$prev" in
  32. -D)
  33. if [[ $cur == *=* ]]; then
  34. # complete values for variables
  35. local var type value
  36. var="${cur%%[:=]*}"
  37. value="${cur#*=}"
  38. if [[ $cur == CMAKE_BUILD_TYPE* ]]; then # most widely used case
  39. COMPREPLY=( $( compgen -W 'Debug Release RelWithDebInfo
  40. MinSizeRel' -- "$value" ) )
  41. return
  42. fi
  43. if [[ $cur == *:* ]]; then
  44. type="${cur#*:}"
  45. type="${type%%=*}"
  46. else # get type from cache if it's not set explicitly
  47. type=$( cmake -LA -N 2>/dev/null | grep "$var:" \
  48. 2>/dev/null )
  49. type="${type#*:}"
  50. type="${type%%=*}"
  51. fi
  52. case "$type" in
  53. FILEPATH)
  54. cur="$value"
  55. _filedir
  56. return
  57. ;;
  58. PATH)
  59. cur="$value"
  60. _filedir -d
  61. return
  62. ;;
  63. BOOL)
  64. COMPREPLY=( $( compgen -W 'ON OFF TRUE FALSE' -- \
  65. "$value" ) )
  66. return
  67. ;;
  68. STRING|INTERNAL)
  69. # no completion available
  70. return
  71. ;;
  72. esac
  73. elif [[ $cur == *:* ]]; then
  74. # complete types
  75. local type="${cur#*:}"
  76. COMPREPLY=( $( compgen -W 'FILEPATH PATH STRING BOOL INTERNAL'\
  77. -S = -- "$type" ) )
  78. compopt -o nospace
  79. else
  80. # complete variable names
  81. COMPREPLY=( $( compgen -W '$( cmake -LA -N 2>/dev/null |
  82. tail -n +2 | cut -f1 -d: )' -P "$prefix" -- "$cur" ) )
  83. compopt -o nospace
  84. fi
  85. return
  86. ;;
  87. -U)
  88. COMPREPLY=( $( compgen -W '$( cmake -LA -N | tail -n +2 |
  89. cut -f1 -d: )' -P "$prefix" -- "$cur" ) )
  90. return
  91. ;;
  92. esac
  93. if $is_old_completion; then
  94. _split_longopt && split=true
  95. fi
  96. case "$prev" in
  97. -C|-P|--graphviz|--system-information)
  98. _filedir
  99. return
  100. ;;
  101. --build)
  102. # Seed the reply with non-directory arguments that we know are
  103. # allowed to follow --build. _filedir will then prepend any valid
  104. # directory matches to these.
  105. COMPREPLY=( $( compgen -W "--preset --list-presets" -- "$cur" ) )
  106. _filedir -d
  107. return
  108. ;;
  109. --target)
  110. local quoted
  111. printf -v quoted %q "$cur"
  112. # Options allowed right after `--target`
  113. local target_options='--verbose --clean-first --config --'
  114. local build_dir="build"
  115. for ((i=0; i < ${#COMP_WORDS[@]}; i++)); do
  116. if [[ "${COMP_WORDS[i]}" == "--build" ]]; then
  117. build_dir="${COMP_WORDS[i+1]}"
  118. break
  119. fi
  120. done
  121. if [[ "$cur" == -* ]]; then
  122. COMPREPLY=( $( compgen -W "$target_options" -- "$quoted" ) )
  123. else
  124. local targets=$( cmake --build "$build_dir" --target help 2>/dev/null \
  125. | sed -n -e 's/^... \([^ ]*\).*$/\1/p' \
  126. -e '/^\[/d' -e 's/^\([^ :]*\):.*$/\1/p' | \
  127. grep -v '^/' | sort -u )
  128. COMPREPLY=( $( compgen -W "$targets $target_options" -- "$quoted" ) )
  129. fi
  130. return
  131. ;;
  132. --install|--open)
  133. _filedir -d
  134. return
  135. ;;
  136. -E)
  137. COMPREPLY=( $( compgen -W "$( cmake -E help |& sed -n \
  138. '/^ [^ ]/{s|^ \([^ ]\{1,\}\) .*$|\1|;p}' 2>/dev/null )" \
  139. -- "$cur" ) )
  140. return
  141. ;;
  142. -G)
  143. local IFS=$'\n'
  144. local quoted
  145. printf -v quoted %q "$cur"
  146. COMPREPLY=( $( compgen -W '$( cmake --help 2>/dev/null | sed -n \
  147. -e "1,/^Generators/d" \
  148. -e "/^ *[^ =]/{s|^ *\([^=]*[^ =]\).*$|\1|;s| |\\\\ |g;p}" \
  149. 2>/dev/null )' -- "$quoted" ) )
  150. return
  151. ;;
  152. --loglevel)
  153. COMPREPLY=( $(compgen -W 'error warning notice status verbose debug trace' -- $cur ) )
  154. ;;
  155. --help-command)
  156. COMPREPLY=( $( compgen -W '$( cmake --help-command-list 2>/dev/null|
  157. grep -v "^cmake version " )' -- "$cur" ) )
  158. return
  159. ;;
  160. --help-manual)
  161. COMPREPLY=( $( compgen -W '$( cmake --help-manual-list 2>/dev/null|
  162. grep -v "^cmake version " | sed -e "s/([0-9])$//" )' -- "$cur" ) )
  163. return
  164. ;;
  165. --help-module)
  166. COMPREPLY=( $( compgen -W '$( cmake --help-module-list 2>/dev/null|
  167. grep -v "^cmake version " )' -- "$cur" ) )
  168. return
  169. ;;
  170. --help-policy)
  171. COMPREPLY=( $( compgen -W '$( cmake --help-policy-list 2>/dev/null |
  172. grep -v "^cmake version " )' -- "$cur" ) )
  173. return
  174. ;;
  175. --help-property)
  176. COMPREPLY=( $( compgen -W '$( cmake --help-property-list \
  177. 2>/dev/null | grep -v "^cmake version " )' -- "$cur" ) )
  178. return
  179. ;;
  180. --help-variable)
  181. COMPREPLY=( $( compgen -W '$( cmake --help-variable-list \
  182. 2>/dev/null | grep -v "^cmake version " )' -- "$cur" ) )
  183. return
  184. ;;
  185. --list-presets)
  186. local IFS=$'\n'
  187. local quoted
  188. printf -v quoted %q "$cur"
  189. if [[ ! "${IFS}${COMP_WORDS[*]}${IFS}" =~ "${IFS}--build${IFS}" ]]; then
  190. COMPREPLY=(
  191. $( compgen -W "configure${IFS}build${IFS}package${IFS}test${IFS}workflow${IFS}all" -- "$quoted" )
  192. )
  193. fi
  194. return
  195. ;;
  196. --preset)
  197. local IFS=$'\n'
  198. local quoted
  199. printf -v quoted %q "$cur"
  200. local preset_type
  201. if [[ "${IFS}${COMP_WORDS[*]}${IFS}" =~ "${IFS}--workflow${IFS}" ]]; then
  202. preset_type="workflow"
  203. elif [[ "${IFS}${COMP_WORDS[*]}${IFS}" =~ "${IFS}--build${IFS}" ]]; then
  204. preset_type="build"
  205. else
  206. preset_type="configure"
  207. fi
  208. local presets=$( cmake --list-presets="$preset_type" 2>/dev/null |
  209. grep -o "^ \".*\"" | sed \
  210. -e "s/^ //g" \
  211. -e "s/\"//g" \
  212. -e 's/ /\\\\ /g' )
  213. COMPREPLY=( $( compgen -W "$presets" -- "$quoted" ) )
  214. return
  215. ;;
  216. --workflow)
  217. local quoted
  218. printf -v quoted %q "$cur"
  219. # Options allowed right after `--workflow`
  220. local workflow_options='--preset --list-presets --fresh'
  221. if [[ "$cur" == -* ]]; then
  222. COMPREPLY=( $( compgen -W "$workflow_options" -- "$quoted" ) )
  223. else
  224. local presets=$( cmake --list-presets=workflow 2>/dev/null |
  225. grep -o "^ \".*\"" | sed \
  226. -e "s/^ //g" \
  227. -e "s/\"//g" \
  228. -e 's/ /\\\\ /g' )
  229. COMPREPLY=( $( compgen -W "$presets $workflow_options" -- "$quoted" ) )
  230. fi
  231. return
  232. ;;
  233. esac
  234. if ($is_old_completion || $is_init_completion); then
  235. $split && return
  236. else
  237. [[ $was_split ]] && return
  238. fi
  239. if [[ "$cur" == -* ]]; then
  240. # FIXME(#26100): `cmake --help` is missing some options and does not
  241. # have any mode-specific options like `cmake --build`'s `--config`.
  242. COMPREPLY=( $(compgen -W '$( _parse_help "$1" --help )' -- ${cur}) )
  243. [[ $COMPREPLY == *= ]] && compopt -o nospace
  244. [[ $COMPREPLY ]] && return
  245. fi
  246. _filedir
  247. } &&
  248. complete -F _cmake cmake
  249. # ex: ts=4 sw=4 et filetype=sh