CheckLanguage.cmake 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # - Check if a language can be enabled
  2. # Usage:
  3. # check_language(<lang>)
  4. # where <lang> is a language that may be passed to enable_language()
  5. # such as "Fortran". If CMAKE_<lang>_COMPILER is already defined the
  6. # check does nothing. Otherwise it tries enabling the language in a
  7. # test project. The result is cached in CMAKE_<lang>_COMPILER as the
  8. # compiler that was found, or NOTFOUND if the language cannot be enabled.
  9. #
  10. # Example:
  11. # check_language(Fortran)
  12. # if(CMAKE_Fortran_COMPILER)
  13. # enable_language(Fortran)
  14. # else()
  15. # message(STATUS "No Fortran support")
  16. # endif()
  17. #=============================================================================
  18. # Copyright 2009-2012 Kitware, Inc.
  19. #
  20. # Distributed under the OSI-approved BSD License (the "License");
  21. # see accompanying file Copyright.txt for details.
  22. #
  23. # This software is distributed WITHOUT ANY WARRANTY; without even the
  24. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  25. # See the License for more information.
  26. #=============================================================================
  27. # (To distribute this file outside of CMake, substitute the full
  28. # License text for the above reference.)
  29. macro(check_language lang)
  30. if(NOT DEFINED CMAKE_${lang}_COMPILER)
  31. set(_desc "Looking for a ${lang} compiler")
  32. message(STATUS ${_desc})
  33. file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang})
  34. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/CMakeLists.txt"
  35. "cmake_minimum_required(VERSION 2.8)
  36. project(Check${lang} ${lang})
  37. file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\"
  38. \"set(CMAKE_${lang}_COMPILER \\\"\${CMAKE_${lang}_COMPILER}\\\")\\n\"
  39. )
  40. ")
  41. execute_process(
  42. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}
  43. COMMAND ${CMAKE_COMMAND} . -G ${CMAKE_GENERATOR}
  44. OUTPUT_VARIABLE output
  45. ERROR_VARIABLE output
  46. RESULT_VARIABLE result
  47. )
  48. include(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/result.cmake OPTIONAL)
  49. if(CMAKE_${lang}_COMPILER AND "${result}" STREQUAL "0")
  50. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  51. "${_desc} passed with the following output:\n"
  52. "${output}\n")
  53. else()
  54. set(CMAKE_${lang}_COMPILER NOTFOUND)
  55. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  56. "${_desc} failed with the following output:\n"
  57. "${output}\n")
  58. endif()
  59. message(STATUS "${_desc} - ${CMAKE_${lang}_COMPILER}")
  60. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE FILEPATH "${lang} compiler")
  61. mark_as_advanced(CMAKE_${lang}_COMPILER)
  62. endif()
  63. endmacro()