[CMake] CMake rebuilding too much

Michael Wild themiwi at gmail.com
Tue Oct 12 07:24:16 EDT 2010


On 12. Oct, 2010, at 13:07 , Thomas Sondergaard wrote:

> On 07-10-2010 14:46, Bill Hoffman wrote:
> 
>> You are going to have to give more information or a small example that
>> shows the problem. It should not be doing that. You could try running
>> make -d to figure out why make is thinking it needs to do that.
>> 
>> -Bill
>> 
> 
> I found the problem. I had the following in my top-level CMakeLists.txt:
> 
> if(CMAKE_COMPILER_IS_GNUCXX)
>  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Woverloaded-virtual -Wno-unknown-pragmas -Werror" CACHE STRING "Flags used by the compiler during all build types." FORCE)
> endif()
> 
> I don't know why I had the FORCE in there, but it caused the warning flags above to be appended again whenever a CMakeLists.txt file was touched. Removing FORCE fixed it.
> 
> Thanks,
> 
> Thomas

The FORCE is OK, but you should do this only once, and only if CMAKE_CXX_FLAGS is the default. You don't want to upset your users, do you? Something like this should do:

if(CMAKE_COMPILER_IS_GNUCXX AND NOT __CHECKED_CXX_FLAGS)
  # this is the guard so we do this only once
  set(__CHECKED_CXX_FLAGS TRUE CACHE INTERNAL "Whether we checked the CXX flags already")
  if("${CMAKE_CXX_FLAGS}" STREQUAL "${CMAKE_CXX_FLAGS_INIT}")
    # only override defaults if the user didn't do so already!
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} Wall -Woverloaded-virtual -Wno-unknown-pragmas -Werror"
      CACHE STRING "Flags used by the compiler during all build types." FORCE)
  endif()
endif()

This way the flags end up in the cache and can be changed by the user without having to change the CMakeLists.txt file, and it also respects the user's choice of flags if he provides them in the initial CMake-run.

--
There is always a well-known solution to every human problem -- neat, plausible, and wrong.
H. L. Mencken

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
URL: <http://www.cmake.org/pipermail/cmake/attachments/20101012/62949bd3/attachment-0001.pgp>


More information about the CMake mailing list