1 cmake_minimum_required (VERSION 2.8.12)
3 cmake_policy(SET CMP0129 NEW)
7 macro(EXPECT_PASS var out)
9 message(SEND_ERROR "Should pass failed:\n${out}")
13 macro(EXPECT_FAIL var out)
15 message(SEND_ERROR "Should fail passed:\n${out}")
19 macro(EXPECT_COMPILED name var out)
21 message(SEND_ERROR "${name} failed compiling:\n${out}")
25 macro(EXPECT_RUN_RESULT name var expected)
26 if(NOT ${var} EQUAL ${expected})
27 message(SEND_ERROR " ${name} gave unexpected run result: ${${var}} expected: ${expected}")
31 macro(TEST_ASSERT value msg)
33 message (SEND_ERROR "Assertion failure:" ${msg} )
37 # run old signature tests
38 set(try_compile_bindir_or_SOURCES ${TryCompile_BINARY_DIR})
39 set(try_compile_redundant_SOURCES SOURCES)
40 set(try_compile_output_vars OUTPUT_VARIABLE TRY_OUT)
41 set(try_compile_compile_output_var TRY_OUT)
42 set(try_compile_run_output_var TRY_OUT)
43 include(old_and_new_signature_tests.cmake)
45 # run new signature tests
46 set(try_compile_bindir_or_SOURCES SOURCES)
47 set(try_compile_redundant_SOURCES "")
48 set(try_compile_output_vars
49 COMPILE_OUTPUT_VARIABLE COMPILE_OUT
50 RUN_OUTPUT_VARIABLE RUN_OUTPUT)
51 set(try_compile_compile_output_var COMPILE_OUT)
52 set(try_compile_run_output_var RUN_OUTPUT)
53 include(old_and_new_signature_tests.cmake)
55 # try to compile an empty source specified directly
56 try_compile(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE
57 SOURCE_FROM_CONTENT empty.c "")
58 if(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE)
59 message(SEND_ERROR "Trying to compile an empty source succeeded?")
62 try_compile(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE
63 SOURCE_FROM_VAR empty.c NAME_OF_A_VAR_THAT_IS_NOT_SET)
64 if(SHOULD_FAIL_DUE_TO_EMPTY_SOURCE)
65 message(SEND_ERROR "Trying to compile an empty source succeeded?")
68 # try to compile a copied source
69 try_compile(SHOULD_PASS
70 SOURCE_FROM_FILE pass.c ${TryCompile_SOURCE_DIR}/pass.c
71 OUTPUT_VARIABLE TRY_OUT)
72 EXPECT_COMPILED("SOURCE_FROM_FILE" SHOULD_PASS "${TRY_OUT}")
74 # try to run a source specified directly
76 "extern int answer(); \n"
77 "int main() { return answer(); }\n")
79 "int answer() { return 42; }\n")
81 try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
82 SOURCE_FROM_CONTENT main.c "${TRY_RUN_MAIN_CODE}"
83 SOURCE_FROM_CONTENT answer.c "${TRY_RUN_EXT_CODE}"
84 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT)
85 EXPECT_COMPILED("SOURCE_FROM_CONTENT" SHOULD_COMPILE "${COMPILE_OUTPUT}")
86 EXPECT_RUN_RESULT("SOURCE_FROM_CONTENT" SHOULD_EXIT_WITH_ERROR 42)
88 try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
89 SOURCE_FROM_VAR main.c TRY_RUN_MAIN_CODE
90 SOURCE_FROM_VAR answer.c TRY_RUN_EXT_CODE
91 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT)
92 EXPECT_COMPILED("SOURCE_FROM_VAR" SHOULD_COMPILE "${COMPILE_OUTPUT}")
93 EXPECT_RUN_RESULT("SOURCE_FROM_VAR" SHOULD_EXIT_WITH_ERROR 42)
95 # try to compile a project (old signature)
96 message("Testing try_compile project mode (old signature)")
97 try_compile(TEST_INNER
98 ${TryCompile_BINARY_DIR}/CMakeFiles/Inner
99 ${TryCompile_SOURCE_DIR}/Inner
100 TryCompileInner innerexe
101 OUTPUT_VARIABLE output)
102 TEST_ASSERT(TEST_INNER "try_compile project mode failed:\n${output}")
104 # try to compile a project (new signature)
105 message("Testing try_compile project mode (new signature)")
106 try_compile(TEST_INNER
107 PROJECT TryCompileInner
108 SOURCE_DIR ${TryCompile_SOURCE_DIR}/Inner
110 OUTPUT_VARIABLE output)
111 TEST_ASSERT(TEST_INNER "try_compile project mode failed:\n${output}")
113 add_executable(TryCompile pass.c)
115 #######################################################################
117 # also test that the check_prototype_definition macro works
119 include(CheckPrototypeDefinition)
121 check_prototype_definition(remove
122 "int remove(const char *pathname)"
126 test_assert(TEST_REMOVE_PROTO "check_prototype_definition for remove() failed")