CMakeLists.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. mysub # VisualAge
  26. mysub_ # GNU, Intel, HP, SunPro, MIPSpro
  27. ${FortranCInterface_GLOBAL_SYMBOLS}
  28. )
  29. list(REMOVE_DUPLICATES global_symbols)
  30. # List manglings of module symbol names to try.
  31. set(module_symbols
  32. __my_module_MOD_my_sub # GNU 4.3
  33. __my_module_NMOD_my_sub # VisualAge
  34. __my_module__my_sub # GNU 4.2
  35. __mymodule_MOD_mysub # GNU 4.3
  36. __mymodule_NMOD_mysub # VisualAge
  37. __mymodule__mysub # GNU 4.2
  38. my_module$my_sub # HP
  39. my_module_mp_my_sub_ # Intel
  40. my_module_my_sub_ # PGI
  41. mymodule$mysub # HP
  42. mymodule_mp_mysub_ # Intel
  43. mymodule_mysub_ # PGI
  44. ${FortranCInterface_MODULE_SYMBOLS}
  45. )
  46. list(REMOVE_DUPLICATES module_symbols)
  47. # Note that some compiler manglings cannot be invoked from C:
  48. # MIPSpro uses "MY_SUB.in.MY_MODULE"
  49. # SunPro uses "my_module.my_sub_"
  50. # Add module symbols only with Fortran90.
  51. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  52. set(myfort_modules mymodule.f90 my_module.f90)
  53. set(call_mod call_mod.f90)
  54. set_property(SOURCE main.F PROPERTY COMPILE_DEFINITIONS CALL_MOD)
  55. else()
  56. set(module_symbols)
  57. endif()
  58. # Generate C symbol sources.
  59. foreach(symbol IN LISTS global_symbols module_symbols)
  60. # Skip symbols with '$' if C cannot handle them.
  61. if(C_SUPPORTS_DOLLAR OR NOT "${symbol}" MATCHES "\\$")
  62. if("${symbol}" MATCHES "SUB")
  63. set(upper "-UPPER")
  64. else()
  65. set(upper)
  66. endif()
  67. string(REPLACE "$" "S" name "${symbol}")
  68. set(source ${CMAKE_CURRENT_BINARY_DIR}/symbols/${name}${upper}.c)
  69. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/symbol.c.in ${source} @ONLY)
  70. list(APPEND symbol_sources ${source})
  71. endif()
  72. endforeach()
  73. # Provide symbols through Fortran.
  74. add_library(myfort STATIC mysub.f my_sub.f ${myfort_modules})
  75. # Provide symbols through C but fall back to Fortran.
  76. add_library(symbols STATIC mymodule_.c my_module_.c ${symbol_sources})
  77. target_link_libraries(symbols myfort)
  78. # Require symbols through Fortran.
  79. add_executable(FortranCInterface main.F call_sub.f ${call_mod})
  80. target_link_libraries(FortranCInterface symbols)