[CMake] COMPILE_FLAGS property that appends instead of replaces

aaron.meadows at thomsonreuters.com aaron.meadows at thomsonreuters.com
Mon Feb 6 17:13:23 EST 2012


I believe that is the defined behavior.  Usually, people would use
add_definitions() to add the flags before creating the target.  If you
have one target per subdirectory, you don't have to worry about removing
them afterwards:

 

CMakeLists.txt:

   add_definitions(/foo)

 

   add_subdirectory(baz)

   add_subdirectory(qux)

 

baz/CMakeLists.txt

   add_definitions(/bar)

   add_executable(baz baz.cpp)

 

qux/CMakeLists.txt

   add_executable(qux qux.cpp)

 

 

baz gets the compiler flags "/foo /bar", qux only gets "/foo".

 

 

 

As an aside, I ran into a similar issue, but with Linker Flags in the
cache.  To overcome that, I wrote a few functions to encapsulate the
writing of flags and handle checking for duplicates.  You could
certainly do something similar with properties:

 

############################################################

## Cache Set Helper Functions

############################################################

function(AppendIfMissing _outVar _inVar _value)

  string(REGEX REPLACE " " ";" _inList "${${_inVar}}")

  list(FIND _inList "${_value}" _pos)

 

  if(${_pos} EQUAL -1)

    set(${_outVar} "${${_inVar}} ${_value}" PARENT_SCOPE)

  else()

    set(${_outVar} "${${_inVar}}" PARENT_SCOPE)

  endif()

endfunction(AppendIfMissing)

 

function(CheckAndAppendCacheForce _varName _value _type )

  AppendIfMissing(_outvar ${_varName} ${_value})

 

  foreach(_arg IN LISTS ARGN)

    set(_desc "${_desc} ${_arg}")

  endforeach()

 

  set(${_varName} "${_outvar}" CACHE ${_type} "${_desc}" FORCE)

endfunction(CheckAndAppendCacheForce)

 

function(CheckAndAppendCache _varName _value _type )

  AppendIfMissing(_outvar ${_varName} ${_value})

 

  foreach(_arg IN LISTS ARGN)

    set(_desc "${_desc} ${_arg}")

  endforeach()

 

  set(${_varName} "${_outvar}" CACHE ${_type} "${_desc}")

endfunction(CheckAndAppendCache)

 

 

Aaron Meadows

 

From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On Behalf
Of Robert Dailey
Sent: Monday, February 06, 2012 3:58 PM
To: CMake ML
Subject: [CMake] COMPILE_FLAGS property that appends instead of replaces

 

I would like to set the COMPILE_FLAGS property multiple times on the
same target through set_target_properties(), however only the last call
seems to persist. Previous flags set get overridden. Is this the correct
behavior? If so, is there a way to make this property append instead of
replace on the same target?


 

---------

Robert Dailey


This email was sent to you by Thomson Reuters, the global news and information company. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Thomson Reuters.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20120206/53511e7e/attachment.htm>


More information about the CMake mailing list