From 2386325e575c78b001b6ff3cb0a5f1c78d460208 Mon Sep 17 00:00:00 2001 From: Roland Kaufmann Date: Thu, 4 Apr 2013 23:06:08 +0200 Subject: [PATCH] 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. --- CMakeLists.txt | 18 +++++++++++------- cmake/Modules/AddOptions.cmake | 21 +++++++++++++++++++++ cmake/Modules/UseDebugSymbols.cmake | 9 +++++++-- cmake/Modules/UseOptimization.cmake | 27 +++++++++++++++++++++++++-- cmake/Modules/UseWarnings.cmake | 8 ++++++-- 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index af3665e3..61346040 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 () diff --git a/cmake/Modules/AddOptions.cmake b/cmake/Modules/AddOptions.cmake index a5fb3864..0ebb8236 100644 --- a/cmake/Modules/AddOptions.cmake +++ b/cmake/Modules/AddOptions.cmake @@ -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) diff --git a/cmake/Modules/UseDebugSymbols.cmake b/cmake/Modules/UseDebugSymbols.cmake index 175579c1..10ec7e83 100644 --- a/cmake/Modules/UseDebugSymbols.cmake +++ b/cmake/Modules/UseDebugSymbols.cmake @@ -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. diff --git a/cmake/Modules/UseOptimization.cmake b/cmake/Modules/UseOptimization.cmake index e66f7a22..1a196355 100644 --- a/cmake/Modules/UseOptimization.cmake +++ b/cmake/Modules/UseOptimization.cmake @@ -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) diff --git a/cmake/Modules/UseWarnings.cmake b/cmake/Modules/UseWarnings.cmake index 2dce6453..65e8dcc6 100644 --- a/cmake/Modules/UseWarnings.cmake +++ b/cmake/Modules/UseWarnings.cmake @@ -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)