CMake 秘籍(四)(5)

简介: CMake 秘籍(四)

CMake 秘籍(四)(4)https://developer.aliyun.com/article/1525216

准备就绪

文件树结构与前两个配方非常相似。我们用 Fortran 源代码替换了 C++,在这种情况下,我们没有头文件:

.
├── CMakeLists.txt
├── external
│   ├── CMakeLists.txt
│   ├── conversion.f90
│   └── README.md
├── src
│   ├── CMakeLists.txt
│   ├── evolution
│   │   ├── ancestors.f90
│   │   ├── CMakeLists.txt
│   │   ├── empty.f90
│   │   └── evolution.f90
│   ├── initial
│   │   ├── CMakeLists.txt
│   │   └── initial.f90
│   ├── io
│   │   ├── CMakeLists.txt
│   │   └── io.f90
│   ├── main.f90
│   └── parser
│       ├── CMakeLists.txt
│       └── parser.f90
└── tests
    ├── CMakeLists.txt
    └── test.f90

主程序在src/main.f90中:

program example
  use parser, only: get_arg_as_int
  use conversion, only: binary_representation
  use initial, only: initial_distribution
  use io, only: print_row
  use evolution, only: evolve
  implicit none
  integer :: num_steps
  integer :: length
  integer :: rule_decimal
  integer :: rule_binary(8)
  integer, allocatable :: row(:)
  integer :: step
  ! parse arguments
  num_steps = get_arg_as_int(1)
  length = get_arg_as_int(2)
  rule_decimal = get_arg_as_int(3)
  ! print information about parameters
  print *, "number of steps: ", num_steps
  print *, "length: ", length
  print *, "rule: ", rule_decimal
  ! obtain binary representation for the rule
  rule_binary = binary_representation(rule_decimal)
  ! create initial distribution
  allocate(row(length))
  call initial_distribution(row)
  ! print initial configuration
  call print_row(row)
  ! the system evolves, print each step
  do step = 1, num_steps
    call evolve(row, rule_binary)
    call print_row(row)
  end do
  deallocate(row)
end program

与之前的配方一样,我们将conversion模块放在external/conversion.f90中:

module conversion
  implicit none
  public binary_representation
  private
contains
  pure function binary_representation(n_decimal)
    integer, intent(in) :: n_decimal
    integer :: binary_representation(8)
    integer :: pos
    integer :: n
    binary_representation = 0
    pos = 8
    n = n_decimal
    do while (n > 0)
      binary_representation(pos) = mod(n, 2)
      n = (n - binary_representation(pos))/2
      pos = pos - 1
    end do
  end function
end module

evolution库,它实现了时间步长,被人工分为三个文件。大部分内容收集在src/evolution/evolution.f90

module evolution
  implicit none
  public evolve
  private
contains
  subroutine not_visible()
    ! no-op call to demonstrate private/public visibility
    call empty_subroutine_no_interface()
  end subroutine
  pure subroutine evolve(row, rule_binary)
    use ancestors, only: compute_ancestors
    integer, intent(inout) :: row(:)
    integer, intent(in) :: rule_binary(8)
    integer :: i
    integer :: left, center, right
    integer :: ancestry
    integer, allocatable :: new_row(:)
    allocate(new_row(size(row)))
    do i = 1, size(row)
      left = i - 1
      center = i
      right = i + 1
      if (left < 1) left = left + size(row)
      if (right > size(row)) right = right - size(row)
      ancestry = compute_ancestors(row, left, center, right)
      new_row(i) = rule_binary(ancestry)
    end do
    row = new_row
    deallocate(new_row)
  end subroutine
end module

祖先的计算在src/evolution/ancestors.f90中执行:

module ancestors
  implicit none
  public compute_ancestors
  private
contains
  pure integer function compute_ancestors(row, left, center, right) result(i)
    integer, intent(in) :: row(:)
    integer, intent(in) :: left, center, right
    i = 4*row(left) + 2*row(center) + 1*row(right)
    i = 8 - i
  end function
end module

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

module empty
  implicit none
  public empty_subroutine
  private
contains
  subroutine empty_subroutine()
  end subroutine
end module
subroutine empty_subroutine_no_interface()
  use empty, only: empty_subroutine
  call empty_subroutine()
end subroutine

我们将在下一节解释这些选择。

起始条件的代码位于src/initial/initial.f90

module initial
  implicit none
  public initial_distribution
  private
contains
  pure subroutine initial_distribution(row)
    integer, intent(out) :: row(:)
    row = 0
    row(size(row)/2) = 1
  end subroutine
end module

src/io/io.f90文件包含一个打印行的函数:

module io
  implicit none
  public print_row
  private
contains
  subroutine print_row(row)
    integer, intent(in) :: row(:)
    character(size(row)) :: line
    integer :: i
    do i = 1, size(row)
      if (row(i) == 1) then
        line(i:i) = '*'
      else
        line(i:i) = ' '
      end if
    end do
    print *, line
  end subroutine
end module

