FortranCInterface.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #[=======================================================================[.rst:
  2. FortranCInterface
  3. -----------------
  4. Fortran/C Interface Detection
  5. This module automatically detects the API by which C and Fortran
  6. languages interact.
  7. Module Variables
  8. ^^^^^^^^^^^^^^^^
  9. Variables that indicate if the mangling is found:
  10. ``FortranCInterface_GLOBAL_FOUND``
  11. Global subroutines and functions.
  12. ``FortranCInterface_MODULE_FOUND``
  13. Module subroutines and functions (declared by "MODULE PROCEDURE").
  14. This module also provides the following variables to specify
  15. the detected mangling, though a typical use case does not need
  16. to reference them and can use the `Module Functions`_ below.
  17. ``FortranCInterface_GLOBAL_PREFIX``
  18. Prefix for a global symbol without an underscore.
  19. ``FortranCInterface_GLOBAL_SUFFIX``
  20. Suffix for a global symbol without an underscore.
  21. ``FortranCInterface_GLOBAL_CASE``
  22. The case for a global symbol without an underscore,
  23. either ``UPPER`` or ``LOWER``.
  24. ``FortranCInterface_GLOBAL__PREFIX``
  25. Prefix for a global symbol with an underscore.
  26. ``FortranCInterface_GLOBAL__SUFFIX``
  27. Suffix for a global symbol with an underscore.
  28. ``FortranCInterface_GLOBAL__CASE``
  29. The case for a global symbol with an underscore,
  30. either ``UPPER`` or ``LOWER``.
  31. ``FortranCInterface_MODULE_PREFIX``
  32. Prefix for a module symbol without an underscore.
  33. ``FortranCInterface_MODULE_MIDDLE``
  34. Middle of a module symbol without an underscore that appears
  35. between the name of the module and the name of the symbol.
  36. ``FortranCInterface_MODULE_SUFFIX``
  37. Suffix for a module symbol without an underscore.
  38. ``FortranCInterface_MODULE_CASE``
  39. The case for a module symbol without an underscore,
  40. either ``UPPER`` or ``LOWER``.
  41. ``FortranCInterface_MODULE__PREFIX``
  42. Prefix for a module symbol with an underscore.
  43. ``FortranCInterface_MODULE__MIDDLE``
  44. Middle of a module symbol with an underscore that appears
  45. between the name of the module and the name of the symbol.
  46. ``FortranCInterface_MODULE__SUFFIX``
  47. Suffix for a module symbol with an underscore.
  48. ``FortranCInterface_MODULE__CASE``
  49. The case for a module symbol with an underscore,
  50. either ``UPPER`` or ``LOWER``.
  51. Module Functions
  52. ^^^^^^^^^^^^^^^^
  53. .. command:: FortranCInterface_HEADER
  54. The ``FortranCInterface_HEADER`` function is provided to generate a
  55. C header file containing macros to mangle symbol names::
  56. FortranCInterface_HEADER(<file>
  57. [MACRO_NAMESPACE <macro-ns>]
  58. [SYMBOL_NAMESPACE <ns>]
  59. [SYMBOLS [<module>:]<function> ...])
  60. It generates in ``<file>`` definitions of the following macros::
  61. #define FortranCInterface_GLOBAL (name,NAME) ...
  62. #define FortranCInterface_GLOBAL_(name,NAME) ...
  63. #define FortranCInterface_MODULE (mod,name, MOD,NAME) ...
  64. #define FortranCInterface_MODULE_(mod,name, MOD,NAME) ...
  65. These macros mangle four categories of Fortran symbols, respectively:
  66. * Global symbols without '_': ``call mysub()``
  67. * Global symbols with '_' : ``call my_sub()``
  68. * Module symbols without '_': ``use mymod; call mysub()``
  69. * Module symbols with '_' : ``use mymod; call my_sub()``
  70. If mangling for a category is not known, its macro is left undefined.
  71. All macros require raw names in both lower case and upper case.
  72. The options are:
  73. ``MACRO_NAMESPACE``
  74. Replace the default ``FortranCInterface_`` prefix with a given
  75. namespace ``<macro-ns>``.
  76. ``SYMBOLS``
  77. List symbols to mangle automatically with C preprocessor definitions::
  78. <function> ==> #define <ns><function> ...
  79. <module>:<function> ==> #define <ns><module>_<function> ...
  80. If the mangling for some symbol is not known then no preprocessor
  81. definition is created, and a warning is displayed.
  82. ``SYMBOL_NAMESPACE``
  83. Prefix all preprocessor definitions generated by the ``SYMBOLS``
  84. option with a given namespace ``<ns>``.
  85. .. command:: FortranCInterface_VERIFY
  86. The ``FortranCInterface_VERIFY`` function is provided to verify
  87. that the Fortran and C/C++ compilers work together::
  88. FortranCInterface_VERIFY([CXX] [QUIET])
  89. It tests whether a simple test executable using Fortran and C (and C++
  90. when the CXX option is given) compiles and links successfully. The
  91. result is stored in the cache entry ``FortranCInterface_VERIFIED_C``
  92. (or ``FortranCInterface_VERIFIED_CXX`` if ``CXX`` is given) as a boolean.
  93. If the check fails and ``QUIET`` is not given the function terminates with a
  94. fatal error message describing the problem. The purpose of this check
  95. is to stop a build early for incompatible compiler combinations. The
  96. test is built in the ``Release`` configuration.
  97. Example Usage
  98. ^^^^^^^^^^^^^
  99. .. code-block:: cmake
  100. include(FortranCInterface)
  101. FortranCInterface_HEADER(FC.h MACRO_NAMESPACE "FC_")
  102. This creates a "FC.h" header that defines mangling macros ``FC_GLOBAL()``,
  103. ``FC_GLOBAL_()``, ``FC_MODULE()``, and ``FC_MODULE_()``.
  104. .. code-block:: cmake
  105. include(FortranCInterface)
  106. FortranCInterface_HEADER(FCMangle.h
  107. MACRO_NAMESPACE "FC_"
  108. SYMBOL_NAMESPACE "FC_"
  109. SYMBOLS mysub mymod:my_sub)
  110. This creates a "FCMangle.h" header that defines the same ``FC_*()``
  111. mangling macros as the previous example plus preprocessor symbols
  112. ``FC_mysub`` and ``FC_mymod_my_sub``.
  113. Additional Manglings
  114. ^^^^^^^^^^^^^^^^^^^^
  115. FortranCInterface is aware of possible ``GLOBAL`` and ``MODULE`` manglings
  116. for many Fortran compilers, but it also provides an interface to specify
  117. new possible manglings. Set the variables::
  118. FortranCInterface_GLOBAL_SYMBOLS
  119. FortranCInterface_MODULE_SYMBOLS
  120. before including FortranCInterface to specify manglings of the symbols
  121. ``MySub``, ``My_Sub``, ``MyModule:MySub``, and ``My_Module:My_Sub``.
  122. For example, the code:
  123. .. code-block:: cmake
  124. set(FortranCInterface_GLOBAL_SYMBOLS mysub_ my_sub__ MYSUB_)
  125. # ^^^^^ ^^^^^^ ^^^^^
  126. set(FortranCInterface_MODULE_SYMBOLS
  127. __mymodule_MOD_mysub __my_module_MOD_my_sub)
  128. # ^^^^^^^^ ^^^^^ ^^^^^^^^^ ^^^^^^
  129. include(FortranCInterface)
  130. tells FortranCInterface to try given ``GLOBAL`` and ``MODULE`` manglings.
  131. (The carets point at raw symbol names for clarity in this example but
  132. are not needed.)
  133. #]=======================================================================]
  134. #=============================================================================
  135. # Copyright 2008-2009 Kitware, Inc.
  136. #
  137. # Distributed under the OSI-approved BSD License (the "License");
  138. # see accompanying file Copyright.txt for details.
  139. #
  140. # This software is distributed WITHOUT ANY WARRANTY; without even the
  141. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  142. # See the License for more information.
  143. #=============================================================================
  144. # (To distribute this file outside of CMake, substitute the full
  145. # License text for the above reference.)
  146. #-----------------------------------------------------------------------------
  147. # Execute at most once in a project.
  148. if(FortranCInterface_SOURCE_DIR)
  149. return()
  150. endif()
  151. cmake_policy(PUSH)
  152. cmake_policy(SET CMP0007 NEW)
  153. #-----------------------------------------------------------------------------
  154. # Verify that C and Fortran are available.
  155. foreach(lang C Fortran)
  156. if(NOT CMAKE_${lang}_COMPILER_LOADED)
  157. message(FATAL_ERROR
  158. "FortranCInterface requires the ${lang} language to be enabled.")
  159. endif()
  160. endforeach()
  161. #-----------------------------------------------------------------------------
  162. set(FortranCInterface_SOURCE_DIR ${CMAKE_ROOT}/Modules/FortranCInterface)
  163. # MinGW's make tool does not always like () in the path
  164. if("${CMAKE_GENERATOR}" MATCHES "MinGW" AND
  165. "${FortranCInterface_SOURCE_DIR}" MATCHES "[()]")
  166. file(COPY ${FortranCInterface_SOURCE_DIR}/
  167. DESTINATION ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterfaceMinGW)
  168. set(FortranCInterface_SOURCE_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterfaceMinGW)
  169. endif()
  170. # Create the interface detection project if it does not exist.
  171. if(NOT FortranCInterface_BINARY_DIR)
  172. set(FortranCInterface_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterface)
  173. include(${FortranCInterface_SOURCE_DIR}/Detect.cmake)
  174. endif()
  175. # Load the detection results.
  176. include(${FortranCInterface_BINARY_DIR}/Output.cmake)
  177. #-----------------------------------------------------------------------------
  178. function(FortranCInterface_HEADER file)
  179. # Parse arguments.
  180. if(IS_ABSOLUTE "${file}")
  181. set(FILE "${file}")
  182. else()
  183. set(FILE "${CMAKE_CURRENT_BINARY_DIR}/${file}")
  184. endif()
  185. set(MACRO_NAMESPACE "FortranCInterface_")
  186. set(SYMBOL_NAMESPACE)
  187. set(SYMBOLS)
  188. set(doing)
  189. foreach(arg ${ARGN})
  190. if("x${arg}" MATCHES "^x(SYMBOLS|SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
  191. set(doing "${arg}")
  192. elseif("x${doing}" MATCHES "^x(SYMBOLS)$")
  193. list(APPEND "${doing}" "${arg}")
  194. elseif("x${doing}" MATCHES "^x(SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
  195. set("${doing}" "${arg}")
  196. set(doing)
  197. else()
  198. message(AUTHOR_WARNING "Unknown argument: \"${arg}\"")
  199. endif()
  200. endforeach()
  201. # Generate macro definitions.
  202. set(HEADER_CONTENT)
  203. set(_desc_GLOBAL "/* Mangling for Fortran global symbols without underscores. */")
  204. set(_desc_GLOBAL_ "/* Mangling for Fortran global symbols with underscores. */")
  205. set(_desc_MODULE "/* Mangling for Fortran module symbols without underscores. */")
  206. set(_desc_MODULE_ "/* Mangling for Fortran module symbols with underscores. */")
  207. foreach(macro GLOBAL GLOBAL_ MODULE MODULE_)
  208. if(FortranCInterface_${macro}_MACRO)
  209. set(HEADER_CONTENT "${HEADER_CONTENT}
  210. ${_desc_${macro}}
  211. #define ${MACRO_NAMESPACE}${macro}${FortranCInterface_${macro}_MACRO}
  212. ")
  213. endif()
  214. endforeach()
  215. # Generate symbol mangling definitions.
  216. if(SYMBOLS)
  217. set(HEADER_CONTENT "${HEADER_CONTENT}
  218. /*--------------------------------------------------------------------------*/
  219. /* Mangle some symbols automatically. */
  220. ")
  221. endif()
  222. foreach(f ${SYMBOLS})
  223. if("${f}" MATCHES ":")
  224. # Module symbol name. Parse "<module>:<function>" syntax.
  225. string(REPLACE ":" ";" pieces "${f}")
  226. list(GET pieces 0 module)
  227. list(GET pieces 1 function)
  228. string(TOUPPER "${module}" m_upper)
  229. string(TOLOWER "${module}" m_lower)
  230. string(TOUPPER "${function}" f_upper)
  231. string(TOLOWER "${function}" f_lower)
  232. if("${function}" MATCHES "_")
  233. set(form "_")
  234. else()
  235. set(form "")
  236. endif()
  237. if(FortranCInterface_MODULE${form}_MACRO)
  238. set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${module}_${function} ${MACRO_NAMESPACE}MODULE${form}(${m_lower},${f_lower}, ${m_upper},${f_upper})\n")
  239. else()
  240. message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
  241. endif()
  242. else()
  243. # Global symbol name.
  244. if("${f}" MATCHES "_")
  245. set(form "_")
  246. else()
  247. set(form "")
  248. endif()
  249. string(TOUPPER "${f}" f_upper)
  250. string(TOLOWER "${f}" f_lower)
  251. if(FortranCInterface_GLOBAL${form}_MACRO)
  252. set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${f} ${MACRO_NAMESPACE}GLOBAL${form}(${f_lower}, ${f_upper})\n")
  253. else()
  254. message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
  255. endif()
  256. endif()
  257. endforeach()
  258. # Store the content.
  259. configure_file(${FortranCInterface_SOURCE_DIR}/Macro.h.in ${FILE} @ONLY)
  260. endfunction()
  261. function(FortranCInterface_VERIFY)
  262. # Check arguments.
  263. set(lang C)
  264. set(quiet 0)
  265. set(verify_cxx 0)
  266. foreach(arg ${ARGN})
  267. if("${arg}" STREQUAL "QUIET")
  268. set(quiet 1)
  269. elseif("${arg}" STREQUAL "CXX")
  270. set(lang CXX)
  271. set(verify_cxx 1)
  272. else()
  273. message(FATAL_ERROR
  274. "FortranCInterface_VERIFY - called with unknown argument:\n ${arg}")
  275. endif()
  276. endforeach()
  277. if(NOT CMAKE_${lang}_COMPILER_LOADED)
  278. message(FATAL_ERROR
  279. "FortranCInterface_VERIFY(${lang}) requires ${lang} to be enabled.")
  280. endif()
  281. # Build the verification project if not yet built.
  282. if(NOT DEFINED FortranCInterface_VERIFIED_${lang})
  283. set(_desc "Verifying Fortran/${lang} Compiler Compatibility")
  284. message(STATUS "${_desc}")
  285. # Build a sample project which reports symbols.
  286. set(CMAKE_TRY_COMPILE_CONFIGURATION Release)
  287. try_compile(FortranCInterface_VERIFY_${lang}_COMPILED
  288. ${FortranCInterface_BINARY_DIR}/Verify${lang}
  289. ${FortranCInterface_SOURCE_DIR}/Verify
  290. VerifyFortranC
  291. CMAKE_FLAGS -DVERIFY_CXX=${verify_cxx}
  292. -DCMAKE_VERBOSE_MAKEFILE=ON
  293. "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS}"
  294. "-DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}"
  295. "-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
  296. "-DCMAKE_C_FLAGS_RELEASE:STRING=${CMAKE_C_FLAGS_RELEASE}"
  297. "-DCMAKE_CXX_FLAGS_RELEASE:STRING=${CMAKE_CXX_FLAGS_RELEASE}"
  298. "-DCMAKE_Fortran_FLAGS_RELEASE:STRING=${CMAKE_Fortran_FLAGS_RELEASE}"
  299. OUTPUT_VARIABLE _output)
  300. file(WRITE "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" "${_output}")
  301. # Report results.
  302. if(FortranCInterface_VERIFY_${lang}_COMPILED)
  303. message(STATUS "${_desc} - Success")
  304. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  305. "${_desc} passed with the following output:\n${_output}\n\n")
  306. set(FortranCInterface_VERIFIED_${lang} 1 CACHE INTERNAL "Fortran/${lang} compatibility")
  307. else()
  308. message(STATUS "${_desc} - Failed")
  309. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  310. "${_desc} failed with the following output:\n${_output}\n\n")
  311. set(FortranCInterface_VERIFIED_${lang} 0 CACHE INTERNAL "Fortran/${lang} compatibility")
  312. endif()
  313. unset(FortranCInterface_VERIFY_${lang}_COMPILED CACHE)
  314. endif()
  315. # Error if compilers are incompatible.
  316. if(NOT FortranCInterface_VERIFIED_${lang} AND NOT quiet)
  317. file(READ "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" _output)
  318. string(REPLACE "\n" "\n " _output "${_output}")
  319. message(FATAL_ERROR
  320. "The Fortran compiler:\n ${CMAKE_Fortran_COMPILER}\n"
  321. "and the ${lang} compiler:\n ${CMAKE_${lang}_COMPILER}\n"
  322. "failed to compile a simple test project using both languages. "
  323. "The output was:\n ${_output}")
  324. endif()
  325. endfunction()
  326. # Restore including context policies.
  327. cmake_policy(POP)