CMakeLists.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. cmake_minimum_required(VERSION ${CMAKE_VERSION})
  4. project(FortranCInterface C Fortran)
  5. include(${FortranCInterface_BINARY_DIR}/Input.cmake OPTIONAL)
  6. # Check if the C compiler supports '$' in identifiers.
  7. include(CheckCSourceCompiles)
  8. check_c_source_compiles("
  9. extern int dollar$(void);
  10. int main() { return 0; }
  11. " C_SUPPORTS_DOLLAR)
  12. # List manglings of global symbol names to try.
  13. set(global_symbols
  14. my_sub # VisualAge
  15. my_sub_ # GNU, Intel, HP, SunPro, MIPSpro, PGI
  16. my_sub__ # GNU g77
  17. MY_SUB # Intel on Windows
  18. mysub # VisualAge
  19. mysub_ # GNU, Intel, HP, SunPro, MIPSpro, PGI
  20. MYSUB # Intel on Windows
  21. ${FortranCInterface_GLOBAL_SYMBOLS}
  22. )
  23. list(REMOVE_DUPLICATES global_symbols)
  24. # List manglings of module symbol names to try.
  25. set(module_symbols
  26. __my_module_MOD_my_sub # GNU 4.3
  27. __my_module_NMOD_my_sub # VisualAge
  28. __my_module__my_sub # GNU 4.2
  29. __mymodule_MOD_mysub # GNU 4.3
  30. __mymodule_NMOD_mysub # VisualAge
  31. __mymodule__mysub # GNU 4.2
  32. my_module$my_sub # HP
  33. my_module_mp_my_sub_ # Intel
  34. MY_MODULE_mp_MY_SUB # Intel on Windows
  35. my_module_my_sub_ # PGI
  36. my_module_MP_my_sub # NAG
  37. mymodule$mysub # HP
  38. mymodule_mp_mysub_ # Intel
  39. MYMODULE_mp_MYSUB # Intel on Windows
  40. mymodule_mysub_ # PGI
  41. mymodule_MP_mysub # NAG
  42. ${FortranCInterface_MODULE_SYMBOLS}
  43. )
  44. list(REMOVE_DUPLICATES module_symbols)
  45. # Note that some compiler manglings cannot be invoked from C:
  46. # MIPSpro uses "MY_SUB.in.MY_MODULE"
  47. # SunPro uses "my_module.my_sub_"
  48. # PathScale uses "MY_SUB.in.MY_MODULE"
  49. # Add module symbols only with Fortran90.
  50. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  51. set(myfort_modules mymodule.f90 my_module.f90)
  52. set(call_mod call_mod.f90)
  53. set_property(SOURCE main.F PROPERTY COMPILE_DEFINITIONS CALL_MOD)
  54. else()
  55. set(module_symbols)
  56. endif()
  57. # Generate C symbol sources.
  58. set(symbol_sources)
  59. if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "^(PathScale|Cray)$")
  60. # Provide mymodule_ and my_module_ init symbols because:
  61. # - PGI Fortran uses module init symbols
  62. # but not for:
  63. # - PathScale Fortran uses module init symbols but module symbols
  64. # use '.in.' so we cannot provide them anyway.
  65. # - Cray Fortran >= 7.3.2 uses module init symbols but module symbols
  66. # use 'mysub$mymodule_' so we cannot provide them anyway.
  67. list(APPEND symbol_sources mymodule_.c my_module_.c MY_MODULE.c MYMODULE.c)
  68. endif()
  69. foreach(symbol IN LISTS global_symbols module_symbols)
  70. # Skip symbols with '$' if C cannot handle them.
  71. if(C_SUPPORTS_DOLLAR OR NOT "${symbol}" MATCHES "\\$")
  72. if("${symbol}" MATCHES "SUB")
  73. set(upper "-UPPER")
  74. else()
  75. set(upper)
  76. endif()
  77. string(REPLACE "$" "S" name "${symbol}")
  78. set(source ${CMAKE_CURRENT_BINARY_DIR}/symbols/${name}${upper}.c)
  79. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/symbol.c.in ${source} @ONLY)
  80. list(APPEND symbol_sources ${source})
  81. endif()
  82. endforeach()
  83. # Provide symbols through Fortran.
  84. add_library(myfort STATIC mysub.f my_sub.f ${myfort_modules})
  85. # Provide symbols through C but fall back to Fortran.
  86. add_library(symbols STATIC ${symbol_sources})
  87. target_link_libraries(symbols PUBLIC myfort)
  88. # In case the Fortran compiler produces PIC by default make sure
  89. # the C compiler produces PIC even if it is not its default.
  90. set_property(TARGET symbols PROPERTY POSITION_INDEPENDENT_CODE 1)
  91. # Require symbols through Fortran.
  92. add_executable(FortranCInterface main.F call_sub.f ${call_mod})
  93. target_link_libraries(FortranCInterface PUBLIC symbols)