Imported Upstream version 2.8.11.2
[platform/upstream/cmake.git] / Tests / RunCMake / RunCMake.cmake
1 foreach(arg
2     RunCMake_GENERATOR
3     RunCMake_SOURCE_DIR
4     RunCMake_BINARY_DIR
5     )
6   if(NOT DEFINED ${arg})
7     message(FATAL_ERROR "${arg} not given!")
8   endif()
9 endforeach()
10
11 function(run_cmake test)
12   set(top_src "${RunCMake_SOURCE_DIR}")
13   set(top_bin "${RunCMake_BINARY_DIR}")
14   if(EXISTS ${top_src}/${test}-result.txt)
15     file(READ ${top_src}/${test}-result.txt expect_result)
16     string(REGEX REPLACE "\n+$" "" expect_result "${expect_result}")
17   else()
18     set(expect_result 0)
19   endif()
20   foreach(o out err)
21     if(EXISTS ${top_src}/${test}-std${o}.txt)
22       file(READ ${top_src}/${test}-std${o}.txt expect_std${o})
23       string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
24     else()
25       unset(expect_std${o})
26     endif()
27   endforeach()
28   set(RunCMake_TEST_SOURCE_DIR "${top_src}")
29   if(NOT RunCMake_TEST_BINARY_DIR)
30     set(RunCMake_TEST_BINARY_DIR "${top_bin}/${test}-build")
31   endif()
32   if(NOT RunCMake_TEST_NO_CLEAN)
33     file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
34   endif()
35   file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
36   if(NOT DEFINED RunCMake_TEST_OPTIONS)
37     set(RunCMake_TEST_OPTIONS "")
38   endif()
39   execute_process(
40     COMMAND ${CMAKE_COMMAND} "${RunCMake_TEST_SOURCE_DIR}"
41               -G "${RunCMake_GENERATOR}"
42               -T "${RunCMake_GENERATOR_TOOLSET}"
43               -DRunCMake_TEST=${test}
44               ${RunCMake_TEST_OPTIONS}
45     WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
46     OUTPUT_VARIABLE actual_stdout
47     ERROR_VARIABLE actual_stderr
48     RESULT_VARIABLE actual_result
49     )
50   set(msg "")
51   if(NOT "${actual_result}" STREQUAL "${expect_result}")
52     set(msg "${msg}Result is [${actual_result}], not [${expect_result}].\n")
53   endif()
54   foreach(o out err)
55     string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
56     set(expect_${o} "")
57     if(DEFINED expect_std${o})
58       if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
59         string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
60           " expect-${o}> ${expect_std${o}}")
61         set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
62         set(msg "${msg}std${o} does not match that expected.\n")
63       endif()
64     endif()
65   endforeach()
66   unset(RunCMake_TEST_FAILED)
67   include(${top_src}/${test}-check.cmake OPTIONAL)
68   if(RunCMake_TEST_FAILED)
69     set(msg "${RunCMake_TEST_FAILED}\n${msg}")
70   endif()
71   if(msg)
72     string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
73     string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
74     message(SEND_ERROR "${test} - FAILED:\n"
75       "${msg}"
76       "${expect_out}"
77       "Actual stdout:\n${actual_out}\n"
78       "${expect_err}"
79       "Actual stderr:\n${actual_err}\n"
80       )
81   else()
82     message(STATUS "${test} - PASSED")
83   endif()
84 endfunction()