From 785df612319d3023ad994f3153d42a067a1adaa9 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Fri, 2 Jul 2021 17:43:58 +0200 Subject: [PATCH] CMake: Fix qt6_extract_metatypes to work with VS Generators CMake has an optimization where it skips creation of a ${target}_autogen target and instead uses PRE_BUILD events for AUTOMOC. That breaks qt6_extract_metatypes which expects the autogen target to always exist. Disable the optimization by generating a dummy header file and adding it as a source to the target. Pick-to: 6.2 Fixes: QTBUG-94944 Change-Id: Ic3d923cc6d3a149fea8c5c7b111576b64d19f608 Reviewed-by: Joerg Bornemann Reviewed-by: Qt CI Bot --- src/corelib/Qt6CoreMacros.cmake | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake index 539c6bd159f..1dca7f887cf 100644 --- a/src/corelib/Qt6CoreMacros.cmake +++ b/src/corelib/Qt6CoreMacros.cmake @@ -1013,6 +1013,32 @@ function(qt6_extract_metatypes target) endif() if (NOT use_dep_files) + # When a project is configured with a Visual Studio generator, CMake's + # cmQtAutoGenInitializer::InitAutogenTarget() can take one of two code paths on how to + # handle AUTOMOC rules. + # It either creates a ${target}_autogen custom target or uses PRE_BUILD build events. + # + # The latter in considered an optimization and is used by CMake when possible. + # Unfortunately that breaks our add_dependency call because we expect on _autogen target + # to always exist. + # + # Ensure the PRE_BUILD path is not taken by generating a dummy header file and adding it + # as a source file to the target. This causes the file to be added to + # cmQtAutoGenInitializer::AutogenTarget.DependFiles, which disables the PRE_BUILD path. + if(CMAKE_GENERATOR MATCHES "Visual Studio") + # The file name should be target specific, but still short, so we don't hit path + # length issues. + string(MAKE_C_IDENTIFIER "ddf_${target}" dummy_dependency_file) + set(dummy_out_file "${CMAKE_CURRENT_BINARY_DIR}/${dummy_dependency_file}.h") + + # The content shouldn't be empty so we don't trigger AUTOMOC warnings about it. + file(GENERATE OUTPUT "${dummy_out_file}" CONTENT "//") + set_source_files_properties("${dummy_out_file}" PROPERTIES + GENERATED TRUE + SKIP_AUTOGEN OFF) + target_sources("${target}" PRIVATE "${dummy_out_file}") + endif() + add_custom_target(${target}_automoc_json_extraction DEPENDS ${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser BYPRODUCTS ${type_list_file}