可能なターゲットに基づいてmakeからオートコンプリート

可能なターゲットに基づいてmakeからオートコンプリート

Makefile:

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

doc.texディレクトリに存在する場合は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}

_mkcache2つの部分、つまりaからすべてのルールと目標を抽出し、それをキャッシュする機能がありますMakefile。また、いくつかの処理が行われるため、ルールはtarget : pre-reqこのキャッシュ内の単一の「」形式に縮小されます。

その後、完了機能は_bc_make完了したいトークンを取得し、パターンルールを使用して前提条件と完成した単語に基づいてglobを拡張してターゲットと一致させようとします。 1 つ以上の一致が見つかった場合は、パターンルールに基づいてターゲットリストを作成します。

makeGNUと仮定しました。以下を正しく処理する必要があります。

  • 目標とパターンの規則(すべてではありませんが、以下を参照)
  • 古いフォームと新しいフォーム.c.o%.o : %.c
  • 前提条件のパス(例RCS/:)
  • すべてのデフォルトルールがある場合とない場合(必要に応じて追加できます-rmake

警告、サポートされていません:

  • 中間またはチェーンの依存関係は、次のように良くありません。make
  • VPATHまたはvpath
  • .SUFFIXES
  • make -C dir
  • "archive(member)" ターゲット、明示的または暗黙的
  • makeオプション拡張
  • 環境の病理学的ゴミがMakefile解析の問題を引き起こす可能性があります(TERMCAP例:)
  • 以下を除く名前付きMakefileMakefile

上記のいくつかの機能は比較的簡単に追加できますが、他のもの(アーカイブ処理など)はそれほど簡単ではありません。

おすすめ記事