[Cmake-commits] [cmake-commits] hoffman committed FortranCInterface.cmake 1.1 1.2

cmake-commits at cmake.org cmake-commits at cmake.org
Mon Oct 27 21:42:43 EDT 2008


Update of /cvsroot/CMake/CMake/Modules
In directory public:/mounts/ram/cvs-serv18666

Modified Files:
	FortranCInterface.cmake 
Log Message:
ENH: add support for module functions


Index: FortranCInterface.cmake
===================================================================
RCS file: /cvsroot/CMake/CMake/Modules/FortranCInterface.cmake,v
retrieving revision 1.1
retrieving revision 1.2
diff -C 2 -d -r1.1 -r1.2
*** FortranCInterface.cmake	27 Oct 2008 19:23:14 -0000	1.1
--- FortranCInterface.cmake	28 Oct 2008 01:42:41 -0000	1.2
***************
*** 17,23 ****
  #   name mangling and call test_fortran_mangling until one of them
  #   works.
  #
! 
! function(test_fortran_mangling PREFIX ISUPPER POSTFIX RESULT)
    if(ISUPPER)
      set(FUNCTION "${PREFIX}SUB${POSTFIX}")
--- 17,35 ----
  #   name mangling and call test_fortran_mangling until one of them
  #   works.
+ #  discover_fortran_module_mangling - try different types of 
+ #  fortran modle name mangling to find one that works
  #
! #
! #
! # this function tests a single fortran mangling.  
! # CODE - test code to try should define a subroutine called "sub"
! # PREFIX - string to put in front of sub
! # POSTFIX - string to put after sub
! # ISUPPER - if TRUE then sub will be called as SUB
! # DOC - string used in status checking Fortran ${DOC} linkage
! # RESULT place to store result TRUE if this linkage works, FALSE
! #        if not.
! #
! function(test_fortran_mangling CODE PREFIX ISUPPER POSTFIX DOC RESULT)
    if(ISUPPER)
      set(FUNCTION "${PREFIX}SUB${POSTFIX}")
***************
*** 30,40 ****
      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckFortranLink")
    file(REMOVE_RECURSE "${TMP_DIR}")
