From 0cdcbb40a1f7fe0288698898027717959f4fbee1 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Tue, 12 May 2020 14:57:23 +0200 Subject: [PATCH] CMake: Enable default usage of utf8 sources for Qt consumers And enable the same default when building Qt itself (it's implicit). Allow opting out on a target-by-target basis, by using the public qt_disable_utf8_sources() API call. Change-Id: Ifc19a744d57b96b1c74a6926a0c6628c2a820464 Reviewed-by: Joerg Bornemann --- cmake/QtBaseGlobalTargets.cmake | 3 +++ cmake/QtBuild.cmake | 16 ++++++++++++++++ cmake/QtInternalTargets.cmake | 2 +- src/corelib/Qt6CoreMacros.cmake | 7 +++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/cmake/QtBaseGlobalTargets.cmake b/cmake/QtBaseGlobalTargets.cmake index d00d536194..e5006f0e20 100644 --- a/cmake/QtBaseGlobalTargets.cmake +++ b/cmake/QtBaseGlobalTargets.cmake @@ -271,6 +271,9 @@ add_library(Qt::GlobalConfigPrivate ALIAS GlobalConfigPrivate) # are computed. qt_set_language_standards_interface_compile_features(Platform) +# By default enable utf8 sources for both Qt and Qt consumers. Can be opted out. +qt_enable_utf8_sources(Platform) + # defines PlatformCommonInternal PlatformModuleInternal PlatformPluginInternal PlatformToolInternal include(QtInternalTargets) diff --git a/cmake/QtBuild.cmake b/cmake/QtBuild.cmake index 340ffc117b..4c37e1d4b1 100644 --- a/cmake/QtBuild.cmake +++ b/cmake/QtBuild.cmake @@ -4674,6 +4674,22 @@ function(qt_enable_msvc_cplusplus_define target visibility) endif() endfunction() +function(qt_enable_utf8_sources target) + set(utf8_flags "") + if(MSVC) + list(APPEND utf8_flags "-utf-8") + elseif(WIN32 AND ICC) + list(APPEND utf8_flags "-Qoption,cpp,--unicode_source_kind,UTF-8") + endif() + + if(utf8_flags) + # Allow opting out by specifying the QT_NO_UTF8_SOURCE target property. + set(genex_condition "$>>") + set(utf8_flags "$<${genex_condition}:${utf8_flags}>") + target_compile_options("${target}" INTERFACE "${utf8_flags}") + endif() +endfunction() + # Equivalent of qmake's qtNomakeTools(directory1 directory2). # If QT_NO_MAKE_TOOLS is true, then targets within the given directories will be excluded from the # default 'all' target, as well as from install phase. diff --git a/cmake/QtInternalTargets.cmake b/cmake/QtInternalTargets.cmake index 6940a367ac..c570771202 100644 --- a/cmake/QtInternalTargets.cmake +++ b/cmake/QtInternalTargets.cmake @@ -147,7 +147,7 @@ if (MSVC) ) endif() - target_compile_options(PlatformCommonInternal INTERFACE -Zc:wchar_t -utf-8) + target_compile_options(PlatformCommonInternal INTERFACE -Zc:wchar_t) target_link_options(PlatformCommonInternal INTERFACE -DYNAMICBASE -NXCOMPAT diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake index c4ccd04c60..53a6405aed 100644 --- a/src/corelib/Qt6CoreMacros.cmake +++ b/src/corelib/Qt6CoreMacros.cmake @@ -1257,3 +1257,10 @@ if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS) endif() endfunction() endif() + +# By default Qt6 forces usage of utf8 sources for consumers of Qt. +# Users can opt out of utf8 sources by calling this function with the target name of their +# application or library. +function(qt_disable_utf8_sources target) + set_target_properties("${target}" PROPERTIES QT_NO_UTF8_SOURCE TRUE) +endfunction()