| 1 |
# @HEADER |
| 2 |
# ***************************************************************************** |
| 3 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 4 |
# |
| 5 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 6 |
# SPDX-License-Identifier: BSD-3-Clause |
| 7 |
# ***************************************************************************** |
| 8 |
# @HEADER |
| 9 |
|
| 10 |
include(GlobalSet) |
| 11 |
|
| 12 |
# Function that defines variables describing the Fortran name mangling |
| 13 |
# convention |
| 14 |
# |
| 15 |
# Sets the following outputs on success: |
| 16 |
# |
| 17 |
# FC_FN_CASE |
| 18 |
# "UPPER" if names are translated to upper-case, |
| 19 |
# "LOWER" otherwise. |
| 20 |
# |
| 21 |
# FC_FN_UNDERSCORE |
| 22 |
# "NO_UNDER" if nothing is appended, "UNDER" if |
| 23 |
# one underscore is appended, and "SECOND_UNDER" |
| 24 |
# if a function with an underscore has a second |
| 25 |
# appended. |
| 26 |
# |
| 27 |
# FC_FUNC_DEFAULT |
| 28 |
# The default mangling for Fortran functions |
| 29 |
# that do not contain an underscore. |
| 30 |
# |
| 31 |
# FC_FUNC__DEFAULT |
| 32 |
# The default mangling for Fortran functions |
| 33 |
# that do contain an underscore. |
| 34 |
# |
| 35 |
# The Fortran 2003 name binding facilities and ISO_C_BINDING module |
| 36 |
# should be preferred over cpp macro trickery whenever possible. |
| 37 |
# |
| 38 |
function(fortran_mangling) |
| 39 |
|
| 40 |
if(NOT DEFINED FC_FN_CASE) |
| 41 |
|
| 42 |
if (${PROJECT_NAME}_VERBOSE_CONFIGURE) |
| 43 |
message("FORTRAN_MANGLING: Testing name Mangling Schemes!\n") |
| 44 |
endif() |
| 45 |
|
| 46 |
find_file(_fcmakelists fmangle/ ${CMAKE_MODULE_PATH}) |
| 47 |
if (NOT _fcmakelists) |
| 48 |
message(STATUS "Error, the directory fmangle could not be found so we can not determine Fortran name mangling!") |
| 49 |
return() |
| 50 |
endif() |
| 51 |
|
| 52 |
set(_fcmangledir ${PROJECT_BINARY_DIR}/CMakeFiles/CMakeTmp/fmangle) |
| 53 |
file(MAKE_DIRECTORY ${_fcmangledir}) |
| 54 |
|
| 55 |
foreach(cdef LOWER UPPER) |
| 56 |
|
| 57 |
foreach(udef UNDER NO_UNDER SECOND_UNDER) |
| 58 |
|
| 59 |
if (${PROJECT_NAME}_VERBOSE_CONFIGURE) |
| 60 |
message("FORTRAN_MANGLING: Testing ${cdef} ${udef}\n\n") |
| 61 |
endif() |
| 62 |
|
| 63 |
set(_fcmangledir_case "${_fcmangledir}/${cdef}/${udef}") |
| 64 |
file(MAKE_DIRECTORY "${_fcmangledir}/${cdef}") |
| 65 |
file(MAKE_DIRECTORY ${_fcmangledir_case}) |
| 66 |
|
| 67 |
set(COMMON_DEFS -DFC_FN_${cdef} -DFC_FN_${udef}) |
| 68 |
set(C_FLAGS "${CMAKE_C_FLAGS} ${${PROJECT_NAME}_EXTRA_LINK_FLAGS}") |
| 69 |
set(F_FLAGS "${CMAKE_Fortran_FLAGS} ${${PROJECT_NAME}_EXTRA_LINK_FLAGS}") |
| 70 |
try_compile(_fcmngl ${_fcmangledir_case} ${_fcmakelists} fmangle |
| 71 |
CMAKE_FLAGS |
| 72 |
"-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}" |
| 73 |
"-DCMAKE_C_FLAGS:STRING=${C_FLAGS}" |
| 74 |
"-DCMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}:STRING=${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}}" |
| 75 |
"-DCMAKE_Fortran_FLAGS:STRING=${F_FLAGS}" |
| 76 |
"-DCMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE}:STRING=${CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE}}" |
| 77 |
"-DCOMMON_DEFS=${COMMON_DEFS}" |
| 78 |
OUTPUT_VARIABLE _fcmngl_output |
| 79 |
) |
| 80 |
if (${PROJECT_NAME}_VERBOSE_CONFIGURE) |
| 81 |
message("${_fcmngl_output}\n\n") |
| 82 |
endif() |
| 83 |
|
| 84 |
if(_fcmngl) |
| 85 |
if (${PROJECT_NAME}_VERBOSE_CONFIGURE) |
| 86 |
message("FORTRAN_MANGLING: Bingo! ${cdef} ${udef} is the correct fortran name mangling!\n") |
| 87 |
endif() |
| 88 |
global_set(FC_FN_CASE ${cdef}) |
| 89 |
global_set(FC_FN_UNDERSCORE ${udef}) |
| 90 |
break() |
| 91 |
endif() |
| 92 |
|
| 93 |
endforeach() |
| 94 |
|
| 95 |
if(_fcmngl) |
| 96 |
break() |
| 97 |
endif() |
| 98 |
|
| 99 |
endforeach() |
| 100 |
|
| 101 |
if(_fcmngl) |
| 102 |
message(STATUS "Fortran name mangling: ${FC_FN_CASE} ${FC_FN_UNDERSCORE}") |
| 103 |
else() |
| 104 |
message(STATUS "Warning, cannot automatically determine Fortran mangling.") |
| 105 |
endif() |
| 106 |
|
| 107 |
endif() |
| 108 |
|
| 109 |
if (FC_FN_CASE STREQUAL LOWER) |
| 110 |
set(FC_NAME_NAME name) |
| 111 |
elseif (FC_FN_CASE STREQUAL UPPER) |
| 112 |
set(FC_NAME_NAME NAME) |
| 113 |
endif() |
| 114 |
|
| 115 |
if (FC_FN_UNDERSCORE) |
| 116 |
if(FC_FN_UNDERSCORE STREQUAL "UNDER") |
| 117 |
set(FC_FUNC_DEFAULT "(name,NAME) ${FC_NAME_NAME} ## _" CACHE INTERNAL "") |
| 118 |
set(FC_FUNC__DEFAULT "(name,NAME) ${FC_NAME_NAME} ## _" CACHE INTERNAL "") |
| 119 |
elseif(FC_FN_UNDERSCORE STREQUAL "SECOND_UNDER") |
| 120 |
set(FC_FUNC_DEFAULT "(name,NAME) ${FC_NAME_NAME} ## _" CACHE INTERNAL "") |
| 121 |
set(FC_FUNC__DEFAULT "(name,NAME) ${FC_NAME_NAME} ## __" CACHE INTERNAL "") |
| 122 |
else() |
| 123 |
set(FC_FUNC_DEFAULT "(name,NAME) ${FC_NAME_NAME}" CACHE INTERNAL "") |
| 124 |
set(FC_FUNC__DEFAULT "(name,NAME) ${FC_NAME_NAME}" CACHE INTERNAL "") |
| 125 |
endif() |
| 126 |
endif() |
| 127 |
|
| 128 |
endfunction() |
| 129 |
|
| 130 |
|
| 131 |
# 2008/10/26: rabartl: Below, these were macros that were also present in the |
| 132 |
# file that I got off of the web but I did not need them so they are disabled |
| 133 |
# (for now) |
| 134 |
|
| 135 |
## - Guess if the Fortran compiler returns REAL in C doubles. |
| 136 |
## fortran_floatret() |
| 137 |
## |
| 138 |
## If the Fortran compiler follows the f2c convention of |
| 139 |
## returning REALs in C doubles, routines like SLAMCH can |
| 140 |
## cause very difficult to find stack corruptions. This |
| 141 |
## test is not perfect; it just checks if C->Fortran->C |
| 142 |
## twice in a row does not crash. If the tests do crash, |
| 143 |
## FC_FN_FLOATRET is set to a true value, otherwise it |
| 144 |
## is set to a false value. |
| 145 |
## |
| 146 |
## The REAL kinds in Fortran 2003's ISO_C_BINDING module should |
| 147 |
## be used instead of this test whenever possible. |
| 148 |
## |
| 149 |
#macro(fortran_floatret) |
| 150 |
# if(NOT DEFINED FC_FN_FLOATRET) |
| 151 |
# # Find inputs |
| 152 |
# find_file(_fcindir floatret/ ${CMAKE_MODULE_PATH}) |
| 153 |
# set(_fcdir ${PROJECT_BINARY_DIR}/CMakeFiles/CMakeTmp/floatret) |
| 154 |
# file(MAKE_DIRECTORY ${_fcdir}) |
| 155 |
# try_compile(_fccrv ${_fcdir} ${_fcindir} floatret |
| 156 |
# CMAKE_FLAGS "-DFC_FN_DEFS:STRING=${FC_FN_DEFS}") |
| 157 |
# if(_fccrv) |
| 158 |
# execute_process(COMMAND ${_fcdir}/ctst |
| 159 |
# WORKING_DIRECTORY ${_fcdir} |
| 160 |
# RESULT_VARIABLE _fcrrv) |
| 161 |
# endif(_fccrv) |
| 162 |
# if(_fcrrv EQUAL 0) |
| 163 |
# set(_fc_fn_floatret 0) |
| 164 |
# else(_fcrrv EQUAL 0) |
| 165 |
# set(_fc_fn_floatret 1) |
| 166 |
# endif(_fcrrv EQUAL 0) |
| 167 |
# set(FC_FN_FLOATRET ${_fc_fn_floatret} CACHE BOOL |
| 168 |
# "Fortran returns REAL in double.") |
| 169 |
# message(STATUS "Fortran returns REAL in double: ${FC_FN_FLOATRET}") |
| 170 |
# endif(NOT DEFINED FC_FN_FLOATRET) |
| 171 |
#endmacro() |
| 172 |
# |
| 173 |
# |
| 174 |
## - Guess the convention for passing strings from C to Fortran. |
| 175 |
## fortran_stringarg() |
| 176 |
## |
| 177 |
## If string lengths are directly appended to each variable, e.g. |
| 178 |
## CALL foo('bar', 1.0) becomes foo({'b','a','r'}, 3, 1.0), then |
| 179 |
## FC_FN_STRINGARG is set to PAIRED. If the lengths are appended |
| 180 |
## to the call, e.g. foo({'b','a','r'}, 1.0, 3), FC_FN_STRINGARG |
| 181 |
## is set to TRAILING. |
| 182 |
## |
| 183 |
## This macro does not currently check for older Cray and VMS |
| 184 |
## conventions that require conversion functions. It also assumes |
| 185 |
## that the length is passed as the "natural" size type, C's size_t. |
| 186 |
## |
| 187 |
## The string kinds in Fortran 2003's ISO_C_BINDING module should |
| 188 |
## be used instead of these conventions whenever possible. |
| 189 |
## |
| 190 |
#set(FC_FN_STRINGARG_TYPES "PAIRED" "TRAILING") |
| 191 |
#macro(fortran_stringarg) |
| 192 |
# if(NOT DEFINED FC_FN_STRINGARG) |
| 193 |
# # Find inputs |
| 194 |
# find_file(_fcstrindir fstrings/ ${CMAKE_MODULE_PATH}) |
| 195 |
# set(_fcstrdir ${PROJECT_BINARY_DIR}/CMakeFiles/CMakeTmp/fstrings) |
| 196 |
# foreach(argtype ${FC_FN_STRINGARG_TYPES}) |
| 197 |
# file(MAKE_DIRECTORY ${_fcstrdir}) |
| 198 |
# try_compile(_fcstrcrv ${_fcstrdir} ${_fcstrindir} fstrings |
| 199 |
# CMAKE_FLAGS "-DFC_FN_DEFS:STRING=${FC_FN_DEFS}" "-Dargtype:STRING=${argtype}") |
| 200 |
# if(_fcstrcrv) |
| 201 |
# execute_process(COMMAND ${_fcstrdir}/ccheck |
| 202 |
# WORKING_DIRECTORY ${_fcstrdir} |
| 203 |
# RESULT_VARIABLE _fcstrrrv) |
| 204 |
# if(_fcstrrrv EQUAL 0) |
| 205 |
# set(_fcstr ${argtype}) |
| 206 |
# break() |
| 207 |
# endif(_fcstrrrv EQUAL 0) |
| 208 |
# endif(_fcstrcrv) |
| 209 |
# file(REMOVE_RECURSE ${_fcstrdir}) |
| 210 |
# endforeach(argtype) |
| 211 |
# if(DEFINED _fcstr) |
| 212 |
# set(FC_FN_STRINGARG ${_fcstr} CACHE STRING |
| 213 |
# "How Fortran accepts string arguments.") |
| 214 |
# message(STATUS "Fortran string passing: ${FC_FN_STRINGARG}") |
| 215 |
# else(DEFINED _fcstr) |
| 216 |
# message(STATUS "Cannot determine Fortran string passing.") |
| 217 |
# endif(DEFINED _fcstr) |
| 218 |
# endif(NOT DEFINED FC_FN_STRINGARG) |
| 219 |
#endmacro() |