1 # SPDX-License-Identifier: GPL-2.0-only
2 # bash completion for GNU make with kbuild extension -*- shell-script -*-
4 # Load the default completion script for make. It is typically located at
5 # /usr/share/bash-completion/completions/make, but we do not rely on it.
6 __kbuild_load_default_make_completion()
8 local -a dirs=("${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions")
9 local ifs=$IFS IFS=: dir compfile this_dir
11 for dir in ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; do
12 dirs+=("$dir"/bash-completion/completions)
16 this_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
18 for dir in "${dirs[@]}"; do
19 if [[ ! -d ${dir} || ${dir} = "${this_dir}" ]]; then
23 for compfile in make make.bash _make; do
24 compfile=$dir/$compfile
25 # Avoid trying to source dirs; https://bugzilla.redhat.com/903540
26 if [[ -f ${compfile} ]] && . "${compfile}" &>/dev/null; then
28 __kbuild_default_make_completion=$(
29 # shellcheck disable=SC2046 # word splitting is the point here
30 set -- $(complete -p make)
32 while [[ $# -gt 1 && "$1" != -F ]]; do
36 if [[ "$1" = -F ]]; then
47 __kbuild_load_default_make_completion
49 __kbuild_handle_variable()
52 local cur=${cur#"${var}"=}
58 # sub-directories under arch/
59 keywords+=($(find "${srctree}/arch" -mindepth 1 -maxdepth 1 -type d -printf '%P\n'))
60 # architectures hard-coded in the top Makefile
61 keywords+=(i386 x86_64 sparc32 sparc64 parisc64)
64 # toolchains with a full path
65 local cross_compile=()
69 for c in "${COMPREPLY[@]}"; do
70 # eval for tilde expansion
71 # suppress error, as this fails when it contains a space
72 eval "c2=${c}" 2>/dev/null || continue
73 if [[ ${c} == *-elfedit && ! -d ${c2} && -x ${c2} ]]; then
74 cross_compile+=("${c%elfedit}")
78 # toolchains in the PATH environment
80 if [[ ${c} == *-elfedit ]]; then
81 keywords+=("${c%elfedit}")
88 # Add cross_compile directly without passing it to compgen.
89 # Otherwise, toolchain paths with a tilde do not work.
91 # CROSS_COMPILE=~/0day/gcc-14.2.0-nolibc/aarch64-linux/bin/aarch64-linux-
92 COMPREPLY+=("${cross_compile[@]}")
95 # LLVM=1 uses the default 'clang' etc.
98 # suffix for a particular version. LLVM=-18 uses 'clang-18' etc.
100 if [[ ${c} == clang-[0-9]* ]]; then
101 keywords+=("${c#clang}")
105 # directory path to LLVM toolchains
109 # KCONFIG_ALLCONFIG=1 selects the default fragment
111 # or the path to a fragment file
120 W | KBUILD_EXTRA_WARN)
121 keywords+=({,1}{,2}{,3}{,c}{,e})
123 KBUILD_ABS_SRCTREE | KBUILD_MODPOST_NOFINAL | KBUILD_MODPOST_WARN | \
124 CLIPPY | KBUILD_CLIPPY | KCONFIG_NOSILENTUPDATE | \
125 KCONFIG_OVERWRITECONFIG | KCONFIG_WARN_UNKNOWN_SYMBOL | \
130 keywords+=(1 --strip-debug --strip-unneeded)
132 O | KBUILD_OUTPUT | M | KBUILD_EXTMOD | MO | KBUILD_EXTMOD_OUTPUT | *_PATH)
133 # variables that take a directory.
137 KBUILD_EXTRA_SYMBOL | KBUILD_KCONFIG | KCONFIG_CONFIG)
138 # variables that take a file.
143 COMPREPLY+=($(compgen -W "${keywords[*]}" -- "${cur}"))
146 # Check the -C, -f options and 'source' symlink. Return the source tree we are
148 __kbuild_get_srctree()
153 # see if a path was specified with -C/--directory
154 for ((i = 1; i < ${#words[@]}; i++)); do
155 if [[ ${words[i]} == -@(C|-directory) ]]; then
156 # eval for tilde expansion.
157 # suppress error, as this fails when it contains a space
158 eval "cwd=${words[i + 1]}" 2>/dev/null
163 if [[ -z ${cwd} ]]; then
167 # see if a Makefile was specified with -f/--file/--makefile
168 for ((i = 1; i < ${#words[@]}; i++)); do
169 if [[ ${words[i]} == -@(f|-?(make)file) ]]; then
170 # eval for tilde expansion
171 # suppress error, as this fails when it contains a space
172 eval "makef_dir=${words[i + 1]%/*}" 2>/dev/null
177 if [ -z "${makef_dir}" ]; then
179 elif [[ ${makef_dir} != /* ]]; then
180 makef_dir=${cwd}/${makef_dir}
183 # If ${makef_dir} is a build directory created by the O= option, there
184 # is a symbolic link 'source', which points to the kernel source tree.
185 if [[ -L ${makef_dir}/source ]]; then
186 makef_dir=$(readlink "${makef_dir}/source")
192 # Get SRCARCH to do a little more clever things
193 __kbuild_get_srcarch()
196 local arch srcarch uname_m
198 # see if ARCH= is explicitly specified
199 for ((i = 1; i < ${#words[@]}; i++)); do
200 if [[ ${words[i]} == ARCH=* ]]; then
201 arch=${words[i]#ARCH=}
206 # If ARCH= is not specified, check the build marchine's architecture
207 if [[ -z ${arch} ]]; then
210 # shellcheck disable=SC2209 # 'sh' is SuperH, not a shell command
212 arm64 | aarch64*) arch=arm64 ;;
213 arm* | sa110) arch=arm ;;
214 i?86 | x86_64) arch=x86 ;;
215 loongarch*) arch=loongarch ;;
217 ppc*) arch=powerpc ;;
218 riscv*) arch=riscv ;;
221 sun4u) arch=sparc64 ;;
222 *) arch=${uname_m} ;;
227 parisc64) srcarch=parisc ;;
228 sparc32 | sparc64) srcarch=sparc ;;
229 i386 | x86_64) srcarch=x86 ;;
230 *) srcarch=${arch} ;;
236 # small Makefile to parse obj-* syntax
237 __kbuild_tmp_makefile()
242 $(foreach m,$(obj-y) $(obj-m) $(obj-),$(foreach s, -objs -y -m -,$($(m:%.o=%$s))) $(m))
249 # shellcheck disable=SC2034 # these are set by _init_completion
250 local cur prev words cword split
251 _init_completion -s || return
254 srctree=$(__kbuild_get_srctree "${words[@]}")
256 # If 'kernel' and 'Documentation' directories are found, we assume this
257 # is a kernel tree. Otherwise, we fall back to the generic rule provided
258 # by the bash-completion project.
259 if [[ ! -d ${srctree}/kernel || ! -d ${srctree}/Documentation ]]; then
260 if [ -n "${__kbuild_default_make_completion}" ]; then
261 "${__kbuild_default_make_completion}" "$@"
266 # make options with a parameter (copied from the bash-completion project)
268 --file | --makefile | --old-file | --assume-old | --what-if | --new-file | \
269 --assume-new | -!(-*)[foW])
273 --include-dir | --directory | -!(-*)[ICm])
278 COMPREPLY=($(compgen -v -- "$cur"))
281 --eval | -!(-*)[DVx])
285 COMPREPLY=($(compgen -W "{1..$(($(_ncpus) * 2))}" -- "$cur"))
294 # make options (copied from the bash-completion project)
296 opts="$(_parse_help "$1")"
297 COMPREPLY=($(compgen -W "${opts:-$(_parse_usage "$1")}" -- "$cur"))
298 if [[ ${COMPREPLY-} == *= ]]; then
304 __kbuild_handle_variable "${cur}" "${srctree}"
308 # There are many variables prefixed with 'KBUILD_'.
309 # Display them only when 'KBUILD_' is entered.
310 # shellcheck disable=SC2191 # '=' is appended for variables
312 KBUILD_{CHECKSRC,EXTMOD,EXTMOD_OUTPUT,OUTPUT,VERBOSE,EXTRA_WARN,CLIPPY}=
313 KBUILD_BUILD_{USER,HOST,TIMESTAMP}=
314 KBUILD_MODPOST_{NOFINAL,WARN}=
315 KBUILD_{ABS_SRCTREE,EXTRA_SYMBOLS,KCONFIG}=
319 # There are many variables prefixed with 'KCONFIG_'.
320 # Display them only when 'KCONFIG_' is entered.
321 # shellcheck disable=SC2191 # '=' is appended for variables
323 KCONFIG_{CONFIG,ALLCONFIG,NOSILENTUPDATE,OVERWRITECONFIG}=
324 KCONFIG_{SEED,PROBABILITY}=
325 KCONFIG_WARN_UNKNOWN_SYMBOL=
330 # By default, hide KBUILD_* and KCONFIG_* variables.
331 # Instead, display only the prefix parts.
332 keywords+=(KBUILD_ KCONFIG_)
336 if [[ ${cur} != /* && ${cur} != *//* ]]; then
337 local dir srcarch kbuild_file tmp
338 srcarch=$(__kbuild_get_srcarch "${words[@]}")
343 if [[ ${dir} == */* ]]; then
349 # Search for 'Kbuild' or 'Makefile' in the parent
350 # directories (may not be a direct parent)
351 if [[ -f ${srctree}/${dir}/Kbuild ]]; then
352 kbuild_file=${srctree}/${dir}/Kbuild
355 if [[ -f ${srctree}/${dir}/Makefile ]]; then
356 kbuild_file=${srctree}/${dir}/Makefile
360 if [[ ${dir} == . ]]; then
365 if [[ -n ${kbuild_file} ]]; then
366 tmp=($(__kbuild_tmp_makefile "${kbuild_file}" |
367 SRCARCH=${srcarch} obj=${dir} src=${srctree}/${dir} \
368 "${1}" -n -f - 2>/dev/null))
371 if [[ ${dir} != . ]]; then
372 tmp=("${tmp[@]/#/${dir}\/}")
375 keywords+=("${tmp[@]}")
378 # *_defconfig and *.config files. These might be grouped into
379 # subdirectories, e.g., arch/powerpc/configs/*/*_defconfig.
380 if [[ ${cur} == */* ]]; then
386 tmp=($(find "${srctree}/arch/${srcarch}/configs/${dir}" \
387 "${srctree}/kernel/configs/${dir}" \
388 -mindepth 1 -maxdepth 1 -type d -printf '%P/\n' \
389 -o -printf '%P\n' 2>/dev/null))
391 if [[ ${dir} != . ]]; then
392 tmp=("${tmp[@]/#/${dir}\/}")
395 keywords+=("${tmp[@]}")
398 # shellcheck disable=SC2191 # '=' is appended for variables
401 # variables (append =)
407 INSTALL{,_MOD,_HDR,_DTBS}_PATH=
414 clean mrproper distclean
415 clang-{tidy,analyzer} compile_commands.json
417 dtbs{,_check,_install} dt_binding_{check,schemas}
420 modules{,_prepare,_install,_sign}
422 tags TAGS cscope gtags
423 rust{available,fmt,fmtcheck}
424 kernel{version,release} image_name
425 kselftest{,-all,-install,-clean,-merge}
428 {,old,olddef,sync,def,savedef,rand,listnew,helpnew,test,tiny}config
429 {,build_}{menu,n,g,x}config
431 all{no,yes,mod,def}config
432 {yes2mod,mod2yes,mod2no}config
435 {html,textinfo,info,latex,pdf,epub,xml,linkcheck,refcheck,clean}docs
438 {,bin,src}{rpm,deb}-pkg
440 tar{,gz,bz2,xz,zst}-pkg
441 perf-tar{,gz,bz2,xz,zst}-src-pkg
444 COMPREPLY=($(compgen -W "${keywords[*]}" -- "${cur}"))
446 # Do not append a space for variables, subdirs, "KBUILD_", "KCONFIG_".
447 if [[ ${COMPREPLY-} == *[=/] || ${COMPREPLY-} =~ ^(KBUILD|KCONFIG)_$ ]]; then
451 } && complete -F _make_for_kbuild make