Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Tests / TryCompile / CMakeLists.txt
1 cmake_minimum_required (VERSION 2.8.12)
2 if(POLICY CMP0129)
3   cmake_policy(SET CMP0129 NEW)
4 endif()
5 project(TryCompile)
6
7 macro(EXPECT_PASS var out)
8   if(NOT ${var})
9     message(SEND_ERROR "Should pass failed:\n${out}")
10   endif()
11 endmacro()
12
13 macro(EXPECT_FAIL var out)
14   if(${var})
15     message(SEND_ERROR "Should fail passed:\n${out}")
16   endif()
17 endmacro()
18
19 macro(EXPECT_COMPILED name var out)
20   if(NOT ${var})
21     message(SEND_ERROR "${name} failed compiling:\n${out}")
22   endif()
23 endmacro()
24
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}")
28   endif()
29 endmacro()
30
31 macro(TEST_ASSERT value msg)
32   if (NOT ${value})
33     message (SEND_ERROR "Assertion failure:" ${msg} )
34   endif ()
35 endmacro()
36
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)
44
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)
54
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?")
60 endif()
61
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?")
66 endif()
67
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}")
73
74 # try to run a source specified directly
75 set(TRY_RUN_MAIN_CODE
76   "extern int answer(); \n"
77   "int main() { return answer(); }\n")
78 set(TRY_RUN_EXT_CODE
79   "int answer() { return 42; }\n")
80
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)
87
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)
94
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}")
103
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
109   TARGET innerexe
110   OUTPUT_VARIABLE output)
111 TEST_ASSERT(TEST_INNER "try_compile project mode failed:\n${output}")
112
113 add_executable(TryCompile pass.c)
114
115 #######################################################################
116 #
117 # also test that the check_prototype_definition macro works
118
119 include(CheckPrototypeDefinition)
120
121 check_prototype_definition(remove
122   "int remove(const char *pathname)"
123   "0"
124   "stdio.h"
125   TEST_REMOVE_PROTO)
126 test_assert(TEST_REMOVE_PROTO "check_prototype_definition for remove() failed")