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