[CMake] remove compile flag with set_source_file_properties

Michael Hertling mhertling at online.de
Fri Aug 19 01:04:08 EDT 2011


On 08/17/2011 10:55 AM, Florian Reinhard wrote:
> Hi!
> 
> My cmake project (using the texas instruments cl6x compiler) contains
> two configurations, release and debug:
> 
> 
> set(CMAKE_C_FLAGS           "-whatever -flags")
> set(CMAKE_CXX_FLAGS         ${CMAKE_C_FLAGS})
> 
> 
> set(CMAKE_C_FLAGS_DEBUG     "-g  -d\"_DEBUG\" ")
> set(CMAKE_C_FLAGS_RELEASE   "-O3")
> set(CMAKE_CXX_FLAGS_DEBUG   ${CMAKE_C_FLAGS_DEBUG})
> set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
> 
> but for some files, even when using the debug configuration, i'd need
> to remove the "-g" for some files. Is there some way to get this done?
> 
> using
> 
> SET_SOURCE_FILES_PROPERTIES( ${LIST_OF_FILES} PROPERTIES COMPILE_FLAGS "-O3")
> 
> would add -O3 again no matter what configuration, which would be fine,
> but i couldn't find out how to set per file C(XX)_FLAGS_DEBUG so i'd
> be able to remove the -g
> 
> Is this somehow possible?
> 
> Thank you and kind regards,
> 
> Florian Reinhard

The following approaches might work:

(1) Collect the affected source files in dedicated directories with a
CMakeLists.txt and remove "-g" from CMAKE_{C,CXX}_FLAGS_DEBUG therein.
However, the sources must go into targets defined in the directory's
CMakeLists.txt; maybe, you need to add particular static libraries
built from these sources, provided your toolchain supports this.

(2) Externalize the affected sources and reintegrate them as an
external project built with the same toolchain but different flags,
i.e. without "-g"; a radical approach with the same drawbacks as (1).

(3) If you get along with Makefile generators, you might use one of
the RULE_LAUNCH_COMPILE properties combined with a shell script:

# CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(EXAMPLE C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/g.c "void g(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/h.c "void h(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c f.c g.c h.c)
SET_TARGET_PROPERTIES(main PROPERTIES RULE_LAUNCH_COMPILE
    "bash ${CMAKE_SOURCE_DIR}/setup <SOURCE> ${CMAKE_BINARY_DIR}/f.c
${CMAKE_BINARY_DIR}/h.c --")

# setup:
SOURCE="$1"; shift
echo "SOURCE: $SOURCE"
unset FILES
while [ "$1" != "--" ]; do
    FILES[${#FILES[@]}]="$1"; shift
done
shift
echo "FILES[${#FILES[@]}]: ${FILES[@]}"
CMDLINE0=("$1"); CMDLINE1=("$1"); shift
for i in "$@"; do
    CMDLINE0[${#CMDLINE0[@]}]="$1"
    if [ "$1" != "-g" ]; then CMDLINE1[${#CMDLINE1[@]}]="$1"; fi
    shift
done
echo "CMDLINE0: ${CMDLINE0[@]}"
echo "CMDLINE1: ${CMDLINE1[@]}"
for i in "${FILES[@]}"; do
    if [ "$i" == "$SOURCE" ]; then
        echo "Executing ${CMDLINE1[@]}"; exec "${CMDLINE1[@]}"
    fi
done
echo "Executing ${CMDLINE0[@]}"; exec "${CMDLINE0[@]}"

The setup script takes the source to compile, a list of source files to
be compiled without "-g" and the actual command line separated by "--".
It stores the source and the files and constructs two command lines,
one without "-g". Finally, it compares the source with each of the
files, and if there's a match, it executes the shortened command
line without "-g", otherwise the unaltered one.

IMO, (3) is a quite smart approach and should be favored if the project
doesn't need to be built with generators other than the Makefile ones.

'hope that helps.

Regards,

Michael


More information about the CMake mailing list