My personal dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_cmake 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #compdef cmake
  2. # ------------------------------------------------------------------------------
  3. # Copyright (c) 2016 Github zsh-users - http://github.com/zsh-users
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of the zsh-users nor the
  14. # names of its contributors may be used to endorse or promote products
  15. # derived from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. # -------------------------------------------------------------------------
  28. # Description
  29. # -----------
  30. #
  31. # Completion script for CMake (http://www.cmake.org).
  32. #
  33. # -------------------------------------------------------------------------
  34. # Authors
  35. # -------
  36. #
  37. # * Scott M. Kroll <skroll@gmail.com> (initial version)
  38. # * Paul Seyfert <pseyfert@mathphys.fsk.uni-heidelberg.de> (handling of --build)
  39. #
  40. # -------------------------------------------------------------------------
  41. # Notes
  42. # -----
  43. #
  44. # * By default only C and C++ languages are supported for compiler flag
  45. # variables. To define your own list of languages:
  46. #
  47. # cmake_langs=('C' 'C'
  48. # 'CXX' 'C++')
  49. # zstyle ':completion:*:cmake:*' languages $cmake_langs
  50. #
  51. # -------------------------------------------------------------------------
  52. local context state line curcontext="$curcontext" cmake_args
  53. local cmake_build_options;cmake_build_options=(
  54. '-C[Pre-load a script to populate the cache]:script:_files'
  55. '*-D-[Create a cmake cache entry]:property:_cmake_define_property'
  56. '-U[Remove matching entries from CMake cache]:globbing expression'
  57. '-G[Specify a makefile generator]:generator:_cmake_generators'
  58. '-T[Specify toolset name if supported by generator]:toolset name'
  59. '(-Wno-dev -Wdev)-Wno-dev[Suppress/Enable developer warnings]'
  60. '(-Wno-dev -Wdev)-Wdev[Suppress/Enable developer warnings]'
  61. '(-Wno-deprecated -Wdeprecated)-Wno-deprecated[Suppress/Enable deprecation warnings]'
  62. '(-Wno-deprecated -Wdeprecated)-Wdeprecated[Suppress/Enable deprecation warnings]'
  63. '(-Wno-error=dev -Werror=dev)-Wno-error=dev[Make developer warnings (not) errors]'
  64. '(-Wno-error=dev -Werror=dev)-Werror=dev[Make developer warnings (not) errors]'
  65. '(-Wno-error=deprecated -Werror=deprecated)-Werror=deprecated[Make deprecated macro and function warnings (not) errors]'
  66. '(-Wno-error=deprecated -Werror=deprecated)-Wno-error=deprecated[Make deprecated macro and function warnings (not) errors]'
  67. '--warn-uninitialized[Warn about uninitialized values.]'
  68. '--warn-unused-vars[Warn about unused variables.]'
  69. '--no-warn-unused-cli[Dont warn about command line options.]'
  70. '-i[Run in wizard mode]'
  71. '-L-[List cache variables]::_values "options" "[non-advanced cache variables]" "A[advanced cache variables]" "H[non-advanced cached variables with help]" "AH[advanced cache variables with help]"'
  72. '--trace[Put cmake in trace mode]'
  73. '--find-package[Run in pkg-config like mode.]'
  74. ':cmake project:_files -/'
  75. )
  76. # ------------------------
  77. # _cmake_generator_options
  78. # ------------------------
  79. (( $+functions[_cmake_generator_options] )) ||
  80. _cmake_generator_options() {
  81. if [ -f $1/Makefile ]
  82. then
  83. $_comps[make]
  84. elif [ -f $1/build.ninja ]
  85. then
  86. $_comps[ninja]
  87. fi
  88. }
  89. # --------------
  90. # _cmake_targets
  91. # --------------
  92. (( $+functions[_cmake_targets] )) ||
  93. _cmake_targets() {
  94. local -a targets
  95. if [ -f $1/Makefile ]
  96. then
  97. # `make help` doesn't work for Makefiles in general, but for cmake generated makefiles it does.
  98. i=1
  99. for target in $(make help | \grep -e "\.\.\." | sed "s/\.\.\. //" | sed "s/ (the default.*//") ; do
  100. targets[$i]=$target
  101. (( i = $i + 1 ))
  102. done
  103. elif [ -f $1/build.ninja ]
  104. then
  105. # `ninja help` doesn't seem to be the list of targets we're interested in
  106. i=1
  107. for target in $(ninja -C $1 -t targets all 2&>/dev/null | awk -F: '{print $1}') ; do
  108. targets[$i]="$target"
  109. (( i++ ))
  110. done
  111. fi
  112. _describe 'build targets' targets
  113. }
  114. _cmake_on_build() {
  115. local build_extras;build_extras=(
  116. '--[Native build tool options]'
  117. '--target[specify build target]'
  118. '--clean-first[build target clean first]'
  119. '--config[For multi-configuration tools]'
  120. '--use-stderr')
  121. local -a undescribed_build_extras
  122. i=1
  123. for be in $build_extras ; do
  124. undescribed_build_extras[$i]=$(echo $be | sed "s/\[.*//")
  125. (( i++ ))
  126. done
  127. inbuild=false
  128. nativemode=false
  129. for ((i = (($CURRENT - 1)); i > 1 ; i--)); do
  130. if [[ $words[$i] == --build ]] ; then
  131. inbuild=true
  132. buildat=$i
  133. (( difference = $CURRENT - $i ))
  134. elif [[ $words[$i] == -- ]] ; then
  135. nativemode=true
  136. fi
  137. done
  138. # check if build mode has been left
  139. outofbuild=false
  140. for ((i = (($CURRENT - 1)); i > (($buildat + 1)); i--)); do
  141. # don't check the word after --build (should be a directory)
  142. if [[ ${undescribed_build_extras[(r)$words[$i]]} == $words[$i] ]] ; then continue ; fi
  143. if [[ $words[(($i - 1))] == --target ]] ; then continue ; fi
  144. if [[ $words[(($i - 1))] == --config ]] ; then continue ; fi
  145. outofbuild=true
  146. done
  147. if [ "$nativemode" = true ] ; then
  148. _cmake_generator_options $words[(($buildat + 1))] && return 0
  149. fi
  150. if [ "$inbuild" = false ] ; then
  151. _arguments -C -s \
  152. - build_opts \
  153. "$cmake_build_options[@]" \
  154. - build_cmds \
  155. "$cmake_suggest_build[@]" && return 0
  156. elif [ $difference -eq 1 ] ; then
  157. # directly after --build comes the build directory
  158. _alternative ':current directory:(.)' 'directory::_directories' && return 0
  159. elif [[ $words[(($CURRENT - 1))] == --target ]] ; then
  160. # after --build <dir> --target, suggest targets
  161. _cmake_targets $words[(($buildat + 1))] && return 0
  162. elif [[ $words[(($CURRENT - 1))] == --config ]] ; then
  163. # after --build <dir> --config, no idea
  164. return 0
  165. elif [ "$outofbuild" = true ] ; then
  166. # after --build <dir> --<not a --build option>, suggest other cmake_build_options (like -Wno-dev)
  167. _arguments "$cmake_build_options[@]" && return 0
  168. else
  169. # after --build <dir>, suggest other cmake_build_options (like -Wno-dev) or --build options (like --clean-first)
  170. _arguments "$build_extras[@]" "$cmake_build_options[@]" && return 0
  171. fi
  172. }
  173. local cmake_help_actions;cmake_help_actions=(
  174. '(- 1)--help-command[Print help for a single command and exit]:command-name:_cmake_command_names'
  175. '(- 1)--help-command-list[List available listfile commands and exit]'
  176. '(- 1)--help-commands[Print help for all commands and exit]'
  177. '(- 1)--help-compatcommands[Print help for compatibility commands]'
  178. '(- 1)--help-module[Print help for compatibility commands]:module-name:_cmake_module_names'
  179. '(- 1)--help-module-list[Print help for a single module and exit]'
  180. '(- 1)--help-modules[Print help for all modules and exit]'
  181. '(- 1)--help-property[List available properties and exit]:property-name:_cmake_property_names'
  182. '(- 1)--help-property-list[List available properties and exit]'
  183. '(- 1)--help-properties[Print help for all properties and exit]'
  184. '(- 1)--help-variable[Print help for a single variable and exit]:variable-name:_cmake_variable_names'
  185. '(- 1)--help-variable-list[List documented variables and exit]'
  186. '(- 1)--help-variables[Print help for all variables and exit]'
  187. '(- 1)--copyright[Print the CMake copyright and exit]'
  188. '(- 1)'{--help,-help,-usage,-h,-H}'[Print usage information and exit]'
  189. '(- 1)--help-full[Print full help and exit]'
  190. '(- 1)--help-html[Print full help in HTML format]'
  191. '(- 1)--help-man[Print full help as a UNIX man page and exit]'
  192. '(- 1)'{--version,-version}'[Print full help as a UNIX man page and exit]'
  193. )
  194. _cmake_help() {
  195. _arguments -C -s - help "$cmake_help_actions[@]"
  196. }
  197. # -------------------
  198. # _cmake_command_names
  199. # -------------------
  200. (( $+functions[_cmake_command_names] )) ||
  201. _cmake_command_names() {
  202. local command_names; command_names=(${(f)"$($service --help-command-list 2> /dev/null)"})
  203. _values 'command name' ${command_names[@]:1} && return 0
  204. }
  205. # -----------------
  206. # _cmake_list_names
  207. # -----------------
  208. (( $+functions[_cmake_list_names] )) ||
  209. _cmake_list_names() {
  210. local command; command="$@[1]"
  211. local desc; desc="$@[2]"
  212. local list_names; list_names=(${(f)"$($service $command 2> /dev/null | sed -e 's/\[/\\\[/' -e 's/\]/\\\]/')"})
  213. _values ${desc} ${list_names[@]:1} && return 0
  214. }
  215. # ------------------
  216. # _cmake_module_names
  217. # ------------------
  218. (( $+functions[_cmake_module_names] )) ||
  219. _cmake_module_names() {
  220. _cmake_list_names '--help-module-list' 'module name' && return 0
  221. }
  222. # --------------------
  223. # _cmake_property_names
  224. # --------------------
  225. (( $+functions[_cmake_property_names] )) ||
  226. _cmake_property_names() {
  227. _cmake_list_names '--help-property-list' 'property name' && return 0
  228. }
  229. # ---------------------
  230. # _cmake_variable_names
  231. # ---------------------
  232. (( $+functions[_cmake_variable_names] )) ||
  233. _cmake_variable_names() {
  234. _cmake_list_names '--help-variable-list' 'variable name' && return 0
  235. }
  236. # ----------------------
  237. # _cmake_define_property
  238. # ----------------------
  239. (( $+functions[_cmake_define_property] )) ||
  240. _cmake_define_property() {
  241. if compset -P '*='; then
  242. _wanted property-values expl 'property value' _cmake_define_property_values ${${IPREFIX%=}#-D} && return 0
  243. else
  244. _wanted property-names expl 'property name' _cmake_define_property_names -qS= && return 0
  245. fi
  246. }
  247. # ----------------------------
  248. # _cmake_define_property_names
  249. # ----------------------------
  250. (( $+functions[_cmake_define_property_names] )) ||
  251. _cmake_define_property_names() {
  252. local alternatives; alternatives=(
  253. 'common-property-names:common property name:_cmake_define_common_property_names -qS='
  254. )
  255. local -A cmake_langs
  256. zstyle -a ":completion:${curcontext}:" languages cmake_langs
  257. [[ $#cmake_langs -eq 0 ]] && cmake_langs=('C' 'C' 'CXX' 'C++')
  258. for cmake_lang in ${(k)cmake_langs}; do
  259. cmake_lang_desc="${cmake_langs[$cmake_lang]}"
  260. alternatives+=("${cmake_lang//:/-}-property-names:${cmake_lang_desc} language property name:_cmake_define_lang_property_names -qS= ${cmake_lang} ${cmake_lang_desc}")
  261. done
  262. _alternative "${alternatives[@]}"
  263. }
  264. # ---------------------------------
  265. # _cmake_define_lang_property_names
  266. # ---------------------------------
  267. (( $+functions[_cmake_define_lang_property_names] )) ||
  268. _cmake_define_lang_property_names() {
  269. local cmake_lang="$@[-2]" cmake_lang_desc="$@[-1]"
  270. local properties; properties=(
  271. "CMAKE_${cmake_lang}_COMPILER:${cmake_lang_desc} compiler"
  272. "CMAKE_${cmake_lang}_FLAGS:${cmake_lang_desc} compiler flags for all builds"
  273. "CMAKE_${cmake_lang}_FLAGS_DEBUG:${cmake_lang_desc} compiler flags for all Debug build"
  274. "CMAKE_${cmake_lang}_FLAGS_RELEASE:${cmake_lang_desc} compiler flags for all Relase build"
  275. "CMAKE_${cmake_lang}_FLAGS_MINSIZREL:${cmake_lang_desc} compiler flags for all MinSizRel build"
  276. "CMAKE_${cmake_lang}_FLAGS_RELWITHDEBINFO:${cmake_lang_desc} compiler flags for all RelWithDebInfo build"
  277. )
  278. _describe -t "${cmake_lang//:/-}-property-names" "${cmake_lang_desc} property name" properties $@[0,-3] && return 0
  279. }
  280. # -----------------------------------
  281. # _cmake_define_common_property_names
  282. # -----------------------------------
  283. (( $+functions[_cmake_define_common_property_names] )) ||
  284. _cmake_define_common_property_names() {
  285. local properties; properties=(
  286. 'CMAKE_MODULE_PATH:Search path for cmake modules'
  287. 'CMAKE_BUILD_TYPE:Specifies the build type for make based generators'
  288. 'CMAKE_TOOLCHAIN_FILE:Absolute or relative path to a cmake script which sets up toolchain related variables'
  289. 'CMAKE_COLOR_MAKEFILE:Enables/disables color output when using the Makefile generator'
  290. 'CMAKE_INSTALL_PREFIX:Install directory used by install'
  291. 'CMAKE_EXPORT_COMPILE_COMMANDS:Enable/disable output of compilation database during generation'
  292. )
  293. _describe -t 'common-property-names' 'common property name' properties $@
  294. }
  295. # ----------------------------
  296. # _cmake_define_property_values
  297. # ----------------------------
  298. (( $+functions[_cmake_define_property_values] )) ||
  299. _cmake_define_property_values() {
  300. local ret=1
  301. setopt localoptions extendedglob
  302. case $@[-1] in
  303. (CMAKE_BUILD_TYPE) _wanted build-types expl 'build type' _cmake_build_types && ret=0;;
  304. (CMAKE_TOOLCHAIN_FILE) _wanted toolchain-files expl 'file' _cmake_toolchain_files && ret=0;;
  305. (CMAKE_COLOR_MAKEFILE) _wanted booleans expl 'boolean' _cmake_booleans && ret=0;;
  306. (CMAKE_INSTALL_PREFIX) _files -/ && ret=0;;
  307. (CMAKE_EXPORT_COMPILE_COMMANDS) _wanted booleans expl 'boolean' _cmake_booleans && ret=0;;
  308. (CMAKE_*_COMPILER) _wanted compilers expl 'compiler' _cmake_compilers && ret=0;;
  309. (CMAKE_*_FLAGS(|_?*)) _message -e compiler-flags 'compiler flags' && ret=0;;
  310. (*) _files && ret=0;;
  311. esac
  312. return ret
  313. }
  314. # ------------------
  315. # _cmake_build_types
  316. # ------------------
  317. (( $+functions[_cmake_build_types] )) ||
  318. _cmake_build_types() {
  319. local build_types; build_types=(
  320. 'Debug'
  321. 'Release'
  322. 'RelWithDebInfo'
  323. 'MinSizeRel'
  324. )
  325. _values 'build type' ${build_types[@]}
  326. }
  327. # -----------------
  328. # _cmake_generators
  329. # -----------------
  330. (( $+functions[_cmake_generators] )) ||
  331. _cmake_generators() {
  332. local generators; generators=(
  333. 'Unix Makefiles'
  334. 'Ninja'
  335. 'CodeBlocks - Ninja'
  336. 'CodeBlocks - Unix Makefiles'
  337. 'Eclipse CDT4 - Ninja'
  338. 'Eclipse CDT4 - Unix Makefiles'
  339. 'KDevelop3'
  340. 'KDevelop3 - Unix Makefiles'
  341. 'Sublime Text 2 - Ninja'
  342. 'Sublime Text 2 - Unix Makefiles'
  343. )
  344. _describe -t generators 'generator' generators
  345. }
  346. # ----------------------
  347. # _cmake_toolchain_files
  348. # ----------------------
  349. (( $+functions[_cmake_toolchain_files] )) ||
  350. _cmake_toolchain_files() {
  351. _files -g '*\.cmake*'
  352. }
  353. # ---------------
  354. # _cmake_booleans
  355. # ---------------
  356. (( $+functions[_cmake_booleans] )) ||
  357. _cmake_booleans() {
  358. local booleans; booleans=(
  359. 'YES'
  360. 'NO'
  361. )
  362. _describe -t booleans 'boolean' booleans
  363. }
  364. # ---------------
  365. # _cmake_compilers
  366. # ---------------
  367. (( $+functions[_cmake_compilers] )) ||
  368. _cmake_compilers() {
  369. _command_names -e
  370. }
  371. local cmake_command_actions;cmake_command_actions=(
  372. '-E[CMake command mode]:*:command'
  373. )
  374. _cmake_command() {
  375. _arguments -C -s - command "$cmake_command_actions[@]"
  376. }
  377. local cmake_suggest_build;cmake_suggest_build=(
  378. '--build[build]'
  379. )
  380. if [ $CURRENT -eq 2 ] ; then
  381. _arguments -C -s \
  382. - help \
  383. "$cmake_help_actions[@]" \
  384. - command \
  385. "$cmake_command_actions[@]" \
  386. - build_opts \
  387. "$cmake_build_options[@]" \
  388. - build_cmds \
  389. "$cmake_suggest_build[@]" && return 0
  390. elif [[ $words[2] = --help* ]] ; then
  391. _cmake_help
  392. elif [[ $words[2] != -E ]] ; then
  393. _cmake_on_build
  394. else
  395. _cmake_command
  396. fi