[CMake] How to set platform-specific compiler-options from within CMakeFiles

Mike Jackson imikejackson at gmail.com
Thu Jun 5 11:58:17 EDT 2008


Sure you can:
  INCLUDE (MSVC_Compiler_Stuff.cmake)

#----------MSVC_Compiler_Stuff.cmake
if (MSVC)

endif (MSVC)


I do this all the time for OS X specific stuff.

For Linux you can try:

IF(CMAKE_SYSTEM MATCHES "Linux-([3-9]\\.[0-9]|2\\.[4-9])\\.")
   # Linux Specific flags

ENDIF(CMAKE_SYSTEM MATCHES "Linux-([3-9]\\.[0-9]|2\\.[4-9])\\.")

if you are using ICC on linux you can also test for that:

IF( ${CMAKE_C_COMPILER} MATCHES "icc" )
     SET(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} -static-intel)
ENDIF( ${CMAKE_C_COMPILER} MATCHES "icc" )

or test for GCC:

IF(CMAKE_COMPILER_IS_GNUCC)
   SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmessage-length=0")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
IF(CMAKE_COMPILER_IS_GNUCXX)
   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmessage-length=0")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)


Hope that helps a bit

--  
Mike Jackson   Senior Research Engineer
Innovative Management & Technology Services


On Jun 5, 2008, at 11:21 AM, Martin Apel wrote:

> Unfortunately I cannot simply add one file with platform specifics  
> for each additional platform. Instead I have to clutter up the main  
> CMakeLists.txt with if statements.
> Especially there is no way to support platforms for which no  
> explicitly testable variable is available e.g. Linux.
> Strange enough is, that WIN32 is set before the PROJECT statement,  
> while CMAKE_SYSTEM_NAME is not.
>
> Martin
>
> Mike Jackson wrote:
>> IF (WIN32)
>> SET(......)
>> ENDIF (WIN32)
>>
>> IF (MSVC)
>>
>> ENDIF (MSVC)
>> -- Mike Jackson   Senior Research Engineer
>> Innovative Management & Technology Services
>>
>>
>> On Jun 5, 2008, at 10:35 AM, Martin Apel wrote:
>>
>>> Hi all,
>>>
>>> I am trying to set the CMAKE_CXX_FLAGS or CMAKE_CXX_FLAGS_INIT  
>>> variable, such that it contains flags to turn off specific  
>>> warnings for the Visual Studio compiler. I definitely want to set  
>>> these additional flags from within a CMakeFile, specifying them  
>>> on the command line or interactively is not an option.
>>> I have included a Windows-specific file by using
>>> INCLUDE (${CMAKE_SYSTEM_NAME}.cmake)
>>>
>>> The file Windows.cmake contains the line
>>> SET (ENV{CXXFLAGS} "/wd4251")
>>>
>>> However this only works after the PROJECT line within the  
>>> CMakeFile, because before it CMAKE_SYSTEM_NAME seems to be unset.  
>>> On the other hand, when writing the include statement after the  
>>> project command, the CMAKE_CXX_FLAGS seem already to be set. If  
>>> possible I would like to specify those flags as initial values,  
>>> avoiding to use the force option of the set command.
>>>
>>> What is the recommended way to add platform-specific compiler  
>>> flags from within CMakeFiles?
>>>
>>> Any help would be appreciated.
>>>
>>> Martin
>>>
>>> _______________________________________________
>>> CMake mailing list
>>> CMake at cmake.org
>>> http://www.cmake.org/mailman/listinfo/cmake
>>
>>
>
>
> -- 
>
> Martin Apel                                     Tel:     0049 8153  
> 9288-47
> Software Architect                              E-Mail:   
> martin.apel at simpack.de
>
> INTEC GmbH                                      Tel:     0049 8153  
> 9288-0
> Argelsrieder Feld 13                            Fax:     0049 8153  
> 9288-11
> 82234 Wessling                                  E-Mail:   
> intec at simpack.de
> Germany                                         URL:     http:// 
> www.simpack.com
>



More information about the CMake mailing list