cmake/Tests/TryCompile/CMakeLists.txt

127 lines
3.9 KiB
CMake
Raw Normal View History

2021-09-14 00:13:48 +02:00
cmake_minimum_required (VERSION 2.8.12)
2022-03-29 21:10:50 +02:00
if(POLICY CMP0129)
cmake_policy(SET CMP0129 NEW)
endif()
2013-03-16 19:13:01 +02:00
project(TryCompile)
2022-11-16 20:14:03 +01:00
macro(EXPECT_PASS var out)
if(NOT ${var})
message(SEND_ERROR "Should pass failed:\n${out}")
endif()
2013-03-16 19:13:01 +02:00
endmacro()
2022-11-16 20:14:03 +01:00
macro(EXPECT_FAIL var out)
if(${var})
message(SEND_ERROR "Should fail passed:\n${out}")
endif()
2013-03-16 19:13:01 +02:00
endmacro()
2022-11-16 20:14:03 +01:00
macro(EXPECT_COMPILED name var out)
if(NOT ${var})
message(SEND_ERROR "${name} failed compiling:\n${out}")
2013-03-16 19:13:01 +02:00
endif()
endmacro()
2022-11-16 20:14:03 +01:00
macro(EXPECT_RUN_RESULT name var expected)
if(NOT ${var} EQUAL ${expected})
message(SEND_ERROR " ${name} gave unexpected run result: ${${var}} expected: ${expected}")
2013-03-16 19:13:01 +02:00
endif()
endmacro()
2022-11-16 20:14:03 +01:00
macro(TEST_ASSERT value msg)
if (NOT ${value})
message (SEND_ERROR "Assertion failure:" ${msg} )
endif ()
endmacro()
2022-11-16 20:14:03 +01:00
# run old signature tests
set(try_compile_bindir_or_SOURCES ${TryCompile_BINARY_DIR})
set(try_compile_redundant_SOURCES SOURCES)
set(try_compile_output_vars OUTPUT_VARIABLE TRY_OUT)
set(try_compile_compile_output_var TRY_OUT)
set(try_compile_run_output_var TRY_OUT)
include(old_and_new_signature_tests.cmake)
# run new signature tests
set(try_compile_bindir_or_SOURCES SOURCES)
set(try_compile_redundant_SOURCES "")
set(try_compile_output_vars
COMPILE_OUTPUT_VARIABLE COMPILE_OUT
RUN_OUTPUT_VARIABLE RUN_OUTPUT)
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_CONTENT 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
2013-11-03 12:27:13 +02:00
try_compile(SHOULD_PASS
2022-11-16 20:14:03 +01:00
SOURCE_FROM_FILE pass.c ${TryCompile_SOURCE_DIR}/pass.c
OUTPUT_VARIABLE TRY_OUT)
EXPECT_COMPILED("SOURCE_FROM_FILE" SHOULD_PASS "${TRY_OUT}")
2013-03-16 19:13:01 +02:00
2022-11-16 20:14:03 +01:00
# 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")
2013-03-16 19:13:01 +02:00
2022-11-16 20:14:03 +01:00
try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
SOURCE_FROM_CONTENT main.c "${TRY_RUN_MAIN_CODE}"
SOURCE_FROM_CONTENT answer.c "${TRY_RUN_EXT_CODE}"
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT)
EXPECT_COMPILED("SOURCE_FROM_CONTENT" SHOULD_COMPILE "${COMPILE_OUTPUT}")
EXPECT_RUN_RESULT("SOURCE_FROM_CONTENT" SHOULD_EXIT_WITH_ERROR 42)
2013-03-16 19:13:01 +02:00
2022-11-16 20:14:03 +01:00
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)")
2013-03-16 19:13:01 +02:00
try_compile(TEST_INNER
2009-10-04 10:30:41 +03:00
${TryCompile_BINARY_DIR}/CMakeFiles/Inner
${TryCompile_SOURCE_DIR}/Inner
TryCompileInner innerexe
OUTPUT_VARIABLE output)
TEST_ASSERT(TEST_INNER "try_compile project mode failed:\n${output}")
2022-11-16 20:14:03 +01:00
# try to compile a project (new signature)
message("Testing try_compile project mode (new signature)")
try_compile(TEST_INNER
PROJECT TryCompileInner
SOURCE_DIR ${TryCompile_SOURCE_DIR}/Inner
TARGET innerexe
OUTPUT_VARIABLE output)
TEST_ASSERT(TEST_INNER "try_compile project mode failed:\n${output}")
2019-11-11 23:01:05 +01:00
2013-03-16 19:13:01 +02:00
add_executable(TryCompile pass.c)
2011-06-19 15:41:06 +03:00
#######################################################################
#
# also test that the check_prototype_definition macro works
include(CheckPrototypeDefinition)
check_prototype_definition(remove
"int remove(const char *pathname)"
"0"
"stdio.h"
TEST_REMOVE_PROTO)
test_assert(TEST_REMOVE_PROTO "check_prototype_definition for remove() failed")