📘
CMake Cookbook
  • Introduction
  • 前言
  • 第0章 配置环境
    • 0.1 获取代码
    • 0.2 Docker镜像
    • 0.3 安装必要的软件
    • 0.4 测试环境
    • 0.5 上报问题并提出改进建议
  • 第1章 从可执行文件到库
    • 1.1 将单个源文件编译为可执行文件
    • 1.2 切换生成器
    • 1.3 构建和链接静态库和动态库
    • 1.4 用条件句控制编译
    • 1.5 向用户显示选项
    • 1.6 指定编译器
    • 1.7 切换构建类型
    • 1.8 设置编译器选项
    • 1.9 为语言设定标准
    • 1.10 使用控制流
  • 第2章 检测环境
    • 2.1 检测操作系统
    • 2.2 处理与平台相关的源代码
    • 2.3 处理与编译器相关的源代码
    • 2.4 检测处理器体系结构
    • 2.5 检测处理器指令集
    • 2.6 为Eigen库使能向量化
  • 第3章 检测外部库和程序
    • 3.1 检测Python解释器
    • 3.2 检测Python库
    • 3.3 检测Python模块和包
    • 3.4 检测BLAS和LAPACK数学库
    • 3.5 检测OpenMP的并行环境
    • 3.6 检测MPI的并行环境
    • 3.7 检测Eigen库
    • 3.8 检测Boost库
    • 3.9 检测外部库:Ⅰ. 使用pkg-config
    • 3.10 检测外部库:Ⅱ. 自定义find模块
  • 第4章 创建和运行测试
    • 4.1 创建一个简单的单元测试
    • 4.2 使用Catch2库进行单元测试
    • 4.3 使用Google Test库进行单元测试
    • 4.4 使用Boost Test进行单元测试
    • 4.5 使用动态分析来检测内存缺陷
    • 4.6 预期测试失败
    • 4.7 使用超时测试运行时间过长的测试
    • 4.8 并行测试
    • 4.9 运行测试子集
    • 4.10 使用测试固件
  • 第5章 配置时和构建时的操作
    • 5.1 使用平台无关的文件操作
    • 5.2 配置时运行自定义命令
    • 5.3 构建时运行自定义命令:Ⅰ. 使用add_custom_command
    • 5.4 构建时运行自定义命令:Ⅱ. 使用add_custom_target
    • 5.5 构建时为特定目标运行自定义命令
    • 5.6 探究编译和链接命令
    • 5.7 探究编译器标志命令
    • 5.8 探究可执行命令
    • 5.9 使用生成器表达式微调配置和编译
  • 第6章 生成源码
    • 6.1 配置时生成源码
    • 6.2 使用Python在配置时生成源码
    • 6.3 构建时使用Python生成源码
    • 6.4 记录项目版本信息以便报告
    • 6.5 从文件中记录项目版本
    • 6.6 配置时记录Git Hash值
    • 6.7 构建时记录Git Hash值
  • 第7章 构建项目
    • 7.1 使用函数和宏重用代码
    • 7.2 将CMake源代码分成模块
    • 7.3 编写函数来测试和设置编译器标志
    • 7.4 用指定参数定义函数或宏
    • 7.5 重新定义函数和宏
    • 7.6 使用废弃函数、宏和变量
    • 7.7 add_subdirectory的限定范围
    • 7.8 使用target_sources避免全局变量
    • 7.9 组织Fortran项目
  • 第8章 超级构建模式
    • 8.1 使用超级构建模式
    • 8.2 使用超级构建管理依赖项:Ⅰ.Boost库
    • 8.3 使用超级构建管理依赖项:Ⅱ.FFTW库
    • 8.4 使用超级构建管理依赖项:Ⅲ.Google Test框架
    • 8.5 使用超级构建支持项目
  • 第9章 语言混合项目
    • 9.1 使用C/C++库构建Fortran项目
    • 9.2 使用Fortran库构建C/C++项目
    • 9.3 使用Cython构建C++和Python项目
    • 9.4 使用Boost.Python构建C++和Python项目
    • 9.5 使用pybind11构建C++和Python项目
    • 9.6 使用Python CFFI混合C,C++,Fortran和Python
  • 第10章 编写安装程序
    • 10.1 安装项目
    • 10.2 生成输出头文件
    • 10.3 输出目标
    • 10.4 安装超级构建
  • 第11章 打包项目
    • 11.1 生成源代码和二进制包
    • 11.2 通过PyPI发布使用CMake/pybind11构建的C++/Python项目
    • 11.3 通过PyPI发布使用CMake/CFFI构建C/Fortran/Python项目
    • 11.4 以Conda包的形式发布一个简单的项目
    • 11.5 将Conda包作为依赖项发布给项目
  • 第12章 构建文档
    • 12.1 使用Doxygen构建文档
    • 12.2 使用Sphinx构建文档
    • 12.3 结合Doxygen和Sphinx
  • 第13章 选择生成器和交叉编译
    • 13.1 使用CMake构建Visual Studio 2017项目
    • 13.2 交叉编译hello world示例
    • 13.3 使用OpenMP并行化交叉编译Windows二进制文件
  • 第14章 测试面板
    • 14.1 将测试部署到CDash
    • 14.2 CDash显示测试覆盖率
    • 14.3 使用AddressSanifier向CDash报告内存缺陷
    • 14.4 使用ThreadSaniiser向CDash报告数据争用
  • 第15章 使用CMake构建已有项目
    • 15.1 如何开始迁移项目
    • 15.2 生成文件并编写平台检查
    • 15.3 检测所需的链接和依赖关系
    • 15.4 复制编译标志
    • 15.5 移植测试
    • 15.6 移植安装目标
    • 15.7 进一步迁移的措施
    • 15.8 项目转换为CMake的常见问题
  • 第16章 可能感兴趣的书
    • 16.1 留下评论——让其他读者知道你的想法
