FortranCInterface.cmake 11 KB

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