CMakeLists.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 MATCHES "GNU|LCC")
  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|LCC")
  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|PathScale|Absoft|Fujitsu")
  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(myfort_obj OBJECT mysub.f)
  94. add_library(myc_use_obj myc.c $<TARGET_OBJECTS:myfort_obj>)
  95. add_executable(mainc_use_obj mainc.c)
  96. target_link_libraries(mainc_use_obj myc_use_obj)
  97. add_library(mycxx mycxx.cxx)
  98. target_link_libraries(mycxx myc)
  99. add_executable(mainc mainc.c)
  100. target_link_libraries(mainc myc)
  101. add_executable(maincxx maincxx.c)
  102. target_link_libraries(maincxx mycxx)
  103. # print out some stuff to help debug on machines via cdash
  104. file(READ "${CMAKE_CURRENT_BINARY_DIR}/foo.h" fooh)
  105. message("foo.h contents:\n${fooh}")
  106. endfunction()
  107. # if the id's match or the compilers are compatible, then
  108. # call the test_fortran_c_interface_module function
  109. if("${CMAKE_Fortran_COMPILER_ID}:${CMAKE_C_COMPILER_ID}" MATCHES
  110. "(Intel(LLVM)?:MSVC|Absoft:GNU)"
  111. OR ("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "${CMAKE_C_COMPILER_ID}" ))
  112. test_fortran_c_interface_module()
  113. else()
  114. message("Fortran does not match c compiler")
  115. message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
  116. message("C = ${CMAKE_C_COMPILER_ID}")
  117. # hack to make g77 work after CL has been enabled
  118. # as a language, cmake needs language specific versions
  119. # of these variables....
  120. if(WIN32 AND CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
  121. set(CMAKE_Fortran_CREATE_CONSOLE_EXE )
  122. set(CMAKE_LIBRARY_PATH_FLAG "-L")
  123. set(CMAKE_LINK_LIBRARY_FLAG "-l")
  124. set(CMAKE_LINK_LIBRARY_SUFFIX )
  125. endif()
  126. # gnu and sunpro do not use the same flags here...
  127. # however if LDFLAGS is used to set -m64 it causes odd stuf
  128. # with the fortran build
  129. if( (CMAKE_C_COMPILER_ID MATCHES "GNU")
  130. AND (CMAKE_Fortran_COMPILER_ID MATCHES "SunPro"))
  131. set(CMAKE_EXE_LINKER_FLAGS "")
  132. set(CMAKE_Fortran_FLAGS "")
  133. endif()
  134. endif()