7.9 组织Fortran项目

NOTE:此示例代码可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-7/recipe-09 中找到,其中有一个Fortran示例。该示例在CMake 3.5版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。

我们来讨论如何构造和组织Fortran项目,原因有二:

  1. 现在,仍然有很多Fortran项目,特别是在数字软件中(有关通用Fortran软件项目的更全面列表,请参见http://fortranwiki.org/fortran/show/Libraries )。
  2. 对于不使用CMake的项目,Fortran 90(以及更高版本)可能更难构建,因为Fortran模块强制执行编译顺序。换句话说,对于手工编写的Makefile,通常需要为Fortran模块文件编写依赖扫描程序。

正如我们在本示例中所示,现代CMake允许我们以非常紧凑和模块化的方式配置和构建项目。作为一个例子,我们将使用前两个示例中的基本元胞自动机,现在将其移植到Fortran。

准备工作

文件树结构与前两个示例非常相似。我们用Fortran源代码替换了C++,现在就没有头文件了:

  1. .
  2. ├── CMakeLists.txt
  3. ├── external
  4. ├── CMakeLists.txt
  5. ├── conversion.f90
  6. └── README.md
  7. ├── src
  8. ├── CMakeLists.txt
  9. ├── evolution
  10. ├── ancestors.f90
  11. ├── CMakeLists.txt
  12. ├── empty.f90
  13. └── evolution.f90
  14. ├── initial
  15. ├── CMakeLists.txt
  16. └── initial.f90
  17. ├── io
  18. ├── CMakeLists.txt
  19. └── io.f90
  20. ├── main.f90
  21. └── parser
  22. ├── CMakeLists.txt
  23. └── parser.f90
  24. └── tests
  25. ├── CMakeLists.txt
  26. └── test.f90

主程序在src/main.f90中:

  1. program example
  2. use parser, only: get_arg_as_int
  3. use conversion, only: binary_representation
  4. use initial, only: initial_distribution
  5. use io, only: print_row
  6. use evolution, only: evolve
  7. implicit none
  8. integer :: num_steps
  9. integer :: length
  10. integer :: rule_decimal
  11. integer :: rule_binary(8)
  12. integer, allocatable :: row(:)
  13. integer :: step
  14. ! parse arguments
  15. num_steps = get_arg_as_int(1)
  16. length = get_arg_as_int(2)
  17. rule_decimal = get_arg_as_int(3)
  18. ! print information about parameters
  19. print *, "number of steps: ", num_steps
  20. print *, "length: ", length
  21. print *, "rule: ", rule_decimal
  22. ! obtain binary representation for the rule
  23. rule_binary = binary_representation(rule_decimal)
  24. ! create initial distribution
  25. allocate(row(length))
  26. call initial_distribution(row)
  27. ! print initial configuration
  28. call print_row(row)
  29. ! the system evolves, print each step
  30. do step = 1, num_steps
  31. call evolve(row, rule_binary)
  32. call print_row(row)
  33. end do
  34. deallocate(row)
  35. end program

与前面的示例一样,我们已经将conversion模块放入external/conversion.f90中:

  1. module conversion
  2. implicit none
  3. public binary_representation
  4. private
  5. contains
  6. pure function binary_representation(n_decimal)
  7. integer, intent(in) :: n_decimal
  8. integer :: binary_representation(8)
  9. integer :: pos
  10. integer :: n
  11. binary_representation = 0
  12. pos = 8
  13. n = n_decimal
  14. do while (n > 0)
  15. binary_representation(pos) = mod(n, 2)
  16. n = (n - binary_representation(pos))/2
  17. pos = pos - 1
  18. end do
  19. end function
  20. end module

evolution库分成三个文件,大部分在src/evolution/evolution.f90中:

  1. module evolution
  2. implicit none
  3. public evolve
  4. private
  5. contains
  6. subroutine not_visible()
  7. ! no-op call to demonstrate private/public visibility
  8. call empty_subroutine_no_interface()
  9. end subroutine
  10. pure subroutine evolve(row, rule_binary)
  11. use ancestors, only: compute_ancestors
  12. integer, intent(inout) :: row(:)
  13. integer, intent(in) :: rule_binary(8)
  14. integer :: i
  15. integer :: left, center, right
  16. integer :: ancestry
  17. integer, allocatable :: new_row(:)
  18. allocate(new_row(size(row)))
  19. do i = 1, size(row)
  20. left = i - 1
  21. center = i
  22. right = i + 1
  23. if (left < 1) left = left + size(row)
  24. if (right > size(row)) right = right - size(row)
  25. ancestry = compute_ancestors(row, left, center, right)
  26. new_row(i) = rule_binary(ancestry)
  27. end do
  28. row = new_row
  29. deallocate(new_row)
  30. end subroutine
  31. end module

祖先计算是在src/evolution/ancestors.f90

  1. module ancestors
  2. implicit none
  3. public compute_ancestors
  4. private
  5. contains
  6. pure integer function compute_ancestors(row, left, center, right) result(i)
  7. integer, intent(in) :: row(:)
  8. integer, intent(in) :: left, center, right
  9. i = 4*row(left) + 2*row(center) + 1*row(right)
  10. i = 8 - i
  11. end function
  12. end module

还有一个“空”模块在src/evolution/empty.f90中:

  1. module empty
  2. implicit none
  3. public empty_subroutine
  4. private
  5. contains
  6. subroutine empty_subroutine()
  7. end subroutine
  8. end module
  9. subroutine
  10. empty_subroutine_no_interface()
  11. use empty, only: empty_subroutine
  12. call empty_subroutine()
  13. end subroutine

启动条件的代码位于src/initial/initial.f90

  1. module initial
  2. implicit none
  3. public initial_distribution
  4. private
  5. contains
  6. pure subroutine initial_distribution(row)
  7. integer, intent(out) :: row(:)
  8. row = 0
  9. row(size(row)/2) = 1
  10. end subroutine
  11. end module

src/io/io.f90包含一个打印输出:

  1. module io
  2. implicit none
  3. public print_row
  4. private
  5. contains
  6. subroutine print_row(row)
  7. integer, intent(in) :: row(:)
  8. character(size(row)) :: line
  9. integer :: i
  10. do i = 1, size(row)
  11. if (row(i) == 1) then
  12. line(i:i) = '*'
  13. else
  14. line(i:i) = ' '
  15. end if
  16. end do
  17. print *, line
  18. end subroutine
  19. end module

src/parser/parser.f90用于解析命令行参数:

  1. module parser
  2. implicit none
  3. public get_arg_as_int
  4. private
  5. contains
  6. integer function get_arg_as_int(n) result(i)
  7. integer, intent(in) :: n
  8. character(len=32) :: arg
  9. call get_command_argument(n, arg)
  10. read(arg , *) i
  11. end function
  12. end module

最后,使用tests/test.f90对上面的实现进行测试:

  1. program test
  2. use evolution, only: evolve
  3. implicit none
  4. integer :: row(9)
  5. integer :: expected_result(9)
  6. integer :: rule_binary(8)
  7. integer :: i
  8. ! test rule 90
  9. row = (/0, 1, 0, 1, 0, 1, 0, 1, 0/)
  10. rule_binary = (/0, 1, 0, 1, 1, 0, 1, 0/)
  11. call evolve(row, rule_binary)
  12. expected_result = (/1, 0, 0, 0, 0, 0, 0, 0, 1/)
  13. do i = 1, 9
  14. if (row(i) /= expected_result(i)) then
  15. print *, 'ERROR: test for rule 90 failed'
  16. call exit(1)
  17. end if
  18. end do
  19. ! test rule 222
  20. row = (/0, 0, 0, 0, 1, 0, 0, 0, 0/)
  21. rule_binary = (/1, 1, 0, 1, 1, 1, 1, 0/)
  22. call evolve(row, rule_binary)
  23. expected_result = (/0, 0, 0, 1, 1, 1, 0, 0, 0/)
  24. do i = 1, 9
  25. if (row(i) /= expected_result(i)) then
  26. print *, 'ERROR: test for rule 222 failed'
  27. call exit(1)
  28. end if
  29. end do
  30. end program

具体实施

  1. CMakeLists.txt类似于第7节,我们只是将CXX换成Fortran,去掉C++11的要求:

    1. cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
    2. project(recipe-09 LANGUAGES Fortran)
    3. include(GNUInstallDirs)
    4. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
    5. ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
    6. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
    7. ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
    8. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
    9. ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
    10. # defines targets and sources
    11. add_subdirectory(src)
    12. # contains an "external" library we will link to
    13. add_subdirectory(external)
    14. # enable testing and define tests
    15. enable_testing()
    16. add_subdirectory(tests)
  2. 目标和源在src/CMakeLists.txt中定义(conversion目标除外):

    1. add_executable(automata main.f90)
    2. add_subdirectory(evolution)
    3. add_subdirectory(initial)
    4. add_subdirectory(io)
    5. add_subdirectory(parser)
    6. target_link_libraries(automata
    7. PRIVATE
    8. conversion
    9. evolution
    10. initial
    11. io
    12. parser
    13. )
  3. conversion库在external/CMakeLists.txt中定义:

    1. add_library(conversion "")
    2. target_sources(conversion
    3. PUBLIC
    4. ${CMAKE_CURRENT_LIST_DIR}/conversion.f90
    5. )
  4. src/CMakeLists.txt文件添加了更多的子目录,这些子目录又包含CMakeLists.txt文件。它们在结构上都是相似的,例如:src/initial/CMakeLists.txt包含以下内容:

    1. add_library(initial "")
    2. target_sources(initial
    3. PUBLIC
    4. ${CMAKE_CURRENT_LIST_DIR}/initial.f90
    5. )
  5. 有个例外的是src/evolution/CMakeLists.txt中的evolution库,我们将其分为三个源文件:

    1. add_library(evolution "")
    2. target_sources(evolution
    3. PRIVATE
    4. empty.f90
    5. PUBLIC
    6. ${CMAKE_CURRENT_LIST_DIR}/ancestors.f90
    7. ${CMAKE_CURRENT_LIST_DIR}/evolution.f90
    8. )
  6. 单元测试在tests/CMakeLists.txt中注册:

    1. add_executable(fortran_test test.f90)
    2. target_link_libraries(fortran_test evolution)
    3. add_test(
    4. NAME
    5. test_evolution
    6. COMMAND
    7. $<TARGET_FILE:fortran_test>
    8. )
  7. 配置和构建项目,将产生以下输出:

    1. $ mkdir -p build
    2. $ cd build
    3. $ cmake ..
    4. $ cmake --build .
    5. Scanning dependencies of target conversion
    6. [ 4%] Building Fortran object external/CMakeFiles/conversion.dir/conversion.f90.o
    7. [ 8%] Linking Fortran static library ../lib64/libconversion.a
    8. [ 8%] Built target conversion
    9. Scanning dependencies of target evolution
    10. [ 12%] Building Fortran object src/evolution/CMakeFiles/evolution.dir/ancestors.f90.o
    11. [ 16%] Building Fortran object src/evolution/CMakeFiles/evolution.dir/empty.f90.o
    12. [ 20%] Building Fortran object src/evolution/CMakeFiles/evolution.dir/evolution.f90.o
    13. [ 25%] Linking Fortran static library ../../lib64/libevolution.a
    14. [ 25%] Built target evolution
    15. Scanning dependencies of target initial
    16. [ 29%] Building Fortran object src/initial/CMakeFiles/initial.dir/initial.f90.o
    17. [ 33%] Linking Fortran static library ../../lib64/libinitial.a
    18. [ 33%] Built target initial
    19. Scanning dependencies of target io
    20. [ 37%] Building Fortran object src/io/CMakeFiles/io.dir/io.f90.o
    21. [ 41%] Linking Fortran static library ../../lib64/libio.a
    22. [ 41%] Built target io
    23. Scanning dependencies of target parser
    24. [ 45%] Building Fortran object src/parser/CMakeFiles/parser.dir/parser.f90.o
    25. [ 50%] Linking Fortran static library ../../lib64/libparser.a
    26. [ 50%] Built target parser
    27. Scanning dependencies of target example
    28. [ 54%] Building Fortran object src/CMakeFiles/example.dir/__/external/conversion.f90.o
    29. [ 58%] Building Fortran object src/CMakeFiles/example.dir/evolution/ancestors.f90.o
    30. [ 62%] Building Fortran object src/CMakeFiles/example.dir/evolution/evolution.f90.o
    31. [ 66%] Building Fortran object src/CMakeFiles/example.dir/initial/initial.f90.o
    32. [ 70%] Building Fortran object src/CMakeFiles/example.dir/io/io.f90.o
    33. [ 75%] Building Fortran object src/CMakeFiles/example.dir/parser/parser.f90.o
    34. [ 79%] Building Fortran object src/CMakeFiles/example.dir/main.f90.o
    35. [ 83%] Linking Fortran executable ../bin/example
    36. [ 83%] Built target example
    37. Scanning dependencies of target fortran_test
    38. [ 87%] Building Fortran object tests/CMakeFiles/fortran_test.dir/__/src/evolution/ancestors.f90.o
    39. [ 91%] Building Fortran object tests/CMakeFiles/fortran_test.dir/__/src/evolution/evolution.f90.o
    40. [ 95%] Building Fortran object tests/CMakeFiles/fortran_test.dir/test.f90.o
    41. [100%] Linking Fortran executable
  8. 最后,运行单元测试:

    1. $ ctest
    2. Running tests...
    3. Start 1: test_evolution
    4. 1/1 Test #1: test_evolution ................... Passed 0.00 sec
    5. 100% tests passed, 0 tests failed out of 1

工作原理

第7节中使用add_subdirectory限制范围,将从下往上讨论CMake结构,从定义每个库的单个CMakeLists.txt文件开始,比如src/evolution/CMakeLists.txt:

  1. add_library(evolution "")
  2. target_sources(evolution
  3. PRIVATE
  4. empty.f90
  5. PUBLIC
  6. ${CMAKE_CURRENT_LIST_DIR}/ancestors.f90
  7. ${CMAKE_CURRENT_LIST_DIR}/evolution.f90
  8. )

这些独立的CMakeLists.txt文件定义了源文件的库,遵循与前两个示例相同的方式:开发或维护人员可以对其中文件分而治之。

首先用add_library定义库名,然后定义它的源和包含目录,以及它们的目标可见性。这种情况下,因为它们的模块接口是在库之外访问,所以ancestors.f90evolution.f90都是PUBLIC,而模块接口empty.f90不能在文件之外访问,因此将其标记为PRIVATE

向上移动一层,库在src/CMakeLists.txt中封装:

  1. add_executable(automata main.f90)
  2. add_subdirectory(evolution)
  3. add_subdirectory(initial)
  4. add_subdirectory(io)
  5. add_subdirectory(parser)
  6. target_link_libraries(automata
  7. PRIVATE
  8. conversion
  9. evolution
  10. initial
  11. io
  12. parser
  13. )

这个文件在主CMakeLists.txt中被引用。这意味着我们使用CMakeLists.txt文件(使用add_subdirectory添加)构建项目。正如第7节中讨论的,使用add_subdirectory限制范围,这种方法可以扩展到更大型的项目,而不需要在多个目录之间的全局变量中携带源文件列表,还可以隔离范围和名称空间。

将这个Fortran示例与C++版本(第7节)进行比较,我们可以注意到,在Fortran的情况下,相对的CMake工作量比较小;我们不需要使用target_include_directory,因为没有头文件,接口是通过生成的Fortran模块文件进行通信。另外,我们既不需要担心target_sources中列出的源文件的顺序,也不需要在库之间强制执行任何显式依赖关系。CMake能够从源文件依赖项推断Fortran模块依赖项。使用target_sourcesPRIVATEPUBLIC资源结合使用,以紧凑和健壮的方式表示接口。

更多信息

这个示例中,我们没有指定应该放置Fortran模块文件的目录,并且保持了这个透明。模块文件的位置可以通过设置CMAKE_Fortran_MODULE_DIRECTORY变量来指定。注意,也可以将其设置为Fortran_MODULE_DIRECTORY,从而实现更好的控制。详细可见:https://cmake.org/cmake/help/v3.5/prop_tgt/Fortran_MODULE_DIRECTORY.html