FortranCInterface.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "FC.h" header that defines the same FC_*() mangling
  51. # macros as the previous example plus preprocessor symbols FC_mysub
  52. # 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. # Execute at most once in a project.
  85. if(FortranCInterface_SOURCE_DIR)
  86. return()
  87. endif()
  88. #-----------------------------------------------------------------------------
  89. # Verify that C and Fortran are available.
  90. foreach(lang C Fortran)
  91. if(NOT CMAKE_${lang}_COMPILER_LOADED)
  92. message(FATAL_ERROR
  93. "FortranCInterface requires the ${lang} language to be enabled.")
  94. endif()
  95. endforeach()
  96. #-----------------------------------------------------------------------------
  97. set(FortranCInterface_SOURCE_DIR ${CMAKE_ROOT}/Modules/FortranCInterface)
  98. # Create the interface detection project if it does not exist.
  99. if(NOT FortranCInterface_BINARY_DIR)
  100. set(FortranCInterface_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterface)
  101. include(${FortranCInterface_SOURCE_DIR}/Detect.cmake)
  102. endif()
  103. # Load the detection results.
  104. include(${FortranCInterface_BINARY_DIR}/Output.cmake)
  105. #-----------------------------------------------------------------------------
  106. function(FortranCInterface_HEADER file)
  107. # Parse arguments.
  108. if(IS_ABSOLUTE "${file}")
  109. set(FILE "${file}")
  110. else()
  111. set(FILE "${CMAKE_CURRENT_BINARY_DIR}/${file}")
  112. endif()
  113. set(MACRO_NAMESPACE "FortranCInterface_")
  114. set(SYMBOL_NAMESPACE)
  115. set(SYMBOLS)
  116. set(doing)
  117. foreach(arg ${ARGN})
  118. if("x${arg}" MATCHES "^x(SYMBOLS|SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
  119. set(doing "${arg}")
  120. elseif("x${doing}" MATCHES "^x(SYMBOLS)$")
  121. list(APPEND "${doing}" "${arg}")
  122. elseif("x${doing}" MATCHES "^x(SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
  123. set("${doing}" "${arg}")
  124. set(doing)
  125. else()
  126. message(AUTHOR_WARNING "Unknown argument: \"${arg}\"")
  127. endif()
  128. endforeach()
  129. # Generate macro definitions.
  130. set(HEADER_CONTENT)
  131. set(_desc_GLOBAL "/* Mangling for Fortran global symbols without underscores. */")
  132. set(_desc_GLOBAL_ "/* Mangling for Fortran global symbols with underscores. */")
  133. set(_desc_MODULE "/* Mangling for Fortran module symbols without underscores. */")
  134. set(_desc_MODULE_ "/* Mangling for Fortran module symbols with underscores. */")
  135. foreach(macro GLOBAL GLOBAL_ MODULE MODULE_)
  136. if(FortranCInterface_${macro}_MACRO)
  137. set(HEADER_CONTENT "${HEADER_CONTENT}
  138. ${_desc_${macro}}
  139. #define ${MACRO_NAMESPACE}${macro}${FortranCInterface_${macro}_MACRO}
  140. ")
  141. endif()
  142. endforeach()
  143. # Generate symbol mangling definitions.
  144. if(SYMBOLS)
  145. set(HEADER_CONTENT "${HEADER_CONTENT}
  146. /*--------------------------------------------------------------------------*/
  147. /* Mangle some symbols automatically. */
  148. ")
  149. endif()
  150. foreach(f ${SYMBOLS})
  151. if("${f}" MATCHES ":")
  152. # Module symbol name. Parse "<module>:<function>" syntax.
  153. string(REPLACE ":" ";" pieces "${f}")
  154. list(GET pieces 0 module)
  155. list(GET pieces 1 function)
  156. string(TOUPPER "${module}" m_upper)
  157. string(TOLOWER "${module}" m_lower)
  158. string(TOUPPER "${function}" f_upper)
  159. string(TOLOWER "${function}" f_lower)
  160. if("${function}" MATCHES "_")
  161. set(form "_")
  162. else()
  163. set(form "")
  164. endif()
  165. if(FortranCInterface_MODULE${form}_MACRO)
  166. set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${module}_${function} ${MACRO_NAMESPACE}MODULE${form}(${m_lower},${f_lower}, ${m_upper},${f_upper})\n")
  167. else()
  168. message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
  169. endif()
  170. else()
  171. # Global symbol name.
  172. if("${f}" MATCHES "_")
  173. set(form "_")
  174. else()
  175. set(form "")
  176. endif()
  177. string(TOUPPER "${f}" f_upper)
  178. string(TOLOWER "${f}" f_lower)
  179. if(FortranCInterface_GLOBAL${form}_MACRO)
  180. set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${f} ${MACRO_NAMESPACE}GLOBAL${form}(${f_lower}, ${f_upper})\n")
  181. else()
  182. message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
  183. endif()
  184. endif()
  185. endforeach(f)
  186. # Store the content.
  187. configure_file(${FortranCInterface_SOURCE_DIR}/Macro.h.in ${FILE} @ONLY)
  188. endfunction()
  189. function(FortranCInterface_VERIFY)
  190. # Check arguments.
  191. set(lang C)
  192. set(quiet 0)
  193. set(verify_cxx 0)
  194. foreach(arg ${ARGN})
  195. if("${arg}" STREQUAL "QUIET")
  196. set(quiet 1)
  197. elseif("${arg}" STREQUAL "CXX")
  198. set(lang CXX)
  199. set(verify_cxx 1)
  200. else()
  201. message(FATAL_ERROR
  202. "FortranCInterface_VERIFY - called with unknown argument:\n ${arg}")
  203. endif()
  204. endforeach()
  205. if(NOT CMAKE_${lang}_COMPILER_LOADED)
  206. message(FATAL_ERROR
  207. "FortranCInterface_VERIFY(${lang}) requires ${lang} to be enabled.")
  208. endif()
  209. # Build the verification project if not yet built.
  210. if(NOT DEFINED FortranCInterface_VERIFIED_${lang})
  211. set(_desc "Verifying Fortran/${lang} Compiler Compatibility")
  212. message(STATUS "${_desc}")
  213. # Build a sample project which reports symbols.
  214. try_compile(FortranCInterface_VERIFY_${lang}_COMPILED
  215. ${FortranCInterface_BINARY_DIR}/Verify${lang}
  216. ${FortranCInterface_SOURCE_DIR}/Verify
  217. VerifyFortranC
  218. CMAKE_FLAGS -DVERIFY_CXX=${verify_cxx}
  219. OUTPUT_VARIABLE _output)
  220. file(WRITE "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" "${_output}")
  221. # Report results.
  222. if(FortranCInterface_VERIFY_${lang}_COMPILED)
  223. message(STATUS "${_desc} - Success")
  224. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  225. "${_desc} passed with the following output:\n${_output}\n\n")
  226. set(FortranCInterface_VERIFIED_${lang} 1 CACHE INTERNAL "Fortran/${lang} compatibility")
  227. else()
  228. message(STATUS "${_desc} - Failed")
  229. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  230. "${_desc} failed with the following output:\n${_output}\n\n")
  231. set(FortranCInterface_VERIFIED_${lang} 0 CACHE INTERNAL "Fortran/${lang} compatibility")
  232. endif()
  233. unset(FortranCInterface_VERIFY_${lang}_COMPILED CACHE)
  234. endif()
  235. # Error if compilers are incompatible.
  236. if(NOT FortranCInterface_VERIFIED_${lang} AND NOT quiet)
  237. file(READ "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" _output)
  238. string(REGEX REPLACE "\n" "\n " _output "${_output}")
  239. message(FATAL_ERROR
  240. "The Fortran compiler:\n ${CMAKE_Fortran_COMPILER}\n"
  241. "and the ${lang} compiler:\n ${CMAKE_${lang}_COMPILER}\n"
  242. "failed to compile a simple test project using both languages. "
  243. "The output was:\n ${_output}")
  244. endif()
  245. endfunction()