Autogen: Move QtAutoMocDeps tests to RunCMake/Autogen

This commit is contained in:
Orkun Tokdemir
2023-09-07 14:52:46 +02:00
parent ebc9e448b3
commit 09b650d000
15 changed files with 113 additions and 52 deletions

View File

@@ -0,0 +1,7 @@
#include "MyWindow.h"
MyWindow::MyWindow(QWidget* parent)
: QWidget(parent)
{
this->m_ui.setupUi(this);
}

View File

@@ -0,0 +1,16 @@
#pragma once
#include <QWidget>
#include "ui_MyWindow.h"
class MyWindow : public QWidget
{
Q_OBJECT
public:
explicit MyWindow(QWidget* parent = nullptr);
private:
Ui::MyWindow m_ui;
};

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MyWindow</class>
<widget class="QWidget" name="MyWindow"/>
</ui>

View File

@@ -1,27 +1,23 @@
enable_language(CXX)
set(QtX Qt${with_qt_version})
find_package(${QtX} REQUIRED COMPONENTS Core)
find_package(Qt${with_qt_version} REQUIRED COMPONENTS Core Widgets Gui)
set(CMAKE_AUTOMOC ON)
add_library(simple_lib SHARED simple_lib.cpp)
add_executable(app_with_qt app.cpp app_qt.cpp)
target_link_libraries(app_with_qt PRIVATE simple_lib ${QtX}::Core)
target_link_libraries(app_with_qt PRIVATE simple_lib Qt${with_qt_version}::Core)
if(${QtX}Widgets_DIR)
find_package(${QtX} REQUIRED COMPONENTS Widgets)
if(${with_qt_version}Widgets_DIR)
if(with_qt_version STREQUAL 5)
qt5_wrap_ui(_headers MyWindow.ui)
else()
qt_wrap_ui(_headers MyWindow.ui)
endif()
add_executable(app_with_widget app.cpp MyWindow.cpp ${_headers})
target_link_libraries(app_with_widget PRIVATE ${QtX}::Widgets)
target_link_libraries(app_with_widget PRIVATE Qt${with_qt_version}::Widgets)
target_include_directories(app_with_widget PRIVATE "${CMAKE_BINARY_DIR}")
endif()
add_subdirectory(QtSubDir1)
add_subdirectory(QtSubDir2)
add_subdirectory(QtSubDir3)

View File

@@ -1,4 +1,4 @@
cmake_policy(SET CMP0116 OLD)
add_executable(sub_exe_1 ../app.cpp)
target_link_libraries(sub_exe_1 PRIVATE ${QtX}::Core)
target_link_libraries(sub_exe_1 PRIVATE Qt${with_qt_version}::Core)

View File

@@ -1,4 +1,4 @@
cmake_policy(SET CMP0116 NEW)
add_executable(sub_exe_2 ../app.cpp)
target_link_libraries(sub_exe_2 PRIVATE ${QtX}::Core)
target_link_libraries(sub_exe_2 PRIVATE Qt${with_qt_version}::Core)

View File

@@ -0,0 +1,2 @@
add_executable(sub_exe_3 ../app.cpp)
target_link_libraries(sub_exe_3 PRIVATE Qt${with_qt_version}::Core)

View File

@@ -122,4 +122,55 @@ if (DEFINED with_qt_version)
endblock()
endif()
endif()
if(RunCMake_GENERATOR MATCHES "Make|Ninja")
block()
if(QtCore_VERSION VERSION_GREATER_EQUAL 5.15.0)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/QtAutoMocDeps-build)
run_cmake(QtAutoMocDeps)
set(RunCMake_TEST_NO_CLEAN 1)
# Build the project.
run_cmake_command(QtAutoMocDeps-build ${CMAKE_COMMAND} --build . --verbose)
# Touch just the library source file, which shouldn't cause a rerun of AUTOMOC
# for app_with_qt target.
file(TOUCH "${RunCMake_SOURCE_DIR}/simple_lib.cpp")
set(RunCMake_TEST_NOT_EXPECT_stdout "Automatic MOC for target app_with_qt|\
Automatic MOC for target sub_exe_1|\
Automatic MOC for target sub_exe_2")
set(RunCMake_TEST_VARIANT_DESCRIPTION "-Don't execute AUTOMOC for 'app_with_qt', 'sub_exe_1' and 'sub_exe_2'")
# Build and assert that AUTOMOC was not run for app_with_qt, sub_exe_1 and sub_exe_2.
run_cmake_command(QtAutoMocDeps-build ${CMAKE_COMMAND} --build . --verbose)
unset(RunCMake_TEST_VARIANT_DESCRIPTION)
unset(RunCMake_TEST_NOT_EXPECT_stdout)
macro(check_file_exists file)
if (EXISTS "${file}")
set(check_result "PASSED")
set(message_type "STATUS")
else()
set(check_result "FAILED")
set(message_type "FATAL_ERROR")
endif()
message(${message_type} "QtAutoMocDeps-build-\"${file}\" was generated - ${check_result}")
endmacro()
check_file_exists("${RunCMake_TEST_BINARY_DIR}/app_with_qt_autogen/deps")
check_file_exists("${RunCMake_TEST_BINARY_DIR}/QtSubDir1/sub_exe_1_autogen/deps")
check_file_exists("${RunCMake_TEST_BINARY_DIR}/QtSubDir2/sub_exe_2_autogen/deps")
check_file_exists("${RunCMake_TEST_BINARY_DIR}/app_with_qt_autogen/timestamp")
check_file_exists("${RunCMake_TEST_BINARY_DIR}/QtSubDir1/sub_exe_1_autogen/timestamp")
check_file_exists("${RunCMake_TEST_BINARY_DIR}/QtSubDir2/sub_exe_2_autogen/timestamp")
# Touch a header file to make sure an automoc dependency cycle is not introduced.
file(TOUCH "${RunCMake_SOURCE_DIR}/MyWindow.h")
set(RunCMake_TEST_VARIANT_DESCRIPTION "-First build after touch to detect dependency cycle")
run_cmake_command(QtAutoMocDeps-build ${CMAKE_COMMAND} --build . --verbose)
# Need to run a second time to hit the dependency cycle.
set(RunCMake_TEST_VARIANT_DESCRIPTION "-Don't hit dependency cycle")
run_cmake_command(QtAutoMocDeps-build ${CMAKE_COMMAND} --build . --verbose)
endif()
endblock()
endif()
endif ()

