CMake/Modules/CheckCXXSourceCompiles.cmake

52 lines
2.1 KiB
CMake
Raw Normal View History

# - Check if the source code provided in the SOURCE argument compiles.
# CHECK_CXX_SOURCE_COMPILES(SOURCE VAR)
# - macro which checks if the source code compiles\
2005-12-15 18:41:19 +03:00
# SOURCE - source code to try to compile
# VAR - variable to store size if the type exists.
2005-08-01 07:05:05 +04:00
# Checks the following optional VARIABLES (not arguments)
# CMAKE_REQUIRED_LIBRARIES - Link to extra libraries
2005-12-15 18:41:19 +03:00
# CMAKE_REQUIRED_FLAGS - Extra flags to C compiler
2005-08-01 07:05:05 +04:00
#
MACRO(CHECK_CXX_SOURCE_COMPILES SOURCE VAR)
IF("${VAR}" MATCHES "^${VAR}$")
SET(MACRO_CHECK_FUNCTION_DEFINITIONS
"-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
IF(CMAKE_REQUIRED_LIBRARIES)
SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
"-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
ENDIF(CMAKE_REQUIRED_LIBRARIES)
IF(CMAKE_REQUIRED_INCLUDES)
SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
"-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
ENDIF(CMAKE_REQUIRED_INCLUDES)
2006-01-12 21:49:32 +03:00
FILE(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/src.c"
2005-08-01 07:05:05 +04:00
"${SOURCE}")
MESSAGE(STATUS "Performing Test ${VAR}")
TRY_COMPILE(${VAR}
${CMAKE_BINARY_DIR}
2006-01-12 21:49:32 +03:00
${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/src.cxx
2005-08-01 07:05:05 +04:00
CMAKE_FLAGS
"${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}"
"${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
OUTPUT_VARIABLE OUTPUT)
IF(${VAR})
SET(${VAR} 1 CACHE INTERNAL "Test ${FUNCTION}")
MESSAGE(STATUS "Performing Test ${VAR} - Success")
2006-01-12 21:49:32 +03:00
WRITE_FILE(${CMAKE_BINARY_DIR}/CMakeFiles/CMakeOutput.log
2005-08-01 07:05:05 +04:00
"Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
"${OUTPUT}\n"
"Source file was:\n${SOURCE}\n" APPEND)
ELSE(${VAR})
MESSAGE(STATUS "Performing Test ${VAR} - Failed")
SET(${VAR} "" CACHE INTERNAL "Test ${FUNCTION}")
2006-01-12 21:49:32 +03:00
WRITE_FILE(${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log
2005-08-01 07:05:05 +04:00
"Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
"${OUTPUT}\n"
"Source file was:\n${SOURCE}\n" APPEND)
ENDIF(${VAR})
ENDIF("${VAR}" MATCHES "^${VAR}$")
ENDMACRO(CHECK_CXX_SOURCE_COMPILES)