[CMake] Overriding build rules to ignore CMAKE_<LANG>_FLAGS_<BUILD_TYPE> - is it possible?

Maciej Mrozowski reavertm at poczta.fm
Tue Mar 3 00:49:37 EST 2009


Hi

I'm working on general purpose CMake applications building facility (in fact 
fixing existing one - part of Gentoo build system) and I need some way of 
filtering out hardcoded "insane" CMAKE_BUILD_TYPE-specific compiler flags still 
being able to preserve user specified CMAKE_BUILD_TYPE (to properly handle 
conditional build-type dependent targets, etc).

Let's imagine arbitrary scenario: (in CMakeLists.txt)
<begin>
# first problem - hardcoded build type - the only way to override it is to
# sed-it-out?
<some includes/code here>
set (CMAKE_BUILD_TYPE Release)
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O6 -funroll-loops -
ftree-vectorize")
add_executable (foo, ${foo_SOURCES})
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -foo-shmoo")
add_executable (bar ${bar_SOURCES})
set (CMAKE_C_FLAGS_RELEASE)
<some other stuff>
<end>

(maybe code above is not able to set CFLAGS per target - anyway let's imagine 
it is, so that clearing CMAKE_C_FLAGS_RELEASE does not clear those CFLAGS for 
executables defined earlier)

Just initializing cache CMAKE_CXX_FLAGS_RELEASE variable (for Release build) 
to empty (using CACHE FORCE attribute) makes it possible to clear this 
variable even if it's set explicitly - and it works for simple projects like 
below:

<begin>
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -vomit-frame-
pointer")
add_executable (foo foo.cpp)
install (TARGETS foo RUNTIME DESTINATION bin)
<end>

However it's not sufficient to override this variable in projects like KDE4 
(tested with kdelibs - I guess it applies to any KDE4 app) .
So that I tried heavier methods, overriding build rules.
The problem is, I cannot (possibly just don't know how) separate 
CMAKE_<LANG>_FLAGS from CMAKE_<LANG>_FLAGS_<BUILD_TYPE> - they are somehow 
magically combined together when they're about to be passed to compiler.
For example we have default simple CMAKE_C_COMPILE_OBJECT rule:

SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> 
-c <SOURCE>")

Replacing <FLAGS> with ${CMAKE_CXX_FLAGS} of course is not possible. Any 
ideas?

About motivation why to do so (why not just set CMAKE_BUILD_TYPE to none and 
get over it):
* I would like to be able for example to build release (optimized) 
applications with debug codepaths (more verbose output etc) or the other way 
around - debug applications with less verbose output.
With example of KDE4 - the latter is the best combination for everyday-use/yet 
testing box - executables with debug symbols are usually nearly as fast as 
those release ones (but can have symbols etc) - apps are not noisy, so they 
don't abuse terminal and don;t slow down everything (as IO operations seem to 
have most impact on performance)
* I need to properly support conditional build-type-specific targets, like:
If (CMAKE_BUILD_TYPE MATCHES Debug)
  add_subdirectory (something)
endif (CMAKE_BUILD_TYPE MATCHES Debug)
* I'd like to be able to set my own CFLAGS and be certain they are really in 
effect, no matter CMAKE_BUILD_TYPE (let's say I will care for -DNDEBUG and 
similar on my own)

On the other hand I don't mind CMAKE_CXX_FLAGS (and LINKER counterparts) 
hardcoded in CMakeLists, as people who put there something usually care for 
them to be safe and portable.

Any ideas?

cheers

-- 
regards
MM

----------------------------------------------------------------------

Zyskaj tak jak ja 9% na lokacie w pierwszym miesiacu! 
Sprawdz! http://link.interia.pl/f207b




More information about the CMake mailing list