CheckLanguage.cmake 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. For CUDA which can have an explicit host compiler, the cache
  16. :variable:`CMAKE_CUDA_HOST_COMPILER` variable will be set if it was required
  17. for compilation.
  18. Example:
  19. ::
  20. check_language(Fortran)
  21. if(CMAKE_Fortran_COMPILER)
  22. enable_language(Fortran)
  23. else()
  24. message(STATUS "No Fortran support")
  25. endif()
  26. #]=======================================================================]
  27. include_guard(GLOBAL)
  28. macro(check_language lang)
  29. if(NOT DEFINED CMAKE_${lang}_COMPILER)
  30. set(_desc "Looking for a ${lang} compiler")
  31. message(STATUS ${_desc})
  32. file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang})
  33. set(extra_compiler_variables)
  34. if(lang STREQUAL CUDA)
  35. set(extra_compiler_variables "set(CMAKE_CUDA_HOST_COMPILER \\\"\${CMAKE_CUDA_HOST_COMPILER}\\\")")
  36. endif()
  37. set(content
  38. "cmake_minimum_required(VERSION ${CMAKE_VERSION})
  39. project(Check${lang} ${lang})
  40. file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\"
  41. \"set(CMAKE_${lang}_COMPILER \\\"\${CMAKE_${lang}_COMPILER}\\\")\\n\"
  42. \"${extra_compiler_variables}\\n\"
  43. )"
  44. )
  45. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/CMakeLists.txt"
  46. "${content}")
  47. if(CMAKE_GENERATOR_INSTANCE)
  48. set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE:INTERNAL=${CMAKE_GENERATOR_INSTANCE}")
  49. else()
  50. set(_D_CMAKE_GENERATOR_INSTANCE "")
  51. endif()
  52. execute_process(
  53. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}
  54. COMMAND ${CMAKE_COMMAND} . -G ${CMAKE_GENERATOR}
  55. -A "${CMAKE_GENERATOR_PLATFORM}"
  56. -T "${CMAKE_GENERATOR_TOOLSET}"
  57. ${_D_CMAKE_GENERATOR_INSTANCE}
  58. OUTPUT_VARIABLE output
  59. ERROR_VARIABLE output
  60. RESULT_VARIABLE result
  61. )
  62. include(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/result.cmake OPTIONAL)
  63. if(CMAKE_${lang}_COMPILER AND "${result}" STREQUAL "0")
  64. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  65. "${_desc} passed with the following output:\n"
  66. "${output}\n")
  67. else()
  68. set(CMAKE_${lang}_COMPILER NOTFOUND)
  69. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  70. "${_desc} failed with the following output:\n"
  71. "${output}\n")
  72. endif()
  73. message(STATUS "${_desc} - ${CMAKE_${lang}_COMPILER}")
  74. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE FILEPATH "${lang} compiler")
  75. mark_as_advanced(CMAKE_${lang}_COMPILER)
  76. if(CMAKE_${lang}_HOST_COMPILER)
  77. message(STATUS "Looking for a ${lang} host compiler - ${CMAKE_${lang}_HOST_COMPILER}")
  78. set(CMAKE_${lang}_HOST_COMPILER "${CMAKE_${lang}_HOST_COMPILER}" CACHE FILEPATH "${lang} host compiler")
  79. mark_as_advanced(CMAKE_${lang}_HOST_COMPILER)
  80. endif()
  81. endif()
  82. endmacro()