CMakeLists.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # This test checks whether adding a source file to the project triggers an AUTOMOC re-run.
  2. cmake_minimum_required(VERSION 3.10)
  3. project(RerunMocOnAddFile)
  4. include("../AutogenCoreTest.cmake")
  5. # Create an executable to generate a clean target
  6. set(main_source "${CMAKE_CURRENT_BINARY_DIR}/generated_main.cpp")
  7. file(WRITE "${main_source}" "int main() {}")
  8. add_executable(exe "${main_source}")
  9. # Utility variables
  10. set(timeformat "%Y.%j.%H.%M%S")
  11. set(testProjectTemplateDir "${CMAKE_CURRENT_SOURCE_DIR}/MocOnAddFile")
  12. set(testProjectSrc "${CMAKE_CURRENT_BINARY_DIR}/MocOnAddFile")
  13. set(testProjectBinDir "${CMAKE_CURRENT_BINARY_DIR}/MocOnAddFile-build")
  14. # Utility macros
  15. macro(sleep)
  16. message(STATUS "Sleeping for a few seconds.")
  17. execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep 1)
  18. endmacro()
  19. macro(acquire_timestamp When)
  20. file(TIMESTAMP "${mocBasicBin}" time${When} "${timeformat}")
  21. endmacro()
  22. macro(rebuild buildName)
  23. message(STATUS "Starting build ${buildName}.")
  24. execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${testProjectBinDir}" RESULT_VARIABLE result)
  25. if (result)
  26. message(FATAL_ERROR "Build ${buildName} failed.")
  27. else()
  28. message(STATUS "Build ${buildName} finished.")
  29. endif()
  30. endmacro()
  31. macro(require_change)
  32. if (timeAfter VERSION_GREATER timeBefore)
  33. message(STATUS "As expected the file ${mocBasicBin} changed.")
  34. else()
  35. message(SEND_ERROR "Unexpectedly the file ${mocBasicBin} did not change!\nTimestamp pre: ${timeBefore}\nTimestamp aft: ${timeAfter}\n")
  36. endif()
  37. endmacro()
  38. macro(require_change_not)
  39. if (timeAfter VERSION_GREATER timeBefore)
  40. message(SEND_ERROR "Unexpectedly the file ${mocBasicBin} changed!\nTimestamp pre: ${timeBefore}\nTimestamp aft: ${timeAfter}\n")
  41. else()
  42. message(STATUS "As expected the file ${mocBasicBin} did not change.")
  43. endif()
  44. endmacro()
  45. # Create the test project from the template
  46. unset(additional_project_sources)
  47. unset(main_cpp_includes)
  48. configure_file("${testProjectTemplateDir}/CMakeLists.txt.in" "${testProjectSrc}/CMakeLists.txt")
  49. configure_file("${testProjectTemplateDir}/main.cpp.in" "${testProjectSrc}/main.cpp")
  50. # Initial build
  51. try_compile(MOC_RERUN
  52. "${testProjectBinDir}"
  53. "${testProjectSrc}"
  54. MocOnAddFile
  55. CMAKE_FLAGS "-DQT_TEST_VERSION=${QT_TEST_VERSION}"
  56. "-DCMAKE_AUTOGEN_VERBOSE=${CMAKE_AUTOGEN_VERBOSE}"
  57. "-DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}"
  58. OUTPUT_VARIABLE output
  59. )
  60. if (NOT MOC_RERUN)
  61. message(FATAL_ERROR "Initial build of mocOnAddFile failed. Output: ${output}")
  62. endif()
  63. # Sleep to ensure new timestamps
  64. sleep()
  65. # Add a QObject class (defined in header) to the project and build
  66. set(additional_project_sources myobject.cpp)
  67. set(main_cpp_includes "#include \"myobject.h\"")
  68. configure_file("${testProjectTemplateDir}/CMakeLists.txt.in" "${testProjectSrc}/CMakeLists.txt"
  69. @ONLY)
  70. configure_file("${testProjectTemplateDir}/main.cpp.in" "${testProjectSrc}/main.cpp" @ONLY)
  71. configure_file("${testProjectTemplateDir}/myobject.h" "${testProjectSrc}/myobject.h" COPYONLY)
  72. configure_file("${testProjectTemplateDir}/myobject.cpp" "${testProjectSrc}/myobject.cpp" COPYONLY)
  73. rebuild(2)
  74. # Sleep to ensure new timestamps
  75. sleep()
  76. # Add a QObject class (defined in source) to the project and build
  77. set(additional_project_sources myobject.cpp anotherobject.cpp)
  78. configure_file("${testProjectTemplateDir}/CMakeLists.txt.in" "${testProjectSrc}/CMakeLists.txt"
  79. @ONLY)
  80. configure_file("${testProjectTemplateDir}/anotherobject.cpp" "${testProjectSrc}/anotherobject.cpp"
  81. COPYONLY)
  82. rebuild(3)