Fishshell: CMake & finding source directories


CMake is a great tool for makefile generation, far better than old arcane configure scripts and such, but it’s great out-of-source build support can lead to a common annoyance of constantly jumping back and forth between build and source directories, or having multiple build directories for a single source checkout. In my case, I frequently find myself forgetting exactly where the correct source tree is when I’m working in a build-tree.

So, here’s a little fish function called prompt_src that you can add to your prompt to let it always show you the source directory for the build tree you’re in, and also show the current git version that you’re working from. The image above shows my OpenCV/build directory (the response is in yellow indicating it’s not a Git source tree), and then my main application directory showing in Red because it’s been modified, but it’s the develop branch.

# src
function prompt_src --description 'Find details on this builds SOURCE dir'
    set -l cdir (pwd)
    while [ $cdir != "/" ]
        if [ -e $cdir/CMakeCache.txt ]
            set -l SourceDir ( cat $cdir/CMakeCache.txt | grep SOURCE_DIR | head -n 1 | cut -d '=' -f 2)
            if [ -d $SourceDir/.git ]
                set -l gitinfo (git --git-dir=$SourceDir/.git --work-tree=$SourceDir status -sb --untracked-files=no)
                set -l branch ( echo $gitinfo[1] | cut -b 4- )
                set -l branch_color (set_color red)\[M\]
                if test (count $gitinfo) -eq 1
                    set branch_color (set_color green)
                end

                echo \* Builds (set_color green)$SourceDir $branch_color \($branch\) (set_color normal)
                return
            else
                echo \* Builds (set_color yellow)$SourceDir (set_color normal)
                return
            end
        end
        set cdir (dirname $cdir)
    end
end

comments powered by Disqus