annotate cmake/CheckCCompilerFlag.cmake @ 8:c54fe2f77030

made build process more robust
author Robert McIntyre <rlm@mit.edu>
date Thu, 27 Oct 2011 02:40:54 -0700
parents f9476ff7637e
children
rev   line source
rlm@0 1 # - Check if the C source code provided in the SOURCE argument compiles.
rlm@0 2 # CHECK_C_SOURCE_COMPILES(SOURCE VAR)
rlm@0 3 #
rlm@0 4 # FLAG - compiler flag to check
rlm@0 5 # VAR - variable to store whether the source code compiled
rlm@0 6 #
rlm@0 7 # The following variables may be set before calling this macro to
rlm@0 8 # modify the way the check is run:
rlm@0 9 #
rlm@0 10 # CMAKE_REQUIRED_FLAGS = string of compile command line flags
rlm@0 11 # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
rlm@0 12 # CMAKE_REQUIRED_INCLUDES = list of include directories
rlm@0 13 # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
rlm@0 14
rlm@0 15 MACRO(CHECK_C_COMPILER_FLAG FLAG VAR)
rlm@0 16 IF("${VAR}" MATCHES "^${VAR}$")
rlm@0 17 SET(MACRO_CHECK_FUNCTION_DEFINITIONS
rlm@0 18 "${FLAG} ${CMAKE_REQUIRED_FLAGS}")
rlm@0 19 IF(CMAKE_REQUIRED_LIBRARIES)
rlm@0 20 SET(CHECK_C_COMPILER_FLAG_ADD_LIBRARIES
rlm@0 21 "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
rlm@0 22 ELSE(CMAKE_REQUIRED_LIBRARIES)
rlm@0 23 SET(CHECK_C_COMPILER_FLAG_ADD_LIBRARIES)
rlm@0 24 ENDIF(CMAKE_REQUIRED_LIBRARIES)
rlm@0 25 IF(CMAKE_REQUIRED_INCLUDES)
rlm@0 26 SET(CHECK_C_COMPILER_FLAG_ADD_INCLUDES
rlm@0 27 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
rlm@0 28 ELSE(CMAKE_REQUIRED_INCLUDES)
rlm@0 29 SET(CHECK_C_COMPILER_FLAG_ADD_INCLUDES)
rlm@0 30 ENDIF(CMAKE_REQUIRED_INCLUDES)
rlm@0 31 FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
rlm@0 32 "int main() {return 0;}\n")
rlm@0 33
rlm@0 34 MESSAGE(STATUS "Checking if C compiler supports ${FLAG}")
rlm@0 35 TRY_COMPILE(${VAR}
rlm@0 36 ${CMAKE_BINARY_DIR}
rlm@0 37 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
rlm@0 38 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
rlm@0 39 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
rlm@0 40 "${CHECK_C_COMPILER_FLAG_ADD_LIBRARIES}"
rlm@0 41 "${CHECK_C_COMPILER_FLAG_ADD_INCLUDES}"
rlm@0 42 OUTPUT_VARIABLE OUTPUT)
rlm@0 43 IF(${VAR})
rlm@0 44 SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
rlm@0 45 MESSAGE(STATUS "Checking if C compiler supports ${FLAG} - Success")
rlm@0 46 FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
rlm@0 47 "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
rlm@0 48 "${OUTPUT}\n"
rlm@0 49 "Source file was:\n${SOURCE}\n")
rlm@0 50 ELSE(${VAR})
rlm@0 51 MESSAGE(STATUS "Checking if C compiler supports ${FLAG} - Failed")
rlm@0 52 SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
rlm@0 53 FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
rlm@0 54 "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
rlm@0 55 "${OUTPUT}\n"
rlm@0 56 "Source file was:\n${SOURCE}\n")
rlm@0 57 ENDIF(${VAR})
rlm@0 58 ENDIF("${VAR}" MATCHES "^${VAR}$")
rlm@0 59 ENDMACRO(CHECK_C_COMPILER_FLAG)