[CMake] Possible to modify COMPILE_FLAGS?

Michael Wild themiwi at gmail.com
Wed Oct 26 00:29:07 EDT 2011


On 10/26/2011 03:56 AM, David Cole wrote:
> On Tue, Oct 25, 2011 at 8:58 PM, Campbell Barton <ideasman42 at gmail.com
> <mailto:ideasman42 at gmail.com>> wrote:
> 
>     Thanks for that, from reading the docs now this is more clear but for
>     some reason I had the impression the flags on a file could be
>     manipulated.
> 
>     # ---
>     # Heres the macro I came up with to disable certain warnings for a
>     given set of files...
> 
>     macro(remove_strict_flags_file
>            filenames)
> 
>            foreach(_SOURCE ${ARGV})
> 
>                    if(CMAKE_COMPILER_IS_GNUCC)
>                            set_source_files_properties(${_SOURCE}
>                                    PROPERTIES
>                                            COMPILE_FLAGS
>     "-Wno-deprecated-declarations"
>                            )
>                    endif()
> 
>                    if(MSVC)
>                            # TODO
>                    endif()
> 
>            endforeach()
> 
>            unset(_SOURCE)
> 
>     endmacro()
> 
>     # ---
>     # Example use
> 
>     remove_strict_flags_file(
>            intern/indexer.c
>            intern/util.c
>            intern/anim_movie.c
>     )
> 
> 
> 
>     On Tue, Oct 25, 2011 at 7:16 PM, Michael Wild <themiwi at gmail.com
>     <mailto:themiwi at gmail.com>> wrote:
>     > On 10/25/2011 09:51 AM, Campbell Barton wrote:
>     >> The problem I'm trying to solve is to have "-Werror" for the whole
>     >> project except a few files which include system headers that give
>     >> warnings we can't workaround (even with -isystem).
>     >>
>     >> So I'm trying to get cmake to replace some flags for a specific file,
>     >> and I ran into the problem that the variable appears not to be set.
>     >>
>     >> get_source_file_property(MYVAR intern/BME_Customdata.c COMPILE_FLAGS)
>     >> ... modify the MYVAR
>     >> ... set the flags back
>     >>
>     >> But the variable isnt set, Im sure the source file is valid because I
>     >> printed it from the list passed to the target, and I tried this
>     before
>     >> and after defining the source file in the target:
>     >> message(FATAL_ERROR "Check: ${MYVAR}")
>     >> Check: NOTFOUND
>     >>
>     >>
>     >> I'm guessing the COMPILE_FLAGS property can be set which
>     overrides the
>     >> default flags but is there a good way to manipulate the existing
>     >> flags?
>     >>
>     >
>     > Problem is, that the COMPILE_FLAGS property only *appends* to the
>     > default compile flags. You'll have to remove -Werror from
>     > CMAKE_<LANG>_FLAGS[_<CONFIG>] variables and set it instead on all the
>     > files you want it to be set for.
>     >
>     > Michael
>     > --
>     >
>     > Powered by www.kitware.com <http://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
>     >
> 
> 
> 
>     --
>     - Campbell
>     --
> 
>     Powered by www.kitware.com <http://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
> 
> 
> The flags on a file CAN be manipulated, but Michael's point was that the
> flags on a  file are ADDED to the global flags and the flags on its
> containing target...
> 
> So there's no reliable way to set file flags that will remove flags that
> were already added by the global or target level flags.
> 
> 
> HTH,
> David
> 

Actually, I just found out, that in the special case of
CMAKE_COMPILER_IS_GNUCC{,XX} and the -WXXX settings, you actually can
effectively remove them for individual files, targets or directories by
setting the COMPILE_FLAGS property to -Wno-XXX. In above case, -Werror
will still be passed on the command line, but it will be overridden by
-Wno-error that comes later on.

HTH

Michael



More information about the CMake mailing list