[CMake] Always compile source file in debug

Michael Hertling mhertling at online.de
Fri Sep 3 09:51:48 EDT 2010


On 09/03/2010 01:07 PM, Eric Noulard wrote:
> 2010/9/3 Belcourt, Kenneth <kbelco at sandia.gov>:
>> Hi,
>>
>> Apologies if this has been answered before.  In an effort to ensure
>> cross-platform consistent results, I need to force some (C++ and Fortran)
>> source files to always be built debug.  Is there any easy syntax to do this?
>>
>> I build our libraries like this.
>>
>> SET(SRCS
>>  ${EXEC_DIR}/AfterStep.f90
>>  ${EXEC_DIR}/ArgCF_Connect.f90
>>  ${EXEC_DIR}/ArgCF_Solve.f90
>>  ...
>> )
>>
>> ADD_LIBRARY(exec ${SRCS})
> 
> You may set some specific flags on specific sources using something like
> 
> set_source_files_properties(file1.f90 file2.f90
>                                                   PROPERTIES
> COMPILE_FLAGS ${CMAKE_FORTRAN_FLAGS_DEBUG})
> 
> but as far as I understand this does not replace default compile flags but add
> extra flags for those particular source files...

If these specific source files are confined to certain targets you
could also impose the COMPILE_FLAGS target property on the latters.

> Now if your "to be compiled with debug options" files may be contained in
> separate libraries, may be you can create a sub-project which contains those
> and enforce thsi subproject to be "CMAKE_BUILD_TYPE=Debug".

Alternatively, you can reset the CMAKE_<LANG>_FLAGS_<CONFIG> variables
in the concerned CMakeLists.txt, e.g. for C like the following:

GET_DIRECTORY_PROPERTY(VARS VARIABLES)
FOREACH(i IN LISTS VARS)
    IF(i MATCHES "^CMAKE_C_FLAGS_.*\$")
        SET(${i} "")
    ENDIF()
ENDFOREACH()
SET(CMAKE_C_FLAGS "-g -O0 -DDEBUG")  # Your debug flags.

This should work also with multi-configuration generators for
which CMAKE_BUILD_TYPE has no meaning, or you differentiate
between CMAKE_CONFIGURATION_TYPES and CMAKE_BUILD_TYPE to
find out the flags to be tweaked.

As a last resort, you may modify the rule variables, e.g.

STRING(REGEX REPLACE "<FLAGS>" "-g -O0 -DDEBUG"
       CMAKE_C_COMPILE_OBJECT ${CMAKE_C_COMPILE_OBJECT})

- again in the concerned CMakeLists.txt - to obtain full
control over the flags seen by the compiler, but note [1].

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg30008.html


More information about the CMake mailing list