CMakeLists.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. cmake_minimum_required (VERSION 3.1)
  2. project(testf C CXX Fortran)
  3. message("CTEST_FULL_OUTPUT ")
  4. set(CMAKE_VERBOSE_MAKEFILE 1)
  5. message("ENV_FLAGS = $ENV{FFLAGS}")
  6. message("CMAKE_Fortran_COMPILER_INIT = ${CMAKE_Fortran_COMPILER_INIT}")
  7. message("CMAKE_Fortran_COMPILER_FULLPATH = ${CMAKE_Fortran_COMPILER_FULLPATH}")
  8. message("CMAKE_Fortran_COMPILER = ${CMAKE_Fortran_COMPILER}")
  9. message("CMAKE_Fortran_FLAGS = ${CMAKE_Fortran_FLAGS}")
  10. set(_SHARED SHARED)
  11. if(CMAKE_Fortran_COMPILER_ID MATCHES "^(XL|VisualAge)$")
  12. # We do not implement SHARED Fortran libs on AIX yet!
  13. # Workaround: Set LINKER_LANGUAGE to C, which uses 'xlc' and Fortran implicits.
  14. set(_SHARED STATIC)
  15. elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  16. # g77 2.96 does not support shared libs on Itanium because g2c is not -fPIC
  17. execute_process(COMMAND ${CMAKE_Fortran_COMPILER} --version
  18. OUTPUT_VARIABLE output ERROR_VARIABLE output)
  19. if("${output}" MATCHES "Red Hat .* 2\\.96")
  20. set(_SHARED STATIC)
  21. endif()
  22. endif()
  23. # Pick a module .def file with the properly mangled symbol name.
  24. set(world_def "")
  25. if(WIN32 AND NOT CYGWIN)
  26. if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
  27. set(world_def world_gnu.def)
  28. elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel" OR
  29. CMAKE_GENERATOR MATCHES "Visual Studio") # Intel plugin
  30. set(world_def world_icl.def)
  31. endif()
  32. endif()
  33. add_library(hello STATIC hello.f)
  34. add_library(world ${_SHARED} world.f ${world_def})
  35. add_executable(testf testf.f)
  36. target_link_libraries(testf hello world)
  37. function(test_fortran_c_interface_module)
  38. message(STATUS "Testing FortranCInterface module")
  39. # test the C to Fortran interface module
  40. include(FortranCInterface)
  41. FortranCInterface_VERIFY()
  42. FortranCInterface_VERIFY(CXX)
  43. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  44. if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "SunPro|MIPSpro|PathScale|Absoft")
  45. set(module_expected 1)
  46. endif()
  47. if(FortranCInterface_MODULE_FOUND OR module_expected)
  48. set(srcs foo.f)
  49. set(FORTRAN_FUNCTIONS test_mod:sub)
  50. set(MYC_DEFS TEST_MOD)
  51. else()
  52. message("${CMAKE_Fortran_COMPILER_ID} compilers do not support"
  53. " linking Fortran module procedures from C")
  54. endif()
  55. endif()
  56. list(APPEND FORTRAN_FUNCTIONS my_sub mysub)
  57. FortranCInterface_HEADER(foo.h
  58. MACRO_NAMESPACE "FC_"
  59. SYMBOL_NAMESPACE "F_"
  60. SYMBOLS ${FORTRAN_FUNCTIONS}
  61. )
  62. include_directories("${CMAKE_CURRENT_BINARY_DIR}")
  63. # if the name mangling is not found for a F90 compiler
  64. # print out some diagnostic stuff for the dashboard
  65. if(NOT FortranCInterface_GLOBAL_FOUND OR
  66. (NOT FortranCInterface_MODULE_FOUND AND module_expected) )
  67. find_program(FortranCInterface_EXE
  68. NAMES FortranCInterface
  69. PATHS ${FortranCInterface_BINARY_DIR} ${FortranCInterface_BINARY_DIR}/Debug
  70. NO_DEFAULT_PATH
  71. )
  72. find_program(DUMPBIN dumpbin)
  73. find_program(NM nm)
  74. if(FortranCInterface_EXE)
  75. if(DEPENDS)
  76. execute_process(COMMAND ${DUMPBIN} /symbols "${FortranCInterface_EXE}"
  77. OUTPUT_VARIABLE out)
  78. message("symbols in ${FortranCInterface_EXE}:\n${out}")
  79. endif()
  80. if(NM)
  81. execute_process(COMMAND ${NM} "${FortranCInterface_EXE}"
  82. OUTPUT_VARIABLE out)
  83. message("symbols in ${FortranCInterface_EXE}:\n${out}")
  84. endif()
  85. endif()
  86. endif()
  87. message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
  88. message("C = ${CMAKE_C_COMPILER_ID}")
  89. add_library(myfort mysub.f ${srcs})
  90. add_library(myc myc.c)
  91. target_link_libraries(myc myfort)
  92. set_property(TARGET myc PROPERTY COMPILE_DEFINITIONS ${MYC_DEFS})
  93. add_library(mycxx mycxx.cxx)
  94. target_link_libraries(mycxx myc)
  95. add_executable(mainc mainc.c)
  96. target_link_libraries(mainc myc)
  97. add_executable(maincxx maincxx.c)
  98. target_link_libraries(maincxx mycxx)
  99. # print out some stuff to help debug on machines via cdash
  100. file(READ "${CMAKE_CURRENT_BINARY_DIR}/foo.h" fooh)
  101. message("foo.h contents:\n${fooh}")
  102. endfunction()
  103. # if the id's match or the compilers are compatible, then
  104. # call the test_fortran_c_interface_module function
  105. if("${CMAKE_Fortran_COMPILER_ID}:${CMAKE_C_COMPILER_ID}" MATCHES
  106. "(Intel:MSVC|Absoft:GNU)"
  107. OR ("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "${CMAKE_C_COMPILER_ID}" ))
  108. test_fortran_c_interface_module()
  109. else()
  110. message("Fortran does not match c compiler")
  111. message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
  112. message("C = ${CMAKE_C_COMPILER_ID}")
  113. # hack to make g77 work after CL has been enabled
  114. # as a language, cmake needs language specific versions
  115. # of these variables....
  116. if(WIN32 AND CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
  117. set(CMAKE_CREATE_CONSOLE_EXE )
  118. set(CMAKE_LIBRARY_PATH_FLAG "-L")
  119. set(CMAKE_LINK_LIBRARY_FLAG "-l")
  120. set(CMAKE_LINK_LIBRARY_SUFFIX )
  121. endif()
  122. # gnu and sunpro do not use the same flags here...
  123. # however if LDFLAGS is used to set -m64 it causes odd stuf
  124. # with the fortran build
  125. if( (CMAKE_C_COMPILER_ID MATCHES "GNU")
  126. AND (CMAKE_Fortran_COMPILER_ID MATCHES "SunPro"))
  127. set(CMAKE_EXE_LINKER_FLAGS "")
  128. set(CMAKE_Fortran_FLAGS "")
  129. endif()
  130. endif()