cmake/Tests/RunCMake/RunCMake.cmake

201 lines
6.8 KiB
CMake
Raw Normal View History

2012-04-19 19:04:21 +03:00
foreach(arg
RunCMake_GENERATOR
RunCMake_SOURCE_DIR
RunCMake_BINARY_DIR
)
if(NOT DEFINED ${arg})
message(FATAL_ERROR "${arg} not given!")
endif()
endforeach()
function(run_cmake test)
2019-11-11 23:01:05 +01:00
if(DEFINED ENV{RunCMake_TEST_FILTER} AND NOT test MATCHES "$ENV{RunCMake_TEST_FILTER}")
return()
endif()
2012-04-19 19:04:21 +03:00
set(top_src "${RunCMake_SOURCE_DIR}")
set(top_bin "${RunCMake_BINARY_DIR}")
if(EXISTS ${top_src}/${test}-result.txt)
file(READ ${top_src}/${test}-result.txt expect_result)
string(REGEX REPLACE "\n+$" "" expect_result "${expect_result}")
else()
set(expect_result 0)
endif()
foreach(o out err)
2015-08-17 11:37:30 +02:00
if(RunCMake-std${o}-file AND EXISTS ${top_src}/${RunCMake-std${o}-file})
file(READ ${top_src}/${RunCMake-std${o}-file} expect_std${o})
string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
elseif(EXISTS ${top_src}/${test}-std${o}.txt)
2012-04-19 19:04:21 +03:00
file(READ ${top_src}/${test}-std${o}.txt expect_std${o})
string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
else()
unset(expect_std${o})
endif()
endforeach()
2015-04-27 22:25:09 +02:00
if (NOT expect_stderr)
if (NOT RunCMake_DEFAULT_stderr)
set(RunCMake_DEFAULT_stderr "^$")
endif()
set(expect_stderr ${RunCMake_DEFAULT_stderr})
endif()
if (NOT RunCMake_TEST_SOURCE_DIR)
set(RunCMake_TEST_SOURCE_DIR "${top_src}")
endif()
2013-03-16 19:13:01 +02:00
if(NOT RunCMake_TEST_BINARY_DIR)
set(RunCMake_TEST_BINARY_DIR "${top_bin}/${test}-build")
endif()
if(NOT RunCMake_TEST_NO_CLEAN)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
endif()
2012-06-27 20:52:58 +03:00
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
2019-11-11 23:01:05 +01:00
if(RunCMake-prep-file AND EXISTS ${top_src}/${RunCMake-prep-file})
include(${top_src}/${RunCMake-prep-file})
else()
include(${top_src}/${test}-prep.cmake OPTIONAL)
endif()
2013-03-16 19:13:01 +02:00
if(NOT DEFINED RunCMake_TEST_OPTIONS)
set(RunCMake_TEST_OPTIONS "")
endif()
2014-08-03 19:52:23 +02:00
if(APPLE)
list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0025=NEW)
endif()
2015-04-27 22:25:09 +02:00
if(RunCMake_MAKE_PROGRAM)
list(APPEND RunCMake_TEST_OPTIONS "-DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM}")
endif()
2015-08-17 11:37:30 +02:00
if(RunCMake_TEST_OUTPUT_MERGE)
set(actual_stderr_var actual_stdout)
set(actual_stderr "")
else()
set(actual_stderr_var actual_stderr)
endif()
2015-11-17 17:22:37 +01:00
if(DEFINED RunCMake_TEST_TIMEOUT)
set(maybe_timeout TIMEOUT ${RunCMake_TEST_TIMEOUT})
else()
set(maybe_timeout "")
endif()
2018-11-29 20:27:00 +01:00
if(RunCMake-stdin-file AND EXISTS ${top_src}/${RunCMake-stdin-file})
set(maybe_input_file INPUT_FILE ${top_src}/${RunCMake-stdin-file})
elseif(EXISTS ${top_src}/${test}-stdin.txt)
set(maybe_input_file INPUT_FILE ${top_src}/${test}-stdin.txt)
else()
set(maybe_input_file "")
endif()
2014-08-03 19:52:23 +02:00
if(RunCMake_TEST_COMMAND)
2019-11-11 23:01:05 +01:00
if(NOT RunCMake_TEST_COMMAND_WORKING_DIRECTORY)
set(RunCMake_TEST_COMMAND_WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
endif()
2014-08-03 19:52:23 +02:00
execute_process(
COMMAND ${RunCMake_TEST_COMMAND}
2019-11-11 23:01:05 +01:00
WORKING_DIRECTORY "${RunCMake_TEST_COMMAND_WORKING_DIRECTORY}"
2014-08-03 19:52:23 +02:00
OUTPUT_VARIABLE actual_stdout
2015-08-17 11:37:30 +02:00
ERROR_VARIABLE ${actual_stderr_var}
2014-08-03 19:52:23 +02:00
RESULT_VARIABLE actual_result
2017-04-14 19:02:05 +02:00
ENCODING UTF8
2015-11-17 17:22:37 +01:00
${maybe_timeout}
2018-11-29 20:27:00 +01:00
${maybe_input_file}
2014-08-03 19:52:23 +02:00
)
else()
2018-04-23 21:13:27 +02:00
if(RunCMake_GENERATOR_INSTANCE)
set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE=${RunCMake_GENERATOR_INSTANCE}")
else()
set(_D_CMAKE_GENERATOR_INSTANCE "")
endif()
2019-11-11 23:01:05 +01:00
if(NOT RunCMake_TEST_NO_SOURCE_DIR)
set(maybe_source_dir "${RunCMake_TEST_SOURCE_DIR}")
else()
set(maybe_source_dir "")
endif()
2014-08-03 19:52:23 +02:00
execute_process(
2019-11-11 23:01:05 +01:00
COMMAND ${CMAKE_COMMAND}
${maybe_source_dir}
2014-08-03 19:52:23 +02:00
-G "${RunCMake_GENERATOR}"
2015-04-27 22:25:09 +02:00
-A "${RunCMake_GENERATOR_PLATFORM}"
2014-08-03 19:52:23 +02:00
-T "${RunCMake_GENERATOR_TOOLSET}"
2018-04-23 21:13:27 +02:00
${_D_CMAKE_GENERATOR_INSTANCE}
2014-08-03 19:52:23 +02:00
-DRunCMake_TEST=${test}
2015-04-27 22:25:09 +02:00
--no-warn-unused-cli
2014-08-03 19:52:23 +02:00
${RunCMake_TEST_OPTIONS}
WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
OUTPUT_VARIABLE actual_stdout
2015-08-17 11:37:30 +02:00
ERROR_VARIABLE ${actual_stderr_var}
2014-08-03 19:52:23 +02:00
RESULT_VARIABLE actual_result
2017-04-14 19:02:05 +02:00
ENCODING UTF8
2015-11-17 17:22:37 +01:00
${maybe_timeout}
2018-11-29 20:27:00 +01:00
${maybe_input_file}
2014-08-03 19:52:23 +02:00
)
endif()
2012-04-19 19:04:21 +03:00
set(msg "")
2015-04-27 22:25:09 +02:00
if(NOT "${actual_result}" MATCHES "${expect_result}")
2016-10-30 18:24:19 +01:00
string(APPEND msg "Result is [${actual_result}], not [${expect_result}].\n")
2012-04-19 19:04:21 +03:00
endif()
2018-04-23 21:13:27 +02:00
string(CONCAT ignore_line_regex
"(^|\n)((==[0-9]+=="
"|BullseyeCoverage"
"|[a-z]+\\([0-9]+\\) malloc:"
"|clang[^:]*: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled:"
"|Error kstat returned"
"|Hit xcodebuild bug"
2018-08-09 18:06:22 +02:00
"|[^\n]*xcodebuild[^\n]*warning: file type[^\n]*is based on missing file type"
"|ld: 0711-224 WARNING: Duplicate symbol: .__init_aix_libgcc_cxa_atexit"
"|ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information"
2018-04-23 21:13:27 +02:00
"|[^\n]*is a member of multiple groups"
"|[^\n]*from Time Machine by path"
"|[^\n]*Bullseye Testing Technology"
")[^\n]*\n)+"
)
2012-04-19 19:04:21 +03:00
foreach(o out err)
2015-04-27 22:25:09 +02:00
string(REGEX REPLACE "\r\n" "\n" actual_std${o} "${actual_std${o}}")
2018-04-23 21:13:27 +02:00
string(REGEX REPLACE "${ignore_line_regex}" "\\1" actual_std${o} "${actual_std${o}}")
2012-04-19 19:04:21 +03:00
string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
set(expect_${o} "")
if(DEFINED expect_std${o})
if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
" expect-${o}> ${expect_std${o}}")
set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
2016-10-30 18:24:19 +01:00
string(APPEND msg "std${o} does not match that expected.\n")
2012-04-19 19:04:21 +03:00
endif()
endif()
endforeach()
2012-06-27 20:52:58 +03:00
unset(RunCMake_TEST_FAILED)
2015-11-17 17:22:37 +01:00
if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
include(${top_src}/${RunCMake-check-file})
else()
include(${top_src}/${test}-check.cmake OPTIONAL)
endif()
2012-06-27 20:52:58 +03:00
if(RunCMake_TEST_FAILED)
set(msg "${RunCMake_TEST_FAILED}\n${msg}")
endif()
2015-04-27 22:25:09 +02:00
if(msg AND RunCMake_TEST_COMMAND)
string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
2016-10-30 18:24:19 +01:00
string(APPEND msg "Command was:\n command> ${command}\n")
2015-04-27 22:25:09 +02:00
endif()
2012-04-19 19:04:21 +03:00
if(msg)
string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
message(SEND_ERROR "${test} - FAILED:\n"
"${msg}"
"${expect_out}"
"Actual stdout:\n${actual_out}\n"
"${expect_err}"
"Actual stderr:\n${actual_err}\n"
)
else()
message(STATUS "${test} - PASSED")
endif()
endfunction()
2014-08-03 19:52:23 +02:00
function(run_cmake_command test)
set(RunCMake_TEST_COMMAND "${ARGN}")
run_cmake(${test})
endfunction()
2015-08-17 11:37:30 +02:00
2019-11-11 23:01:05 +01:00
function(run_cmake_with_options test)
set(RunCMake_TEST_OPTIONS "${ARGN}")
run_cmake(${test})
endfunction()
2015-08-17 11:37:30 +02:00
# Protect RunCMake tests from calling environment.
unset(ENV{MAKEFLAGS})