CMakeDetermineCompiler.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. macro(_cmake_find_compiler lang)
  4. # Use already-enabled languages for reference.
  5. get_property(_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  6. list(REMOVE_ITEM _languages "${lang}")
  7. if(CMAKE_${lang}_COMPILER_INIT)
  8. # Search only for the specified compiler.
  9. set(CMAKE_${lang}_COMPILER_LIST "${CMAKE_${lang}_COMPILER_INIT}")
  10. else()
  11. # Re-order the compiler list with preferred vendors first.
  12. set(_${lang}_COMPILER_LIST "${CMAKE_${lang}_COMPILER_LIST}")
  13. set(CMAKE_${lang}_COMPILER_LIST "")
  14. # Prefer vendors of compilers from reference languages.
  15. foreach(l IN LISTS _languages)
  16. list(APPEND CMAKE_${lang}_COMPILER_LIST
  17. ${_${lang}_COMPILER_NAMES_${CMAKE_${l}_COMPILER_ID}})
  18. endforeach()
  19. # Prefer vendors based on the platform.
  20. list(APPEND CMAKE_${lang}_COMPILER_LIST ${CMAKE_${lang}_COMPILER_NAMES})
  21. # Append the rest of the list and remove duplicates.
  22. list(APPEND CMAKE_${lang}_COMPILER_LIST ${_${lang}_COMPILER_LIST})
  23. unset(_${lang}_COMPILER_LIST)
  24. list(REMOVE_DUPLICATES CMAKE_${lang}_COMPILER_LIST)
  25. if(CMAKE_${lang}_COMPILER_EXCLUDE)
  26. list(REMOVE_ITEM CMAKE_${lang}_COMPILER_LIST
  27. ${CMAKE_${lang}_COMPILER_EXCLUDE})
  28. endif()
  29. endif()
  30. # Look for directories containing compilers of reference languages.
  31. set(_${lang}_COMPILER_HINTS "${CMAKE_${lang}_COMPILER_HINTS}")
  32. foreach(l IN LISTS _languages)
  33. if(CMAKE_${l}_COMPILER AND IS_ABSOLUTE "${CMAKE_${l}_COMPILER}")
  34. get_filename_component(_hint "${CMAKE_${l}_COMPILER}" PATH)
  35. if(IS_DIRECTORY "${_hint}")
  36. list(APPEND _${lang}_COMPILER_HINTS "${_hint}")
  37. endif()
  38. unset(_hint)
  39. endif()
  40. endforeach()
  41. # Find the compiler.
  42. if(_${lang}_COMPILER_HINTS)
  43. # Prefer directories containing compilers of reference languages.
  44. list(REMOVE_DUPLICATES _${lang}_COMPILER_HINTS)
  45. find_program(CMAKE_${lang}_COMPILER
  46. NAMES ${CMAKE_${lang}_COMPILER_LIST}
  47. PATHS ${_${lang}_COMPILER_HINTS}
  48. NO_DEFAULT_PATH
  49. DOC "${lang} compiler")
  50. endif()
  51. if(CMAKE_HOST_WIN32 AND CMAKE_GENERATOR MATCHES "Ninja|MSYS Makefiles|MinGW Makefiles")
  52. # On Windows command-line builds, some generators imply a preferred compiler tool.
  53. # These generators do not, so use the compiler that occurs first in PATH.
  54. find_program(CMAKE_${lang}_COMPILER
  55. NAMES ${CMAKE_${lang}_COMPILER_LIST}
  56. NAMES_PER_DIR
  57. DOC "${lang} compiler"
  58. NO_PACKAGE_ROOT_PATH
  59. NO_CMAKE_PATH
  60. NO_CMAKE_ENVIRONMENT_PATH
  61. NO_CMAKE_SYSTEM_PATH
  62. )
  63. endif()
  64. find_program(CMAKE_${lang}_COMPILER NAMES ${CMAKE_${lang}_COMPILER_LIST} DOC "${lang} compiler")
  65. if(_CMAKE_${lang}_COMPILER_PATHS)
  66. # As a last fall-back, search in language-specific paths
  67. find_program(CMAKE_${lang}_COMPILER
  68. NAMES ${CMAKE_${lang}_COMPILER_LIST}
  69. NAMES_PER_DIR
  70. PATHS ${_CMAKE_${lang}_COMPILER_PATHS}
  71. DOC "${lang} compiler"
  72. NO_DEFAULT_PATH
  73. )
  74. endif()
  75. if(CMAKE_${lang}_COMPILER_INIT AND NOT CMAKE_${lang}_COMPILER)
  76. set_property(CACHE CMAKE_${lang}_COMPILER PROPERTY VALUE "${CMAKE_${lang}_COMPILER_INIT}")
  77. endif()
  78. unset(_${lang}_COMPILER_HINTS)
  79. unset(_languages)
  80. # Look for a make tool provided by Xcode
  81. if(CMAKE_HOST_APPLE)
  82. macro(_query_xcrun compiler_name result_var_keyword result_var)
  83. if(NOT "x${result_var_keyword}" STREQUAL "xRESULT_VAR")
  84. message(FATAL_ERROR "Bad arguments to macro")
  85. endif()
  86. execute_process(COMMAND xcrun --find ${compiler_name}
  87. OUTPUT_VARIABLE _xcrun_out OUTPUT_STRIP_TRAILING_WHITESPACE
  88. ERROR_VARIABLE _xcrun_err)
  89. set("${result_var}" "${_xcrun_out}")
  90. endmacro()
  91. set(xcrun_result)
  92. if (CMAKE_${lang}_COMPILER MATCHES "^/usr/bin/(.+)$")
  93. _query_xcrun("${CMAKE_MATCH_1}" RESULT_VAR xcrun_result)
  94. elseif (CMAKE_${lang}_COMPILER STREQUAL "CMAKE_${lang}_COMPILER-NOTFOUND")
  95. foreach(comp IN LISTS CMAKE_${lang}_COMPILER_LIST)
  96. _query_xcrun("${comp}" RESULT_VAR xcrun_result)
  97. if(xcrun_result)
  98. break()
  99. endif()
  100. endforeach()
  101. endif()
  102. if (xcrun_result)
  103. set_property(CACHE CMAKE_${lang}_COMPILER PROPERTY VALUE "${xcrun_result}")
  104. endif()
  105. endif()
  106. endmacro()
  107. macro(_cmake_find_compiler_path lang)
  108. if(CMAKE_${lang}_COMPILER)
  109. # we only get here if CMAKE_${lang}_COMPILER was specified using -D or a pre-made CMakeCache.txt
  110. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  111. # if CMAKE_${lang}_COMPILER is a list, use the first item as
  112. # CMAKE_${lang}_COMPILER and the rest as CMAKE_${lang}_COMPILER_ARG1
  113. # Otherwise, preserve any existing CMAKE_${lang}_COMPILER_ARG1 that might
  114. # have been saved by CMakeDetermine${lang}Compiler in a previous run.
  115. list(LENGTH CMAKE_${lang}_COMPILER _CMAKE_${lang}_COMPILER_LENGTH)
  116. if(_CMAKE_${lang}_COMPILER_LENGTH GREATER 1)
  117. set(CMAKE_${lang}_COMPILER_ARG1 "${CMAKE_${lang}_COMPILER}")
  118. list(POP_FRONT CMAKE_${lang}_COMPILER_ARG1 CMAKE_${lang}_COMPILER)
  119. list(JOIN CMAKE_${lang}_COMPILER_ARG1 " " CMAKE_${lang}_COMPILER_ARG1)
  120. endif()
  121. unset(_CMAKE_${lang}_COMPILER_LENGTH)
  122. # find the compiler in the PATH if necessary
  123. # if compiler (and arguments) comes from cache then synchronize cache with updated CMAKE_<LANG>_COMPILER
  124. get_filename_component(_CMAKE_USER_${lang}_COMPILER_PATH "${CMAKE_${lang}_COMPILER}" PATH)
  125. if(NOT _CMAKE_USER_${lang}_COMPILER_PATH)
  126. find_program(CMAKE_${lang}_COMPILER_WITH_PATH NAMES ${CMAKE_${lang}_COMPILER})
  127. if(CMAKE_${lang}_COMPILER_WITH_PATH)
  128. set(CMAKE_${lang}_COMPILER ${CMAKE_${lang}_COMPILER_WITH_PATH})
  129. get_property(_CMAKE_${lang}_COMPILER_CACHED CACHE CMAKE_${lang}_COMPILER PROPERTY TYPE)
  130. if(_CMAKE_${lang}_COMPILER_CACHED)
  131. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE STRING "${lang} compiler" FORCE)
  132. endif()
  133. unset(_CMAKE_${lang}_COMPILER_CACHED)
  134. endif()
  135. unset(CMAKE_${lang}_COMPILER_WITH_PATH CACHE)
  136. elseif (EXISTS ${CMAKE_${lang}_COMPILER})
  137. get_property(_CMAKE_${lang}_COMPILER_CACHED CACHE CMAKE_${lang}_COMPILER PROPERTY TYPE)
  138. if(_CMAKE_${lang}_COMPILER_CACHED)
  139. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE STRING "${lang} compiler" FORCE)
  140. endif()
  141. unset(_CMAKE_${lang}_COMPILER_CACHED)
  142. endif()
  143. endif()
  144. endmacro()
  145. function(_cmake_find_compiler_sysroot lang)
  146. if(CMAKE_${lang}_COMPILER_ID STREQUAL "GNU" OR CMAKE_${lang}_COMPILER_ID STREQUAL "LCC")
  147. execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -print-sysroot
  148. OUTPUT_STRIP_TRAILING_WHITESPACE
  149. OUTPUT_VARIABLE _cmake_sysroot_run_out
  150. ERROR_VARIABLE _cmake_sysroot_run_err)
  151. if(_cmake_sysroot_run_out AND NOT _cmake_sysroot_run_err
  152. AND NOT _cmake_sysroot_run_out STREQUAL "/"
  153. AND IS_DIRECTORY "${_cmake_sysroot_run_out}/usr")
  154. file(TO_CMAKE_PATH "${_cmake_sysroot_run_out}/usr" _cmake_sysroot_run_out_usr)
  155. set(CMAKE_${lang}_COMPILER_SYSROOT "${_cmake_sysroot_run_out_usr}" PARENT_SCOPE)
  156. else()
  157. set(CMAKE_${lang}_COMPILER_SYSROOT "" PARENT_SCOPE)
  158. endif()
  159. endif()
  160. endfunction()