CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. cmake_minimum_required (VERSION 2.6)
  2. project(testf Fortran C)
  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. add_executable(testf hello.f)
  11. function(test_fortran_c_interface_module)
  12. message(STATUS "Testing FortranCInterface module")
  13. # test the C to Fortran interface module
  14. include(FortranCInterface)
  15. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  16. if(FORTRAN_C_MODULE_MANGLING_FOUND)
  17. set(srcs foo.f)
  18. set(FORTRAN_FUNCTIONS test_mod:sub)
  19. else(FORTRAN_C_MODULE_MANGLING_FOUND)
  20. if(CMAKE_Fortran_COMPILER_ID MATCHES "SunPro|MIPSpro")
  21. message("${CMAKE_Fortran_COMPILER_ID} compilers do not support"
  22. " linking Fortran module procedures from C")
  23. else()
  24. message("This will fail, but let the user see the error")
  25. set(srcs foo.f)
  26. set(FORTRAN_FUNCTIONS test_mod:sub)
  27. endif()
  28. endif(FORTRAN_C_MODULE_MANGLING_FOUND)
  29. endif()
  30. set(FORTRAN_FUNCTIONS ${FORTRAN_FUNCTIONS} my_sub mysub )
  31. set(srcs ${srcs} mysub.f foo.c)
  32. create_fortran_c_interface("F_" FORTRAN_FUNCTIONS
  33. "${testf_BINARY_DIR}/foo.h")
  34. include_directories("${testf_BINARY_DIR}")
  35. # if the name mangling is not found for a F90 compiler
  36. # print out some diagnostic stuff for the dashboard
  37. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  38. if(NOT FORTRAN_C_MODULE_MANGLING_FOUND)
  39. file(GLOB_RECURSE O_OBJFILES
  40. "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckFortranLink/*.o"
  41. "*.o" )
  42. file(GLOB_RECURSE OBJ_OBJFILES
  43. "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckFortranLink/*.obj")
  44. find_program(DUMPBIN dumpbin)
  45. find_program(NM nm)
  46. foreach(ofile ${O_OBJFILES} ${OBJ_OBJFILES})
  47. if(DEPENDS)
  48. execute_process(COMMAND ${DUMPBIN} /symbols "${ofile}"
  49. OUTPUT_VARIABLE out)
  50. message("symbols in ${ofile}:\n${out}")
  51. endif()
  52. if(NM)
  53. execute_process(COMMAND ${NM} "${ofile}" OUTPUT_VARIABLE out)
  54. message("symbols in ${ofile}:\n${out}")
  55. endif()
  56. endforeach()
  57. endif()
  58. endif()
  59. message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
  60. message("C = ${CMAKE_C_COMPILER_ID}")
  61. add_executable(foo ${srcs})
  62. if(NOT ("$ENV{EXTRA_FORTRAN_C_LIBS}" STREQUAL ""))
  63. message(STATUS
  64. "linking in extra C to Fortran libs $ENV{EXTRA_FORTRAN_C_LIBS}")
  65. target_link_libraries(foo $ENV{EXTRA_FORTRAN_C_LIBS})
  66. endif()
  67. # print out some stuff to help debug on machines via cdash
  68. file(READ "${testf_BINARY_DIR}/foo.h" fooh)
  69. message("foo.h contents:\n${fooh}")
  70. endfunction()
  71. # if the id's match or the compilers are compatible, then
  72. # call the test_fortran_c_interface_module function
  73. if(("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Intel")
  74. AND
  75. ("${CMAKE_C_COMPILER_ID}" MATCHES "MSVC")
  76. )
  77. set(COMPATABLE_COMPILERS TRUE)
  78. endif()
  79. if(COMPATABLE_COMPILERS
  80. OR ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "${CMAKE_C_COMPILER_ID}" ))
  81. test_fortran_c_interface_module()
  82. else()
  83. message("Fortran does not match c compiler")
  84. message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
  85. message("C = ${CMAKE_C_COMPILER_ID}")
  86. # hack to make g77 work after CL has been enabled
  87. # as a languge, cmake needs language specific versions
  88. # of these variables....
  89. if(WIN32 AND "${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  90. set(CMAKE_CREATE_CONSOLE_EXE )
  91. set(CMAKE_LIBRARY_PATH_FLAG "-L")
  92. set(CMAKE_LINK_LIBRARY_FLAG "-l")
  93. set(CMAKE_LINK_LIBRARY_SUFFIX )
  94. endif()
  95. # gnu and sunpro do not use the same flags here...
  96. # however if LDFLAGS is used to set -m64 it causes odd stuf
  97. # with the fortran build
  98. if( ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  99. AND ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "SunPro"))
  100. set(CMAKE_EXE_LINKER_FLAGS "")
  101. set(CMAKE_Fortran_FLAGS "")
  102. endif()
  103. endif()
  104. set(TEST_MODULE_DEPENDS 0)
  105. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  106. add_executable(test_module
  107. test_module_main.f90
  108. test_module_implementation.f90
  109. test_module_interface.f90)
  110. add_executable(test_use_in_comment_fixedform
  111. test_use_in_comment_fixedform.f)
  112. add_executable(test_use_in_comment_freeform
  113. test_use_in_comment_freeform.f90)
  114. add_executable(test_in_interface
  115. in_interface/main.f90
  116. in_interface/module.f90)
  117. add_definitions(-DFOO -DBAR=1)
  118. include_directories(${testf_SOURCE_DIR}/include)
  119. add_executable(test_preprocess test_preprocess.F90)
  120. set(TEST_MODULE_DEPENDS 1)
  121. endif(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  122. if(TEST_MODULE_DEPENDS)
  123. # Build the external project separately using a custom target.
  124. # Make sure it uses the same build configuration as this test.
  125. if(CMAKE_CONFIGURATION_TYPES)
  126. set(External_CONFIG_TYPE -C "${CMAKE_CFG_INTDIR}")
  127. else(CMAKE_CONFIGURATION_TYPES)
  128. set(External_CONFIG_TYPE)
  129. endif(CMAKE_CONFIGURATION_TYPES)
  130. add_custom_command(
  131. OUTPUT ${testf_BINARY_DIR}/ExternalProject
  132. COMMAND ${CMAKE_CTEST_COMMAND}
  133. ARGS ${External_CONFIG_TYPE}
  134. --build-and-test
  135. ${testf_SOURCE_DIR}/External
  136. ${testf_BINARY_DIR}/External
  137. --build-noclean
  138. --build-two-config
  139. --build-project ExtFort
  140. --build-generator ${CMAKE_GENERATOR}
  141. --build-makeprogram ${CMAKE_MAKE_PROGRAM}
  142. --build-options -DCMAKE_Fortran_COMPILER:STRING=${CMAKE_Fortran_COMPILER}
  143. -DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}
  144. -DCMAKE_Fortran_FLAGS_DEBUG:STRING=${CMAKE_Fortran_FLAGS_DEBUG}
  145. -DCMAKE_Fortran_FLAGS_RELEASE:STRING=${CMAKE_Fortran_FLAGS_RELEASE}
  146. -DCMAKE_Fortran_FLAGS_MINSIZEREL:STRING=${CMAKE_Fortran_FLAGS_MINSIZEREL}
  147. -DCMAKE_Fortran_FLAGS_RELWITHDEBINFO:STRING=${CMAKE_Fortran_FLAGS_RELWITHDEBINFO}
  148. )
  149. add_custom_target(ExternalTarget ALL DEPENDS ${testf_BINARY_DIR}/ExternalProject)
  150. # Test module output directory if available.
  151. if(CMAKE_Fortran_MODDIR_FLAG)
  152. set(Library_MODDIR "${testf_BINARY_DIR}/Library/modules")
  153. else(CMAKE_Fortran_MODDIR_FLAG)
  154. set(Library_MODDIR "${testf_BINARY_DIR}/Library")
  155. endif(CMAKE_Fortran_MODDIR_FLAG)
  156. add_subdirectory(Library)
  157. add_subdirectory(Executable)
  158. endif(TEST_MODULE_DEPENDS)