[CMake] Overriding C compiler flags in CMakeLists files

Andrei Buzgan andrei.buzgan at gmail.com
Tue Aug 9 06:33:38 EDT 2011


Hello,

I'm trying to compile out-of-source an embedded C application using CMake
and MinGW and to use specific compiler flags for some source files.
I tried the approach described in
http://www.cmake.org/pipermail/cmake/2011-April/043703.html but it didn't
work for me. I'm describing below what i've done, maybe someone highlights
what i did wrong:

Project structure:

Top_Dir
|-- Build (from where I launch cmake)
|       |-- Output_Dir
|       |-- Toolchain.cmake
|       |-- build.bat
|-- Sources
        |-- Module1
        |         |-- module1.c
        |         |-- module1.h
        |         |-- CMakeLists.txt [1]
        |-- Module2
        |         |-- module2.c
        |         |-- module2.h
        |         |-- CMakeLists.txt [2]
        |-- Module3
        |         |-- module3.c
        |         |-- module3.h
        |         |-- CMakeLists.txt [3]
        |-- CMakeLists.txt [0]

*build.bat*
mkdir Output_Dir
cd Output_Dir
cmake ../../Sources -DCMAKE_TOOLCHAIN_FILE=../Toolchain.cmake -G"MinGW
Makefiles"

*CMakeLists.txt [0]*
cmake_minimum_required( VERSION 2.8 )

project( My_Project C ASM )

include_directories(
    Module1
    Module2
    Module3
)

add_subdirectory( Module1 )
add_subdirectory( Module2 )
add_subdirectory( Module3)

set( USER_C_COMPILER_FLAGS
    "c_flag1"
    "c_flag2"
    "c_flag3"
   )

foreach( flag ${USER_C_COMPILER_FLAGS} )
    set( USER_C_COMPILER_FLAGS_GENERAL "${USER_C_COMPILER_FLAGS_GENERAL}
${flag}" )
endforeach( flag ${USER_C_COMPILER_FLAGS} )

*set( CMAKE_C_FLAGS ${USER_C_COMPILER_FLAGS_GENERAL} )*

add_definitions(
    -D DEF1
    -D DEF2
    -D DEF3
)

set( USER_LINKER_FLAGS
    "link_flag1"
    "link_flag2"
    "link_flag3"
    )

set( USER_TMP_LINKER_FLAGS "" )

foreach( flag ${USER_LINKER_FLAGS} )
    set( USER_TMP_LINKER_FLAGS "${USER_TMP_LINKER_FLAGS} ${flag}" )
endforeach( flag ${USER_LINKER_FLAGS} )

set( CMAKE_EXE_LINKER_FLAGS ${USER_TMP_LINKER_FLAGS} )

set( PRJ_SOURCES
        Module1/module1.c
        Module2/module2.c
        Module3/module3.c
     )

add_executable( My_Exec ${PRJ_SOURCES} )

set( CMAKE_VERBOSE_MAKEFILE ON )

I tried to place in any of the leaf CMakeLists.txt files ([1][2][3])
statements like
set(USER_C_COMPILER_FLAGS_LOCAL "c_loc_flag1 c_loc_flag2 c_loc_flag3")
set( CMAKE_C_FLAGS ${USER_C_COMPILER_FLAGS_LOCAL} )

and I expected to see different compiler flags for the sources in the
respective directory, but instead ALL the sources were compiled with the
flags defined in CMakeLists.txt[0] top level file.

What I did wrong? I clearly miss something in the inheritance mechanisms of
CMake.

In Cmake Platform and Compiler folders i created the custom platform and
compiler files with directives like:
set( CMAKE_C_LINK_EXECUTABLE
        "${USER_CONFIG_LINKER} <LINK_FLAGS> <OBJECTS> -o <TARGET>"
        "${USER_CONFIG_CONVERTER} <TARGET> -o <TARGET>"
)

set( CMAKE_ASM_SOURCE_FILE_EXTENSIONS asm ASM )
set( CMAKE_ASM_OUTPUT_EXTENSION ".obj" )

set( CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <FLAGS> <SOURCE> -o
<OBJECT>" )

set( CMAKE_ASM_FLAGS_INIT " a list of ASM flags " )
to make the assembler work of ASM files and to use the custom linker and
executable converter specific for the compiler kit I use.


Thanks,
Andrei
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110809/29051d0f/attachment.htm>


More information about the CMake mailing list