CMakeLists.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. cmake_minimum_required (VERSION 2.8)
  2. project(FortranOnly Fortran)
  3. message("CTEST_FULL_OUTPUT ")
  4. # create a library with hello and world functions
  5. add_library(FortranOnlylib hello.f world.f)
  6. set_property(TARGET FortranOnlylib PROPERTY Fortran_FORMAT FIXED)
  7. set_property(SOURCE world.f PROPERTY Fortran_FORMAT FREE)
  8. # create an executable that calls hello and world
  9. add_executable(FortranOnly1 testf.f)
  10. set_property(TARGET FortranOnly1 PROPERTY OUTPUT_NAME FortranOnly)
  11. target_link_libraries(FortranOnly1 FortranOnlylib)
  12. # create a custom command that runs FortranOnly1 and puts
  13. # the output into the file testfhello.txt
  14. add_custom_command(OUTPUT ${FortranOnly_BINARY_DIR}/testfhello.txt
  15. COMMAND FortranOnly1
  16. > testfhello.txt)
  17. # create a second executable FortranOnly2 that has
  18. # testfhello.txt has an source file so that it will
  19. # run the above custom command.
  20. add_executable(FortranOnly2 testfhello.txt testf.f)
  21. target_link_libraries(FortranOnly2 FortranOnlylib)
  22. # create a custom target to check the content of testfhello.txt
  23. # by running the cmake script checktestf2.cmake
  24. add_custom_target(checktestf2 ALL
  25. COMMAND ${CMAKE_COMMAND}
  26. -P ${FortranOnly_SOURCE_DIR}/checktestf2.cmake)
  27. # create a custom target that runs FortranOnly1 executable and creates
  28. # a file out.txt that should have hello world in it.
  29. add_custom_target(sayhello ALL
  30. COMMAND FortranOnly1 > out.txt
  31. )
  32. # make sure stuff is built in the right order
  33. add_dependencies(checktestf2 FortranOnly2)
  34. add_dependencies(sayhello FortranOnly1)
  35. add_dependencies(FortranOnly2 FortranOnly1)
  36. # add a custom target that checks that out.txt has the correct
  37. # content
  38. add_custom_target(checksayhello ALL
  39. COMMAND ${CMAKE_COMMAND} -P ${FortranOnly_SOURCE_DIR}/checksayhello.cmake
  40. )
  41. add_dependencies(checksayhello sayhello)
  42. # Exclude this test on IBM XL for now because the check strangely
  43. # fails with 'ld: 0706-029 Use a number with the -H flag'.
  44. if(NOT CMAKE_Fortran_COMPILER_ID STREQUAL XL)
  45. set(err_log ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log)
  46. file(REMOVE "${err_log}")
  47. include(CheckFortranSourceCompiles)
  48. unset(HAVE_PRINT CACHE)
  49. CHECK_Fortran_SOURCE_COMPILES([[
  50. PROGRAM TEST_HAVE_PRINT
  51. PRINT *, 'Hello'
  52. END
  53. ]] HAVE_PRINT)
  54. if(NOT HAVE_PRINT)
  55. if(EXISTS "${err_log}")
  56. file(READ "${err_log}" err)
  57. endif()
  58. string(REPLACE "\n" "\n " err " ${err}")
  59. message(SEND_ERROR "CHECK_Fortran_SOURCE_COMPILES for HAVE_PRINT failed:\n"
  60. "${err}")
  61. endif()
  62. unset(Fortran_BOGUS_FLAG CACHE)
  63. include(CheckFortranCompilerFlag)
  64. CHECK_Fortran_COMPILER_FLAG(-_this_is_not_a_flag_ Fortran_BOGUS_FLAG)
  65. if (Fortran_BOGUS_FLAG)
  66. message (SEND_ERROR "CHECK_Fortran_COMPILER_FLAG() succeeded, but should have failed")
  67. endif ()
  68. endif()
  69. # Test generation of preprocessed sources.
  70. if("${CMAKE_GENERATOR}" MATCHES "Makefile" AND CMAKE_MAKE_PROGRAM)
  71. if(CMAKE_Fortran_CREATE_PREPROCESSED_SOURCE)
  72. # Skip running this part of the test on certain platforms
  73. # until they are fixed.
  74. set(MAYBE_ALL ALL)
  75. list(LENGTH CMAKE_OSX_ARCHITECTURES ARCH_COUNT)
  76. if(ARCH_COUNT GREATER 1)
  77. # OSX does not support preprocessing more than one architecture.
  78. set(MAYBE_ALL)
  79. endif()
  80. add_executable(preprocess preprocess.F)
  81. # Custom target to try preprocessing invocation.
  82. add_custom_target(test_preprocess ${MAYBE_ALL}
  83. COMMAND ${CMAKE_COMMAND} -E remove CMakeFiles/preprocess.dir/preprocess.F.i
  84. COMMAND ${CMAKE_MAKE_PROGRAM} preprocess.i
  85. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/test_preprocess.cmake
  86. # Remove bogus file some compilers leave behind.
  87. COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_SOURCE_DIR}/preprocess.s
  88. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  89. )
  90. endif()
  91. endif()