CMakeLists.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. cmake_minimum_required (VERSION 2.6)
  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. add_library(hello STATIC hello.f)
  24. add_library(world ${_SHARED} world.f world.def)
  25. add_executable(testf testf.f)
  26. target_link_libraries(testf hello world)
  27. function(test_fortran_c_interface_module)
  28. message(STATUS "Testing FortranCInterface module")
  29. # test the C to Fortran interface module
  30. include(FortranCInterface)
  31. FortranCInterface_VERIFY()
  32. FortranCInterface_VERIFY(CXX)
  33. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  34. if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "SunPro|MIPSpro|PathScale")
  35. set(module_expected 1)
  36. endif()
  37. if(FortranCInterface_MODULE_FOUND OR module_expected)
  38. set(srcs foo.f)
  39. set(FORTRAN_FUNCTIONS test_mod:sub)
  40. set(MYC_DEFS TEST_MOD)
  41. else()
  42. message("${CMAKE_Fortran_COMPILER_ID} compilers do not support"
  43. " linking Fortran module procedures from C")
  44. endif()
  45. endif()
  46. list(APPEND FORTRAN_FUNCTIONS my_sub mysub)
  47. FortranCInterface_HEADER(foo.h
  48. MACRO_NAMESPACE "FC_"
  49. SYMBOL_NAMESPACE "F_"
  50. SYMBOLS ${FORTRAN_FUNCTIONS}
  51. )
  52. include_directories("${testf_BINARY_DIR}")
  53. # if the name mangling is not found for a F90 compiler
  54. # print out some diagnostic stuff for the dashboard
  55. if(NOT FortranCInterface_GLOBAL_FOUND OR
  56. (NOT FortranCInterface_MODULE_FOUND AND module_expected) )
  57. find_program(FortranCInterface_EXE
  58. NAMES FortranCInterface
  59. PATHS ${FortranCInterface_BINARY_DIR} ${FortranCInterface_BINARY_DIR}/Debug
  60. NO_DEFAULT_PATH
  61. )
  62. find_program(DUMPBIN dumpbin)
  63. find_program(NM nm)
  64. if(FortranCInterface_EXE)
  65. if(DEPENDS)
  66. execute_process(COMMAND ${DUMPBIN} /symbols "${FortranCInterface_EXE}"
  67. OUTPUT_VARIABLE out)
  68. message("symbols in ${FortranCInterface_EXE}:\n${out}")
  69. endif()
  70. if(NM)
  71. execute_process(COMMAND ${NM} "${FortranCInterface_EXE}"
  72. OUTPUT_VARIABLE out)
  73. message("symbols in ${FortranCInterface_EXE}:\n${out}")
  74. endif()
  75. endif()
  76. endif()
  77. message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
  78. message("C = ${CMAKE_C_COMPILER_ID}")
  79. add_library(myfort mysub.f ${srcs})
  80. add_library(myc myc.c)
  81. target_link_libraries(myc myfort)
  82. set_property(TARGET myc PROPERTY COMPILE_DEFINITIONS ${MYC_DEFS})
  83. add_library(mycxx mycxx.cxx)
  84. target_link_libraries(mycxx myc)
  85. add_executable(mainc mainc.c)
  86. target_link_libraries(mainc myc)
  87. add_executable(maincxx maincxx.c)
  88. target_link_libraries(maincxx mycxx)
  89. # print out some stuff to help debug on machines via cdash
  90. file(READ "${testf_BINARY_DIR}/foo.h" fooh)
  91. message("foo.h contents:\n${fooh}")
  92. endfunction()
  93. # if the id's match or the compilers are compatible, then
  94. # call the test_fortran_c_interface_module function
  95. if(("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Intel")
  96. AND
  97. ("${CMAKE_C_COMPILER_ID}" MATCHES "MSVC")
  98. )
  99. set(COMPATABLE_COMPILERS TRUE)
  100. endif()
  101. if(COMPATABLE_COMPILERS
  102. OR ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "${CMAKE_C_COMPILER_ID}" ))
  103. test_fortran_c_interface_module()
  104. else()
  105. message("Fortran does not match c compiler")
  106. message("Fortran = ${CMAKE_Fortran_COMPILER_ID}")
  107. message("C = ${CMAKE_C_COMPILER_ID}")
  108. # hack to make g77 work after CL has been enabled
  109. # as a languge, cmake needs language specific versions
  110. # of these variables....
  111. if(WIN32 AND "${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  112. set(CMAKE_CREATE_CONSOLE_EXE )
  113. set(CMAKE_LIBRARY_PATH_FLAG "-L")
  114. set(CMAKE_LINK_LIBRARY_FLAG "-l")
  115. set(CMAKE_LINK_LIBRARY_SUFFIX )
  116. endif()
  117. # gnu and sunpro do not use the same flags here...
  118. # however if LDFLAGS is used to set -m64 it causes odd stuf
  119. # with the fortran build
  120. if( ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  121. AND ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "SunPro"))
  122. set(CMAKE_EXE_LINKER_FLAGS "")
  123. set(CMAKE_Fortran_FLAGS "")
  124. endif()
  125. endif()
  126. set(TEST_MODULE_DEPENDS 0)
  127. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  128. add_executable(test_module
  129. test_module_main.f90
  130. test_module_implementation.f90
  131. test_module_interface.f90)
  132. add_executable(test_use_in_comment_fixedform
  133. test_use_in_comment_fixedform.f)
  134. add_executable(test_use_in_comment_freeform
  135. test_use_in_comment_freeform.f90)
  136. add_executable(test_in_interface
  137. in_interface/main.f90
  138. in_interface/module.f90)
  139. add_definitions(-DFOO -DBAR=1)
  140. include_directories(${testf_SOURCE_DIR}/include)
  141. add_executable(test_preprocess test_preprocess.F90)
  142. set(TEST_MODULE_DEPENDS 1)
  143. endif(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  144. if(TEST_MODULE_DEPENDS)
  145. # Build the external project separately using a custom target.
  146. # Make sure it uses the same build configuration as this test.
  147. if(CMAKE_CONFIGURATION_TYPES)
  148. set(External_CONFIG_TYPE -C "${CMAKE_CFG_INTDIR}")
  149. set(External_BUILD_TYPE)
  150. else(CMAKE_CONFIGURATION_TYPES)
  151. set(External_CONFIG_TYPE)
  152. set(External_BUILD_TYPE -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE})
  153. endif(CMAKE_CONFIGURATION_TYPES)
  154. set(External_SOURCE_DIR "${testf_SOURCE_DIR}/External")
  155. set(External_BINARY_DIR "${testf_BINARY_DIR}/External")
  156. if("${testf_BINARY_DIR}" MATCHES " ")
  157. # Our build tree has a space, so the build tool supports spaces.
  158. # Test using modules from a path with spaces.
  159. set(External_BINARY_DIR "${External_BINARY_DIR} Build")
  160. endif()
  161. add_custom_command(
  162. OUTPUT ${testf_BINARY_DIR}/ExternalProject
  163. COMMAND ${CMAKE_CTEST_COMMAND}
  164. ARGS ${External_CONFIG_TYPE}
  165. --build-and-test
  166. ${External_SOURCE_DIR}
  167. ${External_BINARY_DIR}
  168. --build-noclean
  169. --build-two-config
  170. --build-project ExtFort
  171. --build-generator ${CMAKE_GENERATOR}
  172. --build-makeprogram ${CMAKE_MAKE_PROGRAM}
  173. --build-options -DCMAKE_Fortran_COMPILER:STRING=${CMAKE_Fortran_COMPILER}
  174. -DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}
  175. -DCMAKE_Fortran_FLAGS_DEBUG:STRING=${CMAKE_Fortran_FLAGS_DEBUG}
  176. -DCMAKE_Fortran_FLAGS_RELEASE:STRING=${CMAKE_Fortran_FLAGS_RELEASE}
  177. -DCMAKE_Fortran_FLAGS_MINSIZEREL:STRING=${CMAKE_Fortran_FLAGS_MINSIZEREL}
  178. -DCMAKE_Fortran_FLAGS_RELWITHDEBINFO:STRING=${CMAKE_Fortran_FLAGS_RELWITHDEBINFO}
  179. ${External_BUILD_TYPE}
  180. )
  181. add_custom_target(ExternalTarget ALL DEPENDS ${testf_BINARY_DIR}/ExternalProject)
  182. # Test module output directory if available.
  183. if(CMAKE_Fortran_MODDIR_FLAG)
  184. set(Library_MODDIR "${testf_BINARY_DIR}/Library/modules")
  185. else(CMAKE_Fortran_MODDIR_FLAG)
  186. set(Library_MODDIR "${testf_BINARY_DIR}/Library")
  187. endif(CMAKE_Fortran_MODDIR_FLAG)
  188. add_subdirectory(Library)
  189. add_subdirectory(Executable)
  190. endif(TEST_MODULE_DEPENDS)