summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeTestMultipleConfigures/RunCMake.cmake
blob: 96326646b59088f011728b00c3b06d9a3a9a9bc5 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
if(NOT DEFINED CMake_SOURCE_DIR)
  message(FATAL_ERROR "CMake_SOURCE_DIR not defined")
endif()

if(NOT DEFINED dir)
  message(FATAL_ERROR "dir not defined")
endif()

if(NOT DEFINED gen)
  message(FATAL_ERROR "gen not defined")
endif()

# Call cmake once to get a baseline/reference output build tree: "Build".
# Then call cmake N more times, each time making a copy of the entire
# build tree after cmake is done configuring/generating. At the end,
# analyze the diffs in the generated build trees. Expect no diffs.
#
message(STATUS "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)")

set(N 7)

# First setup source and binary trees:
#
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory
  ${dir}/Source
)

execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory
  ${dir}/Build
)

execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory
  ${CMake_SOURCE_DIR}/Tests/CTestTest/SmallAndFast
  ${dir}/Source
)

execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory
  ${dir}/Build
)

# Patch SmallAndFast to build a .cxx executable too:
#
execute_process(COMMAND ${CMAKE_COMMAND} -E copy
  ${dir}/Source/echoargs.c
  ${dir}/Source/echoargs.cxx
)
file(APPEND "${dir}/Source/CMakeLists.txt" "\nadd_executable(echoargsCXX echoargs.cxx)\n")

# Loop N times, saving a copy of the configured/generated build tree each time:
#
foreach(i RANGE 1 ${N})
  # Equivalent sequence of shell commands:
  #
  message(STATUS "${i}: cd Build && cmake -G \"${gen}\" ../Source && cd .. && cp -r Build b${i}")

  # Run cmake:
  #
  execute_process(COMMAND ${CMAKE_COMMAND} -G ${gen} ../Source
    RESULT_VARIABLE result
    OUTPUT_VARIABLE stdout
    ERROR_VARIABLE stderr
    WORKING_DIRECTORY ${dir}/Build
    )

  message(STATUS "result='${result}'")
  message(STATUS "stdout='${stdout}'")
  message(STATUS "stderr='${stderr}'")
  message(STATUS "")

  # Save this iteration of the Build directory:
  #
  execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory
    ${dir}/b${i}
    )
  execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${dir}/Build
    ${dir}/b${i}
    RESULT_VARIABLE result
    OUTPUT_VARIABLE stdout
    ERROR_VARIABLE stderr
    )

  message(STATUS "result='${result}'")
  message(STATUS "stdout='${stdout}'")
  message(STATUS "stderr='${stderr}'")
  message(STATUS "")
endforeach()


# Function to analyze diffs between two directories.
# Set DIFF_EXECUTABLE before calling if 'diff' is available.
#
function(analyze_directory_diffs d1 d2 diff_count_var)
  set(diffs 0)

  message(STATUS "Analyzing directory diffs between:")
  message(STATUS "  d1='${d1}'")
  message(STATUS "  d2='${d2}'")

  if(NOT "${d1}" STREQUAL "" AND NOT "${d2}" STREQUAL "")
    message(STATUS "info: analyzing directories")

    file(GLOB_RECURSE files1 RELATIVE "${d1}" "${d1}/*")
    file(GLOB_RECURSE files2 RELATIVE "${d2}" "${d2}/*")

    if("${files1}" STREQUAL "${files2}")
      message(STATUS "info: file lists the same")
      #message(STATUS "  files='${files1}'")

      foreach(f ${files1})
        execute_process(COMMAND ${CMAKE_COMMAND} -E compare_files
          ${d1}/${f}
          ${d2}/${f}
          RESULT_VARIABLE result
          OUTPUT_VARIABLE stdout
          ERROR_VARIABLE stderr
          )
        if(result STREQUAL 0)
          #message(STATUS "info: file '${f}' the same")
        else()
          math(EXPR diffs "${diffs} + 1")
          message(STATUS "warning: file '${f}' differs from d1 to d2")
          file(READ "${d1}/${f}" f1contents)
          message(STATUS "contents of file '${d1}/${f}'
[===[${f1contents}]===]")
          file(READ "${d2}/${f}" f2contents)
          message(STATUS "contents of file '${d2}/${f}'
[===[${f2contents}]===]")
          if(DIFF_EXECUTABLE)
            message(STATUS "diff of files '${d1}/${f}' '${d2}/${f}'")
            message(STATUS "[====[")
            execute_process(COMMAND ${DIFF_EXECUTABLE} "${d1}/${f}" "${d2}/${f}")
            message(STATUS "]====]")
          endif()
        endif()
      endforeach()
    else()
      math(EXPR diffs "${diffs} + 1")
      message(STATUS "warning: file *lists* differ - some files exist in d1/not-d2 or not-d1/d2...")
      message(STATUS "  files1='${files1}'")
      message(STATUS "  files2='${files2}'")
    endif()
  endif()

  set(${diff_count_var} ${diffs} PARENT_SCOPE)
endfunction()


# Analyze diffs between b1:b2, b2:b3, b3:b4, b4:b5 ... bN-1:bN.
# Expect no diffs.
#
find_program(DIFF_EXECUTABLE diff)
set(total_diffs 0)

foreach(i RANGE 2 ${N})
  math(EXPR prev "${i} - 1")
  set(count 0)
  analyze_directory_diffs(${dir}/b${prev} ${dir}/b${i} count)
  message(STATUS "diff count='${count}'")
  message(STATUS "")
  math(EXPR total_diffs "${total_diffs} + ${count}")
endforeach()

message(STATUS "CMAKE_COMMAND='${CMAKE_COMMAND}'")
message(STATUS "total_diffs='${total_diffs}'")