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-boun...@cmake.org [mailto:cmake-boun...@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.
--

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

Reply via email to