CheckLanguage.cmake 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckLanguage
  5. -------------
  6. Check if a language can be enabled
  7. Usage:
  8. ::
  9. check_language(<lang>)
  10. where ``<lang>`` is a language that may be passed to :command:`enable_language`
  11. such as ``Fortran``. If :variable:`CMAKE_<LANG>_COMPILER` is already defined
  12. the check does nothing. Otherwise it tries enabling the language in a
  13. test project. The result is cached in :variable:`CMAKE_<LANG>_COMPILER`
  14. as the compiler that was found, or ``NOTFOUND`` if the language cannot be
  15. enabled.
  16. Example:
  17. ::
  18. check_language(Fortran)
  19. if(CMAKE_Fortran_COMPILER)
  20. enable_language(Fortran)
  21. else()
  22. message(STATUS "No Fortran support")
  23. endif()
  24. #]=======================================================================]
  25. include_guard(GLOBAL)
  26. macro(check_language lang)
  27. if(NOT DEFINED CMAKE_${lang}_COMPILER)
  28. set(_desc "Looking for a ${lang} compiler")
  29. message(STATUS ${_desc})
  30. file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang})
  31. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/CMakeLists.txt"
  32. "cmake_minimum_required(VERSION ${CMAKE_VERSION})
  33. project(Check${lang} ${lang})
  34. file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\"
  35. \"set(CMAKE_${lang}_COMPILER \\\"\${CMAKE_${lang}_COMPILER}\\\")\\n\"
  36. )
  37. ")
  38. if(CMAKE_GENERATOR_INSTANCE)
  39. set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE:INTERNAL=${CMAKE_GENERATOR_INSTANCE}")
  40. else()
  41. set(_D_CMAKE_GENERATOR_INSTANCE "")
  42. endif()
  43. execute_process(
  44. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}
  45. COMMAND ${CMAKE_COMMAND} . -G ${CMAKE_GENERATOR}
  46. -A "${CMAKE_GENERATOR_PLATFORM}"
  47. -T "${CMAKE_GENERATOR_TOOLSET}"
  48. ${_D_CMAKE_GENERATOR_INSTANCE}
  49. OUTPUT_VARIABLE output
  50. ERROR_VARIABLE output
  51. RESULT_VARIABLE result
  52. )
  53. include(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/result.cmake OPTIONAL)
  54. if(CMAKE_${lang}_COMPILER AND "${result}" STREQUAL "0")
  55. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  56. "${_desc} passed with the following output:\n"
  57. "${output}\n")
  58. else()
  59. set(CMAKE_${lang}_COMPILER NOTFOUND)
  60. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  61. "${_desc} failed with the following output:\n"
  62. "${output}\n")
  63. endif()
  64. message(STATUS "${_desc} - ${CMAKE_${lang}_COMPILER}")
  65. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE FILEPATH "${lang} compiler")
  66. mark_as_advanced(CMAKE_${lang}_COMPILER)
  67. endif()
  68. endmacro()