<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: Times New Roman; font-size: 12pt; color: #000000'>In your leaf CMakeLists.txt, try doing:<br><br>set_source_files_properties(${SOURCE_FILES} PROPERTIES COMPILE_FLAGS ${LOCAL_COMPILE_FLAGS})<br><br>where ${SOURCE_FILES} is a list of the files in the directory and ${LOCAL_COMPILE_FLAGS} is the list of flags for those files.<br><br>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.<br><br>Tim<br><br><hr id="zwchr"><b>From: </b>"Andrei Buzgan" &lt;andrei.buzgan@gmail.com&gt;<br><b>To: </b>cmake@cmake.org<br><b>Sent: </b>Tuesday, August 9, 2011 6:33:38 AM<br><b>Subject: </b>[CMake] &nbsp;Overriding C compiler flags in CMakeLists files<br><br>Hello,<br><br>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.<br>I tried the approach described in <a href="http://www.cmake.org/pipermail/cmake/2011-April/043703.html" target="_blank">http://www.cmake.org/pipermail/cmake/2011-April/043703.html</a> but it didn't work for me. I'm describing below what i've done, maybe someone highlights what i did wrong:<br>
<br>Project structure:<br><br>Top_Dir<br>|-- Build (from where I launch cmake)<br>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- Output_Dir<br>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- Toolchain.cmake<br>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- build.bat<br>|-- Sources<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- Module1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- module1.c<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- module1.h<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- CMakeLists.txt [1]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- Module2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- module2.c<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- module2.h<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- CMakeLists.txt [2]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- Module3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- module3.c<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- module3.h<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- CMakeLists.txt [3]<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |-- CMakeLists.txt [0]<br><br><b>build.bat</b><br>mkdir Output_Dir<br>cd Output_Dir<br>cmake ../../Sources -DCMAKE_TOOLCHAIN_FILE=../Toolchain.cmake -G"MinGW Makefiles"<br><br><b>CMakeLists.txt [0]</b><br>
cmake_minimum_required( VERSION 2.8 )<br><br>project( My_Project C ASM )<br><br>include_directories(<br>&nbsp;&nbsp;&nbsp; Module1<br>&nbsp;&nbsp;&nbsp; Module2<br>&nbsp;&nbsp;&nbsp; Module3<br>)<br><br>add_subdirectory( Module1 )<br>add_subdirectory( Module2 )<br>add_subdirectory( Module3)<br>
<br>set( USER_C_COMPILER_FLAGS <br>&nbsp;&nbsp;&nbsp; "c_flag1"<br>&nbsp;&nbsp;&nbsp; "c_flag2"<br>&nbsp;&nbsp;&nbsp; "c_flag3"<br>&nbsp;&nbsp; )<br><br>foreach( flag ${USER_C_COMPILER_FLAGS} )<br>&nbsp;&nbsp;&nbsp; set( USER_C_COMPILER_FLAGS_GENERAL "${USER_C_COMPILER_FLAGS_GENERAL} ${flag}" )<br>
endforeach( flag ${USER_C_COMPILER_FLAGS} )<br><br><i><b>set( CMAKE_C_FLAGS ${USER_C_COMPILER_FLAGS_GENERAL} )</b></i><br><br>add_definitions(<br>&nbsp;&nbsp;&nbsp; -D DEF1<br>&nbsp;&nbsp;&nbsp; -D DEF2<br>&nbsp;&nbsp;&nbsp; -D DEF3<br>)<br><br>set( USER_LINKER_FLAGS<br>
&nbsp;&nbsp;&nbsp;  "link_flag1"<br>
&nbsp;&nbsp;&nbsp; "link_flag2"<br>
&nbsp;&nbsp;&nbsp; "link_flag3"<br>&nbsp;&nbsp;&nbsp; )<br><br>set( USER_TMP_LINKER_FLAGS "" )<br><br>foreach( flag ${USER_LINKER_FLAGS} )<br>&nbsp;&nbsp;&nbsp; set( USER_TMP_LINKER_FLAGS "${USER_TMP_LINKER_FLAGS} ${flag}" )<br>endforeach( flag ${USER_LINKER_FLAGS} )<br>
<br>set( CMAKE_EXE_LINKER_FLAGS ${USER_TMP_LINKER_FLAGS} )<br><br>set( PRJ_SOURCES <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Module1/module1.c<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Module2/module2.c<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Module3/module3.c<br>&nbsp;&nbsp;&nbsp;&nbsp; )<br><br>add_executable( My_Exec ${PRJ_SOURCES} )<br>
<br>set( CMAKE_VERBOSE_MAKEFILE ON )<br><br>I tried to place in any of the leaf CMakeLists.txt files ([1][2][3]) statements like<br>set(USER_C_COMPILER_FLAGS_LOCAL "c_loc_flag1 c_loc_flag2 c_loc_flag3") <br>set( CMAKE_C_FLAGS ${USER_C_COMPILER_FLAGS_LOCAL} )<br>
<br>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.<br><br>What I did wrong? I clearly miss something in the inheritance mechanisms of CMake.<br>
<br>In Cmake Platform and Compiler folders i created the custom platform and compiler files with directives like:<br>set( CMAKE_C_LINK_EXECUTABLE <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "${USER_CONFIG_LINKER} &lt;LINK_FLAGS&gt; &lt;OBJECTS&gt; -o &lt;TARGET&gt;"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "${USER_CONFIG_CONVERTER} &lt;TARGET&gt; -o &lt;TARGET&gt;"<br>)<br><br>set( CMAKE_ASM_SOURCE_FILE_EXTENSIONS asm ASM )<br>set( CMAKE_ASM_OUTPUT_EXTENSION ".obj" )<br><br>set( CMAKE_ASM_COMPILE_OBJECT "&lt;CMAKE_ASM_COMPILER&gt; &lt;FLAGS&gt; &lt;SOURCE&gt; -o &lt;OBJECT&gt;" )<br>
<br>set( CMAKE_ASM_FLAGS_INIT " a list of ASM flags " )<br>to make the assembler work of ASM files and to use the custom linker and executable converter specific for the compiler kit I use.<br><br><br>Thanks,<br>
Andrei<br><br>
<br>_______________________________________________<br>Powered by www.kitware.com<br><br>Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html<br><br>Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ<br><br>Follow this link to subscribe/unsubscribe:<br>http://www.cmake.org/mailman/listinfo/cmake</div></body></html>