CMakeLists.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. cmake_minimum_required(VERSION 3.10)
  2. project(RerunMocPlugin)
  3. include("../AutogenCoreTest.cmake")
  4. # Tests Q_PLUGIN_METADATA and CMAKE_AUTOMOC_DEPEND_FILTERS
  5. # json file change detection
  6. # Dummy executable to generate a clean target
  7. add_executable(dummy dummy.cpp)
  8. # Utility variables
  9. set(timeformat "%Y.%j.%H.%M%S")
  10. set(mocPlugSrcDir "${CMAKE_CURRENT_SOURCE_DIR}/MocPlugin")
  11. set(mocPlugBinDir "${CMAKE_CURRENT_BINARY_DIR}/MocPlugin")
  12. # Utility macros
  13. macro(sleep)
  14. message(STATUS "Sleeping for a few seconds.")
  15. execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep 1)
  16. endmacro()
  17. macro(rebuild buildName)
  18. message(STATUS "Starting build ${buildName}.")
  19. execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${mocPlugBinDir}" RESULT_VARIABLE result)
  20. if (result)
  21. message(FATAL_ERROR "Build ${buildName} failed.")
  22. else()
  23. message(STATUS "Build ${buildName} finished.")
  24. endif()
  25. endmacro()
  26. macro(require_change PLG)
  27. if (pl${PLG}After VERSION_GREATER pl${PLG}Before)
  28. message(STATUS "As expected the file ${pl${PLG}File} changed.")
  29. else()
  30. message(SEND_ERROR
  31. "Unexpectedly the file ${pl${PLG}File} did not change!\nTimestamp pre: ${pl${PLG}Before}\nTimestamp aft: ${pl${PLG}After}\n")
  32. endif()
  33. endmacro()
  34. macro(require_change_not PLG)
  35. if (pl${PLG}After VERSION_GREATER pl${PLG}Before)
  36. message(SEND_ERROR
  37. "Unexpectedly the file ${pl${PLG}File} changed!\nTimestamp pre: ${pl${PLG}Before}\nTimestamp aft: ${pl${PLG}After}\n")
  38. else()
  39. message(STATUS "As expected the file ${pl${PLG}File} did not change.")
  40. endif()
  41. endmacro()
  42. macro(acquire_timestamps When)
  43. file(TIMESTAMP "${plAFile}" plA${When} "${timeformat}")
  44. file(TIMESTAMP "${plBFile}" plB${When} "${timeformat}")
  45. file(TIMESTAMP "${plCFile}" plC${When} "${timeformat}")
  46. file(TIMESTAMP "${plDFile}" plD${When} "${timeformat}")
  47. file(TIMESTAMP "${plEFile}" plE${When} "${timeformat}")
  48. endmacro()
  49. # Initial build
  50. try_compile(MOC_PLUGIN
  51. "${mocPlugBinDir}"
  52. "${mocPlugSrcDir}"
  53. MocPlugin
  54. CMAKE_FLAGS "-DQT_TEST_VERSION=${QT_TEST_VERSION}"
  55. "-DCMAKE_AUTOGEN_VERBOSE=${CMAKE_AUTOGEN_VERBOSE}"
  56. "-DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}"
  57. OUTPUT_VARIABLE output
  58. )
  59. if (NOT MOC_PLUGIN)
  60. message(FATAL_ERROR "Initial build of mocPlugin failed. Output: ${output}")
  61. endif()
  62. # Get names of the output binaries
  63. find_library(plAFile "PlugA" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
  64. find_library(plBFile "PlugB" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
  65. find_library(plCFile "PlugC" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
  66. find_library(plDFile "PlugD" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
  67. find_library(plEFile "PlugE" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
  68. # To avoid a race condition where the library has the same timestamp
  69. # as a source file and therefore gets rebuild
  70. # - sleep to ensure a timestamp change
  71. # - rebuild library to ensure it has a new timestamp
  72. sleep()
  73. message(STATUS "Rebuilding library files to ensure new timestamps")
  74. rebuild(1)
  75. # - Ensure that the timestamp will change.
  76. # - Change the json files referenced by Q_PLUGIN_METADATA
  77. # - Rebuild
  78. acquire_timestamps(Before)
  79. sleep()
  80. message(STATUS "Changing json files.")
  81. configure_file("${mocPlugSrcDir}/jsonIn/StyleD.json" "${mocPlugBinDir}/jsonFiles/StyleC.json")
  82. configure_file("${mocPlugSrcDir}/jsonIn/StyleE.json" "${mocPlugBinDir}/jsonFiles/sub/StyleD.json")
  83. configure_file("${mocPlugSrcDir}/jsonIn/StyleC.json" "${mocPlugBinDir}/jsonFiles/StyleE.json")
  84. sleep()
  85. rebuild(2)
  86. acquire_timestamps(After)
  87. # Test changes
  88. require_change_not(A)
  89. require_change_not(B)
  90. require_change(C)
  91. require_change(D)
  92. # There's a bug in Ninja on Windows:
  93. # https://gitlab.kitware.com/cmake/cmake/issues/16776
  94. if(NOT ("${CMAKE_GENERATOR}" MATCHES "Ninja"))
  95. require_change(E)
  96. endif()
  97. # - Ensure that the timestamp will change.
  98. # - Change the json files referenced by A_CUSTOM_MACRO
  99. # - Rebuild
  100. acquire_timestamps(Before)
  101. sleep()
  102. message(STATUS "Changing json files")
  103. configure_file("${mocPlugSrcDir}/jsonIn/StyleE.json" "${mocPlugBinDir}/jsonFiles/StyleC_Custom.json")
  104. configure_file("${mocPlugSrcDir}/jsonIn/StyleC.json" "${mocPlugBinDir}/jsonFiles/sub/StyleD_Custom.json")
  105. configure_file("${mocPlugSrcDir}/jsonIn/StyleD.json" "${mocPlugBinDir}/jsonFiles/StyleE_Custom.json")
  106. sleep()
  107. rebuild(3)
  108. acquire_timestamps(After)
  109. # Test changes
  110. require_change_not(A)
  111. require_change_not(B)
  112. require_change(C)
  113. require_change(D)
  114. # There's a bug in Ninja on Windows
  115. # https://gitlab.kitware.com/cmake/cmake/issues/16776
  116. if(NOT ("${CMAKE_GENERATOR}" MATCHES "Ninja"))
  117. require_change(E)
  118. endif()