CheckFortranSourceCompiles.cmake 3.9 KB

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