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

pellegrini at ill.fr pellegrini at ill.fr
Tue Sep 28 13:08:22 EDT 2010


Hi Michael,

so if I get you right I should organize my library as follow:

src/

    Compiler/G95_Fortran.cmake
    CMakeLists.txt
    my_general_f90_src_files

    special_flag_src/

        Compiler/G95_Fortran.cmake
        CMakeLists.txt
        my_special_f90_src_files

where the nested CMakeLists.txt will call

    add_library(my_special_f90_files.a my_special_f90_src_files ...)

and where the main CMakeLists.txt file will call

    add_library(my_general_f90_files.a my_general_f90_src_files ...)

not forgotting in the latter case to specify the dependancy on the
"special_f90_files.a" library. In doing so, both static libraries will be
compiled according to the compilation flags stored in their respective
Compiler/G95_Fortran.cmake files.

sorry for the naive questions but I just started with CMake two weeks ago
and I am quite in hurry to provide a first example to convince my boss
that using CMake is the right solution ...

thanks again

Eric


>
> 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
>
>




More information about the CMake mailing list