Rewrite FortranCInterface module

This is a new FortranCInterface.cmake module to replace the previous
prototype.  All module support files lie in a FortranCInterface
directory next to it.

This module uses a new approach to detect Fortran symbol mangling.  We
build a single test project which defines symbols in a Fortran library
(one per object-file) and calls them from a Fortran executable.  The
executable links to a C library which defines symbols encoding all known
manglings (one per object-file).  The C library falls back to the
Fortran library for symbols it cannot provide.  Therefore the executable
will always link, but prefers the C-implemented symbols when they match.
These symbols store string literals of the form INFO:symbol[<name>] so
we can parse them out of the executable.

This module also provides a simpler interface.  It always detects the
mangling as soon as it is included.  A single macro is provided to
generate mangling macros and optionally pre-mangled symbols.
This commit is contained in:
Brad King 2009-08-05 13:40:29 -04:00
parent 7b36fd637c
commit 80f0201b37
16 changed files with 510 additions and 260 deletions

View File

@ -1,231 +1,334 @@
# FortranCInterface.cmake # - Fortran/C Interface Detection
# This module automatically detects the API by which C and Fortran
# languages interact. Variables indicate if the mangling is found:
# FortranCInterface_GLOBAL_FOUND = Global subroutines and functions
# FortranCInterface_MODULE_FOUND = Module subroutines and functions
# (declared by "MODULE PROCEDURE")
# A function is provided to generate a C header file containing macros
# to mangle symbol names:
# FortranCInterface_HEADER(<file>
# [MACRO_NAMESPACE <macro-ns>]
# [SYMBOL_NAMESPACE <ns>]
# [SYMBOLS [<module>:]<function> ...])
# It generates in <file> definitions of the following macros:
# #define FortranCInterface_GLOBAL (name,NAME) ...
# #define FortranCInterface_GLOBAL_(name,NAME) ...
# #define FortranCInterface_MODULE (mod,name, MOD,NAME) ...
# #define FortranCInterface_MODULE_(mod,name, MOD,NAME) ...
# These macros mangle four categories of Fortran symbols,
# respectively:
# - Global symbols without '_': call mysub()
# - Global symbols with '_' : call my_sub()
# - Module symbols without '_': use mymod; call mysub()
# - Module symbols with '_' : use mymod; call my_sub()
# If mangling for a category is not known, its macro is left undefined.
# All macros require raw names in both lower case and upper case.
# The MACRO_NAMESPACE option replaces the default "FortranCInterface_"
# prefix with a given namespace "<macro-ns>".
# #
# This file defines the function create_fortran_c_interface. # The SYMBOLS option lists symbols to mangle automatically with C
# this function is used to create a configured header file # preprocessor definitions:
# that contains a mapping from C to a Fortran function using # <function> ==> #define <ns><function> ...
# the correct name mangling scheme as defined by the current # <module>:<function> ==> #define <ns><module>_<function> ...
# fortran compiler. # If the mangling for some symbol is not known then no preprocessor
# definition is created, and a warning is displayed.
# The SYMBOL_NAMESPACE option prefixes all preprocessor definitions
# generated by the SYMBOLS option with a given namespace "<ns>".
# #
# The function tages a list of functions and the name of # Example usage:
# a header file to configure. # include(FortranCInterface)
# FortranCInterface_HEADER(FC.h MACRO_NAMESPACE "FC_")
# This creates a "FC.h" header that defines mangling macros
# FC_GLOBAL(), FC_GLOBAL_(), FC_MODULE(), and FC_MODULE_().
# #
# This file also defines some helper functions that are used # Example usage:
# to detect the fortran name mangling scheme used by the # include(FortranCInterface)
# current Fortran compiler. # FortranCInterface_HEADER(FCMangle.h
# test_fortran_mangling - test a single fortran mangling # MACRO_NAMESPACE "FC_"
# discover_fortran_mangling - loop over all combos of fortran # SYMBOL_NAMESPACE "FC_"
# name mangling and call test_fortran_mangling until one of them # SYMBOLS mysub mymod:my_sub)
# works. # This creates a "FC.h" header that defines the same FC_*() mangling
# discover_fortran_module_mangling - try different types of # macros as the previous example plus preprocessor symbols FC_mysub
# fortran modle name mangling to find one that works # and FC_mymod_my_sub.
# #
# # FortranCInterface is aware of possible GLOBAL and MODULE manglings
# # for many Fortran compilers, but it also provides an interface to
# this function tests a single fortran mangling. # specify new possible manglings. Set the variables
# CODE - test code to try should define a subroutine called "sub" # FortranCInterface_GLOBAL_SYMBOLS
# PREFIX - string to put in front of sub # FortranCInterface_MODULE_SYMBOLS
# POSTFIX - string to put after sub # before including FortranCInterface to specify manglings of the
# ISUPPER - if TRUE then sub will be called as SUB # symbols "MySub", "My_Sub", "MyModule:MySub", and "My_Module:My_Sub".
# DOC - string used in status checking Fortran ${DOC} linkage # For example, the code:
# SUB - the name of the SUB to call # set(FortranCInterface_GLOBAL_SYMBOLS mysub_ my_sub__ MYSUB_)
# RESULT place to store result TRUE if this linkage works, FALSE # # ^^^^^ ^^^^^^ ^^^^^
# if not. # set(FortranCInterface_MODULE_SYMBOLS
# # __mymodule_MOD_mysub __my_module_MOD_my_sub)
function(test_fortran_mangling CODE PREFIX ISUPPER POSTFIX DOC SUB RESULT) # # ^^^^^^^^ ^^^^^ ^^^^^^^^^ ^^^^^^
if(ISUPPER) # include(FortranCInterface)
string(TOUPPER "${SUB}" sub) # tells FortranCInterface to try given GLOBAL and MODULE manglings.
else(ISUPPER) # (The carets point at raw symbol names for clarity in this example
string(TOLOWER "${SUB}" sub) # but are not needed.)
endif(ISUPPER)
set(FUNCTION "${PREFIX}${sub}${POSTFIX}") #-----------------------------------------------------------------------------
# create a fortran file with sub called sub # Execute at most once in a project.
# if(FortranCInterface_SOURCE_DIR)
set(TMP_DIR return()
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckFortranLink") endif()
file(REMOVE_RECURSE "${TMP_DIR}")
file(WRITE "${TMP_DIR}/test.f" "${CODE}" ) #-----------------------------------------------------------------------------
message(STATUS "checking Fortran ${DOC} linkage: ${FUNCTION}") # Set up an interface detection project.
file(WRITE "${TMP_DIR}/ctof.c" set(FortranCInterface_SOURCE_DIR ${CMAKE_ROOT}/Modules/FortranCInterface)
" set(FortranCInterface_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/FortranCInterface)
extern ${FUNCTION}(); configure_file(${FortranCInterface_SOURCE_DIR}/Input.cmake.in
int main() { ${FUNCTION}(); return 0;} ${FortranCInterface_BINARY_DIR}/Input.cmake @ONLY)
"
# Detect the Fortran/C interface on the first run or when the
# configuration changes.
if(${FortranCInterface_BINARY_DIR}/Input.cmake
IS_NEWER_THAN ${FortranCInterface_BINARY_DIR}/Output.cmake
OR ${FortranCInterface_SOURCE_DIR}/Output.cmake.in
IS_NEWER_THAN ${FortranCInterface_BINARY_DIR}/Output.cmake
OR ${FortranCInterface_SOURCE_DIR}/CMakeLists.txt
IS_NEWER_THAN ${FortranCInterface_BINARY_DIR}/Output.cmake
OR ${CMAKE_CURRENT_LIST_FILE}
IS_NEWER_THAN ${FortranCInterface_BINARY_DIR}/Output.cmake
) )
file(WRITE "${TMP_DIR}/CMakeLists.txt" message(STATUS "Detecting Fortran/C Interface")
" set(_result)
project(testf C Fortran)
add_library(flib test.f) # Build a sample project which reports symbols.
add_executable(ctof ctof.c) try_compile(FortranCInterface_COMPILED
target_link_libraries(ctof flib) ${FortranCInterface_BINARY_DIR}
" ${FortranCInterface_SOURCE_DIR}
) FortranCInterface
set(FORTRAN_NAME_MANGLE_TEST FALSE) OUTPUT_VARIABLE FortranCInterface_OUTPUT)
try_compile(FORTRAN_NAME_MANGLE_TEST "${TMP_DIR}" "${TMP_DIR}" set(FortranCInterface_COMPILED ${FortranCInterface_COMPILED})
testf unset(FortranCInterface_COMPILED CACHE)
OUTPUT_VARIABLE output)
if(FORTRAN_NAME_MANGLE_TEST) # Locate the sample project executable.
set(${RESULT} TRUE PARENT_SCOPE) if(FortranCInterface_COMPILED)
find_program(FortranCInterface_EXE
NAMES FortranCInterface
PATHS ${FortranCInterface_BINARY_DIR} ${FortranCInterface_BINARY_DIR}/Debug
NO_DEFAULT_PATH
)
set(FortranCInterface_EXE ${FortranCInterface_EXE})
unset(FortranCInterface_EXE CACHE)
else() else()
set(${RESULT} FALSE PARENT_SCOPE) set(_result "Failed to compile")
set(FortranCInterface_EXE)
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Fortran/C interface test project failed with the following output:\n"
"${FortranCInterface_OUTPUT}\n")
endif() endif()
endfunction(test_fortran_mangling)
# this function discovers the name mangling scheme used # Load symbols from INFO:symbol[] strings in the executable.
# for functions in a fortran module. set(FortranCInterface_SYMBOLS)
function(discover_fortran_module_mangling prefix suffix found) if(FortranCInterface_EXE)
set(CODE file(STRINGS "${FortranCInterface_EXE}" _info_strings
" LIMIT_COUNT 8 REGEX "INFO:[^[]*\\[")
module test_interface foreach(info ${_info_strings})
interface dummy if("${info}" MATCHES ".*INFO:symbol\\[([^]]*)\\].*")
module procedure sub string(REGEX REPLACE ".*INFO:symbol\\[([^]]*)\\].*" "\\1" symbol "${info}")
end interface list(APPEND FortranCInterface_SYMBOLS ${symbol})
contains
subroutine sub
end subroutine
end module test_interface
")
set(worked FALSE)
foreach(interface
"test_interface$"
"TEST_INTERFACE_mp_"
"_test_interface__"
"__test_interface__"
"__test_interface_NMOD_"
"__test_interface_MOD_")
test_fortran_mangling("${CODE}" "${interface}"
${FORTRAN_C_MANGLING_UPPERCASE} "" "module" "sub" worked)
if(worked)
# if this is the upper case module match then
# lower case it for the extraction of pre and post strings
if("${interface}" MATCHES "TEST_INTERFACE")
string(TOLOWER "${interface}" interface)
endif() endif()
string(REGEX REPLACE "(.*)test_interface(.*)" "\\1" pre "${interface}") endforeach()
string(REGEX REPLACE "(.*)test_interface(.*)" "\\2" post "${interface}") elseif(NOT _result)
set(${prefix} "${pre}" PARENT_SCOPE) set(_result "Failed to load sample executable")
set(${suffix} "${post}" PARENT_SCOPE) endif()
set(${found} TRUE PARENT_SCOPE)
return()
endif(worked)
endforeach(interface)
if(NOT worked)
message(STATUS "Failed to find C binding to Fortran module functions.")
set(${prefix} "BROKEN_C_FORTRAN_MODULE_BINDING" PARENT_SCOPE)
set(${suffix} "BROKEN_C_FORTRAN_MODULE_BINDING" PARENT_SCOPE)
set(${found} FALSE PARENT_SCOPE)
endif(NOT worked)
endfunction(discover_fortran_module_mangling)
set(_case_mysub "LOWER")
set(_case_my_sub "LOWER")
set(_case_MYSUB "UPPER")
set(_case_MY_SUB "UPPER")
set(_global_regex "^(_*)(mysub|MYSUB)([_$]*)$")
set(_global__regex "^(_*)(my_sub|MY_SUB)([_$]*)$")
set(_module_regex "^(_*)(mymodule|MYMODULE)([A-Za-z_$]*)(mysub|MYSUB)([_$]*)$")
set(_module__regex "^(_*)(my_module|MY_MODULE)([A-Za-z_$]*)(my_sub|MY_SUB)([_$]*)$")
function(discover_fortran_mangling prefix isupper suffix extra_under_score # Parse the symbol names.
found ) foreach(symbol ${FortranCInterface_SYMBOLS})
set(CODE foreach(form "" "_")
" # Look for global symbols.
subroutine sub string(REGEX REPLACE "${_global_${form}regex}"
end subroutine sub "\\1;\\2;\\3" pieces "${symbol}")
") list(LENGTH pieces len)
foreach(post "_" "") if(len EQUAL 3)
foreach(isup FALSE TRUE) set(FortranCInterface_GLOBAL_${form}SYMBOL "${symbol}")
foreach(pre "" "_" "__") list(GET pieces 0 FortranCInterface_GLOBAL_${form}PREFIX)
set(worked FALSE) list(GET pieces 1 name)
test_fortran_mangling("${CODE}" "${pre}" ${isup} list(GET pieces 2 FortranCInterface_GLOBAL_${form}SUFFIX)
"${post}" "function" sub worked ) set(FortranCInterface_GLOBAL_${form}CASE "${_case_${name}}")
if(worked) endif()
message(STATUS "found Fortran function linkage")
set(${isupper} "${isup}" PARENT_SCOPE) # Look for module symbols.
set(${prefix} "${pre}" PARENT_SCOPE) string(REGEX REPLACE "${_module_${form}regex}"
set(${suffix} "${post}" PARENT_SCOPE) "\\1;\\2;\\3;\\4;\\5" pieces "${symbol}")
set(${found} TRUE PARENT_SCOPE) list(LENGTH pieces len)
set(CODE if(len EQUAL 5)
" set(FortranCInterface_MODULE_${form}SYMBOL "${symbol}")
subroutine my_sub list(GET pieces 0 FortranCInterface_MODULE_${form}PREFIX)
end subroutine my_sub list(GET pieces 1 module)
") list(GET pieces 2 FortranCInterface_MODULE_${form}MIDDLE)
set(worked FALSE) list(GET pieces 3 name)
test_fortran_mangling("${CODE}" "${pre}" ${isup} list(GET pieces 4 FortranCInterface_MODULE_${form}SUFFIX)
"${post}" "function with _ " my_sub worked ) set(FortranCInterface_MODULE_${form}CASE "${_case_${name}}")
if(worked) endif()
set(${extra_under_score} FALSE PARENT_SCOPE)
else(worked)
test_fortran_mangling("${CODE}" "${pre}" ${isup}
"${post}_" "function with _ " my_sub worked )
if(worked)
set(${extra_under_score} TRUE PARENT_SCOPE)
endif(worked)
endif(worked)
return()
endif()
endforeach()
endforeach() endforeach()
endforeach() endforeach()
set(${found} FALSE PARENT_SCOPE)
endfunction(discover_fortran_mangling)
function(create_fortran_c_interface NAMESPACE FUNCTIONS HEADER) # Construct mangling macro definitions.
if(NOT FORTRAN_C_MANGLING_FOUND) set(_name_LOWER "name")
# find regular fortran function mangling set(_name_UPPER "NAME")
discover_fortran_mangling(prefix isupper suffix extra_under found) foreach(form "" "_")
if(NOT found) if(FortranCInterface_GLOBAL_${form}SYMBOL)
message(SEND_ERROR "Could not find fortran c name mangling.") if(FortranCInterface_GLOBAL_${form}PREFIX)
return() set(_prefix "${FortranCInterface_GLOBAL_${form}PREFIX}##")
endif(NOT found) else()
# find fortran module function mangling set(_prefix "")
set(FORTRAN_C_PREFIX "${prefix}" CACHE INTERNAL endif()
"PREFIX for Fortran to c name mangling") if(FortranCInterface_GLOBAL_${form}SUFFIX)
set(FORTRAN_C_SUFFIX "${suffix}" CACHE INTERNAL set(_suffix "##${FortranCInterface_GLOBAL_${form}SUFFIX}")
"SUFFIX for Fortran to c name mangling") else()
set(FORTRAN_C_MANGLING_UPPERCASE ${isupper} CACHE INTERNAL set(_suffix "")
"Was fortran to c mangling found" ) endif()
set(FORTRAN_C_MANGLING_EXTRA_UNDERSCORE ${extra_under} CACHE INTERNAL set(_name "${_name_${FortranCInterface_GLOBAL_${form}CASE}}")
"If a function has a _ in the name does the compiler append an extra _" ) set(FortranCInterface_GLOBAL${form}_MACRO
set(FORTRAN_C_MANGLING_FOUND TRUE CACHE INTERNAL "(name,NAME) ${_prefix}${_name}${_suffix}")
"Was fortran to c mangling found" )
set(prefix )
set(suffix )
set(found FALSE)
# only try this if the compiler is F90 compatible
if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
discover_fortran_module_mangling(prefix suffix found)
endif(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
if(found)
message(STATUS "found Fortran module linkage")
else(found)
message(STATUS "Failed to find Fortran module linkage")
endif(found)
set(FORTRAN_C_MODULE_PREFIX "${prefix}" CACHE INTERNAL
"PREFIX for Fortran to c name mangling")
set(FORTRAN_C_MODULE_SUFFIX "${suffix}" CACHE INTERNAL
"SUFFIX for Fortran to c name mangling")
set(FORTRAN_C_MODULE_MANGLING_FOUND ${found} CACHE INTERNAL
"Was for Fortran to c name mangling found for modules")
endif(NOT FORTRAN_C_MANGLING_FOUND)
foreach(f ${${FUNCTIONS}})
if(FORTRAN_C_MANGLING_UPPERCASE)
string(TOUPPER "${f}" fcase)
else()
string(TOLOWER "${f}" fcase)
endif() endif()
if("${f}" MATCHES ":") if(FortranCInterface_MODULE_${form}SYMBOL)
string(REGEX REPLACE "(.*):(.*)" "\\1" module "${f}") if(FortranCInterface_MODULE_${form}PREFIX)
string(REGEX REPLACE "(.*):(.*)" "\\2" function "${f}") set(_prefix "${FortranCInterface_MODULE_${form}PREFIX}##")
string(REGEX REPLACE "(.*):(.*)" "\\1" module_case "${fcase}") else()
string(REGEX REPLACE "(.*):(.*)" "\\2" function_case "${fcase}") set(_prefix "")
set(HEADER_CONTENT "${HEADER_CONTENT} endif()
#define ${NAMESPACE}${module}_${function} ${FORTRAN_C_MODULE_PREFIX}${module_case}${FORTRAN_C_MODULE_SUFFIX}${function_case} if(FortranCInterface_MODULE_${form}SUFFIX)
") set(_suffix "##${FortranCInterface_MODULE_${form}SUFFIX}")
else("${f}" MATCHES ":") else()
set(function "${FORTRAN_C_PREFIX}${fcase}${FORTRAN_C_SUFFIX}") set(_suffix "")
if("${f}" MATCHES "_" AND FORTRAN_C_MANGLING_EXTRA_UNDERSCORE) endif()
set(function "${function}_") set(_name "${_name_${FortranCInterface_MODULE_${form}CASE}}")
endif("${f}" MATCHES "_" AND FORTRAN_C_MANGLING_EXTRA_UNDERSCORE) set(_middle "##${FortranCInterface_MODULE_${form}MIDDLE}##")
set(HEADER_CONTENT "${HEADER_CONTENT} set(FortranCInterface_MODULE${form}_MACRO
#define ${NAMESPACE}${f} ${function} "(mod_name,name, mod_NAME,NAME) ${_prefix}mod_${_name}${_middle}${_name}${_suffix}")
") endif()
endif("${f}" MATCHES ":") endforeach()
endforeach(f)
configure_file(
"${CMAKE_ROOT}/Modules/FortranCInterface.h.in"
${HEADER} @ONLY)
message(STATUS "created ${HEADER}")
endfunction()
# Summarize what is available.
foreach(scope GLOBAL MODULE)
if(FortranCInterface_${scope}_SYMBOL AND
FortranCInterface_${scope}__SYMBOL)
set(FortranCInterface_${scope}_FOUND 1)
else()
set(FortranCInterface_${scope}_FOUND 0)
endif()
endforeach()
# Record the detection results.
configure_file(${FortranCInterface_SOURCE_DIR}/Output.cmake.in
${FortranCInterface_BINARY_DIR}/Output.cmake @ONLY)
file(APPEND ${FortranCInterface_BINARY_DIR}/Output.cmake "\n")
# Report the results.
if(FortranCInterface_GLOBAL_FOUND)
if(FortranCInterface_MODULE_FOUND)
set(_result "Found GLOBAL and MODULE mangling")
else(FortranCInterface_MODULE_FOUND)
set(_result "Found GLOBAL but not MODULE mangling")
endif()
elseif(NOT _result)
set(_result "Failed to recognize symbols")
endif()
message(STATUS "Detecting Fortran/C Interface - ${_result}")
endif()
# Load the detection results.
include(${FortranCInterface_BINARY_DIR}/Output.cmake)
#-----------------------------------------------------------------------------
function(FortranCInterface_HEADER file)
# Parse arguments.
if(IS_ABSOLUTE "${file}")
set(FILE "${file}")
else()
set(FILE "${CMAKE_CURRENT_BINARY_DIR}/${file}")
endif()
set(MACRO_NAMESPACE "FortranCInterface_")
set(SYMBOL_NAMESPACE)
set(SYMBOLS)
set(doing)
foreach(arg ${ARGN})
if("x${arg}" MATCHES "^x(SYMBOLS|SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
set(doing "${arg}")
elseif("x${doing}" MATCHES "^x(SYMBOLS)$")
list(APPEND "${doing}" "${arg}")
elseif("x${doing}" MATCHES "^x(SYMBOL_NAMESPACE|MACRO_NAMESPACE)$")
set("${doing}" "${arg}")
set(doing)
else()
message(AUTHOR_WARNING "Unknown argument: \"${arg}\"")
endif()
endforeach()
# Generate macro definitions.
set(HEADER_CONTENT)
set(_desc_GLOBAL "/* Mangling for Fortran global symbols without underscores. */")
set(_desc_GLOBAL_ "/* Mangling for Fortran global symbols with underscores. */")
set(_desc_MODULE "/* Mangling for Fortran module symbols without underscores. */")
set(_desc_MODULE_ "/* Mangling for Fortran module symbols with underscores. */")
foreach(macro GLOBAL GLOBAL_ MODULE MODULE_)
if(FortranCInterface_${macro}_MACRO)
set(HEADER_CONTENT "${HEADER_CONTENT}
${_desc_${macro}}
#define ${MACRO_NAMESPACE}${macro}${FortranCInterface_${macro}_MACRO}
")
endif()
endforeach()
# Generate symbol mangling definitions.
if(SYMBOLS)
set(HEADER_CONTENT "${HEADER_CONTENT}
/*--------------------------------------------------------------------------*/
/* Mangle some symbols automatically. */
")
endif()
foreach(f ${SYMBOLS})
if("${f}" MATCHES ":")
# Module symbol name. Parse "<module>:<function>" syntax.
string(REPLACE ":" ";" pieces "${f}")
list(GET pieces 0 module)
list(GET pieces 1 function)
string(TOUPPER "${module}" m_upper)
string(TOLOWER "${module}" m_lower)
string(TOUPPER "${function}" f_upper)
string(TOLOWER "${function}" f_lower)
if("${function}" MATCHES "_")
set(form "_")
else()
set(form "")
endif()
if(FortranCInterface_MODULE${form}_MACRO)
set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${module}_${function} ${MACRO_NAMESPACE}MODULE${form}(${m_lower},${f_lower}, ${m_upper},${f_upper})\n")
else()
message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
endif()
else()
# Global symbol name.
if("${f}" MATCHES "_")
set(form "_")
else()
set(form "")
endif()
string(TOUPPER "${f}" f_upper)
string(TOLOWER "${f}" f_lower)
if(FortranCInterface_GLOBAL${form}_MACRO)
set(HEADER_CONTENT "${HEADER_CONTENT}#define ${SYMBOL_NAMESPACE}${f} ${MACRO_NAMESPACE}GLOBAL${form}(${f_lower}, ${f_upper})\n")
else()
message(AUTHOR_WARNING "No FortranCInterface mangling known for ${f}")
endif()
endif()
endforeach(f)
# Store the content.
configure_file(${FortranCInterface_SOURCE_DIR}/Macro.h.in ${FILE} @ONLY)
endfunction()

View File

@ -1,9 +0,0 @@
/* This file is automatically generated by CMake, DO NOT EDIT.
It contains a mapping from Fortran functions so they can
be called from C or C++. */
@HEADER_CONTENT@

View File

@ -0,0 +1,74 @@
cmake_minimum_required(VERSION 2.6.3)
project(FortranCInterface C Fortran)
include(${FortranCInterface_BINARY_DIR}/Input.cmake OPTIONAL)
# Check if the C compiler supports '$' in identifiers.
include(CheckCSourceCompiles)
check_c_source_compiles("
extern int dollar$(void);
int main() { return 0; }
" C_SUPPORTS_DOLLAR)
# List manglings of global symbol names to try.
set(global_symbols
my_sub_ # GNU, Intel, HP, SunPro, MIPSpro
my_sub # VisualAge
mysub_ # GNU, Intel, HP, SunPro, MIPSpro
mysub # VisualAge
${FortranCInterface_GLOBAL_SYMBOLS}
)
list(REMOVE_DUPLICATES global_symbols)
# List manglings of module symbol names to try.
set(module_symbols
__my_module_MOD_my_sub # GNU
__my_module_NMOD_my_sub # VisualAge
__mymodule_MOD_mysub # GNU
__mymodule_NMOD_mysub # VisualAge
my_module$my_sub # HP
my_module_mp_my_sub_ # Intel
mymodule$mysub # HP
mymodule_mp_mysub_ # Intel
${FortranCInterface_MODULE_SYMBOLS}
)
list(REMOVE_DUPLICATES module_symbols)
# Note that some compiler manglings cannot be invoked from C:
# MIPSpro uses "MY_SUB.in.MY_MODULE"
# SunPro uses "my_module.my_sub_"
# Add module symbols only with Fortran90.
if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
set(myfort_modules mymodule.f90 my_module.f90)
set(call_mod call_mod.f90)
set_property(SOURCE main.F PROPERTY COMPILE_DEFINITIONS CALL_MOD)
else()
set(module_symbols)
endif()
# Generate C symbol sources.
foreach(symbol IN LISTS global_symbols module_symbols)
# Skip symbols with '$' if C cannot handle them.
if(C_SUPPORTS_DOLLAR OR NOT "${symbol}" MATCHES "\\$")
if("${symbol}" MATCHES "SUB")
set(upper "-UPPER")
else()
set(upper)
endif()
string(REPLACE "$" "S" name "${symbol}")
set(source ${CMAKE_CURRENT_BINARY_DIR}/symbols/${name}${upper}.c)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/symbol.c.in ${source} @ONLY)
list(APPEND symbol_sources ${source})
endif()
endforeach()
# Provide symbols through Fortran.
add_library(myfort STATIC mysub.f my_sub.f ${myfort_modules})
# Provide symbols through C but fall back to Fortran.
add_library(symbols STATIC ${symbol_sources})
target_link_libraries(symbols myfort)
# Require symbols through Fortran.
add_executable(FortranCInterface main.F call_sub.f ${call_mod})
target_link_libraries(FortranCInterface symbols)

View File

@ -0,0 +1,3 @@
set(CMAKE_Fortran_COMPILER_ID "@CMAKE_Fortran_COMPILER_ID@")
set(FortranCInterface_GLOBAL_SYMBOLS "@FortranCInterface_GLOBAL_SYMBOLS@")
set(FortranCInterface_MODULE_SYMBOLS "@FortranCInterface_MODULE_SYMBOLS@")

View File

@ -0,0 +1,4 @@
#ifndef @MACRO_NAMESPACE@HEADER_INCLUDED
#define @MACRO_NAMESPACE@HEADER_INCLUDED
@HEADER_CONTENT@
#endif

View File

@ -0,0 +1,33 @@
# Global symbol without underscore.
set(FortranCInterface_GLOBAL_SYMBOL "@FortranCInterface_GLOBAL_SYMBOL@")
set(FortranCInterface_GLOBAL_PREFIX "@FortranCInterface_GLOBAL_PREFIX@")
set(FortranCInterface_GLOBAL_SUFFIX "@FortranCInterface_GLOBAL_SUFFIX@")
set(FortranCInterface_GLOBAL_CASE "@FortranCInterface_GLOBAL_CASE@")
set(FortranCInterface_GLOBAL_MACRO "@FortranCInterface_GLOBAL_MACRO@")
# Global symbol with underscore.
set(FortranCInterface_GLOBAL__SYMBOL "@FortranCInterface_GLOBAL__SYMBOL@")
set(FortranCInterface_GLOBAL__PREFIX "@FortranCInterface_GLOBAL__PREFIX@")
set(FortranCInterface_GLOBAL__SUFFIX "@FortranCInterface_GLOBAL__SUFFIX@")
set(FortranCInterface_GLOBAL__CASE "@FortranCInterface_GLOBAL__CASE@")
set(FortranCInterface_GLOBAL__MACRO "@FortranCInterface_GLOBAL__MACRO@")
# Module symbol without underscore.
set(FortranCInterface_MODULE_SYMBOL "@FortranCInterface_MODULE_SYMBOL@")
set(FortranCInterface_MODULE_PREFIX "@FortranCInterface_MODULE_PREFIX@")
set(FortranCInterface_MODULE_MIDDLE "@FortranCInterface_MODULE_MIDDLE@")
set(FortranCInterface_MODULE_SUFFIX "@FortranCInterface_MODULE_SUFFIX@")
set(FortranCInterface_MODULE_CASE "@FortranCInterface_MODULE_CASE@")
set(FortranCInterface_MODULE_MACRO "@FortranCInterface_MODULE_MACRO@")
# Module symbol with underscore.
set(FortranCInterface_MODULE__SYMBOL "@FortranCInterface_MODULE__SYMBOL@")
set(FortranCInterface_MODULE__PREFIX "@FortranCInterface_MODULE__PREFIX@")
set(FortranCInterface_MODULE__MIDDLE "@FortranCInterface_MODULE__MIDDLE@")
set(FortranCInterface_MODULE__SUFFIX "@FortranCInterface_MODULE__SUFFIX@")
set(FortranCInterface_MODULE__CASE "@FortranCInterface_MODULE__CASE@")
set(FortranCInterface_MODULE__MACRO "@FortranCInterface_MODULE__MACRO@")
# Summarize what was found.
set(FortranCInterface_GLOBAL_FOUND @FortranCInterface_GLOBAL_FOUND@)
set(FortranCInterface_MODULE_FOUND @FortranCInterface_MODULE_FOUND@)

View File

@ -0,0 +1,6 @@
subroutine call_mod
use mymodule
use my_module
call mysub()
call my_sub()
end subroutine call_mod

View File

@ -0,0 +1,4 @@
subroutine call_sub
call mysub()
call my_sub()
end

View File

@ -0,0 +1,6 @@
program main
call call_sub()
#ifdef CALL_MOD
call call_mod()
#endif
end

View File

@ -0,0 +1,8 @@
module my_module
interface my_interface
module procedure my_sub
end interface
contains
subroutine my_sub
end subroutine my_sub
end module my_module

View File

@ -0,0 +1,2 @@
subroutine my_sub
end

View File

@ -0,0 +1,8 @@
module mymodule
interface myinterface
module procedure mysub
end interface
contains
subroutine mysub
end subroutine mysub
end module mymodule

View File

@ -0,0 +1,2 @@
subroutine mysub
end

View File

@ -0,0 +1,4 @@
const char* @symbol@(void)
{
return "INFO:symbol[@symbol@]";
}

View File

@ -15,47 +15,48 @@ function(test_fortran_c_interface_module)
# test the C to Fortran interface module # test the C to Fortran interface module
include(FortranCInterface) include(FortranCInterface)
if(CMAKE_Fortran_COMPILER_SUPPORTS_F90) if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
if(FORTRAN_C_MODULE_MANGLING_FOUND) if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "SunPro|MIPSpro")
set(module_expected 1)
endif()
if(FortranCInterface_MODULE_FOUND OR module_expected)
set(srcs foo.f) set(srcs foo.f)
set(FORTRAN_FUNCTIONS test_mod:sub) set(FORTRAN_FUNCTIONS test_mod:sub)
else(FORTRAN_C_MODULE_MANGLING_FOUND) set(MYC_DEFS TEST_MOD)
if(CMAKE_Fortran_COMPILER_ID MATCHES "SunPro|MIPSpro") else()
message("${CMAKE_Fortran_COMPILER_ID} compilers do not support" message("${CMAKE_Fortran_COMPILER_ID} compilers do not support"
" linking Fortran module procedures from C") " linking Fortran module procedures from C")
else() endif()
message("This will fail, but let the user see the error")
set(srcs foo.f)
set(FORTRAN_FUNCTIONS test_mod:sub)
endif()
endif(FORTRAN_C_MODULE_MANGLING_FOUND)
endif() endif()
set(FORTRAN_FUNCTIONS ${FORTRAN_FUNCTIONS} my_sub mysub ) list(APPEND FORTRAN_FUNCTIONS my_sub mysub)
create_fortran_c_interface("F_" FORTRAN_FUNCTIONS FortranCInterface_HEADER(foo.h
"${testf_BINARY_DIR}/foo.h") MACRO_NAMESPACE "FC_"
SYMBOL_NAMESPACE "F_"
SYMBOLS ${FORTRAN_FUNCTIONS}
)
include_directories("${testf_BINARY_DIR}") include_directories("${testf_BINARY_DIR}")
# if the name mangling is not found for a F90 compiler # if the name mangling is not found for a F90 compiler
# print out some diagnostic stuff for the dashboard # print out some diagnostic stuff for the dashboard
if(CMAKE_Fortran_COMPILER_SUPPORTS_F90) if(NOT FortranCInterface_GLOBAL_FOUND OR
if(NOT FORTRAN_C_MODULE_MANGLING_FOUND) (NOT FortranCInterface_MODULE_FOUND AND module_expected) )
file(GLOB_RECURSE O_OBJFILES find_program(FortranCInterface_EXE
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckFortranLink/*.o" NAMES FortranCInterface
"*.o" ) PATHS ${FortranCInterface_BINARY_DIR} ${FortranCInterface_BINARY_DIR}/Debug
file(GLOB_RECURSE OBJ_OBJFILES NO_DEFAULT_PATH
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckFortranLink/*.obj") )
find_program(DUMPBIN dumpbin) find_program(DUMPBIN dumpbin)
find_program(NM nm) find_program(NM nm)
foreach(ofile ${O_OBJFILES} ${OBJ_OBJFILES}) if(FortranCInterface_EXE)
if(DEPENDS) if(DEPENDS)
execute_process(COMMAND ${DUMPBIN} /symbols "${ofile}" execute_process(COMMAND ${DUMPBIN} /symbols "${FortranCInterface_EXE}"
OUTPUT_VARIABLE out) OUTPUT_VARIABLE out)
message("symbols in ${ofile}:\n${out}") message("symbols in ${FortranCInterface_EXE}:\n${out}")
endif() endif()
if(NM) if(NM)
execute_process(COMMAND ${NM} "${ofile}" OUTPUT_VARIABLE out) execute_process(COMMAND ${NM} "${FortranCInterface_EXE}"
message("symbols in ${ofile}:\n${out}") OUTPUT_VARIABLE out)
endif() message("symbols in ${FortranCInterface_EXE}:\n${out}")
endforeach() endif()
endif() endif()
endif() endif()
message("Fortran = ${CMAKE_Fortran_COMPILER_ID}") message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
@ -65,6 +66,7 @@ function(test_fortran_c_interface_module)
add_library(myc myc.c) add_library(myc myc.c)
target_link_libraries(myc myfort) target_link_libraries(myc myfort)
set_property(TARGET myc PROPERTY COMPILE_DEFINITIONS ${MYC_DEFS})
add_library(mycxx mycxx.cxx) add_library(mycxx mycxx.cxx)
target_link_libraries(mycxx myc) target_link_libraries(mycxx myc)

View File

@ -5,7 +5,7 @@ int myc(void)
{ {
F_mysub(); F_mysub();
F_my_sub(); F_my_sub();
#ifdef F_test_mod_sub #ifdef TEST_MOD
F_test_mod_sub(); F_test_mod_sub();
#endif #endif
return 0; return 0;