[CMake] Build library with specific compil flags for a few files

Michael Wild themiwi at gmail.com
Tue Sep 28 10:39:13 EDT 2010


On 28. Sep, 2010, at 16:08 , pellegrini wrote:

> Hello everybody,
> 
> I have a library for which almost all the files should be compiled (e.g. g95) with the same compilation flags  (that I will call later flag_debug, flag_release ...) excepted a few ones for which I have to use slightly different compilation flags (called later flag1_debug, flag1_release ...).
> 
> As in both cases the flags are different from the default ones, I was told on the cmake list to create a personal Compiler/G95-Fortran.cmake file that was placed in my Src directory and that contains the following lines:
> 
> set(CMAKE_Fortran_FLAGS_INIT "")
> set(CMAKE_Fortran_FLAGS_DEBUG_INIT flag_debug)
> set(CMAKE_Fortran_FLAGS_RELEASE_INIT flag_release)
> set(CMAKE_Fortran_MODDIR_FLAG "-fmod=")
> set(CMAKE_Fortran_VERBOSE_FLAG "-v")
> 
> This file allowing to avoid the declaration of the flags in the CMakeLists.txt file. But, how to proceed for the few files for which I have to use
> different compiler flags ? In that case, I do not see any way to escape from writing specifically the flags in the CMakeLists.txt file with command such as:
> 
> if(CMAKE_BUILD_TYPE STREQUAL RELEASE)
>   set_source_files_properties(File1 PROPERTIES COMPILE_FLAGS flag1_release)
>   set_source_files_properties(File2 PROPERTIES COMPILE_FLAGS flag1_release) ...
> elseif(CMAKE_BUILD_TYPE STREQUAL DEBUG)
>   set_source_files_properties(File1 PROPERTIES COMPILE_FLAGS flag1_debug)
>   set_source_files_properties(File2 PROPERTIES COMPILE_FLAGS flag1_debug) ...
> ...
> endif()
> 
> would you have any idea about how to esacpe from this kind of implementation ? is that so ugly ?
> 
> thanks
> 
> Eric

Well, for one you don't need separate set_source_files_properties commands for every single file (that is, if the flags are the same):

if(CMAKE_BUILD_TYPE STREQUAL Release)  # notice the capitalization!
  set_source_files_properties(File1 File2 File3 PROPERTIES
    COMPILE_FLAGS ...)
elseif(CMAKE_BUILD_TYPE STREQUAL Debug) # notice the capitalization!
  set_source_files_properties(File1 File2 File3 PROPERTIES
    COMPILE_FLAGS ...)
endif()

However: such a scheme will break with multi-configuration IDEs since CMAKE_BUILD_TYPE is not known when CMake runs because the user can choose the configuration in the IDE afterwards. Unfortunately there are no COMPILE_FLAGS_<CONFIG> properties...

I think that currently the only reliable way of doing this is to split the special sources out into a separate directory and compile them there as a static library.

Michael

--
There is always a well-known solution to every human problem -- neat, plausible, and wrong.
H. L. Mencken

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
URL: <http://www.cmake.org/pipermail/cmake/attachments/20100928/c2d89050/attachment.pgp>


More information about the CMake mailing list