Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Tests / TryCompile / CMakeLists.txt
blob4667345bf1f0f51fd809b4d3e2648fe621a74e2d
1 cmake_minimum_required (VERSION 2.6)
2 PROJECT(TryCompile)
4 MACRO(TEST_ASSERT value msg)
5   IF (NOT ${value})
6     MESSAGE (SEND_ERROR "Assertion failure:" ${msg} )
7   ENDIF (NOT ${value})
8 ENDMACRO(TEST_ASSERT)
10 MACRO(TEST_FAIL value msg)
11   IF (${value})
12     MESSAGE (SEND_ERROR "Failing test succeeded:" ${msg} )
13   ENDIF (${value})
14 ENDMACRO(TEST_FAIL)
16 MACRO(TEST_EXPECT_EXACT command expected)
17   IF(NOT "x${result}" STREQUAL "x${expected}")
18     MESSAGE(SEND_ERROR "${CMAKE_CURRENT_LIST_LINE}: TEST \"${command}\" failed: \"${result}\" expected: \"${expected}\"")
19   ENDIF(NOT "x${result}" STREQUAL "x${expected}")
20 ENDMACRO(TEST_EXPECT_EXACT command expected)
22 MACRO(TEST_EXPECT_CONTAINS command expected)
23   IF(NOT "${result}" MATCHES "${expected}")
24     MESSAGE(SEND_ERROR "${CMAKE_CURRENT_LIST_LINE}: TEST \"${command}\" failed: \"${result}\" expected: \"${expected}\"")
25   ENDIF(NOT "${result}" MATCHES "${expected}")
26 ENDMACRO(TEST_EXPECT_CONTAINS command expected)
28 # try to compile a file that should compile
29 # also check that COPY_FILE works
30 TRY_COMPILE(SHOULD_PASS
31     ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp  
32     ${TryCompile_SOURCE_DIR}/pass.c
33     OUTPUT_VARIABLE TRY_OUT
34     COPY_FILE ${TryCompile_BINARY_DIR}/CopyOfPass
35     )
37 IF(NOT SHOULD_PASS)
38   MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
39 ENDIF(NOT SHOULD_PASS)
40 IF(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
41    MESSAGE(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfPass\" failed")
42 ELSE(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
43    FILE(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass")
44 ENDIF(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
46 # try to compile a file that should not compile
47 TRY_COMPILE(SHOULD_FAIL
48     ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp  
49     ${TryCompile_SOURCE_DIR}/fail.c
50     OUTPUT_VARIABLE TRY_OUT)
51 IF(SHOULD_FAIL)
52    MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
53 ENDIF(SHOULD_FAIL)
55 # try to compile a file that should compile
56 TRY_COMPILE(SHOULD_PASS
57     ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp  
58     ${TryCompile_SOURCE_DIR}/pass.c
59     OUTPUT_VARIABLE TRY_OUT)
60 IF(NOT SHOULD_PASS)
61   MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
62 ENDIF(NOT SHOULD_PASS)
64 # try to compile a file that should not compile
65 TRY_COMPILE(SHOULD_FAIL
66     ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp 
67     ${TryCompile_SOURCE_DIR}/fail.c
68     OUTPUT_VARIABLE TRY_OUT)
69 IF(SHOULD_FAIL)
70    MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
71 ENDIF(SHOULD_FAIL)
73 IF(NOT SHOULD_FAIL)
74   IF(SHOULD_PASS)
75     MESSAGE("All Tests passed, ignore all previous output.")
76   ELSE(SHOULD_PASS)
77     MESSAGE("Test failed")
78   ENDIF(SHOULD_PASS)
79 ELSE(NOT SHOULD_FAIL)
80   MESSAGE("Test failed")
81 ENDIF(NOT SHOULD_FAIL)
82 TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE   
83   ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp 
84     ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
85 IF (CMAKE_ANSI_FOR_SCOPE)
86    MESSAGE("Compiler supports ansi for")
87 ELSE(CMAKE_ANSI_FOR_SCOPE)
88    MESSAGE("Compiler does not support ansi for scope")
89 ENDIF(CMAKE_ANSI_FOR_SCOPE)
91 TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE
92   ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp 
93     ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
94 IF (CMAKE_ANSI_FOR_SCOPE)
95    MESSAGE("Compiler supports ansi for")
96 ELSE(CMAKE_ANSI_FOR_SCOPE)
97    MESSAGE("Compiler does not support ansi for scope")
98 ENDIF(CMAKE_ANSI_FOR_SCOPE)        
100 MESSAGE("use the module now")
101 INCLUDE(${CMAKE_ROOT}/Modules/TestForANSIForScope.cmake)
102 IF (CMAKE_ANSI_FOR_SCOPE)
103    MESSAGE("Compiler supports ansi for")
104 ELSE(CMAKE_ANSI_FOR_SCOPE)
105    MESSAGE("Compiler does not support ansi for scope")
106 ENDIF(CMAKE_ANSI_FOR_SCOPE)
108 ADD_EXECUTABLE(TryCompile pass.c)
110 ######################################
112 # now two tests for TRY_RUN
114 # try to run a file that should compile and run without error
115 # also check that OUTPUT_VARIABLE contains both the compile output
116 # and the run output
117 TRY_RUN(SHOULD_RUN SHOULD_COMPILE
118     ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp  
119     ${TryCompile_SOURCE_DIR}/exit_success.c
120     OUTPUT_VARIABLE TRY_OUT)
121 IF(NOT SHOULD_COMPILE)
122   MESSAGE(SEND_ERROR "exit_success failed compiling: ${TRY_OUT}")
123 ENDIF(NOT SHOULD_COMPILE)
124 IF(NOT "${SHOULD_RUN}" STREQUAL "0")
125   MESSAGE(SEND_ERROR "exit_success failed running with exit code ${SHOULD_RUN}")
126 ENDIF(NOT "${SHOULD_RUN}" STREQUAL "0")
127 # check the compile output for the filename
128 IF(NOT "${TRY_OUT}" MATCHES "exit_success")
129   MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"exit_success\": \"${TRY_OUT}\"")
130 ENDIF(NOT "${TRY_OUT}" MATCHES "exit_success")
131 # check the run output
132 IF(NOT "${TRY_OUT}" MATCHES "hello world")
133   MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"hello world\": \"${TRY_OUT}\"")
134 ENDIF(NOT "${TRY_OUT}" MATCHES "hello world")
137 # try to run a file that should compile and run, but return an error
138 TRY_RUN(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
139     ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp  
140     ${TryCompile_SOURCE_DIR}/exit_with_error.c
141     COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
142     RUN_OUTPUT_VARIABLE RUN_OUTPUT)
144 IF(NOT SHOULD_COMPILE)
145   MESSAGE(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
146 ENDIF(NOT SHOULD_COMPILE)
147 IF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
148   MESSAGE(SEND_ERROR " exit_with_error passed with exit code ${SHOULD_EXIT_WITH_ERROR}")
149 ENDIF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
151 # check the compile output, it should contain the filename
152 IF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
153   MESSAGE(SEND_ERROR " COMPILE_OUT didn't contain \"exit_with_error\": \"${COMPILE_OUTPUT}\"")
154 ENDIF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
155 #... but not the run time output
156 IF("${COMPILE_OUTPUT}" MATCHES "hello world")
157   MESSAGE(SEND_ERROR " COMPILE_OUT contains the run output: \"${COMPILE_OUTPUT}\"")
158 ENDIF("${COMPILE_OUTPUT}" MATCHES "hello world")
159 # check the run output, it should stdout
160 IF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
161   MESSAGE(SEND_ERROR " RUN_OUTPUT didn't contain \"hello world\": \"${RUN_OUTPUT}\"")
162 ENDIF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
164 #######################################################################
166 # also test that the CHECK_C_SOURCE_COMPILES, CHECK_CXX_SOURCE_COMPILES
167 # CHECK_C_SOURCE_RUNS and CHECK_CXX_SOURCE_RUNS macros work
169 INCLUDE(CheckCSourceCompiles)
170 INCLUDE(CheckCXXSourceCompiles)
171 INCLUDE(CheckCSourceRuns)
172 INCLUDE(CheckCXXSourceRuns)
174 CHECK_C_SOURCE_COMPILES("I dont build" C_BUILD_SHOULD_FAIL)
175 CHECK_C_SOURCE_COMPILES("int main() {return 0;}" C_BUILD_SHOULD_WORK)
176 CHECK_C_SOURCE_RUNS("int main() {return 1;}" C_RUN_SHOULD_FAIL)
177 CHECK_C_SOURCE_RUNS("int main() {return 0;}" C_RUN_SHOULD_WORK)
179 TEST_FAIL(C_BUILD_SHOULD_FAIL "CHECK_C_SOURCE_COMPILES() succeeded, but should have failed")
180 TEST_ASSERT(C_BUILD_SHOULD_WORK "CHECK_C_SOURCE_COMPILES() failed")
181 TEST_FAIL(C_RUN_SHOULD_FAIL "CHECK_C_SOURCE_RUNS() succeeded, but should have failed")
182 TEST_ASSERT(C_RUN_SHOULD_WORK "CHECK_C_SOURCE_RUNS() failed")
184 CHECK_CXX_SOURCE_COMPILES("I dont build" CXX_BUILD_SHOULD_FAIL)
185 CHECK_CXX_SOURCE_COMPILES("int main() {return 0;}" CXX_BUILD_SHOULD_WORK)
186 CHECK_CXX_SOURCE_RUNS("int main() {return 2;}" CXX_RUN_SHOULD_FAIL)
187 CHECK_CXX_SOURCE_RUNS("int main() {return 0;}" CXX_RUN_SHOULD_WORK)
189 TEST_FAIL(CXX_BUILD_SHOULD_FAIL "CHECK_CXX_SOURCE_COMPILES() succeeded, but should have failed")
190 TEST_ASSERT(CXX_BUILD_SHOULD_WORK "CHECK_CXX_SOURCE_COMPILES() failed")
191 TEST_FAIL(CXX_RUN_SHOULD_FAIL "CHECK_CXX_SOURCE_RUNS() succeeded, but should have failed")
192 TEST_ASSERT(CXX_RUN_SHOULD_WORK "CHECK_CXX_SOURCE_RUNS() failed")