CMake/Modules/CheckCXXSourceCompiles.cmake

56 lines
2.2 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 15:41:19 +00:00
# SOURCE - source code to try to compile
# VAR - variable to store size if the type exists.
2005-08-01 03:05:05 +00:00
# Checks the following optional VARIABLES (not arguments)
# CMAKE_REQUIRED_LIBRARIES - Link to extra libraries
2005-12-15 15:41:19 +00:00
# CMAKE_REQUIRED_FLAGS - Extra flags to C compiler
2005-08-01 03:05:05 +00: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}")
ELSE(CMAKE_REQUIRED_LIBRARIES)
SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
2005-08-01 03:05:05 +00:00
ENDIF(CMAKE_REQUIRED_LIBRARIES)
IF(CMAKE_REQUIRED_INCLUDES)
SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
"-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
ELSE(CMAKE_REQUIRED_INCLUDES)
SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
2005-08-01 03:05:05 +00:00
ENDIF(CMAKE_REQUIRED_INCLUDES)
2006-01-24 17:07:45 +00:00
FILE(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/src.cxx"
2005-08-01 03:05:05 +00:00
"${SOURCE}")
MESSAGE(STATUS "Performing Test ${VAR}")
TRY_COMPILE(${VAR}
${CMAKE_BINARY_DIR}
2006-01-12 18:49:32 +00:00
${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/src.cxx
2005-08-01 03:05:05 +00: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 18:49:32 +00:00
WRITE_FILE(${CMAKE_BINARY_DIR}/CMakeFiles/CMakeOutput.log
2005-08-01 03:05:05 +00: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 18:49:32 +00:00
WRITE_FILE(${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log
2005-08-01 03:05:05 +00: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)