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,
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>".
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>".
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_().
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.
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
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.)
84 #-----------------------------------------------------------------------------
85 # Execute at most once in a project.
86 if(FortranCInterface_SOURCE_DIR)
90 #-----------------------------------------------------------------------------
91 # Verify that C and Fortran are available.
92 foreach(lang C Fortran)
93 if(NOT CMAKE_${lang}_COMPILER_LOADED)
95 "FortranCInterface requires the ${lang} language to be enabled.")
99 #-----------------------------------------------------------------------------
100 set(FortranCInterface_SOURCE_DIR ${CMAKE_ROOT}/Modules/FortranCInterface)
102 # Create the interface detection project if it does not exist.
103 if(NOT FortranCInterface_BINARY_DIR)
104 set(FortranCInterface_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterface)
105 include(${FortranCInterface_SOURCE_DIR}/Detect.cmake)
108 # Load the detection results.
109 include(${FortranCInterface_BINARY_DIR}/Output.cmake)
111 #-----------------------------------------------------------------------------
112 function(FortranCInterface_HEADER file)
114 if(IS_ABSOLUTE "${file}")
117 set(FILE "${CMAKE_CURRENT_BINARY_DIR}/${file}")
119 set(MACRO_NAMESPACE "FortranCInterface_")
120 set(SYMBOL_NAMESPACE)
124 if("x${arg}" MATCHES "^x(SYMBOLS|SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
126 elseif("x${doing}" MATCHES "^x(SYMBOLS)$")
127 list(APPEND "${doing}" "${arg}")
128 elseif("x${doing}" MATCHES "^x(SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
129 set("${doing}" "${arg}")
132 message(AUTHOR_WARNING "Unknown argument: \"${arg}\"")
136 # Generate macro definitions.
138 set(_desc_GLOBAL "/* Mangling for Fortran global symbols without underscores. */")
139 set(_desc_GLOBAL_ "/* Mangling for Fortran global symbols with underscores. */")
140 set(_desc_MODULE "/* Mangling for Fortran module symbols without underscores. */")
141 set(_desc_MODULE_ "/* Mangling for Fortran module symbols with underscores. */")
142 foreach(macro GLOBAL GLOBAL_ MODULE MODULE_)
143 if(FortranCInterface_${macro}_MACRO)
144 set(HEADER_CONTENT "${HEADER_CONTENT}
146 #define ${MACRO_NAMESPACE}${macro}${FortranCInterface_${macro}_MACRO}
151 # Generate symbol mangling definitions.
153 set(HEADER_CONTENT "${HEADER_CONTENT}
154 /*--------------------------------------------------------------------------*/
155 /* Mangle some symbols automatically. */
158 foreach(f ${SYMBOLS})
159 if("${f}" MATCHES ":")
160 # Module symbol name. Parse "<module>:<function>" syntax.
161 string(REPLACE ":" ";" pieces "${f}")
162 list(GET pieces 0 module)
163 list(GET pieces 1 function)
164 string(TOUPPER "${module}" m_upper)
165 string(TOLOWER "${module}" m_lower)
166 string(TOUPPER "${function}" f_upper)
167 string(TOLOWER "${function}" f_lower)
168 if("${function}" MATCHES "_")
173 if(FortranCInterface_MODULE${form}_MACRO)
174 set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${module}_${function} ${MACRO_NAMESPACE}MODULE${form}(${m_lower},${f_lower}, ${m_upper},${f_upper})\n")
176 message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
179 # Global symbol name.
180 if("${f}" MATCHES "_")
185 string(TOUPPER "${f}" f_upper)
186 string(TOLOWER "${f}" f_lower)
187 if(FortranCInterface_GLOBAL${form}_MACRO)
188 set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${f} ${MACRO_NAMESPACE}GLOBAL${form}(${f_lower}, ${f_upper})\n")
190 message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
196 configure_file(${FortranCInterface_SOURCE_DIR}/Macro.h.in ${FILE} @ONLY)
199 function(FortranCInterface_VERIFY)
206 if("${arg}" STREQUAL "QUIET")
208 elseif("${arg}" STREQUAL "CXX")
213 "FortranCInterface_VERIFY - called with unknown argument:\n ${arg}")
217 if(NOT CMAKE_${lang}_COMPILER_LOADED)
219 "FortranCInterface_VERIFY(${lang}) requires ${lang} to be enabled.")
222 # Build the verification project if not yet built.
223 if(NOT DEFINED FortranCInterface_VERIFIED_${lang})
224 set(_desc "Verifying Fortran/${lang} Compiler Compatibility")
225 message(STATUS "${_desc}")
227 # Build a sample project which reports symbols.
228 try_compile(FortranCInterface_VERIFY_${lang}_COMPILED
229 ${FortranCInterface_BINARY_DIR}/Verify${lang}
230 ${FortranCInterface_SOURCE_DIR}/Verify
232 CMAKE_FLAGS -DVERIFY_CXX=${verify_cxx}
233 -DCMAKE_VERBOSE_MAKEFILE=ON
234 OUTPUT_VARIABLE _output)
235 file(WRITE "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" "${_output}")
238 if(FortranCInterface_VERIFY_${lang}_COMPILED)
239 message(STATUS "${_desc} - Success")
240 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
241 "${_desc} passed with the following output:\n${_output}\n\n")
242 set(FortranCInterface_VERIFIED_${lang} 1 CACHE INTERNAL "Fortran/${lang} compatibility")
244 message(STATUS "${_desc} - Failed")
245 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
246 "${_desc} failed with the following output:\n${_output}\n\n")
247 set(FortranCInterface_VERIFIED_${lang} 0 CACHE INTERNAL "Fortran/${lang} compatibility")
249 unset(FortranCInterface_VERIFY_${lang}_COMPILED CACHE)
252 # Error if compilers are incompatible.
253 if(NOT FortranCInterface_VERIFIED_${lang} AND NOT quiet)
254 file(READ "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" _output)
255 string(REGEX REPLACE "\n" "\n " _output "${_output}")
257 "The Fortran compiler:\n ${CMAKE_Fortran_COMPILER}\n"
258 "and the ${lang} compiler:\n ${CMAKE_${lang}_COMPILER}\n"
259 "failed to compile a simple test project using both languages. "
260 "The output was:\n ${_output}")