tags:

views:

263

answers:

4

Hi,

cmake advises to use out-of-source builds. While in general I like the idea I find it not comfortable to navigate from out-of-source sub directory to the corresponding source directory. I frequently need the code to perform some actions with code (e.g. grep, svn command etc.).

Is there an easy way in shell to navigate from out-of-source sub directory to the corresponding source directory?

Thanks Dima

A: 

Have you tried the pushd and popd builtins?

When in the /source/directory

pushd /to/build/directory

Work there

popd ## Back to source directory

You can even stack that deeper...

nik
I can use even "cd", and then "cd -" to go forward and back. The problem is when I compile in out-of-source tree I can navigate to any sub directory. Now cd/cd- won't work and I don't won't to devise what the corresponding source directory.
dimba
A: 

Ok this is what what I found. The idea is to use CMakeCache.txt which is found in out-of-source tree. We go up while looking for cache file. Once found we extract source directory, which is stored in CMAKE_HOME_DIRECTORY varible.

function cdoos2src () {
    path=$(pwd)

    while [[ $path != "/" ]]; do
        if [[ -f $path/CMakeCache.txt ]]; then
            break;
        fi

        # go up one directory
        path=$(dirname $path)
    done

    if [[ $path == "/" ]]; then
        echo "This is not svn out-of-source directory - CmakeCache.txt not found"
        return
    fi

    oos_dir=$(pwd) 

    code_dir=$(sed -n '/CMAKE_HOME_DIRECTORY/s/.*=//p' $path/CMakeCache.txt)
    echo "Code dir     [$code_dir]"

    code_sub_dir=$(echo $(pwd) | sed  -n 's#^'$path'##p')
    echo "Code sub dir [$code_sub_dir]"

    cd $code_dir/$code_sub_dir
}
dimba
You should protect pathname variable expansions `"$like_this`, so it works in hierarchies with spaces in the path, too.
+2  A: 

I prefer to keep it simple and have the source checkouts in a src/ directory, and the corresponding builds in a build/ directory. Then I can use

function cs() {
    cd "${PWD/build/src}"
}
function cb() {
    cd "${PWD/src/build}"
}

Cf. also KDE's TechBase for another approach.

Yep, simple is good, but what you do if you have several build directories?
dimba
Then we leave the realm of 'simple' :)
yep, we leave it tooo often :)
dimba
A: 

I think that the most straightforward and convenient way to do this is simply open more than one Shell session, i.e. tabs. For example, KDE4 Konsole supports tabs and you can navigate through them with Shift + ArrowLeft or ArrowRight. IMHO very comfortable plus it preserves history better.

kralyk