CMakeLists.txt 8.2 KB

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