Allow user to override our non-standard defaults

CMake loads option defaults from the platform file and then usually
proceeds to write these to the cache, so it is not possible to see
if an option was specified by the user, or was the default.

By setting CMAKE_NOT_USING_CONFIG_FLAGS, we regain control over the
options and can then set this to what we think is suitable, provided
that the user hasn't specified something for us.
This commit is contained in:
Roland Kaufmann 2013-04-04 23:06:08 +02:00
parent 0838152259
commit 2386325e57
5 changed files with 70 additions and 13 deletions

View File

@ -46,14 +46,8 @@ set (${project}_DEPS
"dune-istl"
)
# C++ project
cmake_minimum_required (VERSION 2.8)
project (${${project}_NAME})
enable_language (C)
enable_language (CXX)
# additional search modules
set (${project}_MODULE_DIR "${PROJECT_SOURCE_DIR}/cmake/Modules")
set (${project}_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
list (APPEND CMAKE_MODULE_PATH ${${project}_MODULE_DIR})
# include special
@ -67,6 +61,16 @@ if (CMAKE_VERSION VERSION_LESS "2.8.7")
list (APPEND CMAKE_MODULE_PATH "${${project}_MODULE_DIR}/compat-2.8.7")
endif (CMAKE_VERSION VERSION_LESS "2.8.7")
# don't write default flags into the cache, preserve that for user set values
include (AddOptions)
no_default_options ()
# C++ project
cmake_minimum_required (VERSION 2.8)
project (${${project}_NAME})
enable_language (C)
enable_language (CXX)
# print system information to better pinpoint issues from log alone
include (UseSystemInfo)
system_info ()

View File

@ -59,3 +59,24 @@ function (add_options langs builds)
endforeach (build)
endforeach (lang)
endfunction (add_options lang build)
# set varname to flag unless user has specified something that matches regex
function (set_default_option varname flag regex)
if (NOT "$ENV{CXXFLAGS}" MATCHES "${regex}"
AND NOT "${CMAKE_CXX_FLAGS}" MATCHES "${regex}"
AND NOT "${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}" MATCHES "${regex}")
set (${varname} ${flag} PARENT_SCOPE)
else (NOT "$ENV{CXXFLAGS}" MATCHES "${regex}"
AND NOT "${CMAKE_CXX_FLAGS}" MATCHES "${regex}"
AND NOT "${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}" MATCHES "${regex}")
set (${varname} PARENT_SCOPE)
endif (NOT "$ENV{CXXFLAGS}" MATCHES "${regex}"
AND NOT "${CMAKE_CXX_FLAGS}" MATCHES "${regex}"
AND NOT "${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}" MATCHES "${regex}")
endfunction (set_default_option)
# note: this must be called before project()
macro (no_default_options)
# prevent the platform probe to set options
set (CMAKE_NOT_USING_CONFIG_FLAGS TRUE)
endmacro (no_default_options)

View File

@ -13,11 +13,16 @@ include (AddOptions)
# only debugging using the GNU toolchain is supported for now
if (CMAKE_COMPILER_IS_GNUCXX)
# default debug level, if not specified by the user
set_default_option (_dbg_flag "-ggdb3" "(^|\ )-g")
# add debug symbols to *all* targets, regardless. there WILL come a
# time when you need to find a bug which only manifests itself in a
# release target on a production system!
message (STATUS "Generating debug symbols: -ggdb3")
add_options (ALL_LANGUAGES ALL_BUILDS "-ggdb3")
if (_dbg_flag)
message (STATUS "Generating debug symbols: ${_dbg_flag}")
add_options (ALL_LANGUAGES ALL_BUILDS "${_dbg_flag}")
endif (_dbg_flag)
# extracting the debug info is done by a separate utility in the GNU
# toolchain. check that this is actually installed.

View File

@ -7,11 +7,15 @@ include (AddOptions)
# otherwise, turn them on. indicate to the code what we have done
# so it can turn on assertions etc.
if (CMAKE_COMPILER_IS_GNUCXX)
# default optimization flags, if not set by user
set_default_option (_opt_dbg "-O0" "(^|\ )-O")
set_default_option (_opt_rel "-O3" "(^|\ )-O")
# use these options for debug builds - no optimizations
add_options (
ALL_LANGUAGES
"Debug"
"-O0" "-DDEBUG"
${_opt_dbg} "-DDEBUG"
)
# extra flags passed for optimization
@ -36,6 +40,25 @@ if (CMAKE_COMPILER_IS_GNUCXX)
add_options (
ALL_LANGUAGES
"Release;RelWithDebInfo;MinSizeRel"
"-O3" "-DNDEBUG" ${_opt_flags}
${_opt_rel} "-DNDEBUG" ${_opt_flags}
)
else (CMAKE_COMPILER_IS_GNUCXX)
# mapping from profile name (in CMAKE_BUILD_TYPE) to variable part
set (_prof_DEBUG "Debug")
set (_prof_RELEASE "Release;RelWithDebInfo;MinSizeRel")
# default information from system
foreach (lang IN ITEMS C CXX Fortran)
if (lang STREQUAL "Fortran")
set (_lang F)
else (lang STREQUAL "Fortran")
set (_lang ${lang})
endif (lang STREQUAL "Fortran")
foreach (profile IN ITEMS DEBUG RELEASE)
if (NOT CMAKE_${lang}_FLAGS_${profile})
add_options (${lang} "${_prof_${profile}}"
"$ENV{${_lang}FLAGS} ${CMAKE_${lang}_FLAGS_${profile}_INIT}")
endif (NOT CMAKE_${lang}_FLAGS_${profile})
endforeach (profile)
endforeach (lang)
endif (CMAKE_COMPILER_IS_GNUCXX)

View File

@ -2,6 +2,10 @@
include (AddOptions)
if (CMAKE_COMPILER_IS_GNUCXX)
message (STATUS "All warnings enabled: -Wall")
add_options (ALL_LANGUAGES ALL_BUILDS "-Wall")
# default warnings flags, if not set by user
set_default_option (_warn_flag "-Wall" "(^|\ )-W")
if (_warn_flag)
message (STATUS "All warnings enabled: ${_warn_flag}")
add_options (ALL_LANGUAGES ALL_BUILDS "${_warn_flag}")
endif (_warn_flag)
endif (CMAKE_COMPILER_IS_GNUCXX)