返回介绍

6.1 配置时生成源码

发布于 2025-05-06 21:45:57 字数 7486 浏览 0 评论 0 收藏 0

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

代码生成在配置时发生,例如:CMake 可以检测操作系统和可用库;基于这些信息,我们可以定制构建的源代码。本节和下面的章节中,我们将演示如何生成一个简单源文件,该文件定义了一个函数,用于报告构建系统配置。

准备工作

此示例的代码使用 Fortran 和 C 语言编写,第 9 章将讨论混合语言编程。主程序是一个简单的 Fortran 可执行程序,它调用一个 C 函数 print_info() ,该函数将打印配置信息。值得注意的是,在使用 Fortran 2003 时,编译器将处理命名问题(对于 C 函数的接口声明),如示例所示。我们将使用的 example.f90 作为源文件:

program hello_world
​
  implicit none
​
  interface
      subroutine print_info() bind(c, name="print_info")
      end subroutine
  end interface
​
  call print_info()
​
end program

C 函数 print_info() 在模板文件 print_info.c.in 中定义。在配置时,以 @ 开头和结尾的变量将被替换为实际值:

#include <stdio.h>
#include <unistd.h>
​
void print_info(void)
{
  printf("\n");
  printf("Configuration and build information\n");
  printf("-----------------------------------\n");
  printf("\n");
  printf("Who compiled | %s\n", "@ [email protected]");
  printf("Compilation hostname | %s\n", "@ [email protected]");
  printf("Fully qualified domain name | %s\n", "@ [email protected]");
  printf("Operating system | %s\n",
         "@ [email protected] , @ [email protected] , @ [email protected]");
  printf("Platform | %s\n", "@ [email protected]");
  printf("Processor info | %s\n",
         "@ [email protected] , @ [email protected]");
  printf("CMake version | %s\n", "@ [email protected]");
  printf("CMake generator | %s\n", "@ [email protected]");
  printf("Configuration time | %s\n", "@ [email protected]");
  printf("Fortran compiler | %s\n", "@ [email protected]");
  printf("C compiler | %s\n", "@ [email protected]");
  printf("\n");
​
  fflush(stdout);
}

具体实施

在 CMakeLists.txt 中,我们首先必须对选项进行配置,并用它们的值替换 print_info.c.in 中相应的占位符。然后,将 Fortran 和 C 源代码编译成一个可执行文件:

  1. 声明了一个 Fortran-C 混合项目:
    cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
    project(recipe-01 LANGUAGES Fortran C)
  2. 使用 execute_process 为项目获取当且使用者的信息:
    execute_process(
      COMMAND
          whoami
      TIMEOUT
          1
      OUTPUT_VARIABLE
          _user_name
      OUTPUT_STRIP_TRAILING_WHITESPACE
      )
  3. 使用 cmake_host_system_information() 函数(已经在第 2 章第 5 节遇到过),可以查询很多系统信息:
    # host name information
    cmake_host_system_information(RESULT _host_name QUERY HOSTNAME)
    cmake_host_system_information(RESULT _fqdn QUERY FQDN)
    ​
    # processor information
    cmake_host_system_information(RESULT _processor_name QUERY PROCESSOR_NAME)
    cmake_host_system_information(RESULT _processor_description QUERY PROCESSOR_DESCRIPTION)
    ​
    # os information
    cmake_host_system_information(RESULT _os_name QUERY OS_NAME)
    cmake_host_system_information(RESULT _os_release QUERY OS_RELEASE)
    cmake_host_system_information(RESULT _os_version QUERY OS_VERSION)
    cmake_host_system_information(RESULT _os_platform QUERY OS_PLATFORM)
  4. 捕获配置时的时间戳,并通过使用字符串操作函数:
    string(TIMESTAMP _configuration_time "%Y-%m-%d %H:%M:%S [UTC]" UTC)
  5. 现在,准备好配置模板文件 print_info.c.in 。通过 CMake 的 configure_file 函数生成代码。注意,这里只要求以 @ 开头和结尾的字符串被替换:
    configure_file(print_info.c.in print_info.c @ONLY)
  6. 最后,我们添加一个可执行目标,并定义目标源:
    add_executable(example "")
    target_sources(example
      PRIVATE
        example.f90
        ${CMAKE_CURRENT_BINARY_DIR}/print_info.c
      )
  7. 下面是一个输出示例:
    $ mkdir -p build
    $ cd build
    $ cmake ..
    $ cmake --build .
    $ ./example
    ​
    Configuration and build information
    -----------------------------------
    Who compiled | somebody
    Compilation hostname | laptop
    Fully qualified domain name | laptop
    Operating system | Linux, 4.16.13-1-ARCH, #1 SMP PREEMPT Thu May 31 23:29:29 UTC 2018
    Platform | x86_64
    Processor info | Unknown P6 family, 2 core Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
    CMake version | 3.11.3
    CMake generator | Unix Makefiles
    Configuration time | 2018-06-25 15:38:03 [UTC]
    Fortran compiler | /usr/bin/f95
    C compiler | /usr/bin/cc

工作原理

configure_file 命令可以复制文件,并用变量值替换它们的内容。示例中,使用 configure_file 修改模板文件的内容,并将其复制到一个位置,然后将其编译到可执行文件中。如何调用 configure_file :

configure_file(print_info.c.in print_info.c @ONLY)

第一个参数是模板的名称为 print_info.c.in 。CMake 假设输入文件的目录,与项目的根目录相对;也就是说,在 ${CMAKE_CURRENT_SOURCE_DIR}/print_info.c.in 。我们选择 print_info.c ,作为第二个参数是配置文件的名称。假设输出文件位于相对于项目构建目录的位置: ${CMAKE_CURRENT_BINARY_DIR}/print_info.c

输入和输出文件作为参数时,CMake 不仅将配置 @ [email protected] 变量,还将配置 ${VAR} 变量。如果 ${VAR} 是语法的一部分,并且不应该修改(例如在 shell 脚本中),那么就很不方便。为了在引导 CMake,应该将选项 @ONLY 传递给 configure_file 的调用,如前所述。

更多信息

注意,用值替换占位符时,CMake 中的变量名应该与将要配置的文件中使用的变量名完全相同,并放在 @ 之间。可以在调用 configure_file 时定义的任何 CMake 变量。我们的示例中,这包括所有内置的 CMake 变量,如 CMAKE_VERSIONCMAKE_GENERATOR 。此外,每当修改模板文件时,重新生成代码将触发生成系统的重新生成。这样,配置的文件将始终保持最新。

TIPS : 通过使用 CMake --help-variable-list ,可以从 CMake 手册中获得完整的内部 CMake 变量列表。

NOTE : file(GENERATE…) 为提供了一个有趣的替代 configure_file ,这是因为 file 允许将生成器表达式作为配置文件的一部分进行计算。但是,每次运行 CMake 时, file(GENERATE…) 都会更新输出文件,这将强制重新构建依赖于该输出的所有目标。详细可参见 https://crascit.com/2017/04/18/generated-sources-in-cmake-build

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。