CheckFortranSourceCompiles.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. CheckFortranSourceCompiles
  5. --------------------------
  6. Check if given Fortran source compiles and links into an executable.
  7. .. command:: check_fortran_source_compiles
  8. ::
  9. check_fortran_source_compiles(code resultVar
  10. [FAIL_REGEX regex1 [regex2...]]
  11. [SRC_EXT ext]
  12. )
  13. Check that the source supplied in ``code`` can be compiled as a Fortran
  14. source file and linked as an executable (so it must contain at least a
  15. ``PROGRAM`` entry point). The result will be stored in the internal cache
  16. variable specified by ``resultVar``, with a boolean true value for success
  17. and boolean false for failure. If ``FAIL_REGEX`` is provided, then failure is
  18. determined by checking if anything in the output matches any of the specified
  19. regular expressions.
  20. By default, the test source file will be given a ``.F`` file extension. The
  21. ``SRC_EXT`` option can be used to override this with ``.ext`` instead.
  22. The underlying check is performed by the :command:`try_compile` command. The
  23. compile and link commands can be influenced by setting any of the following
  24. variables prior to calling ``check_fortran_source_compiles()``:
  25. ``CMAKE_REQUIRED_FLAGS``
  26. Additional flags to pass to the compiler. Note that the contents of
  27. :variable:`CMAKE_Fortran_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  28. configuration-specific variable are automatically added to the compiler
  29. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  30. ``CMAKE_REQUIRED_DEFINITIONS``
  31. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  32. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  33. ``resultVar`` will also be added automatically.
  34. ``CMAKE_REQUIRED_INCLUDES``
  35. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  36. the compiler. These will be the only header search paths used by
  37. ``try_compile()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  38. directory property will be ignored.
  39. ``CMAKE_REQUIRED_LIBRARIES``
  40. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  41. command. These can be the name of system libraries or they can be
  42. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  43. further details).
  44. ``CMAKE_REQUIRED_QUIET``
  45. If this variable evaluates to a boolean true value, all status messages
  46. associated with the check will be suppressed.
  47. The check is only performed once, with the result cached in the variable
  48. named by ``resultVar``. Every subsequent CMake run will re-use this cached
  49. value rather than performing the check again, even if the ``code`` changes.
  50. In order to force the check to be re-evaluated, the variable named by
  51. ``resultVar`` must be manually removed from the cache.
  52. #]=======================================================================]
  53. include_guard(GLOBAL)
  54. macro(CHECK_Fortran_SOURCE_COMPILES SOURCE VAR)
  55. if(NOT DEFINED "${VAR}")
  56. set(_FAIL_REGEX)
  57. set(_SRC_EXT)
  58. set(_key)
  59. foreach(arg ${ARGN})
  60. if("${arg}" MATCHES "^(FAIL_REGEX|SRC_EXT)$")
  61. set(_key "${arg}")
  62. elseif(_key)
  63. list(APPEND _${_key} "${arg}")
  64. else()
  65. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  66. endif()
  67. endforeach()
  68. if(NOT _SRC_EXT)
  69. set(_SRC_EXT F)
  70. endif()
  71. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  72. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  73. if(CMAKE_REQUIRED_LIBRARIES)
  74. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES
  75. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  76. else()
  77. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES)
  78. endif()
  79. if(CMAKE_REQUIRED_INCLUDES)
  80. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES
  81. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  82. else()
  83. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES)
  84. endif()
  85. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}"
  86. "${SOURCE}\n")
  87. if(NOT CMAKE_REQUIRED_QUIET)
  88. message(STATUS "Performing Test ${VAR}")
  89. endif()
  90. try_compile(${VAR}
  91. ${CMAKE_BINARY_DIR}
  92. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}
  93. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  94. ${CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES}
  95. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  96. "${CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES}"
  97. OUTPUT_VARIABLE OUTPUT)
  98. foreach(_regex ${_FAIL_REGEX})
  99. if("${OUTPUT}" MATCHES "${_regex}")
  100. set(${VAR} 0)
  101. endif()
  102. endforeach()
  103. if(${VAR})
  104. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  105. if(NOT CMAKE_REQUIRED_QUIET)
  106. message(STATUS "Performing Test ${VAR} - Success")
  107. endif()
  108. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  109. "Performing Fortran SOURCE FILE Test ${VAR} succeeded with the following output:\n"
  110. "${OUTPUT}\n"
  111. "Source file was:\n${SOURCE}\n")
  112. else()
  113. if(NOT CMAKE_REQUIRED_QUIET)
  114. message(STATUS "Performing Test ${VAR} - Failed")
  115. endif()
  116. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  117. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  118. "Performing Fortran SOURCE FILE Test ${VAR} failed with the following output:\n"
  119. "${OUTPUT}\n"
  120. "Source file was:\n${SOURCE}\n")
  121. endif()
  122. endif()
  123. endmacro()