src/parser/parser.f90文件解析命令行参数:

module parser
  implicit none
  public get_arg_as_int
  private
contains
  integer function get_arg_as_int(n) result(i)
    integer, intent(in) :: n
    character(len=32) :: arg
    call get_command_argument(n, arg)
    read(arg , *) i
  end function
end module

最后,我们有测试源文件在tests/test.f90

program test
  use evolution, only: evolve
  implicit none
  integer :: row(9)
  integer :: expected_result(9)
  integer :: rule_binary(8)
  integer :: i
  ! test rule 90
  row = (/0, 1, 0, 1, 0, 1, 0, 1, 0/)
  rule_binary = (/0, 1, 0, 1, 1, 0, 1, 0/)
  call evolve(row, rule_binary)
  expected_result = (/1, 0, 0, 0, 0, 0, 0, 0, 1/)
  do i = 1, 9
    if (row(i) /= expected_result(i)) then
      print *, 'ERROR: test for rule 90 failed'
      call exit(1)
    end if
  end do
  ! test rule 222
  row = (/0, 0, 0, 0, 1, 0, 0, 0, 0/)
  rule_binary = (/1, 1, 0, 1, 1, 1, 1, 0/)
  call evolve(row, rule_binary)
  expected_result = (/0, 0, 0, 1, 1, 1, 0, 0, 0/)
  do i = 1, 9
    if (row(i) /= expected_result(i)) then
      print *, 'ERROR: test for rule 222 failed'
      call exit(1)
    end if
  end do
end program

如何做到这一点

我们现在将讨论相应的 CMake 结构:

  1. 顶层的CMakeLists.txt与第 7 个配方类似;我们只将CXX替换为Fortran并删除 C++11 要求:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-09 LANGUAGES Fortran)
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
  ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
  ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
  ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
# defines targets and sources
add_subdirectory(src)
# contains an "external" library we will link to
add_subdirectory(external)
# enable testing and define tests
enable_testing()
add_subdirectory(tests)
  1. 目标和源文件在src/CMakeLists.txt中定义(除了conversion目标):
add_executable(automata main.f90)
add_subdirectory(evolution)
add_subdirectory(initial)
add_subdirectory(io)
add_subdirectory(parser)
target_link_libraries(automata
  PRIVATE
    conversion
    evolution
    initial
    io
    parser
  )
  1. 转换库在external/CMakeLists.txt中定义:
add_library(conversion "")
target_sources(conversion
  PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/conversion.f90
  )
  1. src/CMakeLists.txt文件添加了进一步的子目录,这些子目录又包含CMakeLists.txt文件。它们的结构都类似;例如,src/initial/CMakeLists.txt包含以下内容:
add_library(initial "")
target_sources(initial
  PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/initial.f90
  )
  1. 例外是src/evolution/CMakeLists.txt中的evolution库,我们将其分为三个源文件:
add_library(evolution "")
target_sources(evolution
  PRIVATE
    empty.f90
  PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/ancestors.f90
    ${CMAKE_CURRENT_LIST_DIR}/evolution.f90
  )
  1. 单元测试在tests/CMakeLists.txt中注册:
add_executable(fortran_test test.f90)
target_link_libraries(fortran_test evolution)
add_test(
  NAME
    test_evolution
  COMMAND
    $<TARGET_FILE:fortran_test>
  )
  1. 配置和构建项目会产生以下输出:
$ mkdir -p build
$ cd build
$ cmake ..
$ cmake --build .
Scanning dependencies of target conversion
[ 4%] Building Fortran object external/CMakeFiles/conversion.dir/conversion.f90.o
[ 8%] Linking Fortran static library ../lib64/libconversion.a
[ 8%] Built target conversion
Scanning dependencies of target evolution
[ 12%] Building Fortran object src/evolution/CMakeFiles/evolution.dir/ancestors.f90.o
[ 16%] Building Fortran object src/evolution/CMakeFiles/evolution.dir/empty.f90.o
[ 20%] Building Fortran object src/evolution/CMakeFiles/evolution.dir/evolution.f90.o
[ 25%] Linking Fortran static library ../../lib64/libevolution.a
[ 25%] Built target evolution
Scanning dependencies of target initial
[ 29%] Building Fortran object src/initial/CMakeFiles/initial.dir/initial.f90.o
[ 33%] Linking Fortran static library ../../lib64/libinitial.a
[ 33%] Built target initial
Scanning dependencies of target io
[ 37%] Building Fortran object src/io/CMakeFiles/io.dir/io.f90.o
[ 41%] Linking Fortran static library ../../lib64/libio.a
[ 41%] Built target io
Scanning dependencies of target parser
[ 45%] Building Fortran object src/parser/CMakeFiles/parser.dir/parser.f90.o
[ 50%] Linking Fortran static library ../../lib64/libparser.a
[ 50%] Built target parser
Scanning dependencies of target example
[ 54%] Building Fortran object src/CMakeFiles/example.dir/__/external/conversion.f90.o
[ 58%] Building Fortran object src/CMakeFiles/example.dir/evolution/ancestors.f90.o
[ 62%] Building Fortran object src/CMakeFiles/example.dir/evolution/evolution.f90.o
[ 66%] Building Fortran object src/CMakeFiles/example.dir/initial/initial.f90.o
[ 70%] Building Fortran object src/CMakeFiles/example.dir/io/io.f90.o
[ 75%] Building Fortran object src/CMakeFiles/example.dir/parser/parser.f90.o
[ 79%] Building Fortran object src/CMakeFiles/example.dir/main.f90.o
[ 83%] Linking Fortran executable ../bin/example
[ 83%] Built target example
Scanning dependencies of target fortran_test
[ 87%] Building Fortran object tests/CMakeFiles/fortran_test.dir/__/src/evolution/ancestors.f90.o
[ 91%] Building Fortran object tests/CMakeFiles/fortran_test.dir/__/src/evolution/evolution.f90.o
[ 95%] Building Fortran object tests/CMakeFiles/fortran_test.dir/test.f90.o
[100%] Linking Fortran executable
  1. 最后,我们运行单元测试:
