From c1038019fcab3f244929d599d5201e8244131ba2 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Fri, 14 Aug 2020 16:49:12 +0200 Subject: [PATCH] CMake: Fix various issues with building CMake Android projects Fix detection of qt android platform plugin by globbing inside the install qt6 prefix location. This is just a sanity check. Fix platform plugin detection for CMake standalone tests configured using qt-cmake-standalone-test, which used to look into the fake standalone prefix location instead of the real Qt location. Fix detection of stdlib path using CMAKE_SYSROOT. Add a global apk target that allows easier building of all apk targets defined in the project. Creation of this target can be opted out by setting QT_NO_GLOBAL_APK_TARGET to TRUE. Amends b1f8ca8032bd0500f356c55c335937f7fb89d3f5. Task-number: QTBUG-85399 Change-Id: Ic9c1646b4f00e0084fe3f4397df471b8f925afd8 Reviewed-by: Cristian Adam --- cmake/QtPlatformAndroid.cmake | 47 +++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/cmake/QtPlatformAndroid.cmake b/cmake/QtPlatformAndroid.cmake index 15f40f42c3b..0f8e62dff00 100644 --- a/cmake/QtPlatformAndroid.cmake +++ b/cmake/QtPlatformAndroid.cmake @@ -167,16 +167,26 @@ function(qt_android_generate_deployment_settings target) " \"description\": \"This file is generated by cmake to be read by androiddeployqt and should not be modified by hand.\",\n") # Host Qt Android install path - if (NOT QT_BUILDING_QT) - set(file_check "${Qt6_DIR}/plugins/platforms/android/libqtforandroid_${CMAKE_ANDROID_ARCH_ABI}.so") - if (NOT EXISTS ${file_check}) - message(SEND_ERROR "Detected Qt installation does not contain libqtforandroid.so. This is most likely due to the installation not being a build of Qt for Android. Please update your settings.") + if (NOT QT_BUILDING_QT OR QT_STANDALONE_TEST_PATH) + set(qt_path "${QT6_INSTALL_PREFIX}") + set(android_plugin_dir_path "${qt_path}/${QT6_INSTALL_PLUGINS}/platforms") + set(glob_expression "${android_plugin_dir_path}/*qtforandroid*${CMAKE_ANDROID_ARCH_ABI}.so") + file(GLOB plugin_dir_files LIST_DIRECTORIES FALSE "${glob_expression}") + if (NOT plugin_dir_files) + message(SEND_ERROR + "Detected Qt installation does not contain qtforandroid_${CMAKE_ANDROID_ARCH_ABI}.so in the following dir: +${android_plugin_dir_path} +This is most likely due to the installation not being a Qt for Android build. +Please recheck your build configuration.") return() + else() + list(GET plugin_dir_files 0 android_platform_plugin_path) + message(STATUS "Found android platform plugin at: ${android_platform_plugin_path}") endif() - set(qt_android_install_dir ${Qt6_Dir}) + set(qt_android_install_dir "${qt_path}") else() - # Building from source, use the same install prefix - set(qt_android_install_dir ${CMAKE_INSTALL_PREFIX}) + # Building from source, use the same install prefix. + set(qt_android_install_dir "${CMAKE_INSTALL_PREFIX}") endif() file(TO_NATIVE_PATH "${qt_android_install_dir}" qt_android_install_dir_native) @@ -298,9 +308,8 @@ endif() # Last item in json file # base location of stdlibc++, will be suffixed by androiddeploy qt - set(android_ndk_stdlib_base_path - "${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/" - ) + # Sysroot is set by Android toolchain file and is composed of ANDROID_TOOLCHAIN_ROOT. + set(android_ndk_stdlib_base_path "${CMAKE_SYSROOT}/usr/lib/") string(APPEND file_contents " \"stdcpp-path\": \"${android_ndk_stdlib_base_path}\"\n") @@ -331,6 +340,20 @@ function(qt_android_add_apk_target target) message(FATAL_ERROR "Target ${target} is not a valid android executable target\n") endif() + # Create a top-level "apk" target for convenience, so that users can call 'ninja apk'. + # It will trigger building all the target specific apk build targets that are added via this + # function. + # Allow opt-out. + if(NOT QT_NO_GLOBAL_APK_TARGET) + if(NOT TARGET apk) + add_custom_target(apk + DEPENDS ${target}_prepare_apk_dir + COMMENT "Building all apks" + ) + endif() + set(should_add_to_global_apk TRUE) + endif() + set(deployment_tool "${QT_HOST_PATH}/bin/androiddeployqt") set(apk_dir "$/android-build") add_custom_target(${target}_prepare_apk_dir @@ -348,6 +371,10 @@ function(qt_android_add_apk_target target) --output ${apk_dir} COMMENT "Creating APK for ${target}" ) + + if(should_add_to_global_apk) + add_dependencies(apk "${target}_make_apk") + endif() endfunction()