View File

@@ -0,0 +1,6 @@
int main(int argc, char* argv[])
{
(void)argc;
(void)argv;
return 0;
}

View File

@@ -0,0 +1,11 @@
#include <QObject>
class Mango : public QObject
{
Q_OBJECT
public:
Q_SIGNALS:
void eatFruit();
};
#include "app_qt.moc"

View File

@@ -0,0 +1,6 @@
#ifdef _WIN32
__declspec(dllexport)
#endif
void dummy_symbol()
{
}

View File

@@ -274,6 +274,7 @@ if(CMake_TEST_Qt6 AND Qt6Widgets_FOUND)
cmake_path(GET base_dir PARENT_PATH base_dir) # <base>
add_RunCMake_test(AutogenQt6 TEST_DIR Autogen
-Dwith_qt_version=6
-DQtCore_VERSION=${Qt6Core_VERSION}
"-DQt6_DIR:PATH=${Qt6_DIR}"
"-DCMAKE_PREFIX_PATH:STRING=${base_dir}"
-DPSEUDO_TIDY=$<TARGET_FILE:pseudo_tidy>
@@ -286,6 +287,7 @@ endif ()
if(CMake_TEST_Qt5 AND Qt5Widgets_FOUND)
add_RunCMake_test(AutogenQt5 TEST_DIR Autogen
-Dwith_qt_version=5
-DQtCore_VERSION=${Qt5Core_VERSION}
"-DQt5_DIR:PATH=${Qt5_DIR}"
)
set(want_NoQt_test FALSE)

View File

@@ -1,2 +0,0 @@
add_executable(sub_exe_3 ../app.cpp)
target_link_libraries(sub_exe_3 PRIVATE ${QtX}::Core)

View File

@@ -380,45 +380,6 @@ function (run_ChangeBuildType)
endfunction()
run_ChangeBuildType()
function(run_QtAutoMocDeps)
set(QtX Qt${CMake_TEST_Qt_version})
if(CMake_TEST_${QtX}Core_Version VERSION_GREATER_EQUAL 5.15.0)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/QtAutoMocDeps-build)
run_cmake_with_options(QtAutoMocDeps
"-Dwith_qt_version=${CMake_TEST_Qt_version}"
"-D${QtX}_DIR=${${QtX}_DIR}"
"-D${QtX}Core_DIR=${${QtX}Core_DIR}"
"-D${QtX}Widgets_DIR=${${QtX}Widgets_DIR}"
"-DCMAKE_PREFIX_PATH:STRING=${CMAKE_PREFIX_PATH}"
)
# Build the project.
run_ninja("${RunCMake_TEST_BINARY_DIR}")
# Touch just the library source file, which shouldn't cause a rerun of AUTOMOC
# for app_with_qt target.
file(TOUCH "${RunCMake_SOURCE_DIR}/simple_lib.cpp")
# Build and assert that AUTOMOC was not run for app_with_qt.
run_ninja("${RunCMake_TEST_BINARY_DIR}")
if(ninja_stdout MATCHES "Automatic MOC for target app_with_qt")
message(FATAL_ERROR
"AUTOMOC should not have executed for 'app_with_qt' target:\nstdout:\n${ninja_stdout}")
endif()
# Assert that the subdir executables were not rebuilt.
if(ninja_stdout MATCHES "Automatic MOC for target sub_exe_1")
message(FATAL_ERROR
"AUTOMOC should not have executed for 'sub_exe_1' target:\nstdout:\n${ninja_stdout}")
endif()
if(ninja_stdout MATCHES "Automatic MOC for target sub_exe_2")
message(FATAL_ERROR
"AUTOMOC should not have executed for 'sub_exe_2' target:\nstdout:\n${ninja_stdout}")
endif()
# Touch a header file to make sure an automoc dependency cycle is not introduced.
file(TOUCH "${RunCMake_SOURCE_DIR}/MyWindow.h")
run_ninja("${RunCMake_TEST_BINARY_DIR}")
# Need to run a second time to hit the dependency cycle.
run_ninja("${RunCMake_TEST_BINARY_DIR}")
endif()
endfunction()
function(run_QtAutoMocSkipPch)
set(QtX Qt${CMake_TEST_Qt_version})
if(CMake_TEST_${QtX}Core_Version VERSION_GREATER_EQUAL 5.15.0)
@@ -433,7 +394,7 @@ function(run_QtAutoMocSkipPch)
run_ninja("${RunCMake_TEST_BINARY_DIR}")
endif()
endfunction()
if(CMake_TEST_Qt_version)
run_QtAutoMocDeps()
run_QtAutoMocSkipPch()
endif()