CheckLanguage.cmake 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #
  7. # Check if a language can be enabled
  8. #
  9. # Usage:
  10. #
  11. # ::
  12. #
  13. # check_language(<lang>)
  14. #
  15. # where <lang> is a language that may be passed to enable_language()
  16. # such as "Fortran". If CMAKE_<lang>_COMPILER is already defined the
  17. # check does nothing. Otherwise it tries enabling the language in a
  18. # test project. The result is cached in CMAKE_<lang>_COMPILER as the
  19. # compiler that was found, or NOTFOUND if the language cannot be
  20. # enabled.
  21. #
  22. # Example:
  23. #
  24. # ::
  25. #
  26. # check_language(Fortran)
  27. # if(CMAKE_Fortran_COMPILER)
  28. # enable_language(Fortran)
  29. # else()
  30. # message(STATUS "No Fortran support")
  31. # endif()
  32. macro(check_language lang)
  33. if(NOT DEFINED CMAKE_${lang}_COMPILER)
  34. set(_desc "Looking for a ${lang} compiler")
  35. message(STATUS ${_desc})
  36. file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang})
  37. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/CMakeLists.txt"
  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. )
  43. ")
  44. execute_process(
  45. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}
  46. COMMAND ${CMAKE_COMMAND} . -G ${CMAKE_GENERATOR}
  47. -A "${CMAKE_GENERATOR_PLATFORM}"
  48. -T "${CMAKE_GENERATOR_TOOLSET}"
  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()