!   file(WRITE "${TMP_DIR}/test.f"
!     "
!       subroutine sub
!       end subroutine sub
!     "
!     )
!   message(STATUS "checking Fortran linkage: ${FUNCTION}")
    file(WRITE "${TMP_DIR}/ctof.c"
      "
--- 42,47 ----
      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckFortranLink")
    file(REMOVE_RECURSE "${TMP_DIR}")
!   file(WRITE "${TMP_DIR}/test.f" "${CODE}"    )
!   message(STATUS "checking Fortran ${DOC} linkage: ${FUNCTION}")
    file(WRITE "${TMP_DIR}/ctof.c"
      "
***************
*** 62,73 ****
  endfunction(test_fortran_mangling)
  
  function(discover_fortran_mangling prefix isupper suffix found )
    foreach(pre "_" "" "__")
      foreach(isup TRUE FALSE)
        foreach(post "" "_")
          set(worked FALSE)
!         test_fortran_mangling("${pre}" ${isup} "${post}" worked )
          if(worked)
!           message(STATUS "found Fotran linkage")
            set(${isupper} "${isup}" PARENT_SCOPE)
            set(${prefix} "${pre}" PARENT_SCOPE)
--- 69,118 ----
  endfunction(test_fortran_mangling)
  
+ # this function discovers the name mangling scheme used
+ # for functions in a fortran module.  
+ function(discover_fortran_module_mangling prefix suffix found)
+   set(CODE 
+     "
+       module test_interface
+       interface dummy
+          module procedure sub
+       end interface
+       contains
+         subroutine sub
+         end subroutine
+       end module test_interface
+     ")
+   
+   foreach(interface 
+       "test_interface$" 
+       ".__test_interface_NMOD_" 
+       "__test_interface_MOD_")
+     test_fortran_mangling("${CODE}" "${interface}"
+       ${FORTRAN_C_MANGLING_UPPERCASE} "" "module" worked)
+     if(worked)
+       string(REGEX REPLACE "(.*)test_interface(.*)" "\\1" pre "${interface}")
+       string(REGEX REPLACE "(.*)test_interface(.*)" "\\2" post "${interface}")
+       set(${prefix} "${pre}" PARENT_SCOPE)
+       set(${suffix} "${post}" PARENT_SCOPE)
+       set(${found} TRUE PARENT_SCOPE)
+       return()
+     endif(worked)
+   endforeach(interface)
+ endfunction(discover_fortran_module_mangling)
+ 
+ 
  function(discover_fortran_mangling prefix isupper suffix found )
+   set(CODE 
+     "
+       subroutine sub
+       end subroutine sub
+     ")
    foreach(pre "_" "" "__")
      foreach(isup TRUE FALSE)
        foreach(post "" "_")
          set(worked FALSE)
!         test_fortran_mangling("${CODE}" "${pre}" ${isup} "${post}" "function" worked )
          if(worked)
!           message(STATUS "found Fortran function linkage")
            set(${isupper} "${isup}" PARENT_SCOPE)
            set(${prefix} "${pre}" PARENT_SCOPE)
***************
*** 84,87 ****
--- 129,133 ----
  function(create_fortran_c_interface NAMESPACE FUNCTIONS HEADER)
    if(NOT FORTRAN_C_MANGLING_FOUND)
+     # find regular fortran function mangling
      discover_fortran_mangling(prefix isupper suffix found)
      if(NOT found)
***************
*** 89,92 ****
--- 135,139 ----
        return()
      endif(NOT found)
+     # find fortran module function mangling
      set(FORTRAN_C_PREFIX "${prefix}" CACHE INTERNAL
        "PREFIX for Fortran to c name mangling")
***************
*** 97,100 ****
--- 144,163 ----
      set(FORTRAN_C_MANGLING_FOUND TRUE CACHE INTERNAL 
        "Was fortran to c mangling found" )
+     set(prefix )
+     set(suffix )
+     set(found FALSE)
+     discover_fortran_module_mangling(prefix suffix found)
+     if(found)
+       message(STATUS "found Fortran module linkage")
+       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 TRUE CACHE INTERNAL
+         "SUFFIX for Fortran to c name mangling")
+     else(found)
+       set(FORTRAN_C_MODULE_MANGLING_FOUND FALSE CACHE INTERNAL
+         "SUFFIX for Fortran to c name mangling")
+     endif(found)
    endif(NOT FORTRAN_C_MANGLING_FOUND)
    foreach(f ${${FUNCTIONS}})
***************
*** 104,111 ****
        string(TOLOWER "${f}" ff)
      endif()
!     set(function "${FORTRAN_C_PREFIX}${ff}${FORTRAN_C_SUFFIX}")
!     set(HEADER_CONTENT "${HEADER_CONTENT}
  #define ${NAMESPACE}${f} ${function}
  ")
    endforeach(f)
    configure_file(
--- 167,182 ----
        string(TOLOWER "${f}" ff)
      endif()
!     if("${f}" MATCHES ":")
!       string(REGEX REPLACE "(.*):(.*)" "\\1" module "${f}")
!       string(REGEX REPLACE "(.*):(.*)" "\\2" function "${f}")
!       set(HEADER_CONTENT "${HEADER_CONTENT}
! #define ${NAMESPACE}${module}_${function} ${FORTRAN_C_MODULE_PREFIX}${module}${FORTRAN_C_MODULE_SUFFIX}${function}
! ")
!     else("${f}" MATCHES ":")
!       set(function "${FORTRAN_C_PREFIX}${ff}${FORTRAN_C_SUFFIX}")
!       set(HEADER_CONTENT "${HEADER_CONTENT}
  #define ${NAMESPACE}${f} ${function}
  ")
+     endif("${f}" MATCHES ":")
    endforeach(f)
    configure_file(
***************
*** 115,135 ****
  endfunction()
  
- #  TODO
- # need to add support for module linking
- # module test_interface
- #
- #    interface dummy
- #        module procedure module_function
- #    end interface
- #
- # contains
- #
- #    subroutine module_function
- #    end subroutine
- #
- # end module test_interface
- #
- # produces this:
- # __test_interface_MOD_module_function
- #
- # 
--- 186,187 ----



More information about the Cmake-commits mailing list