1 # This test checks whether adding a source file to the project triggers an AUTOMOC re-run.
3 cmake_minimum_required(VERSION 3.10)
4 project(RerunMocOnAddFile)
5 include("../AutogenCoreTest.cmake")
7 # Create an executable to generate a clean target
8 set(main_source "${CMAKE_CURRENT_BINARY_DIR}/generated_main.cpp")
9 file(WRITE "${main_source}" "int main() {}")
10 add_executable(exe "${main_source}")
13 set(timeformat "%Y.%j.%H.%M%S")
14 set(testProjectTemplateDir "${CMAKE_CURRENT_SOURCE_DIR}/MocOnAddFile")
15 set(testProjectSrc "${CMAKE_CURRENT_BINARY_DIR}/MocOnAddFile")
16 set(testProjectBinDir "${CMAKE_CURRENT_BINARY_DIR}/MocOnAddFile-build")
20 message(STATUS "Sleeping for a few seconds.")
21 execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep 1)
24 macro(acquire_timestamp When)
25 file(TIMESTAMP "${mocBasicBin}" time${When} "${timeformat}")
28 macro(rebuild buildName)
29 message(STATUS "Starting build ${buildName}.")
30 execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${testProjectBinDir}" RESULT_VARIABLE result)
32 message(FATAL_ERROR "Build ${buildName} failed.")
34 message(STATUS "Build ${buildName} finished.")
39 if (timeAfter VERSION_GREATER timeBefore)
40 message(STATUS "As expected the file ${mocBasicBin} changed.")
42 message(SEND_ERROR "Unexpectedly the file ${mocBasicBin} did not change!\nTimestamp pre: ${timeBefore}\nTimestamp aft: ${timeAfter}\n")
46 macro(require_change_not)
47 if (timeAfter VERSION_GREATER timeBefore)
48 message(SEND_ERROR "Unexpectedly the file ${mocBasicBin} changed!\nTimestamp pre: ${timeBefore}\nTimestamp aft: ${timeAfter}\n")
50 message(STATUS "As expected the file ${mocBasicBin} did not change.")
54 # Create the test project from the template
55 unset(additional_project_sources)
56 unset(main_cpp_includes)
57 configure_file("${testProjectTemplateDir}/CMakeLists.txt.in" "${testProjectSrc}/CMakeLists.txt")
58 configure_file("${testProjectTemplateDir}/main.cpp.in" "${testProjectSrc}/main.cpp")
62 "${testProjectBinDir}"
65 CMAKE_FLAGS "-DQT_TEST_VERSION=${QT_TEST_VERSION}"
66 "-DCMAKE_AUTOGEN_VERBOSE=${CMAKE_AUTOGEN_VERBOSE}"
67 "-DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}"
68 OUTPUT_VARIABLE output
71 message(FATAL_ERROR "Initial build of mocOnAddFile failed. Output: ${output}")
74 # Sleep to ensure new timestamps
77 # Add a QObject class (defined in header) to the project and build
78 set(additional_project_sources myobject.cpp)
79 set(main_cpp_includes "#include \"myobject.h\"")
80 configure_file("${testProjectTemplateDir}/CMakeLists.txt.in" "${testProjectSrc}/CMakeLists.txt"
82 configure_file("${testProjectTemplateDir}/main.cpp.in" "${testProjectSrc}/main.cpp" @ONLY)
83 configure_file("${testProjectTemplateDir}/myobject.h" "${testProjectSrc}/myobject.h" COPYONLY)
84 configure_file("${testProjectTemplateDir}/myobject.cpp" "${testProjectSrc}/myobject.cpp" COPYONLY)
87 # Sleep to ensure new timestamps
90 # Add a QObject class (defined in source) to the project and build
91 set(additional_project_sources myobject.cpp anotherobject.cpp)
92 configure_file("${testProjectTemplateDir}/CMakeLists.txt.in" "${testProjectSrc}/CMakeLists.txt"
94 configure_file("${testProjectTemplateDir}/anotherobject.cpp" "${testProjectSrc}/anotherobject.cpp"