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 "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.
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. The test is built in the Release configuration.
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 # Copyright 2008-2009 Kitware, Inc.
87 # Distributed under the OSI-approved BSD License (the "License");
88 # see accompanying file Copyright.txt for details.
90 # This software is distributed WITHOUT ANY WARRANTY; without even the
91 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
92 # See the License for more information.
93 #=============================================================================
94 # (To distribute this file outside of CMake, substitute the full
95 # License text for the above reference.)
97 #-----------------------------------------------------------------------------
98 # Execute at most once in a project.
99 if(FortranCInterface_SOURCE_DIR)
103 # Use CMake 2.8.0 behavior for this module regardless of including context.
105 cmake_policy(VERSION 2.8.0)
107 #-----------------------------------------------------------------------------
108 # Verify that C and Fortran are available.
109 foreach(lang C Fortran)
110 if(NOT CMAKE_${lang}_COMPILER_LOADED)
112 "FortranCInterface requires the ${lang} language to be enabled.")
116 #-----------------------------------------------------------------------------
117 set(FortranCInterface_SOURCE_DIR ${CMAKE_ROOT}/Modules/FortranCInterface)
119 # MinGW's make tool does not always like () in the path
120 if("${CMAKE_GENERATOR}" MATCHES "MinGW" AND
121 "${FortranCInterface_SOURCE_DIR}" MATCHES "[()]")
122 file(COPY ${FortranCInterface_SOURCE_DIR}/
123 DESTINATION ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterfaceMinGW)
124 set(FortranCInterface_SOURCE_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterfaceMinGW)
127 # Create the interface detection project if it does not exist.
128 if(NOT FortranCInterface_BINARY_DIR)
129 set(FortranCInterface_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterface)
130 include(${FortranCInterface_SOURCE_DIR}/Detect.cmake)
133 # Load the detection results.
134 include(${FortranCInterface_BINARY_DIR}/Output.cmake)
136 #-----------------------------------------------------------------------------
137 function(FortranCInterface_HEADER file)
139 if(IS_ABSOLUTE "${file}")
142 set(FILE "${CMAKE_CURRENT_BINARY_DIR}/${file}")
144 set(MACRO_NAMESPACE "FortranCInterface_")
145 set(SYMBOL_NAMESPACE)
149 if("x${arg}" MATCHES "^x(SYMBOLS|SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
151 elseif("x${doing}" MATCHES "^x(SYMBOLS)$")
152 list(APPEND "${doing}" "${arg}")
153 elseif("x${doing}" MATCHES "^x(SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
154 set("${doing}" "${arg}")
157 message(AUTHOR_WARNING "Unknown argument: \"${arg}\"")
161 # Generate macro definitions.
163 set(_desc_GLOBAL "/* Mangling for Fortran global symbols without underscores. */")
164 set(_desc_GLOBAL_ "/* Mangling for Fortran global symbols with underscores. */")
165 set(_desc_MODULE "/* Mangling for Fortran module symbols without underscores. */")
166 set(_desc_MODULE_ "/* Mangling for Fortran module symbols with underscores. */")
167 foreach(macro GLOBAL GLOBAL_ MODULE MODULE_)
168 if(FortranCInterface_${macro}_MACRO)
169 set(HEADER_CONTENT "${HEADER_CONTENT}
171 #define ${MACRO_NAMESPACE}${macro}${FortranCInterface_${macro}_MACRO}
176 # Generate symbol mangling definitions.
178 set(HEADER_CONTENT "${HEADER_CONTENT}
179 /*--------------------------------------------------------------------------*/
180 /* Mangle some symbols automatically. */
183 foreach(f ${SYMBOLS})
184 if("${f}" MATCHES ":")
185 # Module symbol name. Parse "<module>:<function>" syntax.
186 string(REPLACE ":" ";" pieces "${f}")
187 list(GET pieces 0 module)
188 list(GET pieces 1 function)
189 string(TOUPPER "${module}" m_upper)
190 string(TOLOWER "${module}" m_lower)
191 string(TOUPPER "${function}" f_upper)
192 string(TOLOWER "${function}" f_lower)
193 if("${function}" MATCHES "_")
198 if(FortranCInterface_MODULE${form}_MACRO)
199 set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${module}_${function} ${MACRO_NAMESPACE}MODULE${form}(${m_lower},${f_lower}, ${m_upper},${f_upper})\n")
201 message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
204 # Global symbol name.
205 if("${f}" MATCHES "_")
210 string(TOUPPER "${f}" f_upper)
211 string(TOLOWER "${f}" f_lower)
212 if(FortranCInterface_GLOBAL${form}_MACRO)
213 set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${f} ${MACRO_NAMESPACE}GLOBAL${form}(${f_lower}, ${f_upper})\n")
215 message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
221 configure_file(${FortranCInterface_SOURCE_DIR}/Macro.h.in ${FILE} @ONLY)
224 function(FortranCInterface_VERIFY)
231 if("${arg}" STREQUAL "QUIET")
233 elseif("${arg}" STREQUAL "CXX")
238 "FortranCInterface_VERIFY - called with unknown argument:\n ${arg}")
242 if(NOT CMAKE_${lang}_COMPILER_LOADED)
244 "FortranCInterface_VERIFY(${lang}) requires ${lang} to be enabled.")
247 # Build the verification project if not yet built.
248 if(NOT DEFINED FortranCInterface_VERIFIED_${lang})
249 set(_desc "Verifying Fortran/${lang} Compiler Compatibility")
250 message(STATUS "${_desc}")
252 # Build a sample project which reports symbols.
253 set(CMAKE_TRY_COMPILE_CONFIGURATION Release)
254 try_compile(FortranCInterface_VERIFY_${lang}_COMPILED
255 ${FortranCInterface_BINARY_DIR}/Verify${lang}
256 ${FortranCInterface_SOURCE_DIR}/Verify
258 CMAKE_FLAGS -DVERIFY_CXX=${verify_cxx}
259 -DCMAKE_VERBOSE_MAKEFILE=ON
260 "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS}"
261 "-DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}"
262 "-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
263 "-DCMAKE_C_FLAGS_RELEASE:STRING=${CMAKE_C_FLAGS_RELEASE}"
264 "-DCMAKE_CXX_FLAGS_RELEASE:STRING=${CMAKE_CXX_FLAGS_RELEASE}"
265 "-DCMAKE_Fortran_FLAGS_RELEASE:STRING=${CMAKE_Fortran_FLAGS_RELEASE}"
266 OUTPUT_VARIABLE _output)
267 file(WRITE "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" "${_output}")
270 if(FortranCInterface_VERIFY_${lang}_COMPILED)
271 message(STATUS "${_desc} - Success")
272 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
273 "${_desc} passed with the following output:\n${_output}\n\n")
274 set(FortranCInterface_VERIFIED_${lang} 1 CACHE INTERNAL "Fortran/${lang} compatibility")
276 message(STATUS "${_desc} - Failed")
277 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
278 "${_desc} failed with the following output:\n${_output}\n\n")
279 set(FortranCInterface_VERIFIED_${lang} 0 CACHE INTERNAL "Fortran/${lang} compatibility")
281 unset(FortranCInterface_VERIFY_${lang}_COMPILED CACHE)
284 # Error if compilers are incompatible.
285 if(NOT FortranCInterface_VERIFIED_${lang} AND NOT quiet)
286 file(READ "${FortranCInterface_BINARY_DIR}/Verify${lang}/output.txt" _output)
287 string(REGEX REPLACE "\n" "\n " _output "${_output}")
289 "The Fortran compiler:\n ${CMAKE_Fortran_COMPILER}\n"
290 "and the ${lang} compiler:\n ${CMAKE_${lang}_COMPILER}\n"
291 "failed to compile a simple test project using both languages. "
292 "The output was:\n ${_output}")
296 # Restore including context policies.