$ ctest
Running tests...
 Start 1: test_evolution
1/1 Test #1: test_evolution ................... Passed 0.00 sec
100% tests passed, 0 tests failed out of 1

它是如何工作的

按照第 7 个配方,使用add_subdirectory限制范围,我们将从下至上讨论 CMake 结构,从定义每个库的单独CMakeLists.txt文件开始,例如src/evolution/CMakeLists.txt

add_library(evolution "")
target_sources(evolution
  PRIVATE
empty.f90
  PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/ancestors.f90
    ${CMAKE_CURRENT_LIST_DIR}/evolution.f90
  )

这些单独的CMakeLists.txt文件尽可能接近源文件定义库,遵循与前两个配方相同的推理:了解此库的代码开发人员,可能对 CMake 框架的了解有限,只需要编辑此目录中的文件:分而治之。

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

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

add_executable(automata main.f90)
add_subdirectory(evolution)
add_subdirectory(initial)
add_subdirectory(io)
add_subdirectory(parser)
target_link_libraries(automata
  PRIVATE
    conversion
    evolution
    initial
    io
    parser
  )

反过来,此文件在顶层的CMakeLists.txt中被引用。这意味着我们使用CMakeLists.txt文件的树构建了我们的项目库树,使用add_subdirectory添加。如第 7 个配方,使用add_subdirectory限制范围所述,这种方法可以扩展到大型项目,无需在目录之间携带源文件列表的全局变量,并且具有隔离作用域和命名空间的额外好处。

将此 Fortran 示例与 C++版本(配方 7)进行比较,我们可以注意到,在 Fortran 情况下,我们不得不做的 CMake 工作较少;我们不需要使用target_include_directories,因为没有头文件,接口是通过生成的 Fortran 模块文件进行通信的。此外,我们也不必担心源文件在target_sources中列出的顺序,也不必在库之间施加任何显式依赖关系!CMake 能够从源文件依赖关系中推断出 Fortran 模块依赖关系。结合使用target_sourcesPRIVATEPUBLIC,我们可以以紧凑且稳健的方式表达接口。

还有更多内容。

在本配方中,我们没有指定 Fortran 模块文件应放置的目录,并保持了这种透明性。可以通过设置CMAKE_Fortran_MODULE_DIRECTORY CMake 变量来指定模块文件的位置。请注意,也可以将其设置为目标属性,即Fortran_MODULE_DIRECTORY,从而实现更精细的控制。请参阅cmake.org/cmake/help/v3.5/prop_tgt/Fortran_MODULE_DIRECTORY.html

相关文章
|
5月前
|
编译器 Shell 开发工具
CMake 秘籍(八)(5)
CMake 秘籍(八)
32 2
|
5月前
|
Linux 编译器 C++
CMake 秘籍(七)(2)
CMake 秘籍(七)
36 1
|
5月前
|
Linux iOS开发 C++
CMake 秘籍(六)(3)
CMake 秘籍(六)
32 1
|
5月前
|
Linux API iOS开发
CMake 秘籍(六)(1)
CMake 秘籍(六)
38 1
|
5月前
|
编译器 Linux C++
CMake 秘籍(六)(5)
CMake 秘籍(六)
30 1
|
5月前
|
消息中间件 Unix C语言
CMake 秘籍(二)(5)
CMake 秘籍(二)
79 1
|
5月前
|
编译器 开发工具 git
CMake 秘籍(八)(1)
CMake 秘籍(八)
23 1
|
5月前
|
并行计算 编译器 Linux
CMake 秘籍(二)(3)
CMake 秘籍(二)
26 0
|
5月前
|
Linux C++ iOS开发
CMake 秘籍(四)(3)
CMake 秘籍(四)
17 0
|
5月前
|
编译器 Linux 开发工具
CMake 秘籍(四)(2)
CMake 秘籍(四)
20 0
下一篇
无影云桌面