Fix dependency chain that collects the metatype json files

cmake_automoc_parser has the logic preventing the run of moc with the
--collect-json parameter if metatype json files are not changed.
This logic only verify if the file list is changed but not their
content. This change adds a timestamp file that contains the last
metatype json file timestamp that was modified during the last
cmake_automoc_parser run. The logic still prevents of running
'moc --collect-json' when the list of metatype json files is not
changed, but also checks if their content is no changed.

Another approach it to generate the depfile that can be utilized by
CMake in add_custom_command as DEPFILE argument. But this concept only
works from the second build attempt because of an issue related to
dyndep.

Fixes: QTBUG-98532
Change-Id: I713f8bfa9ae769cefe0beac0b7fa19750b00a765
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
(cherry picked from commit 63b8840380e70c1258f56c67a2ec5edb5bdea53d)
This commit is contained in:
Alexey Edelev 2021-11-24 16:22:18 +01:00
parent ec0e11201e
commit 147877550b
15 changed files with 447 additions and 38 deletions

View File

@ -1068,6 +1068,8 @@ function(qt6_extract_metatypes target)
endif() endif()
endif() endif()
set(cmake_automoc_parser_timestamp "${type_list_file}.timestamp")
if (NOT use_dep_files) if (NOT use_dep_files)
# When a project is configured with a Visual Studio generator, CMake's # When a project is configured with a Visual Studio generator, CMake's
# cmQtAutoGenInitializer::InitAutogenTarget() can take one of two code paths on how to # cmQtAutoGenInitializer::InitAutogenTarget() can take one of two code paths on how to
@ -1097,12 +1099,15 @@ function(qt6_extract_metatypes target)
add_custom_target(${target}_automoc_json_extraction add_custom_target(${target}_automoc_json_extraction
DEPENDS ${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser DEPENDS ${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser
BYPRODUCTS ${type_list_file} BYPRODUCTS
${type_list_file}
"${cmake_automoc_parser_timestamp}"
COMMAND COMMAND
${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser ${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser
--cmake-autogen-cache-file "${cmake_autogen_cache_file}" --cmake-autogen-cache-file "${cmake_autogen_cache_file}"
--cmake-autogen-info-file "${cmake_autogen_info_file}" --cmake-autogen-info-file "${cmake_autogen_info_file}"
--output-file-path "${type_list_file}" --output-file-path "${type_list_file}"
--timestamp-file-path "${cmake_automoc_parser_timestamp}"
${multi_config_args} ${multi_config_args}
COMMENT "Running AUTOMOC file extraction for target ${target}" COMMENT "Running AUTOMOC file extraction for target ${target}"
COMMAND_EXPAND_LISTS COMMAND_EXPAND_LISTS
@ -1116,11 +1121,13 @@ function(qt6_extract_metatypes target)
add_custom_command(OUTPUT ${type_list_file} add_custom_command(OUTPUT ${type_list_file}
DEPENDS ${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser DEPENDS ${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser
${cmake_autogen_timestamp_file} ${cmake_autogen_timestamp_file}
BYPRODUCTS "${cmake_automoc_parser_timestamp}"
COMMAND COMMAND
${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser ${QT_CMAKE_EXPORT_NAMESPACE}::cmake_automoc_parser
--cmake-autogen-cache-file "${cmake_autogen_cache_file}" --cmake-autogen-cache-file "${cmake_autogen_cache_file}"
--cmake-autogen-info-file "${cmake_autogen_info_file}" --cmake-autogen-info-file "${cmake_autogen_info_file}"
--output-file-path "${type_list_file}" --output-file-path "${type_list_file}"
--timestamp-file-path "${cmake_automoc_parser_timestamp}"
${multi_config_args} ${multi_config_args}
COMMENT "Running AUTOMOC file extraction for target ${target}" COMMENT "Running AUTOMOC file extraction for target ${target}"
COMMAND_EXPAND_LISTS COMMAND_EXPAND_LISTS

View File

@ -30,6 +30,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <limits>
#include <qcommandlineoption.h> #include <qcommandlineoption.h>
#include <qcommandlineparser.h> #include <qcommandlineparser.h>
@ -46,6 +47,7 @@
#include <qset.h> #include <qset.h>
#include <qstring.h> #include <qstring.h>
#include <qstack.h> #include <qstack.h>
#include <qdatastream.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -194,26 +196,33 @@ static bool readParseCache(ParseCacheMap &entries, const QString &parseCacheFile
return true; return true;
} }
static bool readJsonFiles(QList<QString> &entries, const QString &filePath) static bool writeJsonFiles(const QList<QString> &fileList, const QString &fileListFilePath,
const QString &timestampFilePath)
{ {
QFile timestampFile(timestampFilePath);
QFile file(filePath); if (!timestampFile.open(QIODevice::ReadWrite)) {
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { fprintf(stderr, "Could not open: %s\n", qPrintable(timestampFilePath));
fprintf(stderr, "Could not open: %s\n", qPrintable(filePath));
return false; return false;
} }
QTextStream textStream(&file); qint64 timestamp = std::numeric_limits<qint64>::min();
QString line; QByteArray timestampBuffer = timestampFile.readAll();
while (textStream.readLineInto(&line)) { if (timestampBuffer.size() == sizeof(timestamp)) {
entries.push_back(line); QDataStream istream(&timestampBuffer, QIODevice::ReadOnly);
} istream >> timestamp;
file.close();
return true;
} }
static bool writeJsonFiles(const QList<QString> &fileList, const QString &fileListFilePath) // Check if any of the metatype json files produced by automoc is newer than the last file
{ // processed by cmake_automoc parser
for (const auto &jsonFile : fileList) {
const qint64 jsonFileLastModified =
QFileInfo(jsonFile).lastModified().toMSecsSinceEpoch();
if (jsonFileLastModified > timestamp) {
timestamp = jsonFileLastModified;
}
}
if (timestamp != std::numeric_limits<qint64>::min() || !QFile::exists(fileListFilePath)) {
QFile file(fileListFilePath); QFile file(fileListFilePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
fprintf(stderr, "Could not open: %s\n", qPrintable(fileListFilePath)); fprintf(stderr, "Could not open: %s\n", qPrintable(fileListFilePath));
@ -221,11 +230,18 @@ static bool writeJsonFiles(const QList<QString> &fileList, const QString &fileLi
} }
QTextStream textStream(&file); QTextStream textStream(&file);
for (const auto &file : fileList) { for (const auto &jsonFile : fileList) {
textStream << file << Qt::endl; textStream << jsonFile << Qt::endl;
} }
textStream.flush();
file.close(); // Update the timestamp according the newest json file timestamp.
timestampBuffer.clear();
QDataStream ostream(&timestampBuffer, QIODevice::WriteOnly);
ostream << timestamp;
timestampFile.resize(0);
timestampFile.write(timestampBuffer);
}
return true; return true;
} }
@ -270,6 +286,13 @@ int main(int argc, char **argv)
QStringLiteral("Set this option when using CMake with a multi-config generator")); QStringLiteral("Set this option when using CMake with a multi-config generator"));
parser.addOption(isMultiConfigOption); parser.addOption(isMultiConfigOption);
QCommandLineOption timestampFilePathOption(QStringLiteral("timestamp-file-path"));
timestampFilePathOption.setDescription(
QStringLiteral("The path to a timestamp file that determines whether the output"
" file needs to be updated."));
timestampFilePathOption.setValueName(QStringLiteral("timestamp file"));
parser.addOption(timestampFilePathOption);
QStringList arguments = QCoreApplication::arguments(); QStringList arguments = QCoreApplication::arguments();
parser.process(arguments); parser.process(arguments);
@ -378,20 +401,10 @@ int main(int argc, char **argv)
jsonFileList.sort(); jsonFileList.sort();
// Read Previous file list (if any) // Read Previous file list (if any)
const QString fileListFilePath = parser.value(outputFileOption); if (!writeJsonFiles(jsonFileList, parser.value(outputFileOption),
QList<QString> previousList; parser.value(timestampFilePathOption))) {
QFile prev_file(fileListFilePath);
// Only try to open file if it exists to avoid error messages
if (prev_file.exists()) {
(void)readJsonFiles(previousList, fileListFilePath);
}
if (previousList != jsonFileList || !QFile(fileListFilePath).exists()) {
if (!writeJsonFiles(jsonFileList, fileListFilePath)) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -280,3 +280,4 @@ _qt_internal_test_expect_pass(tst_qaddpreroutine
_qt_internal_test_expect_pass(test_static_resources _qt_internal_test_expect_pass(test_static_resources
BINARY "${CMAKE_CTEST_COMMAND}" BINARY "${CMAKE_CTEST_COMMAND}"
BINARY_ARGS "-V") BINARY_ARGS "-V")
_qt_internal_test_expect_pass(test_qt_extract_metatypes)

View File

@ -0,0 +1,106 @@
cmake_minimum_required(VERSION 3.16)
project(test_qt_extract_metatypes VERSION 0.1 LANGUAGES CXX)
set(test_project_source_dir "${CMAKE_CURRENT_SOURCE_DIR}/test_qt_extract_metatypes_project")
set(test_project_build_dir "${CMAKE_CURRENT_BINARY_DIR}/build_qt_extract_metatypes_test_project")
# Make sure that file paths are 'real' paths
get_filename_component(test_project_source_dir "${test_project_source_dir}" REALPATH)
get_filename_component(test_project_build_dir "${test_project_build_dir}" REALPATH)
get_cmake_property(is_multi_config GENERATOR_IS_MULTI_CONFIG)
if (CMAKE_BUILD_TYPE AND NOT is_multi_config)
string(TOLOWER "qt6metatypetest_${CMAKE_BUILD_TYPE}" metatypes_file_basename)
else()
string(TOLOWER "qt6metatypetest" metatypes_file_basename)
endif()
set(meta_types_file
"${test_project_build_dir}/meta_types/${metatypes_file_basename}_metatypes.json")
file(REMOVE_RECURSE "${test_project_build_dir}")
file(MAKE_DIRECTORY "${test_project_build_dir}")
find_package(Qt6 COMPONENTS Core REQUIRED)
include("${_Qt6CTestMacros}")
macro(try_build)
execute_process(COMMAND
"${CMAKE_COMMAND}"
--build "${test_project_build_dir}"
RESULT_VARIABLE result
)
if(NOT result EQUAL 0)
message(FATAL_ERROR "Unable to build test project")
endif()
endmacro()
macro(copy_test_header header)
file(COPY "${test_project_source_dir}/testdata/${header}"
DESTINATION "${test_project_build_dir}")
file(RENAME "${test_project_build_dir}/${header}" "${test_project_build_dir}/MetaType.h")
file(TOUCH "${test_project_build_dir}/MetaType.h")
endmacro()
macro(check_generated_metatypes_file reference expect)
set(reference_meta_types_file "${test_project_source_dir}/testdata/${reference}")
execute_process(COMMAND
"${CMAKE_COMMAND}"
-E compare_files "${meta_types_file}" "${reference_meta_types_file}"
RESULT_VARIABLE compare_result
)
if(NOT compare_result EQUAL ${expect})
message(FATAL_ERROR "${meta_types_file} and ${reference_meta_types_file} content differs")
endif()
unset(reference_meta_types_file)
endmacro()
copy_test_header(MetaTypeQ_OBJECT.h)
_qt_internal_get_cmake_test_configure_options(option_list)
execute_process(COMMAND
"${CMAKE_COMMAND}"
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
"-G${CMAKE_GENERATOR}"
${option_list}
-B "${test_project_build_dir}"
-S "${test_project_source_dir}"
RESULT_VARIABLE result
)
if(NOT result EQUAL 0)
message(FATAL_ERROR "Unable to configure test project")
endif()
try_build()
check_generated_metatypes_file(qt6metatypetest_metatypesQ_OBJECT.json 0)
copy_test_header(MetaTypeQ_OBJECTandQ_PROPERTY.h)
try_build()
check_generated_metatypes_file(qt6metatypetest_metatypesQ_OBJECTandQ_PROPERTY.json 0)
copy_test_header(MetaTypeEmpty.h)
try_build()
check_generated_metatypes_file(qt6metatypetest_metatypesEmpty.json 0)
file(TIMESTAMP "${meta_types_file}" metatypes_timestamp)
copy_test_header(MetaTypeEmptyWithComment.h)
try_build()
check_generated_metatypes_file(qt6metatypetest_metatypesEmpty.json 0)
file(TIMESTAMP "${meta_types_file}" new_metatypes_timestamp)
# Depending on the way how qt_extract_metatypes executes automoc it might or might not
# change the resulting .json file content.
set(extract_metatypes_uses_dep_files FALSE)
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.17") # Requires automoc changes present only in 3.17
if(CMAKE_GENERATOR STREQUAL "Ninja" OR CMAKE_GENERATOR STREQUAL "Ninja Multi-Config")
set(extract_metatypes_uses_dep_files TRUE)
endif()
endif()
if(extract_metatypes_uses_dep_files)
if(NOT metatypes_timestamp STREQUAL new_metatypes_timestamp)
message(FATAL_ERROR "${meta_types_file} timestamp is changed but should not")
endif()
endif()

View File

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.16)
project(qt_extract_metatypes_test_project VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 COMPONENTS Core REQUIRED)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
qt_add_executable(MetaTypeTest
MetaType.cpp
"${CMAKE_CURRENT_BINARY_DIR}/MetaType.h"
main.cpp
)
target_include_directories(MetaTypeTest PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
qt_extract_metatypes(MetaTypeTest)
target_link_libraries(MetaTypeTest PRIVATE Qt6::Core)

View File

@ -0,0 +1,29 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "MetaType.h"

View File

@ -0,0 +1,32 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
int main(int argc, char *argv[])
{
return 0;
}

View File

@ -0,0 +1 @@
*.json text eol=lf

View File

@ -0,0 +1,35 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#include <QObject>
class MetaType : public QObject
{
};

View File

@ -0,0 +1,36 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#include <QObject>
class MetaType : public QObject
{
// Changes of no value to automoc
};

View File

@ -0,0 +1,35 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#include <QObject>
class MetaType : public QObject
{
Q_OBJECT
};

View File

@ -0,0 +1,38 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#include <QObject>
class MetaType : public QObject
{
Q_OBJECT
Q_PROPERTY(int test READ test)
int test() { return 0; }
};

View File

@ -0,0 +1,19 @@
[
{
"classes": [
{
"className": "MetaType",
"object": true,
"qualifiedClassName": "MetaType",
"superClasses": [
{
"access": "public",
"name": "QObject"
}
]
}
],
"inputFile": "MetaType.h",
"outputRevision": 68
}
]

View File

@ -0,0 +1,34 @@
[
{
"classes": [
{
"className": "MetaType",
"object": true,
"properties": [
{
"constant": false,
"designable": true,
"final": false,
"index": 0,
"name": "test",
"read": "test",
"required": false,
"scriptable": true,
"stored": true,
"type": "int",
"user": false
}
],
"qualifiedClassName": "MetaType",
"superClasses": [
{
"access": "public",
"name": "QObject"
}
]
}
],
"inputFile": "MetaType.h",
"outputRevision": 68
}
]