[CMake] Native build system invocation

gga ggarra at advancedsl.com.ar
Mon Dec 25 23:51:59 EST 2006


Roman Yakovenko wrote:
> Hi. It seems that I cannot to find answer for pretty simple question:
> how I can invoke native build system from\using CMake?
Short answer is that you invoke the native build system as usual (make,
nmake, etc).

Find attached a wrapper bash script I am using for cmake and normal
Makefiles. 
It automatically creates out-of-source builds when run under cmake.
For running it on windows, you will need something like cygwin or MSYS
(for bash+uname), albeit it will probably not work as is, as I need to
update the -m32/-m64 flags for VC8 (which I don't have).

For docs, run:

> mk -h

PS.  If you do any improvements to it, send me a patch.

-- 
Gonzalo Garramuño
ggarra at advancedsl.com.ar

AMD4400 - ASUS48N-E
GeForce7300GT
Kubuntu Edgy


-------------- next part --------------
#!/bin/bash --norc


#
# Determine CPU architecture
#
KERNEL=`uname -s`
RELEASE=`uname -r`
export CMAKE_BUILD_ARCH=32
export CMAKE_BUILD_TYPE=Release
arch=`uname -a`

if [[ $arch == *x86_64* ]]; then
    CMAKE_BUILD_ARCH=64
else
    if [[ $arch == *ia64* ]]; then
	CMAKE_BUILD_ARCH=64
    fi
fi


OS=$KERNEL-$RELEASE


usage()
{
    echo $0
    echo ""
    echo "$0 [options] [--] [make options]"
    echo ""
    echo "Wrapper around CMake to easily create out of source"
    echo "builds (ie. compilations where everything goes into"
    echo "a BUILD/OS-VERSION-\$CMAKE_BUILD_TYPE-\$CMAKE_BUILD_ARCH directory."
    echo ""
    echo "For this platform (default settings): "
    echo "  BUILD/$OS-$CMAKE_BUILD_TYPE-$CMAKE_BUILD_ARCH"
    echo ""
    echo "It must be run from a directory with a valid CMakeLists.txt"
    echo "file outside of the build tree."
    echo ""
    echo "Options:"
    echo ""
    echo "  -q         quiet build"
    echo "  -j N       number of cpus to use (default: ${CMAKE_PROCS=4})"
    echo ""
    echo "  debug|release|reldebug|small"
    echo "  debug    - build a debug build"
    echo "  release  - build a release build (default)"
    echo "  reldebug - build a release build with debug information"
    echo "  small    - build a small release build"
    echo ""
    echo "  clean    - Cleans Makefile and CMakeCache.txt"
    echo ""
    echo "  both|32|64"
    echo "             Builds both 32/64 bit versions, 32-bit only, "
    echo "             64-bit only (default: $CMAKE_BUILD_ARCH)"
    echo ""
    exit 1
}

#
# Parse command-line options
#

clean=0
opts='VERBOSE=1'
while [ $# -gt 0 ]; do
    case $1 in
	clean)
	    shift
	    if [ -r CMakeLists.txt ]; then
		if [ -d BUILD ]; then
		    clean=1
		fi
	    fi
	    ;;
	debug)
	    shift
	    export CMAKE_BUILD_TYPE=Debug
	    ;;
	release)
	    shift
	    export CMAKE_BUILD_TYPE=Release
	    ;;
	small)
	    shift
	    export CMAKE_BUILD_TYPE=MinSizeRel
	    ;;
	both)
	    shift
	    CMAKE_BUILD_ARCH='Both'
	    ;;
	64)
	    shift
	    CMAKE_BUILD_ARCH=64
	    export LDFLAGS=
	    export CXXFLAGS=
	    ;;
	32)
	    shift
	    CMAKE_BUILD_ARCH=32
	    export LDFLAGS=-m32
	    export CXXFLAGS=-m32
	    ;;
	-h)
	    shift
	    usage
	    ;;
	--help)
	    shift
	    usage
	    ;;
	-j)
	    shift
	    if [ $# == 0 ]; then
		echo "Missing parameter for -j!"
		usage
	    fi
	    CMAKE_PROCS=$1
	    shift
	    ;;
	-q)
	    shift
	    opts=""
	    ;;
	--)
	    shift
	    break
	    ;;
	*)
	    break
	    ;;
    esac
done

#
# Simple function to run a command and print it out
#
run_cmd()
{
    echo
    echo "> $*"
    echo
    command $*
}

#
# Function to just run make on a dir with a Makefile
#
run_make()
{
    cmd=''
    if [[ $KERNEL == *CYGWIN* ]]; then
	cmd="nmake $"@
    else
	cmd="make -j ${CMAKE_PROCS=4} $@"
    fi
    run_cmd $cmd
}

#
# Function to run cmake and then run make on generated Makefile
#
run_cmake()
{
    builddir=$OS-$CMAKE_BUILD_TYPE-$CMAKE_BUILD_ARCH
    echo "Buildir ${builddir}"
    if [ ! -d $builddir ]; then
	cmd="mkdir $builddir $builddir/bin $builddir/lib"
	run_cmd $cmd
    fi


    cd $builddir

    cmd="cmake ../.. -DEXECUTABLE_OUTPUT_PATH=$PWD/bin -DLIBRARY_OUTPUT_PATH=$PWD/lib -DCMAKE_LIBRARY_PATH=$PWD/lib -DCMAKE_BUILD_ARCH=$CMAKE_BUILD_ARCH -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE"

    run_cmd  $cmd
    run_make $@

    cd ..
}

#
# Function used to clean the build directory and exit
#
run_clean()
{
    builddir=$OS-$CMAKE_BUILD_TYPE-$CMAKE_BUILD_ARCH
    echo "Cleaned BUILD/$builddir"
    rm -rf BUILD/$builddir
    exit 0
}


#
# Main routine
#
if [ $clean == 1 ]; then
    run_clean
fi

if [ -r Makefile ]; then
    run_make $opts $@
else

    if [ ! -r CMakeLists.txt ]; then
	usage
    fi

    if [ ! -d BUILD ]; then
	mkdir BUILD
    fi

    cd BUILD

    if [ $CMAKE_BUILD_ARCH == 'Both' ]; then
	export CMAKE_BUILD_ARCH=32
	run_cmake $opts $@
	export CMAKE_BUILD_ARCH=64
	run_cmake $opts $@
    else
	run_cmake $opts $@
    fi
fi


More information about the CMake mailing list