CMakeDetermineCCompiler.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 CC 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 the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
  8. # as prefix for the tools (e.g. arm-elf-gcc, arm-elf-ar etc.). This works
  9. # currently with the GNU crosscompilers.
  10. # It also tries to detect a MS crosscompiler and find out its
  11. # suffix (clarm.exe), which will be stored in _CMAKE_TOOLCHAIN_SUFFIX and
  12. # reused for the CXX compiler.
  13. #
  14. #
  15. # Sets the following variables:
  16. # CMAKE_C_COMPILER
  17. # CMAKE_AR
  18. # CMAKE_RANLIB
  19. # CMAKE_COMPILER_IS_GNUCC
  20. #
  21. # If not already set before, it also sets
  22. # _CMAKE_TOOLCHAIN_PREFIX
  23. # _CMAKE_TOOLCHAIN_SUFFIX
  24. IF(NOT CMAKE_C_COMPILER)
  25. SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
  26. # prefer the environment variable CC
  27. IF($ENV{CC} MATCHES ".+")
  28. GET_FILENAME_COMPONENT(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
  29. IF(CMAKE_C_FLAGS_ENV_INIT)
  30. SET(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler")
  31. ENDIF(CMAKE_C_FLAGS_ENV_INIT)
  32. IF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
  33. MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
  34. ENDIF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
  35. ENDIF($ENV{CC} MATCHES ".+")
  36. # next try prefer the compiler specified by the generator
  37. IF(CMAKE_GENERATOR_CC)
  38. IF(NOT CMAKE_C_COMPILER_INIT)
  39. SET(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
  40. ENDIF(NOT CMAKE_C_COMPILER_INIT)
  41. ENDIF(CMAKE_GENERATOR_CC)
  42. # finally list compilers to try
  43. IF(CMAKE_C_COMPILER_INIT)
  44. SET(CMAKE_C_COMPILER_LIST ${CMAKE_C_COMPILER_INIT})
  45. ELSE(CMAKE_C_COMPILER_INIT)
  46. SET(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}gcc ${_CMAKE_TOOLCHAIN_PREFIX}cc cl${_CMAKE_TOOLCHAIN_SUFFIX} bcc xlc)
  47. ENDIF(CMAKE_C_COMPILER_INIT)
  48. # Find the compiler.
  49. IF (_CMAKE_USER_CXX_COMPILER_PATH)
  50. FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} PATHS ${_CMAKE_USER_CXX_COMPILER_PATH} DOC "C compiler" NO_DEFAULT_PATH)
  51. ENDIF (_CMAKE_USER_CXX_COMPILER_PATH)
  52. FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} DOC "C compiler")
  53. IF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
  54. SET(CMAKE_C_COMPILER "${CMAKE_C_COMPILER_INIT}" CACHE FILEPATH "C compiler" FORCE)
  55. ENDIF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
  56. ELSE(NOT CMAKE_C_COMPILER)
  57. # we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt
  58. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  59. #
  60. # if a compiler was specified by the user but without path,
  61. # now try to find it with the full path
  62. # if it is found, force it into the cache,
  63. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  64. # if the C compiler already had a path, reuse it for searching the CXX compiler
  65. GET_FILENAME_COMPONENT(_CMAKE_USER_C_COMPILER_PATH "${CMAKE_C_COMPILER}" PATH)
  66. IF(NOT _CMAKE_USER_C_COMPILER_PATH)
  67. FIND_PROGRAM(CMAKE_C_COMPILER_WITH_PATH NAMES ${CMAKE_C_COMPILER})
  68. MARK_AS_ADVANCED(CMAKE_C_COMPILER_WITH_PATH)
  69. IF(CMAKE_C_COMPILER_WITH_PATH)
  70. SET(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE FILEPATH "C compiler" FORCE)
  71. ENDIF(CMAKE_C_COMPILER_WITH_PATH)
  72. ENDIF(NOT _CMAKE_USER_C_COMPILER_PATH)
  73. ENDIF(NOT CMAKE_C_COMPILER)
  74. MARK_AS_ADVANCED(CMAKE_C_COMPILER)
  75. IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  76. GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
  77. ENDIF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  78. # if we have a gcc cross compiler, they have usually some prefix, like
  79. # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc
  80. # the other tools of the toolchain usually have the same prefix
  81. IF (NOT _CMAKE_TOOLCHAIN_PREFIX)
  82. GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME_WE)
  83. IF (COMPILER_BASENAME MATCHES "^(.+-)g?cc")
  84. STRING(REGEX REPLACE "^(.+-)g?cc" "\\1" _CMAKE_TOOLCHAIN_PREFIX "${COMPILER_BASENAME}")
  85. ENDIF (COMPILER_BASENAME MATCHES "^(.+-)g?cc")
  86. ENDIF (NOT _CMAKE_TOOLCHAIN_PREFIX)
  87. # if we have a MS cross compiler, it usually has a suffix, like
  88. # e.g. clarm.exe or clmips.exe. Use this suffix for the CXX compiler too.
  89. # the same is true e.g. for the IAR cross compiler, which is "icc<suffix>"
  90. IF (NOT _CMAKE_TOOLCHAIN_SUFFIX)
  91. GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  92. IF (COMPILER_BASENAME MATCHES "^cl(.+)\\.exe$")
  93. STRING(REGEX REPLACE "^cl(.+)\\.exe$" "\\1" _CMAKE_TOOLCHAIN_SUFFIX "${COMPILER_BASENAME}")
  94. ENDIF (COMPILER_BASENAME MATCHES "^cl(.+)\\.exe$")
  95. ENDIF (NOT _CMAKE_TOOLCHAIN_SUFFIX)
  96. # some exotic compilers have different extensions (e.g. sdcc uses .rel)
  97. # so don't overwrite it if it has been already defined by the user
  98. IF(NOT CMAKE_C_OUTPUT_EXTENSION)
  99. IF(UNIX)
  100. SET(CMAKE_C_OUTPUT_EXTENSION .o)
  101. ELSE(UNIX)
  102. SET(CMAKE_C_OUTPUT_EXTENSION .obj)
  103. ENDIF(UNIX)
  104. ENDIF(NOT CMAKE_C_OUTPUT_EXTENSION)
  105. # Build a small source file to identify the compiler.
  106. IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  107. SET(CMAKE_C_COMPILER_ID_RUN 1)
  108. SET(CMAKE_C_PLATFORM_ID "Windows")
  109. # TODO: Set the compiler id. It is probably MSVC but
  110. # the user may be using an integrated Intel compiler.
  111. # SET(CMAKE_C_COMPILER_ID "MSVC")
  112. ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  113. IF(NOT CMAKE_C_COMPILER_ID_RUN)
  114. SET(CMAKE_C_COMPILER_ID_RUN 1)
  115. # Try to identify the compiler.
  116. SET(CMAKE_C_COMPILER_ID)
  117. INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  118. CMAKE_DETERMINE_COMPILER_ID(C CFLAGS ${CMAKE_ROOT}/Modules/CMakeCCompilerId.c)
  119. # Set old compiler and platform id variables.
  120. IF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  121. SET(CMAKE_COMPILER_IS_GNUCC 1)
  122. ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  123. IF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
  124. SET(CMAKE_COMPILER_IS_MINGW 1)
  125. ELSEIF("${CMAKE_C_PLATFORM_ID}" MATCHES "Cygwin")
  126. SET(CMAKE_COMPILER_IS_CYGWIN 1)
  127. ENDIF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
  128. ENDIF(NOT CMAKE_C_COMPILER_ID_RUN)
  129. INCLUDE(CMakeFindBinUtils)
  130. # configure variables set in this file for fast reload later on
  131. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  132. "${CMAKE_PLATFORM_ROOT_BIN}/CMakeCCompiler.cmake" IMMEDIATE)
  133. SET(CMAKE_C_COMPILER_ENV_VAR "CC")