cmlib/CMLibCompilerFlags.cmake
2019-02-12 19:31:17 +03:00

116 lines
4.3 KiB
CMake

# based on https://github.com/bluescarni/yacma
include(CheckCXXCompilerFlag)
macro(CHECK_ENABLE_CXX_FLAG flag)
set(CMAKE_REQUIRED_QUIET TRUE)
check_cxx_compiler_flag("${flag}" CHECK_CXX_FLAG)
unset(CMAKE_REQUIRED_QUIET)
if(CHECK_CXX_FLAG)
message(STATUS "'${flag}': flag is enabled.")
add_compile_options("${flag}")
else()
message(STATUS "'${flag}': flag is NOT supported.")
endif()
# NOTE: check_cxx_compiler stores variables in the cache.
unset(CHECK_CXX_FLAG CACHE)
endmacro()
# Common configuration for GCC, clang and Intel.
if(CMAKE_CXX_COMPILER_IS_CLANG OR CMAKE_CXX_COMPILER_IS_INTEL OR CMAKE_CXX_COMPILER_IS_GCC)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS YES)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
CHECK_ENABLE_CXX_FLAG(-Wall)
CHECK_ENABLE_CXX_FLAG(-Wextra)
CHECK_ENABLE_CXX_FLAG(-Wnon-virtual-dtor)
CHECK_ENABLE_CXX_FLAG(-Wnoexcept)
CHECK_ENABLE_CXX_FLAG(-Wlogical-op)
CHECK_ENABLE_CXX_FLAG(-Wconversion)
CHECK_ENABLE_CXX_FLAG(-Wdeprecated)
# This limit is supposed to be at least 1024 in C++11, but for some reason
# clang sets this to 256, and gcc to 900.
CHECK_ENABLE_CXX_FLAG(-ftemplate-depth=1024)
CHECK_ENABLE_CXX_FLAG(-Wold-style-cast)
CHECK_ENABLE_CXX_FLAG(-Wdisabled-optimization)
# This is useful when the compiler decides the template backtrace is too verbose.
CHECK_ENABLE_CXX_FLAG(-ftemplate-backtrace-limit=0)
CHECK_ENABLE_CXX_FLAG(-fstack-protector-all)
# A few suggestion flags.
CHECK_ENABLE_CXX_FLAG(-Wsuggest-attribute=pure)
CHECK_ENABLE_CXX_FLAG(-Wsuggest-attribute=const)
CHECK_ENABLE_CXX_FLAG(-Wsuggest-attribute=noreturn)
CHECK_ENABLE_CXX_FLAG(-Wsuggest-attribute=format)
# From GCC 5.
CHECK_ENABLE_CXX_FLAG(-Wodr)
CHECK_ENABLE_CXX_FLAG(-Wsuggest-final-types)
CHECK_ENABLE_CXX_FLAG(-Wsuggest-final-methods)
CHECK_ENABLE_CXX_FLAG(-Wsuggest-override)
# From GCC 6.
CHECK_ENABLE_CXX_FLAG(-Wshift-negative-value)
CHECK_ENABLE_CXX_FLAG(-Wshift-overflow=2)
CHECK_ENABLE_CXX_FLAG(-Wduplicated-cond)
CHECK_ENABLE_CXX_FLAG(-Wnull-dereference)
# From GCC 7.
CHECK_ENABLE_CXX_FLAG(-Wrestrict)
CHECK_ENABLE_CXX_FLAG(-Waligned-new)
# From GCC 8.
CHECK_ENABLE_CXX_FLAG(-Wcast-align=strict)
# This is supposed to produce a nice graphical visualization
# of mismatching template errors.
CHECK_ENABLE_CXX_FLAG(-fdiagnostics-show-template-tree)
CHECK_ENABLE_CXX_FLAG(-fdiagnostics-show-option)
CHECK_ENABLE_CXX_FLAG(-pedantic)
CHECK_ENABLE_CXX_FLAG(-Wcast-align)
CHECK_ENABLE_CXX_FLAG(-Wcast-qual)
CHECK_ENABLE_CXX_FLAG(-Wctor-dtor-privacy)
CHECK_ENABLE_CXX_FLAG(-Wdisabled-optimization)
CHECK_ENABLE_CXX_FLAG(-Wformat=2)
CHECK_ENABLE_CXX_FLAG(-Winit-self)
CHECK_ENABLE_CXX_FLAG(-Wmissing-include-dirs)
CHECK_ENABLE_CXX_FLAG(-Woverloaded-virtual)
CHECK_ENABLE_CXX_FLAG(-Wredundant-decls)
CHECK_ENABLE_CXX_FLAG(-Wsign-promo)
CHECK_ENABLE_CXX_FLAG(-Wstrict-overflow=5)
CHECK_ENABLE_CXX_FLAG(-Wundef)
CHECK_ENABLE_CXX_FLAG(-Wno-unused)
CHECK_ENABLE_CXX_FLAG(-Wno-variadic-macros)
CHECK_ENABLE_CXX_FLAG(-Wno-parentheses)
CHECK_ENABLE_CXX_FLAG(-Wstrict-null-sentinel)
CHECK_ENABLE_CXX_FLAG(-Wshadow)
CHECK_ENABLE_CXX_FLAG(-Wshadow-all)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Release")
CHECK_ENABLE_CXX_FLAG(-flto)
if(CMAKE_CXX_COMPILER_IS_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "4.8")
# Doesn't work with GCC 4.7.2 in Astra Linux 1.5
CHECK_ENABLE_CXX_FLAG(-fno-fat-lto-objects)
endif()
endif()
endif()
if (CMAKE_CXX_COMPILER_IS_GCC)
CHECK_ENABLE_CXX_FLAG(-fdiagnostics-color=auto)
# The -Wmaybe-uninitialized flag is enabled by -Wall, but it is known
# to emit a lot of possibly spurious warnings. Let's just disable it.
CHECK_ENABLE_CXX_FLAG(-Wno-maybe-uninitialized)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "5.999")
# NOTE: GCC >= 6 seems to be wrongly warning about visibility attributes
# in some situations:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80947
# Let's just disable the warning for now.
CHECK_ENABLE_CXX_FLAG(-Wno-attributes)
endif()
endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "e2k")
add_definitions(-DQT_NO_VERSION_TAGGING)
list(APPEND CMAKE_LIBRARY_PATH "/usr/lib/e2k-linux-gnu")
endif()