Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / FortranCInterface.cmake
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.  The test is built in the Release configuration.
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 #=============================================================================
85 # Copyright 2008-2009 Kitware, Inc.
86 #
87 # Distributed under the OSI-approved BSD License (the "License");
88 # see accompanying file Copyright.txt for details.
89 #
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.)
96
97 #-----------------------------------------------------------------------------
98 # Execute at most once in a project.
99 if(FortranCInterface_SOURCE_DIR)
100   return()
101 endif()
102
103 # Use CMake 2.8.0 behavior for this module regardless of including context.
104 cmake_policy(PUSH)
105 cmake_policy(VERSION 2.8.0)
106
107 #-----------------------------------------------------------------------------
108 # Verify that C and Fortran are available.
109 foreach(lang C Fortran)
110   if(NOT CMAKE_${lang}_COMPILER_LOADED)
111     message(FATAL_ERROR
112       "FortranCInterface requires the ${lang} language to be enabled.")
113   endif()
114 endforeach()
115
116 #-----------------------------------------------------------------------------
117 set(FortranCInterface_SOURCE_DIR ${CMAKE_ROOT}/Modules/FortranCInterface)
118
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)
125 endif()
126
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)
131 endif()
132
133 # Load the detection results.
134 include(${FortranCInterface_BINARY_DIR}/Output.cmake)
135
136 #-----------------------------------------------------------------------------
137 function(FortranCInterface_HEADER file)
138   # Parse arguments.
139   if(IS_ABSOLUTE "${file}")
140     set(FILE "${file}")
141   else()
142     set(FILE "${CMAKE_CURRENT_BINARY_DIR}/${file}")
143   endif()
144   set(MACRO_NAMESPACE "FortranCInterface_")
145   set(SYMBOL_NAMESPACE)
146   set(SYMBOLS)
147   set(doing)
148   foreach(arg ${ARGN})
149     if("x${arg}" MATCHES "^x(SYMBOLS|SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
150       set(doing "${arg}")
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}")
155       set(doing)
156     else()
157       message(AUTHOR_WARNING "Unknown argument: \"${arg}\"")
158     endif()
159   endforeach()
160
161   # Generate macro definitions.
162   set(HEADER_CONTENT)
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}
170 ${_desc_${macro}}
171 #define ${MACRO_NAMESPACE}${macro}${FortranCInterface_${macro}_MACRO}
172 ")
173     endif()
174   endforeach()
175
176   # Generate symbol mangling definitions.
177   if(SYMBOLS)
178     set(HEADER_CONTENT "${HEADER_CONTENT}
179 /*--------------------------------------------------------------------------*/
180 /* Mangle some symbols automatically.                                       */
181 ")
182   endif()
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 "_")
194         set(form "_")
195       else()
196         set(form "")
197       endif()
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")
200       else()
201         message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
202       endif()
203     else()
204       # Global symbol name.
205       if("${f}" MATCHES "_")
206         set(form "_")
207       else()
208         set(form "")
209       endif()
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")
214       else()
215         message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
216       endif()
217     endif()
218   endforeach()
219
220   # Store the content.
221   configure_file(${FortranCInterface_SOURCE_DIR}/Macro.h.in ${FILE} @ONLY)
222 endfunction()
223
224 function(FortranCInterface_VERIFY)
225   # Check arguments.
226
227   set(lang C)
228   set(quiet 0)
229   set(verify_cxx 0)
230   foreach(arg ${ARGN})
231     if("${arg}" STREQUAL "QUIET")
232       set(quiet 1)
233     elseif("${arg}" STREQUAL "CXX")
234       set(lang CXX)
235       set(verify_cxx 1)
236     else()
237       message(FATAL_ERROR
238         "FortranCInterface_VERIFY - called with unknown argument:\n  ${arg}")
239     endif()
240   endforeach()
241
242   if(NOT CMAKE_${lang}_COMPILER_LOADED)
243     message(FATAL_ERROR
244       "FortranCInterface_VERIFY(${lang}) requires ${lang} to be enabled.")
245   endif()
246
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}")
251
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
257       VerifyFortranC
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}")
268
269     # Report results.
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")
275     else()
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")
280     endif()
281     unset(FortranCInterface_VERIFY_${lang}_COMPILED CACHE)
282   endif()
283
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}")
288     message(FATAL_ERROR
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}")
293   endif()
294 endfunction()
295
296 # Restore including context policies.
297 cmake_policy(POP)