CMakeDetermineCompiler.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ${_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)
  32. foreach(l ${_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. find_program(CMAKE_${lang}_COMPILER NAMES ${CMAKE_${lang}_COMPILER_LIST} DOC "${lang} compiler")
  52. if(CMAKE_${lang}_COMPILER_INIT AND NOT CMAKE_${lang}_COMPILER)
  53. set_property(CACHE CMAKE_${lang}_COMPILER PROPERTY VALUE "${CMAKE_${lang}_COMPILER_INIT}")
  54. endif()
  55. unset(_${lang}_COMPILER_HINTS)
  56. unset(_languages)
  57. # Look for a make tool provided by Xcode
  58. if(CMAKE_HOST_APPLE)
  59. macro(_query_xcrun compiler_name result_var_keyword result_var)
  60. if(NOT "x${result_var_keyword}" STREQUAL "xRESULT_VAR")
  61. message(FATAL_ERROR "Bad arguments to macro")
  62. endif()
  63. execute_process(COMMAND xcrun --find ${compiler_name}
  64. OUTPUT_VARIABLE _xcrun_out OUTPUT_STRIP_TRAILING_WHITESPACE
  65. ERROR_VARIABLE _xcrun_err)
  66. set("${result_var}" "${_xcrun_out}")
  67. endmacro()
  68. set(xcrun_result)
  69. if (CMAKE_${lang}_COMPILER MATCHES "^/usr/bin/(.+)$")
  70. _query_xcrun("${CMAKE_MATCH_1}" RESULT_VAR xcrun_result)
  71. elseif (CMAKE_${lang}_COMPILER STREQUAL "CMAKE_${lang}_COMPILER-NOTFOUND")
  72. foreach(comp ${CMAKE_${lang}_COMPILER_LIST})
  73. _query_xcrun("${comp}" RESULT_VAR xcrun_result)
  74. if(xcrun_result)
  75. break()
  76. endif()
  77. endforeach()
  78. endif()
  79. if (xcrun_result)
  80. set_property(CACHE CMAKE_${lang}_COMPILER PROPERTY VALUE "${xcrun_result}")
  81. endif()
  82. endif()
  83. endmacro()
  84. macro(_cmake_find_compiler_path lang)
  85. if(CMAKE_${lang}_COMPILER)
  86. # we only get here if CMAKE_${lang}_COMPILER was specified using -D or a pre-made CMakeCache.txt
  87. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  88. # if CMAKE_${lang}_COMPILER is a list of length 2, use the first item as
  89. # CMAKE_${lang}_COMPILER and the 2nd one as CMAKE_${lang}_COMPILER_ARG1
  90. list(LENGTH CMAKE_${lang}_COMPILER _CMAKE_${lang}_COMPILER_LIST_LENGTH)
  91. if("${_CMAKE_${lang}_COMPILER_LIST_LENGTH}" EQUAL 2)
  92. list(GET CMAKE_${lang}_COMPILER 1 CMAKE_${lang}_COMPILER_ARG1)
  93. list(GET CMAKE_${lang}_COMPILER 0 CMAKE_${lang}_COMPILER)
  94. endif()
  95. unset(_CMAKE_${lang}_COMPILER_LIST_LENGTH)
  96. # find the compiler in the PATH if necessary
  97. get_filename_component(_CMAKE_USER_${lang}_COMPILER_PATH "${CMAKE_${lang}_COMPILER}" PATH)
  98. if(NOT _CMAKE_USER_${lang}_COMPILER_PATH)
  99. find_program(CMAKE_${lang}_COMPILER_WITH_PATH NAMES ${CMAKE_${lang}_COMPILER})
  100. if(CMAKE_${lang}_COMPILER_WITH_PATH)
  101. set(CMAKE_${lang}_COMPILER ${CMAKE_${lang}_COMPILER_WITH_PATH})
  102. get_property(_CMAKE_${lang}_COMPILER_CACHED CACHE CMAKE_${lang}_COMPILER PROPERTY TYPE)
  103. if(_CMAKE_${lang}_COMPILER_CACHED)
  104. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE STRING "${lang} compiler" FORCE)
  105. endif()
  106. unset(_CMAKE_${lang}_COMPILER_CACHED)
  107. endif()
  108. unset(CMAKE_${lang}_COMPILER_WITH_PATH CACHE)
  109. endif()
  110. endif()
  111. endmacro()