CMakeDetermineCompiler.cmake 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #=============================================================================
  2. # Copyright 2004-2012 Kitware, Inc.
  3. #
  4. # Distributed under the OSI-approved BSD License (the "License");
  5. # see accompanying file Copyright.txt for details.
  6. #
  7. # This software is distributed WITHOUT ANY WARRANTY; without even the
  8. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the License for more information.
  10. #=============================================================================
  11. # (To distribute this file outside of CMake, substitute the full
  12. # License text for the above reference.)
  13. macro(_cmake_find_compiler lang)
  14. # Use already-enabled languages for reference.
  15. get_property(_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  16. list(REMOVE_ITEM _languages "${lang}")
  17. if(CMAKE_${lang}_COMPILER_INIT)
  18. # Search only for the specified compiler.
  19. set(CMAKE_${lang}_COMPILER_LIST "${CMAKE_${lang}_COMPILER_INIT}")
  20. else()
  21. # Re-order the compiler list with preferred vendors first.
  22. set(_${lang}_COMPILER_LIST "${CMAKE_${lang}_COMPILER_LIST}")
  23. set(CMAKE_${lang}_COMPILER_LIST "")
  24. # Prefer vendors of compilers from reference languages.
  25. foreach(l ${_languages})
  26. list(APPEND CMAKE_${lang}_COMPILER_LIST
  27. ${_${lang}_COMPILER_NAMES_${CMAKE_${l}_COMPILER_ID}})
  28. endforeach()
  29. # Prefer vendors based on the platform.
  30. list(APPEND CMAKE_${lang}_COMPILER_LIST ${CMAKE_${lang}_COMPILER_NAMES})
  31. # Append the rest of the list and remove duplicates.
  32. list(APPEND CMAKE_${lang}_COMPILER_LIST ${_${lang}_COMPILER_LIST})
  33. unset(_${lang}_COMPILER_LIST)
  34. list(REMOVE_DUPLICATES CMAKE_${lang}_COMPILER_LIST)
  35. if(CMAKE_${lang}_COMPILER_EXCLUDE)
  36. list(REMOVE_ITEM CMAKE_${lang}_COMPILER_LIST
  37. ${CMAKE_${lang}_COMPILER_EXCLUDE})
  38. endif()
  39. endif()
  40. # Look for directories containing compilers of reference languages.
  41. set(_${lang}_COMPILER_HINTS)
  42. foreach(l ${_languages})
  43. if(CMAKE_${l}_COMPILER AND IS_ABSOLUTE "${CMAKE_${l}_COMPILER}")
  44. get_filename_component(_hint "${CMAKE_${l}_COMPILER}" PATH)
  45. if(IS_DIRECTORY "${_hint}")
  46. list(APPEND _${lang}_COMPILER_HINTS "${_hint}")
  47. endif()
  48. unset(_hint)
  49. endif()
  50. endforeach()
  51. # Find the compiler.
  52. if(_${lang}_COMPILER_HINTS)
  53. # Prefer directories containing compilers of reference languages.
  54. list(REMOVE_DUPLICATES _${lang}_COMPILER_HINTS)
  55. find_program(CMAKE_${lang}_COMPILER
  56. NAMES ${CMAKE_${lang}_COMPILER_LIST}
  57. PATHS ${_${lang}_COMPILER_HINTS}
  58. NO_DEFAULT_PATH
  59. DOC "${lang} compiler")
  60. endif()
  61. find_program(CMAKE_${lang}_COMPILER NAMES ${CMAKE_${lang}_COMPILER_LIST} DOC "${lang} compiler")
  62. if(CMAKE_${lang}_COMPILER_INIT AND NOT CMAKE_${lang}_COMPILER)
  63. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER_INIT}" CACHE FILEPATH "${lang} compiler" FORCE)
  64. endif()
  65. unset(_${lang}_COMPILER_HINTS)
  66. unset(_languages)
  67. endmacro()