diff options
author | Brad King <brad.king@kitware.com> | 2022-09-23 12:52:02 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-09-23 12:52:13 (GMT) |
commit | 98aef0929f4c5ce7bbf7ce9c2dfc5cad382f5a96 (patch) | |
tree | bfb6ee80bbd3e3e4ee81cd3c0aca30dca0cd747c /Tests | |
parent | 14a0794b66d64e856d8b41afec086f1fb94f503d (diff) | |
parent | 611d80179006ba6670273fcfcad46b0bb64a36c2 (diff) | |
download | CMake-98aef0929f4c5ce7bbf7ce9c2dfc5cad382f5a96.zip CMake-98aef0929f4c5ce7bbf7ce9c2dfc5cad382f5a96.tar.gz CMake-98aef0929f4c5ce7bbf7ce9c2dfc5cad382f5a96.tar.bz2 |
Merge topic 'try_compile-source-from'
611d801790 try_compile: Add SOURCE_FROM_FILE
a04eaf6742 Tests: Clean up and simplify TryCompile tests
cb14ae2b87 try_compile: Add SOURCE_FROM_{ARG,VAR}
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !7700
Diffstat (limited to 'Tests')
16 files changed, 94 insertions, 34 deletions
diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index bb11a57..d63624c 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -20,6 +20,11 @@ set(RunCMake_TEST_OPTIONS -Dtry_compile_DEFS=new_signature.cmake) include(${RunCMake_SOURCE_DIR}/old_and_new_signature_tests.cmake) unset(RunCMake_TEST_OPTIONS) +run_cmake(SourceFromOneArg) +run_cmake(SourceFromThreeArgs) +run_cmake(SourceFromBadName) +run_cmake(SourceFromBadFile) + run_cmake(ProjectCopyFile) run_cmake(NonSourceCopyFile) run_cmake(NonSourceCompileDefinitions) diff --git a/Tests/RunCMake/try_compile/SourceFromBadFile-result.txt b/Tests/RunCMake/try_compile/SourceFromBadFile-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromBadFile-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_compile/SourceFromBadFile-stderr.txt b/Tests/RunCMake/try_compile/SourceFromBadFile-stderr.txt new file mode 100644 index 0000000..53a6d8d --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromBadFile-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at SourceFromBadFile.cmake:[0-9]+ \(try_compile\): + SOURCE_FROM_FILE failed to copy "bad#source.c": No such file or directory +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/try_compile/SourceFromBadFile.cmake b/Tests/RunCMake/try_compile/SourceFromBadFile.cmake new file mode 100644 index 0000000..0a37f11 --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromBadFile.cmake @@ -0,0 +1 @@ +try_compile(RESULT SOURCE_FROM_FILE bad.c "bad#source.c") diff --git a/Tests/RunCMake/try_compile/SourceFromBadName-result.txt b/Tests/RunCMake/try_compile/SourceFromBadName-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromBadName-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_compile/SourceFromBadName-stderr.txt b/Tests/RunCMake/try_compile/SourceFromBadName-stderr.txt new file mode 100644 index 0000000..ef6847c --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromBadName-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at SourceFromBadName.cmake:[0-9]+ \(try_compile\): + SOURCES_FROM_ARG given invalid filename "bad/name.c" +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/try_compile/SourceFromBadName.cmake b/Tests/RunCMake/try_compile/SourceFromBadName.cmake new file mode 100644 index 0000000..e53a73f --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromBadName.cmake @@ -0,0 +1 @@ +try_compile(RESULT SOURCE_FROM_ARG bad/name.c "int main();") diff --git a/Tests/RunCMake/try_compile/SourceFromOneArg-result.txt b/Tests/RunCMake/try_compile/SourceFromOneArg-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromOneArg-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_compile/SourceFromOneArg-stderr.txt b/Tests/RunCMake/try_compile/SourceFromOneArg-stderr.txt new file mode 100644 index 0000000..bebe8bb --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromOneArg-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at SourceFromOneArg.cmake:[0-9]+ \(try_compile\): + SOURCE_FROM_ARG requires exactly two arguments +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/try_compile/SourceFromOneArg.cmake b/Tests/RunCMake/try_compile/SourceFromOneArg.cmake new file mode 100644 index 0000000..39ca11e --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromOneArg.cmake @@ -0,0 +1 @@ +try_compile(RESULT SOURCE_FROM_ARG test.c) diff --git a/Tests/RunCMake/try_compile/SourceFromThreeArgs-result.txt b/Tests/RunCMake/try_compile/SourceFromThreeArgs-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromThreeArgs-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_compile/SourceFromThreeArgs-stderr.txt b/Tests/RunCMake/try_compile/SourceFromThreeArgs-stderr.txt new file mode 100644 index 0000000..2cf201d --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromThreeArgs-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at SourceFromThreeArgs.cmake:[0-9]+ \(try_compile\): + SOURCE_FROM_ARG requires exactly two arguments +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/try_compile/SourceFromThreeArgs.cmake b/Tests/RunCMake/try_compile/SourceFromThreeArgs.cmake new file mode 100644 index 0000000..af6340d --- /dev/null +++ b/Tests/RunCMake/try_compile/SourceFromThreeArgs.cmake @@ -0,0 +1 @@ +try_compile(RESULT SOURCE_FROM_ARG test.c "int" "main();") diff --git a/Tests/TryCompile/CMakeLists.txt b/Tests/TryCompile/CMakeLists.txt index 8ebb00a..9396cfa 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() @@ -64,6 +76,46 @@ set(try_compile_compile_output_var COMPILE_OUT) set(try_compile_run_output_var RUN_OUTPUT) include(old_and_new_signature_tests.cmake) +# try to compile an empty source specified directly +try_compile(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE + SOURCE_FROM_ARG empty.c "") +if(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE) + message(SEND_ERROR "Trying to compile an empty source succeeded?") +endif() + +try_compile(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE + SOURCE_FROM_VAR empty.c NAME_OF_A_VAR_THAT_IS_NOT_SET) +if(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE) + message(SEND_ERROR "Trying to compile an empty source succeeded?") +endif() + +# try to compile a copied source +try_compile(SHOULD_PASS + SOURCE_FROM_FILE pass.c ${TryCompile_SOURCE_DIR}/pass.c + OUTPUT_VARIABLE TRY_OUT) +EXPECT_COMPILED("SOURCE_FROM_FILE" SHOULD_PASS "${TRY_OUT}") + +# try to run a source specified directly +set(TRY_RUN_MAIN_CODE + "extern int answer(); \n" + "int main() { return answer(); }\n") +set(TRY_RUN_EXT_CODE + "int answer() { return 42; }\n") + +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) +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) +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)") try_compile(TEST_INNER 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") |