根據可能的目標在 make 中自動完成

根據可能的目標在 make 中自動完成

Makefile

%.pdf: %.tex
    rubber -d $<

doc.tex如果目錄中有 a ,則make doc.pdf建置doc.pdf.問題是,當我輸入 時make,自動補全什麼也沒給:它甚至不允許自動補全到make doc.tex。對此我們能做些什麼呢?

答案1

bash-completion套件不會執行此操作,它會執行一些雜技來處理命令列選項並提取目標列表Makefile,但它不會嘗試透過應用通配符或以其他方式處理任何內容來產生匹配項模式規則

不過這是可以做到的,這是一個簡單的版本,有一些注意事項。

function _mkcache() {
    local _file="$1"
    # add "-r" to omit defaults (60+ rules)
    ${MAKE:-make} ${_file:+-f "$_file"} -qp 2>/dev/null |
    gawk '/^# *Make data base/,/^# *Finished Make data base/{
      if (/^# Not a target/) { getline; next }
      ## handle "target: ..."
      if (match($0,/^([^.#% ][^:%=]+) *:($|[^=])(.*)/,bits)) {
          #if (bits[3]=="") next # OPT: skip phony
          printf("%s\n",bits[1])
      }
      ## handle "%.x [...]: %.y [| x]", split into distinct targets/prereqs
      else if (match($0,/^([^:]*%[^:]*) *(::?) *(.*%.*) *(\| *(.*))?/,bits)) {
          #if (bits[3]=="%") next # OPT: skip wildcard ones
          nb1=split(bits[1],bb1)
          nb3=split(bits[3],bb3)
          for (nn=1; nn<=nb1; nn++) 
            for (mm=1; mm<=nb3; mm++) 
              printf("%s : %s\n",bb1[nn],bb3[mm])
      }
      ## handle fixed (no %) deps
      else if (match($0,/^([^:]*%[^:]*) *(::?) *([^%]*)$/,bits)) {
          if (bits[3]=="") next # phony
          printf("%s : %s\n",bits[1],bits[3])
      }
      ## handle old form ".c.o:"  rewrite to new form "%.o: %.c"
      else if (match($0,/^\.([^.]+)\.([^.]+): *(.*)/,bits)) {
          printf("%%.%s : %%.%s\n", bits[2],bits[1])
      }
    }' > ".${_file:-Makefile}.targets"
}

function _bc_make() {
    local ctok=${COMP_WORDS[COMP_CWORD]}   # curr token
    local ptok=${COMP_WORDS[COMP_CWORD-1]} # prev token
    local -a mkrule maybe
    local try rr lhs rhs rdir pat makefile=Makefile

    ## check we're not doing any make options 
    [[ ${ctok:0:1} != "-" && ! $ptok =~ ^-[fCIjloW] ]] && {
        COMPREPLY=()
        [[ "$makefile" -nt .${makefile}.targets ]] && 
            _mkcache "$makefile"

        mapfile -t mkrule < ".${makefile}.targets"
        # mkrule+=( "%.o : %.c" )  # stuff in extra rules

        for rr in "${mkrule[@]}"; do
            IFS=": " read lhs rhs <<< $rr

            ## special "archive(member):"
            [[ "$lhs" =~ ^(.*)?\((.+)\) ]] && {
                continue # not handled
            }

            ## handle simple targets
            [[ "$rhs" == "" ]] && {
                COMPREPLY+=( $(compgen -W "$lhs" -- "$ctok" ) )
                continue
            }

            ## rules with a path, like "% : RCS/%,v" 
            rdir=""
            [[ "$rhs" == */* ]] && rdir="${rhs/%\/*/}/" 
            rhs=${rhs/#*\//}

            ## expand (glob) that matches RHS 
            ## if current token already ends in a "." strip it
            ## match by replacing "%" stem with "*"

            [[ $ctok == *. ]] && try="${rdir}${rhs/\%./$ctok*}" \
                              || try="${rdir}${rhs/\%/$ctok*}"

            maybe=( $(compgen -G "$try") )  # try must be quoted

            ## maybe[] is an array of filenames from expanded prereq globs
            (( ${#maybe[*]} )) && {

               [[ "$rhs" =~ % ]] && {
                   ## promote rhs glob to a regex: % -> (.*)
                   rhs="${rhs/./\\.}"
                   pat="${rdir}${rhs/\%/(.*)}"

                   ## use regex to extract stem from RHS, sub "%" on LHS
                   for nn in "${maybe[@]}"; do 
                       [[ $nn =~ $pat ]] && {
                           COMPREPLY+=( "${lhs/\%/${BASH_REMATCH[1]}}" )
                       }
                   done
               } || {
                   # fixed prereqs (no % on RHS)
                   COMPREPLY+=( "${lhs/\%/$ctok}" )   
               }
            }
        done
        return
    }
    COMPREPLY=() #default
}
complete -F _bc_make ${MAKE:-make}

有兩個部分,一個函數_mkcache從 a 中提取所有規則和目標Makefile並緩存它們。它還進行了一些處理,因此規則被簡化target : pre-req為該快取中的單一“”形式。

然後,完成函數_bc_make獲取您嘗試完成的標記並嘗試與目標進行匹配​​,並使用模式規則根據先決條件和完成的單字來擴展 glob。如果找到一個或多個匹配項,它會根據模式規則建立目標清單。

make假定為GNU 。它應該正確處理:

  • 目標和模式規則(雖然不是全部,見下文)
  • 新舊形式.c.o%.o : %.c
  • 先決條件中的路徑(例如RCS/
  • 有或沒有所有預設規則(如果願意,可以-r添加)make

警告,不支援:

  • 中間或鍊式依賴項,它不如make
  • VPATH或者vpath
  • .SUFFIXES
  • make -C dir
  • 「archive(member)」目標,顯式或隱式
  • make選項擴展
  • 環境中的病態垃圾可能會導致 Makefile 解析問題(TERMCAP例如)
  • Makefiles 命名為除Makefile

上面的一些內容可以相對簡單地添加,而其他內容(例如存檔處理)則不是那麼簡單。

相關內容