[CMake] Overriding C compiler flags in CMakeLists files

Tim Gallagher tim.gallagher at gatech.edu
Tue Aug 9 10:28:51 EDT 2011


In your leaf CMakeLists.txt, try doing: 

set_source_files_properties(${SOURCE_FILES} PROPERTIES COMPILE_FLAGS ${LOCAL_COMPILE_FLAGS}) 

where ${SOURCE_FILES} is a list of the files in the directory and ${LOCAL_COMPILE_FLAGS} is the list of flags for those files. 

Ideally you would want to set this for the entire directory, but according to the documentation, the COMPILE_FLAGS property doesn't exist for directories. 

Tim 

----- Original Message -----
From: "Andrei Buzgan" <andrei.buzgan at gmail.com> 
To: cmake at cmake.org 
Sent: Tuesday, August 9, 2011 6:33:38 AM 
Subject: [CMake] Overriding C compiler flags in CMakeLists files 

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 


_______________________________________________ 
Powered by www.kitware.com 

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html 

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ 

Follow this link to subscribe/unsubscribe: 
http://www.cmake.org/mailman/listinfo/cmake 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110809/14967edd/attachment-0001.htm>


More information about the CMake mailing list