Imported Upstream version 3.17.5
[platform/upstream/cmake.git] / Tests / QtAutogen / RerunMocOnAddFile / CMakeLists.txt
1 # This test checks whether adding a source file to the project triggers an AUTOMOC re-run.
2
3 cmake_minimum_required(VERSION 3.10)
4 project(RerunMocOnAddFile)
5 include("../AutogenCoreTest.cmake")
6
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}")
11
12 # Utility variables
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")
17
18 # Utility macros
19 macro(sleep)
20   message(STATUS "Sleeping for a few seconds.")
21   execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep 1)
22 endmacro()
23
24 macro(acquire_timestamp When)
25   file(TIMESTAMP "${mocBasicBin}" time${When} "${timeformat}")
26 endmacro()
27
28 macro(rebuild buildName)
29   message(STATUS "Starting build ${buildName}.")
30   execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${testProjectBinDir}" RESULT_VARIABLE result)
31   if (result)
32     message(FATAL_ERROR "Build ${buildName} failed.")
33   else()
34     message(STATUS "Build ${buildName} finished.")
35   endif()
36 endmacro()
37
38 macro(require_change)
39   if (timeAfter VERSION_GREATER timeBefore)
40     message(STATUS "As expected the file ${mocBasicBin} changed.")
41   else()
42     message(SEND_ERROR "Unexpectedly the file ${mocBasicBin} did not change!\nTimestamp pre: ${timeBefore}\nTimestamp aft: ${timeAfter}\n")
43   endif()
44 endmacro()
45
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")
49   else()
50     message(STATUS "As expected the file ${mocBasicBin} did not change.")
51   endif()
52 endmacro()
53
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")
59
60 # Initial build
61 try_compile(MOC_RERUN
62   "${testProjectBinDir}"
63   "${testProjectSrc}"
64   MocOnAddFile
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
69 )
70 if (NOT MOC_RERUN)
71   message(FATAL_ERROR "Initial build of mocOnAddFile failed. Output: ${output}")
72 endif()
73
74 # Sleep to ensure new timestamps
75 sleep()
76
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"
81     @ONLY)
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)
85 rebuild(2)
86
87 # Sleep to ensure new timestamps
88 sleep()
89
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"
93     @ONLY)
94 configure_file("${testProjectTemplateDir}/anotherobject.cpp" "${testProjectSrc}/anotherobject.cpp"
95     COPYONLY)
96 rebuild(3)