Pick the latest available C/C++ standard when compiling Qt

This maps the behavior of mkspecs/features/qt_common.prf and enables the
use of C++17 for example in Android, where the toolchain supports it
anyway.

Change-Id: I41f4bdb160a3929e2fb78f36efb1ad5f2ad391a5
Reviewed-by: Qt CMake Build Bot
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Simon Hausmann 2019-10-17 10:14:09 +02:00
parent 2fa23e46c0
commit 7e7f41e40c
3 changed files with 23 additions and 5 deletions

View File

@ -35,6 +35,8 @@ include(QtBaseCMakeTesting)
## Targets for global features, etc.:
include(QtBaseGlobalTargets)
qt_set_language_standards()
## Should this Qt be static or dynamically linked?
option(BUILD_SHARED_LIBS "Build Qt statically or dynamically" ON)
set(QT_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})

View File

@ -3298,3 +3298,22 @@ function(qt_generate_qconfig_cpp)
configure_file(global/qconfig.cpp.in global/qconfig.cpp @ONLY)
endfunction()
function(qt_set_language_standards)
## Use the latest standard the compiler supports (same as qt_common.prf)
if (QT_FEATURE_cxx2a)
set(CMAKE_CXX_STANDARD 20 PARENT_SCOPE)
elseif (QT_FEATURE_cxx17)
set(CMAKE_CXX_STANDARD 17 PARENT_SCOPE)
elseif (QT_FEATURE_cxx14)
set(CMAKE_CXX_STANDARD 14 PARENT_SCOPE)
elseif (QT_FEATURE_cxx11)
set(CMAKE_CXX_STANDARD 11 PARENT_SCOPE)
endif()
if (c_std_11 IN_LIST CMAKE_C_COMPILE_FEATURES)
set(CMAKE_C_STANDARD 11 PARENT_SCOPE)
elseif (c_std_99 IN_LIST CMAKE_C_COMPILE_FEATURES)
set(CMAKE_C_STANDARD 99 PARENT_SCOPE)
endif()
endfunction()

View File

@ -24,11 +24,6 @@ elseif(APPLE)
set(CMAKE_DEBUG_POSTFIX "_debug")
endif()
## Force C++ standard, do not fall back, do not use compiler extensions
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
## Position independent code:
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@ -110,6 +105,8 @@ include(QtCompilerFlags)
## Set up non-prefix build:
qt_set_up_nonprefix_build()
qt_set_language_standards()
## Find host tools (if non native):
set(QT_HOST_PATH "" CACHE PATH "Installed Qt host directory path, used for cross compiling.")