Powered by GitBook
On this page
  • 准备工作
  • 具体实施
  • 工作原理
  • 更多信息

Was this helpful?

  1. 第7章 构建项目

7.9 组织Fortran项目

Previous7.8 使用target_sources避免全局变量Next第8章 超级构建模式

Last updated 5 years ago

Was this helpful?

NOTE:此示例代码可以在 中找到,其中有一个Fortran示例。该示例在CMake 3.5版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。

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

  1. 现在,仍然有很多Fortran项目,特别是在数字软件中(有关通用Fortran软件项目的更全面列表,请参见 )。

  2. 对于不使用CMake的项目,Fortran 90(以及更高版本)可能更难构建,因为Fortran模块强制执行编译顺序。换句话说,对于手工编写的Makefile,通常需要为Fortran模块文件编写依赖扫描程序。

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

准备工作

文件树结构与前两个示例非常相似。我们用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

具体实施

  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)
  2. 目标和源在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
      )
  3. conversion库在external/CMakeLists.txt中定义:

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

    add_library(initial "")
    
    target_sources(initial
      PUBLIC
          ${CMAKE_CURRENT_LIST_DIR}/initial.f90
      )
  5. 有个例外的是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
      )
  6. 单元测试在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>
      )
  7. 配置和构建项目,将产生以下输出:

    $ 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
  8. 最后,运行单元测试:

    $ 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文件定义了源文件的库,遵循与前两个示例相同的方式:开发或维护人员可以对其中文件分而治之。

首先用add_library定义库名,然后定义它的源和包含目录,以及它们的目标可见性。这种情况下,因为它们的模块接口是在库之外访问,所以ancestors.f90和evolution.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_directory,因为没有头文件,接口是通过生成的Fortran模块文件进行通信。另外,我们既不需要担心target_sources中列出的源文件的顺序,也不需要在库之间强制执行任何显式依赖关系。CMake能够从源文件依赖项推断Fortran模块依赖项。使用target_sources与PRIVATE和PUBLIC资源结合使用,以紧凑和健壮的方式表示接口。

更多信息

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

https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-7/recipe-09
http://fortranwiki.org/fortran/show/Libraries
https://cmake.org/cmake/help/v3.5/prop_tgt/Fortran_MODULE_DIRECTORY.html