CMakeDetermineCCompiler.cmake 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # determine the compiler to use for C programs
  2. # NOTE, a generator may set CMAKE_C_COMPILER before
  3. # loading this file to force a compiler.
  4. # use environment variable CCC first if defined by user, next use
  5. # the cmake variable CMAKE_GENERATOR_CC which can be defined by a generator
  6. # as a default compiler
  7. IF(NOT CMAKE_C_COMPILER)
  8. SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
  9. # prefer the environment variable CC
  10. IF($ENV{CC} MATCHES ".+")
  11. GET_FILENAME_COMPONENT(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
  12. IF(EXISTS ${CMAKE_C_COMPILER_INIT})
  13. ELSE(EXISTS ${CMAKE_C_COMPILER_INIT})
  14. MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
  15. ENDIF(EXISTS ${CMAKE_C_COMPILER_INIT})
  16. ENDIF($ENV{CC} MATCHES ".+")
  17. # next try prefer the compiler specified by the generator
  18. IF(CMAKE_GENERATOR_CC)
  19. IF(NOT CMAKE_C_COMPILER_INIT)
  20. SET(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
  21. ENDIF(NOT CMAKE_C_COMPILER_INIT)
  22. ENDIF(CMAKE_GENERATOR_CC)
  23. # if no compiler has been specified yet, then look for one
  24. IF(NOT CMAKE_C_COMPILER_INIT)
  25. # if not in the envionment then search for the compiler in the path
  26. SET(CMAKE_C_COMPILER_LIST gcc cc cl bcc xlc)
  27. FIND_PROGRAM(CMAKE_C_COMPILER_FULLPATH NAMES ${CMAKE_C_COMPILER_LIST} )
  28. GET_FILENAME_COMPONENT(CMAKE_C_COMPILER_INIT
  29. ${CMAKE_C_COMPILER_FULLPATH} NAME)
  30. SET(CMAKE_C_COMPILER_FULLPATH "${CMAKE_C_COMPILER_FULLPATH}" CACHE INTERNAL "full path to the compiler cmake found")
  31. ENDIF(NOT CMAKE_C_COMPILER_INIT)
  32. SET(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_INIT} CACHE STRING "C compiler")
  33. ENDIF(NOT CMAKE_C_COMPILER)
  34. MARK_AS_ADVANCED(CMAKE_C_COMPILER)
  35. FIND_PROGRAM(CMAKE_AR NAMES ar PATHS /bin /usr/bin /usr/local/bin)
  36. FIND_PROGRAM(CMAKE_RANLIB NAMES ranlib PATHS /bin /usr/bin /usr/local/bin)
  37. IF(NOT CMAKE_RANLIB)
  38. SET(CMAKE_RANLIB : CACHE INTERNAL "noop for ranlib")
  39. ENDIF(NOT CMAKE_RANLIB)
  40. MARK_AS_ADVANCED(CMAKE_RANLIB)
  41. # test to see if the c compiler is gnu
  42. EXEC_PROGRAM(${CMAKE_C_COMPILER} ARGS -E ${CMAKE_ROOT}/Modules/CMakeTestGNU.c OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RETURN_VALUE CMAKE_COMPILER_RETURN)
  43. IF(NOT CMAKE_COMPILER_RETURN)
  44. IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  45. SET(CMAKE_COMPILER_IS_GNUCC 1)
  46. FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeOutput.log
  47. "Determining if the C compiler is GNU succeeded with "
  48. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  49. ELSE("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  50. FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeOutput.log
  51. "Determining if the C compiler is GNU failed with "
  52. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  53. ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  54. ENDIF(NOT CMAKE_COMPILER_RETURN)
  55. # configure variables set in this file for fast reload later on
  56. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  57. ${CMAKE_BINARY_DIR}/CMakeCCompiler.cmake IMMEDIATE)
  58. MARK_AS_ADVANCED(CMAKE_AR)