make 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. # bash completion for GNU make with kbuild extension -*- shell-script -*-
  3. # Load the default completion script for make. It is typically located at
  4. # /usr/share/bash-completion/completions/make, but we do not rely on it.
  5. __kbuild_load_default_make_completion()
  6. {
  7. local -a dirs=("${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions")
  8. local ifs=$IFS IFS=: dir compfile this_dir
  9. for dir in ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; do
  10. dirs+=("$dir"/bash-completion/completions)
  11. done
  12. IFS=$ifs
  13. this_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
  14. for dir in "${dirs[@]}"; do
  15. if [[ ! -d ${dir} || ${dir} = "${this_dir}" ]]; then
  16. continue
  17. fi
  18. for compfile in make make.bash _make; do
  19. compfile=$dir/$compfile
  20. # Avoid trying to source dirs; https://bugzilla.redhat.com/903540
  21. if [[ -f ${compfile} ]] && . "${compfile}" &>/dev/null; then
  22. __kbuild_default_make_completion=$(
  23. # shellcheck disable=SC2046 # word splitting is the point here
  24. set -- $(complete -p make)
  25. while [[ $# -gt 1 && "$1" != -F ]]; do
  26. shift
  27. done
  28. if [[ "$1" = -F ]]; then
  29. echo "$2"
  30. fi
  31. )
  32. return
  33. fi
  34. done
  35. done
  36. }
  37. __kbuild_load_default_make_completion
  38. __kbuild_handle_variable()
  39. {
  40. local var=${1%%=*}
  41. local cur=${cur#"${var}"=}
  42. local srctree=$2
  43. local keywords=()
  44. case $var in
  45. ARCH)
  46. # sub-directories under arch/
  47. keywords+=($(find "${srctree}/arch" -mindepth 1 -maxdepth 1 -type d -printf '%P\n'))
  48. # architectures hard-coded in the top Makefile
  49. keywords+=(i386 x86_64 sparc32 sparc64 parisc64)
  50. ;;
  51. CROSS_COMPILE)
  52. # toolchains with a full path
  53. local cross_compile=()
  54. local c c2
  55. _filedir
  56. for c in "${COMPREPLY[@]}"; do
  57. # eval for tilde expansion
  58. # suppress error, as this fails when it contains a space
  59. eval "c2=${c}" 2>/dev/null || continue
  60. if [[ ${c} == *-elfedit && ! -d ${c2} && -x ${c2} ]]; then
  61. cross_compile+=("${c%elfedit}")
  62. fi
  63. done
  64. # toolchains in the PATH environment
  65. while read -r c; do
  66. if [[ ${c} == *-elfedit ]]; then
  67. keywords+=("${c%elfedit}")
  68. fi
  69. done < <(compgen -c)
  70. COMPREPLY=()
  71. _filedir -d
  72. # Add cross_compile directly without passing it to compgen.
  73. # Otherwise, toolchain paths with a tilde do not work.
  74. # e.g.)
  75. # CROSS_COMPILE=~/0day/gcc-14.2.0-nolibc/aarch64-linux/bin/aarch64-linux-
  76. COMPREPLY+=("${cross_compile[@]}")
  77. ;;
  78. LLVM)
  79. # LLVM=1 uses the default 'clang' etc.
  80. keywords+=(1)
  81. # suffix for a particular version. LLVM=-18 uses 'clang-18' etc.
  82. while read -r c; do
  83. if [[ ${c} == clang-[0-9]* ]]; then
  84. keywords+=("${c#clang}")
  85. fi
  86. done < <(compgen -c)
  87. # directory path to LLVM toolchains
  88. _filedir -d
  89. ;;
  90. KCONFIG_ALLCONFIG)
  91. # KCONFIG_ALLCONFIG=1 selects the default fragment
  92. keywords+=(1)
  93. # or the path to a fragment file
  94. _filedir
  95. ;;
  96. C | KBUILD_CHECKSRC)
  97. keywords+=(1 2)
  98. ;;
  99. V | KBUILD_VERBOSE)
  100. keywords+=({,1}{,2})
  101. ;;
  102. W | KBUILD_EXTRA_WARN)
  103. keywords+=({,1}{,2}{,3}{,c}{,e})
  104. ;;
  105. KBUILD_ABS_SRCTREE | KBUILD_MODPOST_NOFINAL | KBUILD_MODPOST_WARN | \
  106. CLIPPY | KBUILD_CLIPPY | KCONFIG_NOSILENTUPDATE | \
  107. KCONFIG_OVERWRITECONFIG | KCONFIG_WARN_UNKNOWN_SYMBOL | \
  108. KCONFIG_WERROR )
  109. keywords+=(1)
  110. ;;
  111. INSTALL_MOD_STRIP)
  112. keywords+=(1 --strip-debug --strip-unneeded)
  113. ;;
  114. O | KBUILD_OUTPUT | M | KBUILD_EXTMOD | MO | KBUILD_EXTMOD_OUTPUT | *_PATH)
  115. # variables that take a directory.
  116. _filedir -d
  117. return
  118. ;;
  119. KBUILD_EXTRA_SYMBOL | KBUILD_KCONFIG | KCONFIG_CONFIG)
  120. # variables that take a file.
  121. _filedir
  122. return
  123. esac
  124. COMPREPLY+=($(compgen -W "${keywords[*]}" -- "${cur}"))
  125. }
  126. # Check the -C, -f options and 'source' symlink. Return the source tree we are
  127. # working in.
  128. __kbuild_get_srctree()
  129. {
  130. local words=("$@")
  131. local cwd makef_dir
  132. # see if a path was specified with -C/--directory
  133. for ((i = 1; i < ${#words[@]}; i++)); do
  134. if [[ ${words[i]} == -@(C|-directory) ]]; then
  135. # eval for tilde expansion.
  136. # suppress error, as this fails when it contains a space
  137. eval "cwd=${words[i + 1]}" 2>/dev/null
  138. break
  139. fi
  140. done
  141. if [[ -z ${cwd} ]]; then
  142. cwd=.
  143. fi
  144. # see if a Makefile was specified with -f/--file/--makefile
  145. for ((i = 1; i < ${#words[@]}; i++)); do
  146. if [[ ${words[i]} == -@(f|-?(make)file) ]]; then
  147. # eval for tilde expansion
  148. # suppress error, as this fails when it contains a space
  149. eval "makef_dir=${words[i + 1]%/*}" 2>/dev/null
  150. break
  151. fi
  152. done
  153. if [ -z "${makef_dir}" ]; then
  154. makef_dir=${cwd}
  155. elif [[ ${makef_dir} != /* ]]; then
  156. makef_dir=${cwd}/${makef_dir}
  157. fi
  158. # If ${makef_dir} is a build directory created by the O= option, there
  159. # is a symbolic link 'source', which points to the kernel source tree.
  160. if [[ -L ${makef_dir}/source ]]; then
  161. makef_dir=$(readlink "${makef_dir}/source")
  162. fi
  163. echo "${makef_dir}"
  164. }
  165. # Get SRCARCH to do a little more clever things
  166. __kbuild_get_srcarch()
  167. {
  168. local words=("$@")
  169. local arch srcarch uname_m
  170. # see if ARCH= is explicitly specified
  171. for ((i = 1; i < ${#words[@]}; i++)); do
  172. if [[ ${words[i]} == ARCH=* ]]; then
  173. arch=${words[i]#ARCH=}
  174. break
  175. fi
  176. done
  177. # If ARCH= is not specified, check the build marchine's architecture
  178. if [[ -z ${arch} ]]; then
  179. uname_m=$(uname -m)
  180. # shellcheck disable=SC2209 # 'sh' is SuperH, not a shell command
  181. case ${uname_m} in
  182. arm64 | aarch64*) arch=arm64 ;;
  183. arm* | sa110) arch=arm ;;
  184. i?86 | x86_64) arch=x86 ;;
  185. loongarch*) arch=loongarch ;;
  186. mips*) arch=mips ;;
  187. ppc*) arch=powerpc ;;
  188. riscv*) arch=riscv ;;
  189. s390x) arch=s390 ;;
  190. sh[234]*) arch=sh ;;
  191. sun4u) arch=sparc64 ;;
  192. *) arch=${uname_m} ;;
  193. esac
  194. fi
  195. case ${arch} in
  196. parisc64) srcarch=parisc ;;
  197. sparc32 | sparc64) srcarch=sparc ;;
  198. i386 | x86_64) srcarch=x86 ;;
  199. *) srcarch=${arch} ;;
  200. esac
  201. echo "$srcarch"
  202. }
  203. # small Makefile to parse obj-* syntax
  204. __kbuild_tmp_makefile()
  205. {
  206. cat <<'EOF'
  207. .PHONY: __default
  208. __default:
  209. $(foreach m,$(obj-y) $(obj-m) $(obj-),$(foreach s, -objs -y -m -,$($(m:%.o=%$s))) $(m))
  210. EOF
  211. echo "include ${1}"
  212. }
  213. _make_for_kbuild ()
  214. {
  215. # shellcheck disable=SC2034 # these are set by _init_completion
  216. local cur prev words cword split
  217. _init_completion -s || return
  218. local srctree
  219. srctree=$(__kbuild_get_srctree "${words[@]}")
  220. # If 'kernel' and 'Documentation' directories are found, we assume this
  221. # is a kernel tree. Otherwise, we fall back to the generic rule provided
  222. # by the bash-completion project.
  223. if [[ ! -d ${srctree}/kernel || ! -d ${srctree}/Documentation ]]; then
  224. if [ -n "${__kbuild_default_make_completion}" ]; then
  225. "${__kbuild_default_make_completion}" "$@"
  226. fi
  227. return
  228. fi
  229. # make options with a parameter (copied from the bash-completion project)
  230. case ${prev} in
  231. --file | --makefile | --old-file | --assume-old | --what-if | --new-file | \
  232. --assume-new | -!(-*)[foW])
  233. _filedir
  234. return
  235. ;;
  236. --include-dir | --directory | -!(-*)[ICm])
  237. _filedir -d
  238. return
  239. ;;
  240. -!(-*)E)
  241. COMPREPLY=($(compgen -v -- "$cur"))
  242. return
  243. ;;
  244. --eval | -!(-*)[DVx])
  245. return
  246. ;;
  247. --jobs | -!(-*)j)
  248. COMPREPLY=($(compgen -W "{1..$(($(_ncpus) * 2))}" -- "$cur"))
  249. return
  250. ;;
  251. esac
  252. local keywords=()
  253. case ${cur} in
  254. -*)
  255. # make options (copied from the bash-completion project)
  256. local opts
  257. opts="$(_parse_help "$1")"
  258. COMPREPLY=($(compgen -W "${opts:-$(_parse_usage "$1")}" -- "$cur"))
  259. if [[ ${COMPREPLY-} == *= ]]; then
  260. compopt -o nospace
  261. fi
  262. return
  263. ;;
  264. *=*)
  265. __kbuild_handle_variable "${cur}" "${srctree}"
  266. return
  267. ;;
  268. KBUILD_*)
  269. # There are many variables prefixed with 'KBUILD_'.
  270. # Display them only when 'KBUILD_' is entered.
  271. # shellcheck disable=SC2191 # '=' is appended for variables
  272. keywords+=(
  273. KBUILD_{CHECKSRC,EXTMOD,EXTMOD_OUTPUT,OUTPUT,VERBOSE,EXTRA_WARN,CLIPPY}=
  274. KBUILD_BUILD_{USER,HOST,TIMESTAMP}=
  275. KBUILD_MODPOST_{NOFINAL,WARN}=
  276. KBUILD_{ABS_SRCTREE,EXTRA_SYMBOLS,KCONFIG}=
  277. )
  278. ;;
  279. KCONFIG_*)
  280. # There are many variables prefixed with 'KCONFIG_'.
  281. # Display them only when 'KCONFIG_' is entered.
  282. # shellcheck disable=SC2191 # '=' is appended for variables
  283. keywords+=(
  284. KCONFIG_{CONFIG,ALLCONFIG,NOSILENTUPDATE,OVERWRITECONFIG}=
  285. KCONFIG_{SEED,PROBABILITY}=
  286. KCONFIG_WARN_UNKNOWN_SYMBOL=
  287. KCONFIG_WERROR=
  288. )
  289. ;;
  290. *)
  291. # By default, hide KBUILD_* and KCONFIG_* variables.
  292. # Instead, display only the prefix parts.
  293. keywords+=(KBUILD_ KCONFIG_)
  294. ;;
  295. esac
  296. if [[ ${cur} != /* && ${cur} != *//* ]]; then
  297. local dir srcarch kbuild_file tmp
  298. srcarch=$(__kbuild_get_srcarch "${words[@]}")
  299. # single build
  300. dir=${cur}
  301. while true; do
  302. if [[ ${dir} == */* ]]; then
  303. dir=${dir%/*}
  304. else
  305. dir=.
  306. fi
  307. # Search for 'Kbuild' or 'Makefile' in the parent
  308. # directories (may not be a direct parent)
  309. if [[ -f ${srctree}/${dir}/Kbuild ]]; then
  310. kbuild_file=${srctree}/${dir}/Kbuild
  311. break
  312. fi
  313. if [[ -f ${srctree}/${dir}/Makefile ]]; then
  314. kbuild_file=${srctree}/${dir}/Makefile
  315. break
  316. fi
  317. if [[ ${dir} == . ]]; then
  318. break
  319. fi
  320. done
  321. if [[ -n ${kbuild_file} ]]; then
  322. tmp=($(__kbuild_tmp_makefile "${kbuild_file}" |
  323. SRCARCH=${srcarch} obj=${dir} src=${srctree}/${dir} \
  324. "${1}" -n -f - 2>/dev/null))
  325. # Add $(obj)/ prefix
  326. if [[ ${dir} != . ]]; then
  327. tmp=("${tmp[@]/#/${dir}\/}")
  328. fi
  329. keywords+=("${tmp[@]}")
  330. fi
  331. # *_defconfig and *.config files. These might be grouped into
  332. # subdirectories, e.g., arch/powerpc/configs/*/*_defconfig.
  333. if [[ ${cur} == */* ]]; then
  334. dir=${cur%/*}
  335. else
  336. dir=.
  337. fi
  338. tmp=($(find "${srctree}/arch/${srcarch}/configs/${dir}" \
  339. "${srctree}/kernel/configs/${dir}" \
  340. -mindepth 1 -maxdepth 1 -type d -printf '%P/\n' \
  341. -o -printf '%P\n' 2>/dev/null))
  342. if [[ ${dir} != . ]]; then
  343. tmp=("${tmp[@]/#/${dir}\/}")
  344. fi
  345. keywords+=("${tmp[@]}")
  346. fi
  347. # shellcheck disable=SC2191 # '=' is appended for variables
  348. keywords+=(
  349. #
  350. # variables (append =)
  351. #
  352. ARCH=
  353. CROSS_COMPILE=
  354. LLVM=
  355. C= M= MO= O= V= W=
  356. INSTALL{,_MOD,_HDR,_DTBS}_PATH=
  357. KERNELRELEASE=
  358. #
  359. # targets
  360. #
  361. all help
  362. clean mrproper distclean
  363. clang-{tidy,analyzer} compile_commands.json
  364. coccicheck
  365. dtbs{,_check,_install} dt_binding_{check,schemas}
  366. headers{,_install}
  367. vmlinux install
  368. modules{,_prepare,_install,_sign}
  369. vdso_install
  370. tags TAGS cscope gtags
  371. rust{available,fmt,fmtcheck}
  372. kernel{version,release} image_name
  373. kselftest{,-all,-install,-clean,-merge}
  374. # configuration
  375. {,old,olddef,sync,def,savedef,rand,listnew,helpnew,test,tiny}config
  376. {,build_}{menu,n,g,x}config
  377. local{mod,yes}config
  378. all{no,yes,mod,def}config
  379. {yes2mod,mod2yes,mod2no}config
  380. # docs
  381. {html,textinfo,info,latex,pdf,epub,xml,linkcheck,refcheck,clean}docs
  382. # package
  383. {,bin,src}{rpm,deb}-pkg
  384. {pacman,dir,tar}-pkg
  385. tar{,gz,bz2,xz,zst}-pkg
  386. perf-tar{,gz,bz2,xz,zst}-src-pkg
  387. )
  388. COMPREPLY=($(compgen -W "${keywords[*]}" -- "${cur}"))
  389. # Do not append a space for variables, subdirs, "KBUILD_", "KCONFIG_".
  390. if [[ ${COMPREPLY-} == *[=/] || ${COMPREPLY-} =~ ^(KBUILD|KCONFIG)_$ ]]; then
  391. compopt -o nospace
  392. fi
  393. } && complete -F _make_for_kbuild make