CMake: Fix qt6_extract_metatypes json file generation rule

Change the copying of
  ${CMAKE_CURRENT_BINARY_DIR}/${target}_metatypes.json
to
  ${prefix}/lib/metatypes/${target}_metatypes.json
to happen as part of the command generating the file, rather
than copying the file as part of a separate custom target or
POST_BUILD event.

This ensures that the custom commands in qt6_qml_type_registration
that use those files as dependencies will cause them to be
generated before the qml type registration happens, thus eliminating
errors like

Error 5 while parsing
 qtbase/lib/metatypes/qt6quick_metatypes.json: illegal value

Fixes: QTBUG-94942
Change-Id: Idddd73786d1a622984965c60ac9b4c3bc2c13ab5
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
(cherry picked from commit bcca14a1933de9f188950a9a2d3a450a0772d479)
Reviewed-by: Craig Scott <craig.scott@qt.io>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Alexandru Croitor 2021-07-05 17:37:15 +02:00
parent 8c1210c745
commit a83cf2c10e

View File

@ -1013,12 +1013,27 @@ function(qt6_extract_metatypes target)
set(arg_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${arg_INSTALL_DIR}")
endif()
if (should_install AND arg_COPY_OVER_INSTALL
AND NOT EXISTS ${arg_INSTALL_DIR}/${metatypes_file_name})
set(metatypes_file_non_prefix)
if(should_install AND arg_COPY_OVER_INSTALL)
set(metatypes_file_non_prefix "${arg_INSTALL_DIR}/${metatypes_file_name}")
set(metatypes_non_prefix_copy
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${metatypes_file}"
"${metatypes_file_non_prefix}"
)
# The reason for this configure-time file creation is described in a comment slightly above.
if(NOT EXISTS "${metatypes_file_non_prefix}")
file(MAKE_DIRECTORY "${arg_INSTALL_DIR}")
file(TOUCH "${arg_INSTALL_DIR}/${metatypes_file_name}")
file(TOUCH "${metatypes_file_non_prefix}")
endif()
add_custom_command(OUTPUT ${metatypes_file_gen} ${metatypes_file}
endif()
add_custom_command(
OUTPUT
${metatypes_file_gen}
${metatypes_file}
${metatypes_file_non_prefix}
DEPENDS ${QT_CMAKE_EXPORT_NAMESPACE}::moc ${automoc_dependencies} ${manual_dependencies}
COMMAND ${QT_CMAKE_EXPORT_NAMESPACE}::moc
-o ${metatypes_file_gen}
@ -1026,6 +1041,8 @@ function(qt6_extract_metatypes target)
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${metatypes_file_gen}
${metatypes_file}
# One more command
${metatypes_non_prefix_copy}
COMMENT "Running automoc with --collect-json"
)
@ -1062,41 +1079,8 @@ function(qt6_extract_metatypes target)
)
target_sources(${target} INTERFACE ${metatypes_file_genex_build} ${metatypes_file_genex_install})
# Installation is complicated, because there are multiple combinations.
# In non-prefix builds (signaled by arg_COPY_OVER_INSTALL == TRUE), Qt modules are /copied/
# into the qt_prefix/lib/metatypes.
# In prefix builds (signaled by arg_COPY_OVER_INSTALL == FALSE), Qt modules are /installed/
# into the qt_prefix/lib/metatypes.
# Currently only the internal qt_add_module sets arg_COPY_OVER_INSTALL.
#
# Tests and examples are executables, and thus will not have their meta types installed, but
# they will have them generated (if this function is called).
#
# Regular libraries and plugins (which are not part of the Qt build), will be /installed/
# into a lib/metatypes directory relative to their prefix, rather than the Qt prefix (only
# outside of a Qt build).
# We don't support non-prefix builds for libraries or plugins which are not part of the official
# Qt build. Aka everything non-prefix / COPY_OVER_INSTALL related are implementation details
# that users shouldn't use.
if(should_install)
if (arg_COPY_OVER_INSTALL)
set(command_args
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${metatypes_file}"
"${arg_INSTALL_DIR}/${metatypes_file_name}"
)
if (target_type STREQUAL "OBJECT_LIBRARY")
add_custom_target(${target}_metatypes_copy
DEPENDS "${metatypes_file}"
${command_args}
)
add_dependencies(${target} ${target}_metatypes_copy)
else()
add_custom_command(TARGET ${target} POST_BUILD
${command_args}
)
endif()
else()
if(NOT arg_COPY_OVER_INSTALL)
install(FILES "${metatypes_file}" DESTINATION "${arg_INSTALL_DIR}")
endif()
endif()