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