Fix logic bug and make language explicit

The operator precedence in CMake can be surprising, so add parenthesis
to make in unambigious. Also, make it more explicit from which language
we get the default options. (They are all set from the settings given
for C++; why would have different optimization options for C and C++?)
This commit is contained in:
Roland Kaufmann
2013-09-18 13:11:03 +02:00
committed by Bård Skaflestad
parent b6c5d3572d
commit b13f9d251b
4 changed files with 17 additions and 14 deletions

View File

@@ -61,18 +61,21 @@ function (add_options langs builds)
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}")
function (set_default_option lang varname flag regex)
# lang is either C, CXX or Fortran
if ("${lang}" STREQUAL "Fortran")
set (letter "F")
else ()
set (letter "${lang}")
endif ()
string (TOUPPER "${CMAKE_BUILD_TYPE}" _build)
if ((NOT ("$ENV{${letter}FLAGS}" MATCHES "${regex}"))
AND (NOT ("${CMAKE_${lang}_FLAGS}" MATCHES "${regex}"))
AND (NOT ("${CMAKE_${lang}_FLAGS_${_build}}" 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}")
else ()
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}")
endif ()
endfunction (set_default_option)
# note: this must be called before project()

View File

@@ -16,7 +16,7 @@ is_compiler_gcc_compatible ()
# only debugging using the GNU toolchain is supported for now
if (CXX_COMPAT_GCC)
# default debug level, if not specified by the user
set_default_option (_dbg_flag "-ggdb3" "(^|\ )-g")
set_default_option (CXX _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

View File

@@ -37,8 +37,8 @@ if (CXX_COMPAT_GCC)
endif (WITH_NATIVE)
# default optimization flags, if not set by user
set_default_option (_opt_dbg "-O0" "(^|\ )-O")
set_default_option (_opt_rel "-O3" "(^|\ )-O")
set_default_option (CXX _opt_dbg "-O0" "(^|\ )-O")
set_default_option (CXX _opt_rel "-O3" "(^|\ )-O")
# use these options for debug builds - no optimizations
add_options (ALL_LANGUAGES "${_prof_DEBUG}" ${_opt_dbg} "-DDEBUG")

View File

@@ -6,7 +6,7 @@ is_compiler_gcc_compatible ()
if (CXX_COMPAT_GCC)
# default warnings flags, if not set by user
set_default_option (_warn_flag "-Wall" "(^|\ )-W")
set_default_option (CXX _warn_flag "-Wall" "(^|\ )-W")
if (_warn_flag)
message (STATUS "All warnings enabled: ${_warn_flag}")
add_options (ALL_LANGUAGES ALL_BUILDS "${_warn_flag}")