1
#=============================================================================
2
# Copyright 2009 Kitware, Inc.
4
# Distributed under the OSI-approved BSD License (the "License");
5
# see accompanying file Copyright.txt for details.
7
# This software is distributed WITHOUT ANY WARRANTY; without even the
8
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
# See the License for more information.
10
#=============================================================================
12
cmake_minimum_required(VERSION 2.6.3)
13
project(FortranCInterface C Fortran)
14
include(${FortranCInterface_BINARY_DIR}/Input.cmake OPTIONAL)
16
# Check if the C compiler supports '$' in identifiers.
17
include(CheckCSourceCompiles)
18
check_c_source_compiles("
19
extern int dollar$(void);
20
int main() { return 0; }
23
# List manglings of global symbol names to try.
26
my_sub_ # GNU, Intel, HP, SunPro, MIPSpro
28
MY_SUB # Intel on Windows
30
mysub_ # GNU, Intel, HP, SunPro, MIPSpro
31
MYSUB # Intel on Windows
32
${FortranCInterface_GLOBAL_SYMBOLS}
34
list(REMOVE_DUPLICATES global_symbols)
36
# List manglings of module symbol names to try.
38
__my_module_MOD_my_sub # GNU 4.3
39
__my_module_NMOD_my_sub # VisualAge
40
__my_module__my_sub # GNU 4.2
41
__mymodule_MOD_mysub # GNU 4.3
42
__mymodule_NMOD_mysub # VisualAge
43
__mymodule__mysub # GNU 4.2
45
my_module_mp_my_sub_ # Intel
46
MY_MODULE_mp_MY_SUB # Intel on Windows
47
my_module_my_sub_ # PGI
49
mymodule_mp_mysub_ # Intel
50
MYMODULE_mp_MYSUB # Intel on Windows
52
${FortranCInterface_MODULE_SYMBOLS}
54
list(REMOVE_DUPLICATES module_symbols)
56
# Note that some compiler manglings cannot be invoked from C:
57
# MIPSpro uses "MY_SUB.in.MY_MODULE"
58
# SunPro uses "my_module.my_sub_"
60
# Add module symbols only with Fortran90.
61
if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
62
set(myfort_modules mymodule.f90 my_module.f90)
63
set(call_mod call_mod.f90)
64
set_property(SOURCE main.F PROPERTY COMPILE_DEFINITIONS CALL_MOD)
69
# Generate C symbol sources.
70
foreach(symbol IN LISTS global_symbols module_symbols)
71
# Skip symbols with '$' if C cannot handle them.
72
if(C_SUPPORTS_DOLLAR OR NOT "${symbol}" MATCHES "\\$")
73
if("${symbol}" MATCHES "SUB")
78
string(REPLACE "$" "S" name "${symbol}")
79
set(source ${CMAKE_CURRENT_BINARY_DIR}/symbols/${name}${upper}.c)
80
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/symbol.c.in ${source} @ONLY)
81
list(APPEND symbol_sources ${source})
85
# Provide symbols through Fortran.
86
add_library(myfort STATIC mysub.f my_sub.f ${myfort_modules})
88
# Provide symbols through C but fall back to Fortran.
89
add_library(symbols STATIC mymodule_.c my_module_.c ${symbol_sources})
90
target_link_libraries(symbols myfort)
92
# Require symbols through Fortran.
93
add_executable(FortranCInterface main.F call_sub.f ${call_mod})
94
target_link_libraries(FortranCInterface symbols)