summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Woehlke <matthew.woehlke@kitware.com>2022-09-22 17:26:01 (GMT)
committerMatthew Woehlke <matthew.woehlke@kitware.com>2022-09-22 18:56:50 (GMT)
commita04eaf6742d98dd415ee939eae8c955c8afd1ce6 (patch)
treeecca44feca010ac68715ec11859c333605f63fd8
parentcb14ae2b870e9568d18990992882e0facacf22d4 (diff)
downloadCMake-a04eaf6742d98dd415ee939eae8c955c8afd1ce6.zip
CMake-a04eaf6742d98dd415ee939eae8c955c8afd1ce6.tar.gz
CMake-a04eaf6742d98dd415ee939eae8c955c8afd1ce6.tar.bz2
Tests: Clean up and simplify TryCompile tests
Add and use some additional helper macros to simplify repetitive checks. Use existing macros in more places. Tweak macros to improve output in case of failure.
-rw-r--r--Tests/TryCompile/CMakeLists.txt32
-rw-r--r--Tests/TryCompile/exit_with_error.c2
-rw-r--r--Tests/TryCompile/old_and_new_signature_tests.cmake41
3 files changed, 29 insertions, 46 deletions
diff --git a/Tests/TryCompile/CMakeLists.txt b/Tests/TryCompile/CMakeLists.txt
index 9560190..cca19bc 100644
--- a/Tests/TryCompile/CMakeLists.txt
+++ b/Tests/TryCompile/CMakeLists.txt
@@ -6,13 +6,25 @@ project(TryCompile)
macro(EXPECT_PASS var out)
if(NOT ${var})
- message(SEND_ERROR "Should pass failed ${out}")
+ message(SEND_ERROR "Should pass failed:\n${out}")
endif()
endmacro()
macro(EXPECT_FAIL var out)
if(${var})
- message(SEND_ERROR "Should fail passed ${out}")
+ message(SEND_ERROR "Should fail passed:\n${out}")
+ endif()
+endmacro()
+
+macro(EXPECT_COMPILED name var out)
+ if(NOT ${var})
+ message(SEND_ERROR "${name} failed compiling:\n${out}")
+ endif()
+endmacro()
+
+macro(EXPECT_RUN_RESULT name var expected)
+ if(NOT ${var} EQUAL ${expected})
+ message(SEND_ERROR " ${name} gave unexpected run result: ${${var}} expected: ${expected}")
endif()
endmacro()
@@ -88,23 +100,15 @@ try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
SOURCE_FROM_ARG main.c "${TRY_RUN_MAIN_CODE}"
SOURCE_FROM_ARG answer.c "${TRY_RUN_EXT_CODE}"
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT)
-if(NOT SHOULD_COMPILE)
- message(SEND_ERROR " SOURCE_FROM_ARG failed compiling: ${COMPILE_OUTPUT}")
-endif()
-if(NOT SHOULD_EXIT_WITH_ERROR EQUAL 42)
- message(SEND_ERROR " SOURCE_FROM_ARG gave unexpected run result: ${SHOULD_EXIT_WITH_ERROR}")
-endif()
+EXPECT_COMPILED("SOURCE_FROM_ARG" SHOULD_COMPILE "${COMPILE_OUTPUT}")
+EXPECT_RUN_RESULT("SOURCE_FROM_ARG" SHOULD_EXIT_WITH_ERROR 42)
try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
SOURCE_FROM_VAR main.c TRY_RUN_MAIN_CODE
SOURCE_FROM_VAR answer.c TRY_RUN_EXT_CODE
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT)
-if(NOT SHOULD_COMPILE)
- message(SEND_ERROR " SOURCE_FROM_VAR failed compiling: ${COMPILE_OUTPUT}")
-endif()
-if(NOT SHOULD_EXIT_WITH_ERROR EQUAL 42)
- message(SEND_ERROR " SOURCE_FROM_VAR gave unexpected run result: ${SHOULD_EXIT_WITH_ERROR}")
-endif()
+EXPECT_COMPILED("SOURCE_FROM_VAR" SHOULD_COMPILE "${COMPILE_OUTPUT}")
+EXPECT_RUN_RESULT("SOURCE_FROM_VAR" SHOULD_EXIT_WITH_ERROR 42)
# try to compile a project (old signature)
message("Testing try_compile project mode (old signature)")
diff --git a/Tests/TryCompile/exit_with_error.c b/Tests/TryCompile/exit_with_error.c
index f3c523d..dbddcf5 100644
--- a/Tests/TryCompile/exit_with_error.c
+++ b/Tests/TryCompile/exit_with_error.c
@@ -3,5 +3,5 @@
int main()
{
printf("hello world\n");
- return -1;
+ return 1;
}
diff --git a/Tests/TryCompile/old_and_new_signature_tests.cmake b/Tests/TryCompile/old_and_new_signature_tests.cmake
index c2dd233..997a5a3 100644
--- a/Tests/TryCompile/old_and_new_signature_tests.cmake
+++ b/Tests/TryCompile/old_and_new_signature_tests.cmake
@@ -130,18 +130,14 @@ if(APPLE)
${try_compile_bindir_or_SOURCES}
${TryCompile_SOURCE_DIR}/pass.m
OUTPUT_VARIABLE TRY_OUT)
- if(NOT SHOULD_PASS)
- message(SEND_ERROR "should pass failed ${TRY_OUT}")
- endif()
+ EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
# try to compile a file that should not compile
try_compile(SHOULD_FAIL
${try_compile_bindir_or_SOURCES}
${TryCompile_SOURCE_DIR}/fail.m
OUTPUT_VARIABLE TRY_OUT)
- if(SHOULD_FAIL)
- message(SEND_ERROR "Should fail passed ${TRY_OUT}")
- endif()
+ EXPECT_FAIL(SHOULD_FAIL "${TRY_OUT}")
endif()
######################################
@@ -155,14 +151,9 @@ try_run(SHOULD_RUN SHOULD_COMPILE
${try_compile_bindir_or_SOURCES}
${TryCompile_SOURCE_DIR}/exit_success.c
${try_compile_output_vars})
-if(NOT SHOULD_COMPILE)
- message(SEND_ERROR
- "exit_success failed compiling: ${${try_compile_compile_output_var}}")
-endif()
-if(NOT "${SHOULD_RUN}" STREQUAL "0")
- message(SEND_ERROR
- "exit_success failed running with exit code ${SHOULD_RUN}")
-endif()
+EXPECT_COMPILED("exit_success" SHOULD_COMPILE "${${try_compile_compile_output_var}}")
+EXPECT_RUN_RESULT("exit_success" SHOULD_RUN 0)
+
# check the compile output for the filename
if(NOT "${${try_compile_compile_output_var}}" MATCHES "exit_success")
message(SEND_ERROR
@@ -181,12 +172,8 @@ try_run(ARG_TEST_RUN ARG_TEST_COMPILE
${TryCompile_SOURCE_DIR}/expect_arg.c
COMPILE_OUTPUT_VARIABLE TRY_OUT
ARGS arg1 arg2)
-if(NOT ARG_TEST_COMPILE)
- message(SEND_ERROR "expect_arg failed compiling: ${TRY_OUT}")
-endif()
-if(NOT "${ARG_TEST_RUN}" STREQUAL "0")
- message(SEND_ERROR "expect_arg failed running with exit code ${ARG_TEST_RUN} ${TRY_OUT}")
-endif()
+EXPECT_COMPILED("expect_arg" ARG_TEST_COMPILE "${TRY_OUT}")
+EXPECT_RUN_RESULT("expect_arg" ARG_TEST_RUN 0)
# try to run a file that should compile and run, but return an error
try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
@@ -194,13 +181,8 @@ try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
${TryCompile_SOURCE_DIR}/exit_with_error.c
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
RUN_OUTPUT_VARIABLE RUN_OUTPUT)
-
-if(NOT SHOULD_COMPILE)
- message(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
-endif()
-if("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
- message(SEND_ERROR " exit_with_error passed with exit code ${SHOULD_EXIT_WITH_ERROR}")
-endif()
+EXPECT_COMPILED("exit_with_error" SHOULD_COMPILE "${COMPILE_OUTPUT}")
+EXPECT_RUN_RESULT("exit_with_error" SHOULD_EXIT_WITH_ERROR 1)
# check the compile output, it should contain the filename
if(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
@@ -224,10 +206,7 @@ try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
RUN_OUTPUT_STDOUT_VARIABLE RUN_OUTPUT_STDOUT
RUN_OUTPUT_STDERR_VARIABLE RUN_OUTPUT_STDERR)
-
-if(NOT SHOULD_COMPILE)
- message(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
-endif()
+EXPECT_PASS(SHOULD_COMPILE "${COMPILE_OUTPUT}")
if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfRun")
message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfRun\" failed")