CheckFortranSourceCompiles.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #.rst:
  2. # CheckFortranSourceCompiles
  3. # --------------------------
  4. #
  5. # Check if given Fortran source compiles and links into an executable::
  6. #
  7. # CHECK_Fortran_SOURCE_COMPILES(<code> <var> [FAIL_REGEX <fail-regex>]
  8. # [SRC_EXT <ext>])
  9. #
  10. # The arguments are:
  11. #
  12. # ``<code>``
  13. # Source code to try to compile. It must define a PROGRAM entry point.
  14. # ``<var>``
  15. # Variable to store whether the source code compiled.
  16. # Will be created as an internal cache variable.
  17. # ``FAIL_REGEX <fail-regex>``
  18. # Fail if test output matches this regex.
  19. # ``SRC_EXT <ext>``
  20. # Use source extension ``.<ext>`` instead of the default ``.F``.
  21. #
  22. # The following variables may be set before calling this macro to modify
  23. # the way the check is run::
  24. #
  25. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  26. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  27. # CMAKE_REQUIRED_INCLUDES = list of include directories
  28. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  29. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  30. #=============================================================================
  31. # Copyright 2005-2009 Kitware, Inc.
  32. #
  33. # Distributed under the OSI-approved BSD License (the "License");
  34. # see accompanying file Copyright.txt for details.
  35. #
  36. # This software is distributed WITHOUT ANY WARRANTY; without even the
  37. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  38. # See the License for more information.
  39. #=============================================================================
  40. # (To distribute this file outside of CMake, substitute the full
  41. # License text for the above reference.)
  42. macro(CHECK_Fortran_SOURCE_COMPILES SOURCE VAR)
  43. if(NOT DEFINED "${VAR}")
  44. set(_FAIL_REGEX)
  45. set(_SRC_EXT)
  46. set(_key)
  47. foreach(arg ${ARGN})
  48. if("${arg}" MATCHES "^(FAIL_REGEX|SRC_EXT)$")
  49. set(_key "${arg}")
  50. elseif(_key)
  51. list(APPEND _${_key} "${arg}")
  52. else()
  53. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  54. endif()
  55. endforeach()
  56. if(NOT _SRC_EXT)
  57. set(_SRC_EXT F)
  58. endif()
  59. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  60. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  61. if(CMAKE_REQUIRED_LIBRARIES)
  62. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES
  63. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  64. else()
  65. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES)
  66. endif()
  67. if(CMAKE_REQUIRED_INCLUDES)
  68. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES
  69. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  70. else()
  71. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES)
  72. endif()
  73. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}"
  74. "${SOURCE}\n")
  75. if(NOT CMAKE_REQUIRED_QUIET)
  76. message(STATUS "Performing Test ${VAR}")
  77. endif()
  78. try_compile(${VAR}
  79. ${CMAKE_BINARY_DIR}
  80. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}
  81. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  82. ${CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES}
  83. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  84. "${CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES}"
  85. OUTPUT_VARIABLE OUTPUT)
  86. foreach(_regex ${_FAIL_REGEX})
  87. if("${OUTPUT}" MATCHES "${_regex}")
  88. set(${VAR} 0)
  89. endif()
  90. endforeach()
  91. if(${VAR})
  92. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  93. if(NOT CMAKE_REQUIRED_QUIET)
  94. message(STATUS "Performing Test ${VAR} - Success")
  95. endif()
  96. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  97. "Performing Fortran SOURCE FILE Test ${VAR} succeeded with the following output:\n"
  98. "${OUTPUT}\n"
  99. "Source file was:\n${SOURCE}\n")
  100. else()
  101. if(NOT CMAKE_REQUIRED_QUIET)
  102. message(STATUS "Performing Test ${VAR} - Failed")
  103. endif()
  104. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  105. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  106. "Performing Fortran SOURCE FILE Test ${VAR} failed with the following output:\n"
  107. "${OUTPUT}\n"
  108. "Source file was:\n${SOURCE}\n")
  109. endif()
  110. endif()
  111. endmacro()