From 659817e287c0948829b8d3c4239cc7fa6fcf70a2 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Wed, 24 Mar 2021 16:03:35 +0100 Subject: [PATCH] CMake: Fix building multi-arch universal macOS Qt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the same approach we use for iOS, which is to set multiple CMAKE_OSX_ARCHITECTURES values and let the clang front end deal with lipo-ing the final libraries. For now, Qt can be configured to build universal macOS libraries by passing 2 architectures to CMake, either via: -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" or -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" Currently we recommend specifying the intel x86_64 arch as the first one, to get an intel slice configuration that is comparable to a non-universal intel build. Specifying the arm64 slice first could pessimize optimizations and reduce the feature set for the intel slice due to the limitation that we run configure tests only once. The first specified architecture is the one used to do all the configure tests. It 'mostly' defines the common feature set of both architecture slices, with the excepion of some special handling for sse2 and neon instructions. In the future we might want to run at least the Qt architecture config test for all specified architectures, so that we can extract all the supported sub-arches and instruction sets in a reliable way. For now, we use the same sse2 hack as for iOS simulator_and_device builds, otherwise QtGui fails to link due to missing qt_memfill32_sse2 and other symbols. The hack is somewhat augmented to ensure that reconfiguration still succeeds (same issue happened with iOS). Previously the sse2 feature condition was broken due to force setting the feature to be ON. Now the condition also checks for a special QT_FORCE_FEATURE_sse2 variable which we set internally. Note that we shouldn't build for arm64e, because the binaries get killed when running on AS with the following message: kernel: exec_mach_imgact: not running binary built against preview arm64e ABI. Aslo, by default, we disable the arm64 slice for qt sql plugins, mostly because the CI provisioned sql libraries that we depend on only contain x86_64 slices, and trying to build the sql plugins for both slices will fail with linker errors. This behavior can be disabled for all targets marked by qt_internal_force_macos_intel_arch, by setting the QT_FORCE_MACOS_ALL_ARCHES CMake option to ON. To disble it per-target one can set QT_FORCE_MACOS_ALL_ARCHES_${target} to ON. Task-number: QTBUG-85447 Change-Id: Iccb5dfcc1a21a8a8292bd3817df0ea46c3445f75 Reviewed-by: Tor Arne Vestbø --- cmake/QtAutoDetect.cmake | 27 ++++++++++++++++++++ cmake/QtBaseConfigureTests.cmake | 8 ++++++ cmake/QtBaseGlobalTargets.cmake | 16 +++++++++++- cmake/QtFeature.cmake | 19 ++++++++++---- cmake/QtPostProcessHelpers.cmake | 5 ++++ cmake/QtTargetHelpers.cmake | 22 ++++++++++++++++ cmake/QtToolchainHelpers.cmake | 8 +++--- configure.cmake | 4 +-- src/corelib/global/qfloat16_f16c.c | 11 +++----- src/gui/CMakeLists.txt | 13 +++++----- src/plugins/sqldrivers/db2/CMakeLists.txt | 2 ++ src/plugins/sqldrivers/ibase/CMakeLists.txt | 2 ++ src/plugins/sqldrivers/mysql/CMakeLists.txt | 2 ++ src/plugins/sqldrivers/oci/CMakeLists.txt | 2 ++ src/plugins/sqldrivers/odbc/CMakeLists.txt | 2 ++ src/plugins/sqldrivers/psql/CMakeLists.txt | 2 ++ src/plugins/sqldrivers/sqlite/CMakeLists.txt | 4 +++ 17 files changed, 125 insertions(+), 24 deletions(-) diff --git a/cmake/QtAutoDetect.cmake b/cmake/QtAutoDetect.cmake index b32258e929..fd62fcb406 100644 --- a/cmake/QtAutoDetect.cmake +++ b/cmake/QtAutoDetect.cmake @@ -371,6 +371,32 @@ function(qt_auto_detect_darwin) qt_internal_get_xcode_version(xcode_version) set(QT_MAC_XCODE_VERSION "${xcode_version}" CACHE STRING "Xcode version.") + + set(device_names "iOS" "watchOS" "tvOS") + list(LENGTH CMAKE_OSX_ARCHITECTURES arch_count) + if(NOT CMAKE_SYSTEM_NAME IN_LIST device_names AND arch_count GREATER 0) + foreach(arch ${CMAKE_OSX_ARCHITECTURES}) + if(arch STREQUAL "arm64e") + message(WARNING "Applications built against an arm64e Qt architecture will " + "likely fail to run on Apple Silicon. Consider targeting " + "'arm64' instead.") + endif() + endforeach() + endif() + endif() +endfunction() + +function(qt_auto_detect_macos_universal) + set(device_names "iOS" "watchOS" "tvOS") + if(APPLE AND NOT CMAKE_SYSTEM_NAME IN_LIST device_names) + list(LENGTH CMAKE_OSX_ARCHITECTURES arch_count) + + set(is_universal "OFF") + if(arch_count GREATER 1) + set(is_universal "ON") + endif() + + set(QT_IS_MACOS_UNIVERSAL "${is_universal}" CACHE INTERNAL "Build universal Qt for macOS") endif() endfunction() @@ -394,6 +420,7 @@ qt_auto_detect_cmake_generator() qt_auto_detect_cyclic_toolchain() qt_auto_detect_cmake_config() qt_auto_detect_darwin() +qt_auto_detect_macos_universal() qt_auto_detect_ios() qt_auto_detect_android() qt_auto_detect_vpckg() diff --git a/cmake/QtBaseConfigureTests.cmake b/cmake/QtBaseConfigureTests.cmake index 67cee65d4a..15d506db16 100644 --- a/cmake/QtBaseConfigureTests.cmake +++ b/cmake/QtBaseConfigureTests.cmake @@ -160,9 +160,17 @@ function(qt_internal_print_cmake_darwin_info) message(STATUS "CMAKE_OSX_DEPLOYMENT_TARGET: \"${CMAKE_OSX_DEPLOYMENT_TARGET}\"") message(STATUS "QT_MAC_SDK_VERSION: \"${QT_MAC_SDK_VERSION}\"") message(STATUS "QT_MAC_XCODE_VERSION: \"${QT_MAC_XCODE_VERSION}\"") + + if(DEFINED CACHE{QT_IS_MACOS_UNIVERSAL}) + message(STATUS "QT_IS_MACOS_UNIVERSAL: \"${QT_IS_MACOS_UNIVERSAL}\"") + endif() if(QT_UIKIT_SDK) message(STATUS "QT_UIKIT_SDK: \"${QT_UIKIT_SDK}\"") endif() + qt_internal_get_first_osx_arch(osx_first_arch) + if(osx_first_arch) + message(STATUS "Configure tests main architecture (in multi-arch build): \"${osx_first_arch}\"") + endif() endif() endfunction() qt_internal_print_cmake_darwin_info() diff --git a/cmake/QtBaseGlobalTargets.cmake b/cmake/QtBaseGlobalTargets.cmake index 580e84f80a..23a2dfbcf4 100644 --- a/cmake/QtBaseGlobalTargets.cmake +++ b/cmake/QtBaseGlobalTargets.cmake @@ -91,13 +91,27 @@ include("${CMAKE_CURRENT_SOURCE_DIR}/configure.cmake") # Do what mkspecs/features/uikit/default_pre.prf does, aka enable sse2 for # simulator_and_device_builds. -if(UIKIT AND NOT QT_UIKIT_SDK) + +qt_internal_get_first_osx_arch(__qt_osx_first_arch) +set(__qt_apple_silicon_arches "arm64;arm64e") +if((UIKIT AND NOT QT_UIKIT_SDK) + OR (MACOS AND QT_IS_MACOS_UNIVERSAL + AND __qt_osx_first_arch IN_LIST __qt_apple_silicon_arches)) + set(QT_FORCE_FEATURE_sse2 ON CACHE INTERNAL "Force enable sse2 due to platform requirements.") set(__QtFeature_custom_enabled_cache_variables TEST_subarch_sse2 FEATURE_sse2 QT_FEATURE_sse2) endif() +if(MACOS AND QT_IS_MACOS_UNIVERSAL AND __qt_osx_first_arch STREQUAL "x86_64") + set(QT_FORCE_FEATURE_neon ON CACHE INTERNAL "Force enable neon due to platform requirements.") + set(__QtFeature_custom_enabled_cache_variables + TEST_subarch_neon + FEATURE_neon + QT_FEATURE_neon) +endif() + qt_feature_module_end(GlobalConfig OUT_VAR_PREFIX "__GlobalConfig_") qt_generate_global_config_pri_file() diff --git a/cmake/QtFeature.cmake b/cmake/QtFeature.cmake index 75a9d91560..7f4a9d8b52 100644 --- a/cmake/QtFeature.cmake +++ b/cmake/QtFeature.cmake @@ -544,10 +544,10 @@ function(qt_feature_module_end) # Evaluate custom cache assignments. foreach(cache_var_name ${__QtFeature_custom_enabled_cache_variables}) - set(${cache_var_name} ON CACHE BOOL "Force enabled by platform." FORCE) + set(${cache_var_name} ON CACHE BOOL "Force enabled by platform requirements." FORCE) endforeach() foreach(cache_var_name ${__QtFeature_custom_disabled_cache_variables}) - set(${cache_var_name} OFF CACHE BOOL "Force disabled by platform." FORCE) + set(${cache_var_name} OFF CACHE BOOL "Force disabled by platform requirements." FORCE) endforeach() set(enabled_public_features "") @@ -890,9 +890,8 @@ function(qt_get_platform_try_compile_vars out_var) # Pass darwin specific options. # The architectures need to be passed explicitly to project-based try_compile calls even on # macOS, so that arm64 compilation works on Apple silicon. - if(CMAKE_OSX_ARCHITECTURES) - list(GET CMAKE_OSX_ARCHITECTURES 0 osx_first_arch) - + qt_internal_get_first_osx_arch(osx_first_arch) + if(osx_first_arch) # Do what qmake does, aka when doing a simulator_and_device build, build the # target architecture test only with the first given architecture, which should be the # device architecture, aka some variation of "arm" (armv7, arm64). @@ -909,6 +908,16 @@ function(qt_get_platform_try_compile_vars out_var) set("${out_var}" "${flags_cmd_line}" PARENT_SCOPE) endfunction() +# Set out_var to the first value of CMAKE_OSX_ARCHITECTURES. +# Sets an empty string if no architecture is present. +function(qt_internal_get_first_osx_arch out_var) + set(value "") + if(CMAKE_OSX_ARCHITECTURES) + list(GET CMAKE_OSX_ARCHITECTURES 0 value) + endif() + set(${out_var} "${value}" PARENT_SCOPE) +endfunction() + function(qt_config_compile_test_x86simd extension label) if (DEFINED TEST_X86SIMD_${extension}) return() diff --git a/cmake/QtPostProcessHelpers.cmake b/cmake/QtPostProcessHelpers.cmake index aa57c3aa9f..4b6065e7a9 100644 --- a/cmake/QtPostProcessHelpers.cmake +++ b/cmake/QtPostProcessHelpers.cmake @@ -525,6 +525,11 @@ endif()\n") "set(BUILD_WITH_PCH \"${BUILD_WITH_PCH}\" CACHE STRING \"\")\n") endif() + if(DEFINED QT_IS_MACOS_UNIVERSAL) + string(APPEND QT_EXTRA_BUILD_INTERNALS_VARS + "set(QT_IS_MACOS_UNIVERSAL \"${QT_IS_MACOS_UNIVERSAL}\" CACHE BOOL \"\")\n") + endif() + if(CMAKE_CROSSCOMPILING AND QT_BUILD_TOOLS_WHEN_CROSSCOMPILING) string(APPEND QT_EXTRA_BUILD_INTERNALS_VARS "set(QT_BUILD_TOOLS_WHEN_CROSSCOMPILING \"TRUE\" CACHE BOOL \"\" FORCE)\n") diff --git a/cmake/QtTargetHelpers.cmake b/cmake/QtTargetHelpers.cmake index 99e84a2cd7..5ea46a7468 100644 --- a/cmake/QtTargetHelpers.cmake +++ b/cmake/QtTargetHelpers.cmake @@ -558,3 +558,25 @@ function(qt_internal_install_pdb_files target install_dir_path) endif() endif() endfunction() + +# Certain targets might have dependencies on libraries that don't have an Apple Silicon arm64 +# slice. When doing a universal macOS build, force those targets to be built only for the +# Intel x86_64 arch. +# This behavior can be disabled for all targets by setting the QT_FORCE_MACOS_ALL_ARCHES cache +# variable to TRUE or by setting the target specific cache variable +# QT_FORCE_MACOS_ALL_ARCHES_${target} to TRUE. +# +# TODO: Ideally we'd use something like _apple_resolve_supported_archs_for_sdk_from_system_lib +# from CMake's codebase to parse which architectures are available in a library, but it's +# not straightforward to extract the library absolute file path from a CMake target. Furthermore +# Apple started using a built-in dynamic linker cache of all system-provided libraries as per +# https://gitlab.kitware.com/cmake/cmake/-/issues/20863 +# so if the target is a library in the dynamic cache, that might further complicate how to get +# the list of arches in it. +function(qt_internal_force_macos_intel_arch target) + if(MACOS AND QT_IS_MACOS_UNIVERSAL AND NOT QT_FORCE_MACOS_ALL_ARCHES + AND NOT QT_FORCE_MACOS_ALL_ARCHES_${target}) + set(arches "x86_64") + set_target_properties(${target} PROPERTIES OSX_ARCHITECTURES "${arches}") + endif() +endfunction() diff --git a/cmake/QtToolchainHelpers.cmake b/cmake/QtToolchainHelpers.cmake index 5495cbf6c5..f2ef6147e1 100644 --- a/cmake/QtToolchainHelpers.cmake +++ b/cmake/QtToolchainHelpers.cmake @@ -112,15 +112,17 @@ function(qt_internal_create_toolchain_file) "set(CMAKE_OSX_DEPLOYMENT_TARGET \"${CMAKE_OSX_DEPLOYMENT_TARGET}\" CACHE STRING \"\")") endif() - if(UIKIT) - list(APPEND init_platform - "set(CMAKE_SYSTEM_NAME \"${CMAKE_SYSTEM_NAME}\" CACHE STRING \"\")") + if(UIKIT OR (MACOS AND QT_IS_MACOS_UNIVERSAL)) set(_qt_osx_architectures_escaped "${CMAKE_OSX_ARCHITECTURES}") string(REPLACE ";" "LITERAL_SEMICOLON" _qt_osx_architectures_escaped "${_qt_osx_architectures_escaped}") list(APPEND init_platform "set(CMAKE_OSX_ARCHITECTURES \"${_qt_osx_architectures_escaped}\" CACHE STRING \"\")") + endif() + if(UIKIT) + list(APPEND init_platform + "set(CMAKE_SYSTEM_NAME \"${CMAKE_SYSTEM_NAME}\" CACHE STRING \"\")") list(APPEND init_platform "if(CMAKE_GENERATOR STREQUAL \"Xcode\" AND NOT QT_NO_XCODE_EMIT_EPN)") list(APPEND init_platform " set_property(GLOBAL PROPERTY XCODE_EMIT_EFFECTIVE_PLATFORM_NAME OFF)") list(APPEND init_platform "endif()") diff --git a/configure.cmake b/configure.cmake index b477499915..88e0c360dd 100644 --- a/configure.cmake +++ b/configure.cmake @@ -651,7 +651,7 @@ qt_feature("signaling_nan" PUBLIC ) qt_feature("sse2" PRIVATE LABEL "SSE2" - CONDITION ( ( TEST_architecture_arch STREQUAL i386 ) OR ( TEST_architecture_arch STREQUAL x86_64 ) ) AND TEST_subarch_sse2 + CONDITION ( ( ( TEST_architecture_arch STREQUAL i386 ) OR ( TEST_architecture_arch STREQUAL x86_64 ) ) AND TEST_subarch_sse2 ) OR QT_FORCE_FEATURE_sse2 # special case ) qt_feature_definition("sse2" "QT_COMPILER_SUPPORTS_SSE2" VALUE "1") qt_feature_config("sse2" QMAKE_PRIVATE_CONFIG) @@ -795,7 +795,7 @@ qt_feature_definition("mips_dspr2" "QT_COMPILER_SUPPORTS_MIPS_DSPR2" VALUE "1") qt_feature_config("mips_dspr2" QMAKE_PRIVATE_CONFIG) qt_feature("neon" PRIVATE LABEL "NEON" - CONDITION ( ( TEST_architecture_arch STREQUAL arm ) OR ( TEST_architecture_arch STREQUAL arm64 ) ) AND TEST_arch_${TEST_architecture_arch}_subarch_neon + CONDITION ( ( ( TEST_architecture_arch STREQUAL arm ) OR ( TEST_architecture_arch STREQUAL arm64 ) ) AND TEST_arch_${TEST_architecture_arch}_subarch_neon ) OR QT_FORCE_FEATURE_neon # special case ) qt_feature_definition("neon" "QT_COMPILER_SUPPORTS_NEON" VALUE "1") qt_feature_config("neon" QMAKE_PRIVATE_CONFIG) diff --git a/src/corelib/global/qfloat16_f16c.c b/src/corelib/global/qfloat16_f16c.c index ba1e16f481..d60a021bdb 100644 --- a/src/corelib/global/qfloat16_f16c.c +++ b/src/corelib/global/qfloat16_f16c.c @@ -40,13 +40,8 @@ #include "private/qsimd_p.h" // The x86 F16C instructions operate on AVX registers, so AVX support is -// required. We don't need to check for __F16C__ because we this file wouldn't -// have been compiled if the support was missing in the first place, and not -// all compilers define it. Technically, we didn't need to check for __AVX__ -// either. -#if !QT_COMPILER_SUPPORTS_HERE(AVX) -# error "AVX support required" -#endif +// required. +#if QT_COMPILER_SUPPORTS_HERE(AVX) #ifdef __cplusplus QT_BEGIN_NAMESPACE @@ -89,3 +84,5 @@ void qFloatFromFloat16_fast(float *out, const quint16 *in, qsizetype len) Q_DECL } // extern "C" QT_END_NAMESPACE #endif + +#endif // QT_COMPILER_SUPPORTS_HERE(AVX) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 89cefdc818..31e727189b 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -615,6 +615,8 @@ if(NOT ANDROID) qt_internal_add_simd_part(Gui SIMD arch_haswell SOURCES painting/qdrawhelper_avx2.cpp + EXCLUDE_OSX_ARCHITECTURES + arm64 ) endif() @@ -767,12 +769,11 @@ qt_internal_extend_target(Gui CONDITION MINGW AND WIN32 uuid ) -qt_internal_extend_target(Gui CONDITION UNIX AND NOT ANDROID AND NOT INTEGRITY AND NOT (TEST_architecture_arch STREQUAL "arm64") AND NOT UIKIT - DEFINES - ENABLE_PIXMAN_DRAWHELPERS -) - -if(UNIX AND NOT ANDROID AND NOT INTEGRITY AND NOT (TEST_architecture_arch STREQUAL "arm64") AND NOT UIKIT) +# Only enable the Pixman draw-helpers on platforms that support the GAS syntax of their asm files +# Note: These helpers are only used for 16-bit surfaces, so excluding them does not generally +# exclude neon-drawhelpers on these platforms. +if(UNIX AND NOT ANDROID AND NOT INTEGRITY AND NOT (TEST_architecture_arch STREQUAL "arm64") AND NOT UIKIT AND NOT QT_IS_MACOS_UNIVERSAL) + qt_internal_extend_target(Gui DEFINES ENABLE_PIXMAN_DRAWHELPERS) qt_internal_add_simd_part(Gui SIMD neon SOURCES ../3rdparty/pixman/pixman-arm-neon-asm.S diff --git a/src/plugins/sqldrivers/db2/CMakeLists.txt b/src/plugins/sqldrivers/db2/CMakeLists.txt index 6d662434ae..de90d92581 100644 --- a/src/plugins/sqldrivers/db2/CMakeLists.txt +++ b/src/plugins/sqldrivers/db2/CMakeLists.txt @@ -30,3 +30,5 @@ qt_internal_extend_target(QDB2DriverPlugin CONDITION (TEST_architecture_arch STR DEFINES ODBC64 ) + +qt_internal_force_macos_intel_arch(QDB2DriverPlugin) diff --git a/src/plugins/sqldrivers/ibase/CMakeLists.txt b/src/plugins/sqldrivers/ibase/CMakeLists.txt index 5f200597f5..4b914401ed 100644 --- a/src/plugins/sqldrivers/ibase/CMakeLists.txt +++ b/src/plugins/sqldrivers/ibase/CMakeLists.txt @@ -12,3 +12,5 @@ qt_internal_add_plugin(QIBaseDriverPlugin Qt::Core Qt::CorePrivate Qt::SqlPrivate) + +qt_internal_force_macos_intel_arch(QIBaseDriverPlugin) diff --git a/src/plugins/sqldrivers/mysql/CMakeLists.txt b/src/plugins/sqldrivers/mysql/CMakeLists.txt index 8e8244acb3..0679bcb698 100644 --- a/src/plugins/sqldrivers/mysql/CMakeLists.txt +++ b/src/plugins/sqldrivers/mysql/CMakeLists.txt @@ -22,3 +22,5 @@ qt_internal_add_plugin(QMYSQLDriverPlugin #### Keys ignored in scope 1:.:.:mysql.pro:: # OTHER_FILES = "mysql.json" + +qt_internal_force_macos_intel_arch(QMYSQLDriverPlugin) diff --git a/src/plugins/sqldrivers/oci/CMakeLists.txt b/src/plugins/sqldrivers/oci/CMakeLists.txt index c6e38f4cdf..4830586064 100644 --- a/src/plugins/sqldrivers/oci/CMakeLists.txt +++ b/src/plugins/sqldrivers/oci/CMakeLists.txt @@ -30,3 +30,5 @@ qt_internal_extend_target(QOCIDriverPlugin CONDITION APPLE LINK_OPTIONS "-Wl,-flat_namespace,-U,_environ" ) + +qt_internal_force_macos_intel_arch(QOCIDriverPlugin) diff --git a/src/plugins/sqldrivers/odbc/CMakeLists.txt b/src/plugins/sqldrivers/odbc/CMakeLists.txt index 99a6b9104d..032d89a244 100644 --- a/src/plugins/sqldrivers/odbc/CMakeLists.txt +++ b/src/plugins/sqldrivers/odbc/CMakeLists.txt @@ -32,3 +32,5 @@ qt_internal_extend_target(QODBCDriverPlugin CONDITION UNIX DEFINES UNICODE ) + +qt_internal_force_macos_intel_arch(QODBCDriverPlugin) diff --git a/src/plugins/sqldrivers/psql/CMakeLists.txt b/src/plugins/sqldrivers/psql/CMakeLists.txt index b151932da7..648d29f582 100644 --- a/src/plugins/sqldrivers/psql/CMakeLists.txt +++ b/src/plugins/sqldrivers/psql/CMakeLists.txt @@ -35,3 +35,5 @@ if(MINGW) DISABLE_PRECOMPILE_HEADERS ON ) endif() + +qt_internal_force_macos_intel_arch(QPSQLDriverPlugin) diff --git a/src/plugins/sqldrivers/sqlite/CMakeLists.txt b/src/plugins/sqldrivers/sqlite/CMakeLists.txt index ad39f1547b..bbd03e451f 100644 --- a/src/plugins/sqldrivers/sqlite/CMakeLists.txt +++ b/src/plugins/sqldrivers/sqlite/CMakeLists.txt @@ -36,6 +36,10 @@ if (NOT QT_FEATURE_system_sqlite) # On newer compilers compiling sqlite.c produces warnings qt_disable_warnings(QSQLiteDriverPlugin) endif() + +if(QT_FEATURE_system_sqlite) + qt_internal_force_macos_intel_arch(QSQLiteDriverPlugin) +endif() # special case end qt_internal_extend_target(QSQLiteDriverPlugin CONDITION NOT QT_FEATURE_system_sqlite