summaryrefslogtreecommitdiffstats
path: root/Tests/QtAutogen/RerunMocPlugin/CMakeLists.txt
blob: 6ab95387f014bad66935e8a71b1b27edb7e3b28f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
cmake_minimum_required(VERSION 3.10)
project(RerunMocPlugin)
include("../AutogenCoreTest.cmake")

# Tests Q_PLUGIN_METADATA and CMAKE_AUTOMOC_DEPEND_FILTERS
# json file change detection

# Dummy executable to generate a clean target
add_executable(dummy dummy.cpp)

# Utility variables
set(timeformat "%Y.%j.%H.%M%S")
set(mocPlugSrcDir "${CMAKE_CURRENT_SOURCE_DIR}/MocPlugin")
set(mocPlugBinDir "${CMAKE_CURRENT_BINARY_DIR}/MocPlugin")

# Utility macros
macro(sleep)
  message(STATUS "Sleeping for a few seconds.")
  execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep 1)
endmacro()

macro(rebuild buildName)
  message(STATUS "Starting build ${buildName}.")
  execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${mocPlugBinDir}" RESULT_VARIABLE result)
  if (result)
    message(FATAL_ERROR "Build ${buildName} failed.")
  else()
    message(STATUS "Build ${buildName} finished.")
  endif()
endmacro()

macro(require_change PLG)
  if (pl${PLG}After VERSION_GREATER pl${PLG}Before)
    message(STATUS "As expected the file ${pl${PLG}File} changed.")
  else()
    message(SEND_ERROR
      "Unexpectedly the file ${pl${PLG}File} did not change!\nTimestamp pre: ${pl${PLG}Before}\nTimestamp aft: ${pl${PLG}After}\n")
  endif()
endmacro()

macro(require_change_not PLG)
  if (pl${PLG}After VERSION_GREATER pl${PLG}Before)
    message(SEND_ERROR
      "Unexpectedly the file ${pl${PLG}File} changed!\nTimestamp pre: ${pl${PLG}Before}\nTimestamp aft: ${pl${PLG}After}\n")
  else()
    message(STATUS "As expected the file ${pl${PLG}File} did not change.")
  endif()
endmacro()

macro(acquire_timestamps When)
  file(TIMESTAMP "${plAFile}" plA${When} "${timeformat}")
  file(TIMESTAMP "${plBFile}" plB${When} "${timeformat}")
  file(TIMESTAMP "${plCFile}" plC${When} "${timeformat}")
  file(TIMESTAMP "${plDFile}" plD${When} "${timeformat}")
  file(TIMESTAMP "${plEFile}" plE${When} "${timeformat}")
endmacro()


# Initial build
try_compile(MOC_PLUGIN
  "${mocPlugBinDir}"
  "${mocPlugSrcDir}"
  MocPlugin
  CMAKE_FLAGS "-DQT_TEST_VERSION=${QT_TEST_VERSION}"
              "-DCMAKE_AUTOGEN_VERBOSE=${CMAKE_AUTOGEN_VERBOSE}"
              "-DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}"
  OUTPUT_VARIABLE output
)
if (NOT MOC_PLUGIN)
  message(FATAL_ERROR "Initial build of mocPlugin failed. Output: ${output}")
endif()

# Get names of the output binaries
find_library(plAFile "PlugA" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
find_library(plBFile "PlugB" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
find_library(plCFile "PlugC" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
find_library(plDFile "PlugD" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)
find_library(plEFile "PlugE" PATHS "${mocPlugBinDir}/Debug" "${mocPlugBinDir}" NO_DEFAULT_PATH)

# To avoid a race condition where the library has the same timestamp
# as a source file and therefore gets rebuild
# - sleep to ensure a timestamp change
# - touch library to ensure it has a new timestamp
acquire_timestamps(Before)
sleep()
message(STATUS "Touching library files to ensure new timestamps")
file(TOUCH_NOCREATE "${plAFile}" "${plBFile}" "${plCFile}" "${plDFile}" "${plEFile}")
acquire_timestamps(After)
require_change(A)
require_change(B)
require_change(C)
require_change(D)
require_change(E)


# - Ensure that the timestamp will change.
# - Change the json files referenced by Q_PLUGIN_METADATA
# - Rebuild
acquire_timestamps(Before)
sleep()
message(STATUS "Changing json files")
configure_file("${mocPlugSrcDir}/jsonIn/StyleD.json" "${mocPlugBinDir}/jsonFiles/StyleC.json")
configure_file("${mocPlugSrcDir}/jsonIn/StyleE.json" "${mocPlugBinDir}/jsonFiles/sub/StyleD.json")
configure_file("${mocPlugSrcDir}/jsonIn/StyleC.json" "${mocPlugBinDir}/jsonFiles/StyleE.json")
sleep()
rebuild(2)
acquire_timestamps(After)
# Test changes
require_change_not(A)
require_change_not(B)
require_change(C)
require_change(D)
# There's a bug in Ninja on Windows:
# https://gitlab.kitware.com/cmake/cmake/issues/16776
if(NOT ("${CMAKE_GENERATOR}" MATCHES "Ninja"))
  require_change(E)
endif()


# - Ensure that the timestamp will change.
# - Change the json files referenced by A_CUSTOM_MACRO
# - Rebuild
acquire_timestamps(Before)
sleep()
message(STATUS "Changing json files")
configure_file("${mocPlugSrcDir}/jsonIn/StyleE.json" "${mocPlugBinDir}/jsonFiles/StyleC_Custom.json")
configure_file("${mocPlugSrcDir}/jsonIn/StyleC.json" "${mocPlugBinDir}/jsonFiles/sub/StyleD_Custom.json")
configure_file("${mocPlugSrcDir}/jsonIn/StyleD.json" "${mocPlugBinDir}/jsonFiles/StyleE_Custom.json")
sleep()
rebuild(3)
acquire_timestamps(After)
# Test changes
require_change_not(A)
require_change_not(B)
require_change(C)
require_change(D)
# There's a bug in Ninja on Windows
# https://gitlab.kitware.com/cmake/cmake/issues/16776
if(NOT ("${CMAKE_GENERATOR}" MATCHES "Ninja"))
  require_change(E)
endif()