1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-20 04:24:36 +08:00

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.
This commit is contained in:
Matthew Woehlke
2022-09-22 13:26:01 -04:00
parent cb14ae2b87
commit a04eaf6742
3 changed files with 29 additions and 46 deletions

View File

@@ -6,13 +6,25 @@ project(TryCompile)
macro(EXPECT_PASS var out) macro(EXPECT_PASS var out)
if(NOT ${var}) if(NOT ${var})
message(SEND_ERROR "Should pass failed ${out}") message(SEND_ERROR "Should pass failed:\n${out}")
endif() endif()
endmacro() endmacro()
macro(EXPECT_FAIL var out) macro(EXPECT_FAIL var out)
if(${var}) 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() endif()
endmacro() 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 main.c "${TRY_RUN_MAIN_CODE}"
SOURCE_FROM_ARG answer.c "${TRY_RUN_EXT_CODE}" SOURCE_FROM_ARG answer.c "${TRY_RUN_EXT_CODE}"
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT) COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT)
if(NOT SHOULD_COMPILE) EXPECT_COMPILED("SOURCE_FROM_ARG" SHOULD_COMPILE "${COMPILE_OUTPUT}")
message(SEND_ERROR " SOURCE_FROM_ARG failed compiling: ${COMPILE_OUTPUT}") EXPECT_RUN_RESULT("SOURCE_FROM_ARG" SHOULD_EXIT_WITH_ERROR 42)
endif()
if(NOT SHOULD_EXIT_WITH_ERROR EQUAL 42)
message(SEND_ERROR " SOURCE_FROM_ARG gave unexpected run result: ${SHOULD_EXIT_WITH_ERROR}")
endif()
try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
SOURCE_FROM_VAR main.c TRY_RUN_MAIN_CODE SOURCE_FROM_VAR main.c TRY_RUN_MAIN_CODE
SOURCE_FROM_VAR answer.c TRY_RUN_EXT_CODE SOURCE_FROM_VAR answer.c TRY_RUN_EXT_CODE
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT) COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT)
if(NOT SHOULD_COMPILE) EXPECT_COMPILED("SOURCE_FROM_VAR" SHOULD_COMPILE "${COMPILE_OUTPUT}")
message(SEND_ERROR " SOURCE_FROM_VAR failed compiling: ${COMPILE_OUTPUT}") EXPECT_RUN_RESULT("SOURCE_FROM_VAR" SHOULD_EXIT_WITH_ERROR 42)
endif()
if(NOT SHOULD_EXIT_WITH_ERROR EQUAL 42)
message(SEND_ERROR " SOURCE_FROM_VAR gave unexpected run result: ${SHOULD_EXIT_WITH_ERROR}")
endif()
# try to compile a project (old signature) # try to compile a project (old signature)
message("Testing try_compile project mode (old signature)") message("Testing try_compile project mode (old signature)")

View File

@@ -3,5 +3,5 @@
int main() int main()
{ {
printf("hello world\n"); printf("hello world\n");
return -1; return 1;
} }

View File

@@ -130,18 +130,14 @@ if(APPLE)
${try_compile_bindir_or_SOURCES} ${try_compile_bindir_or_SOURCES}
${TryCompile_SOURCE_DIR}/pass.m ${TryCompile_SOURCE_DIR}/pass.m
OUTPUT_VARIABLE TRY_OUT) OUTPUT_VARIABLE TRY_OUT)
if(NOT SHOULD_PASS) EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
message(SEND_ERROR "should pass failed ${TRY_OUT}")
endif()
# try to compile a file that should not compile # try to compile a file that should not compile
try_compile(SHOULD_FAIL try_compile(SHOULD_FAIL
${try_compile_bindir_or_SOURCES} ${try_compile_bindir_or_SOURCES}
${TryCompile_SOURCE_DIR}/fail.m ${TryCompile_SOURCE_DIR}/fail.m
OUTPUT_VARIABLE TRY_OUT) OUTPUT_VARIABLE TRY_OUT)
if(SHOULD_FAIL) EXPECT_FAIL(SHOULD_FAIL "${TRY_OUT}")
message(SEND_ERROR "Should fail passed ${TRY_OUT}")
endif()
endif() endif()
###################################### ######################################
@@ -155,14 +151,9 @@ try_run(SHOULD_RUN SHOULD_COMPILE
${try_compile_bindir_or_SOURCES} ${try_compile_bindir_or_SOURCES}
${TryCompile_SOURCE_DIR}/exit_success.c ${TryCompile_SOURCE_DIR}/exit_success.c
${try_compile_output_vars}) ${try_compile_output_vars})
if(NOT SHOULD_COMPILE) EXPECT_COMPILED("exit_success" SHOULD_COMPILE "${${try_compile_compile_output_var}}")
message(SEND_ERROR EXPECT_RUN_RESULT("exit_success" SHOULD_RUN 0)
"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()
# check the compile output for the filename # check the compile output for the filename
if(NOT "${${try_compile_compile_output_var}}" MATCHES "exit_success") if(NOT "${${try_compile_compile_output_var}}" MATCHES "exit_success")
message(SEND_ERROR message(SEND_ERROR
@@ -181,12 +172,8 @@ try_run(ARG_TEST_RUN ARG_TEST_COMPILE
${TryCompile_SOURCE_DIR}/expect_arg.c ${TryCompile_SOURCE_DIR}/expect_arg.c
COMPILE_OUTPUT_VARIABLE TRY_OUT COMPILE_OUTPUT_VARIABLE TRY_OUT
ARGS arg1 arg2) ARGS arg1 arg2)
if(NOT ARG_TEST_COMPILE) EXPECT_COMPILED("expect_arg" ARG_TEST_COMPILE "${TRY_OUT}")
message(SEND_ERROR "expect_arg failed compiling: ${TRY_OUT}") EXPECT_RUN_RESULT("expect_arg" ARG_TEST_RUN 0)
endif()
if(NOT "${ARG_TEST_RUN}" STREQUAL "0")
message(SEND_ERROR "expect_arg failed running with exit code ${ARG_TEST_RUN} ${TRY_OUT}")
endif()
# try to run a file that should compile and run, but return an error # try to run a file that should compile and run, but return an error
try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE 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 ${TryCompile_SOURCE_DIR}/exit_with_error.c
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
RUN_OUTPUT_VARIABLE RUN_OUTPUT) RUN_OUTPUT_VARIABLE RUN_OUTPUT)
EXPECT_COMPILED("exit_with_error" SHOULD_COMPILE "${COMPILE_OUTPUT}")
if(NOT SHOULD_COMPILE) EXPECT_RUN_RESULT("exit_with_error" SHOULD_EXIT_WITH_ERROR 1)
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()
# check the compile output, it should contain the filename # check the compile output, it should contain the filename
if(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error") 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 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
RUN_OUTPUT_STDOUT_VARIABLE RUN_OUTPUT_STDOUT RUN_OUTPUT_STDOUT_VARIABLE RUN_OUTPUT_STDOUT
RUN_OUTPUT_STDERR_VARIABLE RUN_OUTPUT_STDERR) RUN_OUTPUT_STDERR_VARIABLE RUN_OUTPUT_STDERR)
EXPECT_PASS(SHOULD_COMPILE "${COMPILE_OUTPUT}")
if(NOT SHOULD_COMPILE)
message(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
endif()
if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfRun") if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfRun")
message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfRun\" failed") message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfRun\" failed")