[CMake] how to compile single source file with debug options?
Sergey Spiridonov
sena at hurd.homeunix.org
Mon Jun 13 11:37:11 EDT 2016
Hi all
On 10/06/16 14:05, Sergey Spiridonov wrote:
> To debug and fix our applications/libraries we often need to recompile
> single source file in a debug mode. With the our current build system it
> is as simple, as touching a file an starting make with debug option. It
> will then recompile that single file with -O0 -g and with -DDEBUG and
> relink target application/library.
I implemented following solution (proposed by Elizabeth A. Fischer
elizabeth.fischer at columbia.edu):
1. Create a wrapper script ggcc/gg++ around the gcc/g++. It checks if
env variable PDL has "d" and if true, then removes release options and
inserts debug options instead.
2. The very first time cmake must be started like "CC=ggcc CXX=gg++
cmake" (it is not enough just to rerun cmake, you must clean everything
first)
3. To recompile some of the source files in debug mode, one has to touch
them and run "make PDL=d"
Here is the wrapper script (inspired by [1])
[1] https://github.com/gawen947/gcc-wrapper/
#!/bin/bash
# This is a wrapper around gcc compiler to allow switching to debug mode
"on the fly"
# you can enable debugging by executing "make PDL=d"
#
#
# (may be also trigger debug mode by creating empty sourcefile.cc.debug
in the same directory?)
# TODO: pass this from cmake(CMakeList.txt)->make
GAM_DEBUG_FLAGS="-Og -g -DGAM_DEBUG -ggdb -fno-inline -fno-default-inline"
GAM_REMOVE_DEBUG_FLAGS="-O3 -finline-functions -fexpensive-optimizations"
# what is cheaper, call external program or use bash instead of sh?
newcmd=""
if [ ${0##*/} == "ggcc" ]; then
TOOL="gcc"
elif [ ${0##*/} == "gg++" ]; then
TOOL="g++"
else
echo "wrapper for gcc or g++"
exit 120
fi
if [[ "$PDL" == *"d"* ]]; then
# add GAM_DEBUG_FLAGS, remove GAM_REMOVE_DEBUG_FLAGS
INSERTED=0
for arg in $*; do
if [ "-O3" == "$arg" ]; then
INSERTED=1
newcmd="$newcmd $GAM_DEBUG_FLAGS"
continue
fi
# note spaces around env variables, they are essential
if [[ " $GAM_DEBUG_FLAGS $GAM_REMOVE_FLAGS " == *" $arg "* ]]; then
continue
fi
newcmd="$newcmd $arg"
done
if [ "$INSERTED" == 1 ]; then
echo $TOOL $newcmd
exec $TOOL $newcmd
else
exec $TOOL "$@"
fi
else
exec $TOOL "$@"
fi
Hope this can be useful for somebody else...
--
Best regards, Sergey Spiridonov
More information about the CMake
mailing list