[CMake] Removing compiler options

Robert Dailey rcdailey.lists at gmail.com
Sun Nov 9 23:02:55 EST 2014


When setting up a CLR project through CMake, it becomes important to
be able to *remove* compiler options at the project level. For
example, the following need to be removed (MSVC12):

/RTC1
/EHsc

There is no "remove_compile_options()" (which would be convenient).
Any reason why this doesn't exist?

Right now I'm trying to remove these via CMAKE_CXX_FLAGS locally but
this doesn't work unless I forcefully change the cache. But this is a
permanent operation that affects *all* projects, which I do not want.
This is crazy, I'm hoping there is a better way. I'm using CMake 3.1
RC1. Can anyone help me figure out how to remove compiler options on a
per-project basis?

Here is what I'm doing:

function( add_clr_library target_name references )
    set( source_files ${ARGN} )

    set( default_references
        System
        System.Core
        System.Data
        System.Drawing
        #System.Xml
        #WindowsBase
    )

    if( CMAKE_CXX_FLAGS_DEBUG MATCHES "/RTC1" )
        string( REGEX REPLACE "/RTC(su|[1su])" ""
CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}" )
    endif()

    if( CMAKE_CXX_FLAGS MATCHES "/EHsc" )
        string( REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" )
    endif()

    add_library( ${target_name} SHARED ${source_files} )

    list( APPEND references ${default_references} )

    set_target_properties( ${target_name} PROPERTIES
        VS_DOTNET_REFERENCES "${references}"
        VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.5"
        COMPILE_FLAGS "/clr /EHa"
        DEBUG_POSTFIX "d"
    )
endfunction()


More information about the CMake mailing list