summaryrefslogtreecommitdiffstats
path: root/Tests/QtAutogen/RerunMocBasic/CMakeLists.txt
blob: 9b32e59515f05ce363f76940521f280dd74e4539 (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
cmake_minimum_required(VERSION 3.10)
project(RerunMocBasic)
include("../AutogenCoreTest.cmake")

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

# Utility variables
set(timeformat "%Y.%j.%H.%M%S")
set(mocBasicSrcDir "${CMAKE_CURRENT_SOURCE_DIR}/MocBasic")
set(mocBasicBinDir "${CMAKE_CURRENT_BINARY_DIR}/MocBasic")

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

macro(acquire_timestamp When)
  file(TIMESTAMP "${mocBasicBin}" time${When} "${timeformat}")
endmacro()

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

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

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


# Initial build
configure_file("${mocBasicSrcDir}/test1a.h.in" "${mocBasicBinDir}/test1.h" COPYONLY)
try_compile(MOC_RERUN
  "${mocBasicBinDir}"
  "${mocBasicSrcDir}"
  MocBasic
  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_RERUN)
  message(FATAL_ERROR "Initial build of mocBasic failed. Output: ${output}")
endif()

# Get name of the output binary
file(STRINGS "${mocBasicBinDir}/mocBasic.txt" mocBasicList ENCODING UTF-8)
list(GET mocBasicList 0 mocBasicBin)

# To avoid a race condition where the binary has the same timestamp
# as a source file and therefore gets rebuild
# - sleep to ensure a timestamp change
# - touch binary to ensure it has a new timestamp
acquire_timestamp(Before)
sleep()
message(STATUS "Touching binary file to ensure a new timestamps")
file(TOUCH_NOCREATE "${mocBasicBin}")
acquire_timestamp(After)
require_change()


# - Ensure that the timestamp will change
# - Change header file content
# - Rebuild
acquire_timestamp(Before)
sleep()
message(STATUS "Changing the header content for a MOC re-run")
configure_file("${mocBasicSrcDir}/test1b.h.in" "${mocBasicBinDir}/test1.h" COPYONLY)
sleep()
rebuild(2)
acquire_timestamp(After)
require_change()


# - Ensure that the timestamp would change
# - Change nothing
# - Rebuild
acquire_timestamp(Before)
sleep()
message(STATUS "Changing nothing for no MOC re-run")
rebuild(3)
acquire_timestamp(After)
require_change_not()