From 633ab16bc8631df84276323ea299d80a8c45d3f9 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Fri, 25 Nov 2022 16:48:26 +0200 Subject: [PATCH] CMake:Android: copy templates, bindings and gradle files pre_build At the moment when doing a non-prefix build and changing some of those files, CMake will not update the changes to the build folder unless done manually or a re-configure is done manually. qt_copy_or_install() only does copy those files at configure time once, and using CMAKE_CONFIGURE_DEPENDS would be an overkill here and even a bit of an extra annoyance, so in this case having a custom command seem to be suitable. Done-with: Alexey Edelev Change-Id: I55aa9e9d3eea32a4bb54c64abd4cbdcb891c44b6 Reviewed-by: Alexandru Croitor Reviewed-by: Alexey Edelev --- cmake/QtInstallHelpers.cmake | 64 ++++++++++++++++++++++++++++ src/3rdparty/gradle/CMakeLists.txt | 8 ++++ src/android/java/CMakeLists.txt | 7 +++ src/android/templates/CMakeLists.txt | 9 ++++ 4 files changed, 88 insertions(+) diff --git a/cmake/QtInstallHelpers.cmake b/cmake/QtInstallHelpers.cmake index 41888d30908..deab48cda5c 100644 --- a/cmake/QtInstallHelpers.cmake +++ b/cmake/QtInstallHelpers.cmake @@ -183,3 +183,67 @@ function(_qt_internal_create_versioned_link_or_copy install_dir base_name suffix list(JOIN code "\n" code) install(CODE "${code}") endfunction() + +# Use case is copying files or directories in a non-prefix build with each build, so that changes +# are available each time, this is useful for some Android templates that are needed for building, +# apks and need to sync changes each time a build is started +function(qt_internal_copy_at_build_time) + set(flags) + set(options TARGET DESTINATION) + set(multiopts FILES DIRECTORIES) + cmake_parse_arguments(arg "${flags}" "${options}" "${multiopts}" ${ARGN}) + + file(MAKE_DIRECTORY "${arg_DESTINATION}") + + unset(outputs) + foreach(dir_to_copy IN LISTS arg_DIRECTORIES) + get_filename_component(file_name "${dir_to_copy}" NAME) + set(destination_file_name "${arg_DESTINATION}/${file_name}") + + file(GLOB_RECURSE all_files_in_dir RELATIVE "${dir_to_copy}" "${dir_to_copy}/*") + set(dir_outputs ${all_files_in_dir}) + set(dir_deps ${all_files_in_dir}) + + list(TRANSFORM dir_outputs PREPEND "${destination_file_name}/") + list(TRANSFORM dir_deps PREPEND "${dir_to_copy}/") + + add_custom_command(OUTPUT ${dir_outputs} + COMMAND ${CMAKE_COMMAND} -E copy_directory ${dir_to_copy} "${destination_file_name}" + DEPENDS ${dir_deps} + COMMENT "Copying directory ${dir_to_copy} to ${arg_DESTINATION}." + ) + list(APPEND outputs ${dir_outputs}) + endforeach() + + unset(file_outputs) + unset(files_to_copy) + foreach(path_to_copy IN LISTS arg_FILES) + get_filename_component(file_name "${path_to_copy}" NAME) + set(destination_file_name "${arg_DESTINATION}/${file_name}") + + list(APPEND file_outputs "${destination_file_name}") + list(APPEND files_to_copy "${path_to_copy}") + endforeach() + + if(files_to_copy) + add_custom_command(OUTPUT ${file_outputs} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${files_to_copy} ${arg_DESTINATION} + DEPENDS ${files_to_copy} + COMMENT "Copying files ${files_to_copy} to ${arg_DESTINATION}." + ) + list(APPEND outputs ${file_outputs}) + endif() + + get_property(count GLOBAL PROPERTY _qt_internal_copy_at_build_time_count) + if(NOT count) + set(count 0) + endif() + + add_custom_target(qt_internal_copy_at_build_time_${count} DEPENDS ${outputs}) + if(arg_TARGET) + add_dependencies(${arg_TARGET} qt_internal_copy_at_build_time_${count}) + endif() + + math(EXPR count "${count} + 1") + set_property(GLOBAL PROPERTY _qt_internal_copy_at_build_time_count ${count}) +endfunction() diff --git a/src/3rdparty/gradle/CMakeLists.txt b/src/3rdparty/gradle/CMakeLists.txt index e9060a669f7..3013eca1f97 100644 --- a/src/3rdparty/gradle/CMakeLists.txt +++ b/src/3rdparty/gradle/CMakeLists.txt @@ -42,3 +42,11 @@ qt_copy_or_install( DESTINATION "${destination}" ) + +if(NOT QT_WILL_INSTALL) + qt_internal_copy_at_build_time(TARGET Qt${QtBase_VERSION_MAJOR}GradleScripts + FILES ${gradle_programs} ${gradle_files} + DIRECTORIES ${gradle_wrapper} + DESTINATION ${destination} + ) +endif() diff --git a/src/android/java/CMakeLists.txt b/src/android/java/CMakeLists.txt index 71446e596c5..8f0c0da542c 100644 --- a/src/android/java/CMakeLists.txt +++ b/src/android/java/CMakeLists.txt @@ -53,4 +53,11 @@ qt_path_join(destination ${QT_INSTALL_DIR} "src/android/java") qt_copy_or_install(DIRECTORY ${resource_directories} DESTINATION "${destination}" ) + +if(NOT QT_WILL_INSTALL) + qt_internal_copy_at_build_time(TARGET Qt${QtBase_VERSION_MAJOR}AndroidBindings + DIRECTORIES ${resource_directories} + DESTINATION ${destination}) +endif() + # special case end diff --git a/src/android/templates/CMakeLists.txt b/src/android/templates/CMakeLists.txt index 73dac9aa24d..3eba4611e80 100644 --- a/src/android/templates/CMakeLists.txt +++ b/src/android/templates/CMakeLists.txt @@ -25,4 +25,13 @@ qt_copy_or_install(FILES ${template_files} qt_copy_or_install(DIRECTORY ${template_directories} DESTINATION "${destination}") + +if(NOT QT_WILL_INSTALL) + qt_internal_copy_at_build_time(TARGET Qt${QtBase_VERSION_MAJOR}AndroidTemplates + FILES ${template_files} + DIRECTORIES ${template_directories} + DESTINATION ${destination} + ) +endif() + # special case end