CMakeLists.txt 3.2 KB

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