目录

一、准备工作

1.print_info.c.in

二、配置源码

1.方式一

(1)CMakeLists.txt

(2)配置

2.方式二

(1)模拟configure_file

(2)CMakeLists.txt


在配置时生成源码有两种方式:

  1. 提前写好配置文件print_info.c.in,利用
    cmake_host_system_information、configure_file和target_sources等生成源码print_info.c。(推荐使用)
  2. 提前写好配置文件print_info.c.in,使用python编写configure_file函数,利用python解释器生成print_info.c。

一、准备工作

1.print_info.c.in

        在配置时,以 @ 开头和结尾的变量将被替换为实际值。

#include <stdio.h>
#include <unistd.h>

void print_info(void) {
  printf("Configuration and build information\n");
  printf("Who compiled                | %s\n", "@_user_name@");
  printf("Compilation hostname        | %s\n", "@_host_name@");
  printf("Fully qualified domain name | %s\n", "@_fqdn@");
  printf("Operating system            | %s\n",
         "@_os_name@, @_os_release@, @_os_version@");
  printf("Processor info              | %s\n",
         "@_processor_name@, @_processor_description@");
  printf("CMake version               | %s\n", "@CMAKE_VERSION@");
  printf("CMake generator             | %s\n", "@CMAKE_GENERATOR@");
  printf("Configuration time          | %s\n", "@_configuration_time@");

  fflush(stdout);
}

二、配置源码

1.方式一

  • 使用 cmake_host_system_information() 函数,可以查询很多系统信息。
  • configure_file 命令可以复制文件,并用变量值替换它们的内容。

(详细信息可查看CMake I 检测处理器体系结构_

        file(GENERATE…)为configure_file提供了一个有趣的替代方案,这是因为 file允许将生成器表达式作为配置文件的一部分进行计算。但是,每次运行CMake时, file(GENERATE…) 都会更新输出文件,这将强制重新构建依赖于该输出的所有目标。

(1)CMakeLists.txt

#为项目获取当且使用者的信息
execute_process(
  COMMAND
    whoami
  TIMEOUT
    1
  OUTPUT_VARIABLE
    _user_name
  OUTPUT_STRIP_TRAILING_WHITESPACE
  )
#查询系统信息
cmake_host_system_information(RESULT _host_name QUERY HOSTNAME)
cmake_host_system_information(RESULT _fqdn QUERY FQDN)

#捕获配置时的时间戳,并通过使用字符串操作函数
string(TIMESTAMP _configuration_time "%Y-%m-%d %H:%M:%S [UTC]" UTC)

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

add_executable(example "")

target_sources(example
  PRIVATE
    example.f90 #example.f90是用Fortran语言编写的调用prnt_info()的代码
    ${CMAKE_CURRENT_BINARY_DIR}/print_info.c
  )

(2)配置

        配置后可生成print_info.c:

#include <stdio.h>
#include <unistd.h>

void print_info(void) {
  printf("Configuration and build information\n");
  printf("Who compiled                | %s\n", "laptop-2l54p572\fengchujun");
  printf("Compilation hostname        | %s\n", "LAPTOP-2L54P572");
  printf("Fully qualified domain name | %s\n", "LAPTOP-2L54P572");
  printf("Operating system            | %s\n",
         "Windows,  Personal,  (Build 19043)");
  printf("Processor info              | %s\n",
         "Pentium II (0.25 micron), 6 core 2592 MHz GenuineIntel Pentium II (0.25 micron)");
  printf("CMake version               | %s\n", "3.21.0-rc3");
  printf("CMake generator             | %s\n", "Visual Studio 16 2019");
  printf("Configuration time          | %s\n", "2022-02-21 03:03:02 [UTC]");

  fflush(stdout);
}

2.方式二

        方式二使用Python脚本模拟configure_file过程来生成源码。

        对于实际的项目,我们可能更倾向于使用 configure_file() ,但有时使用Python生成源代码的需要时,我们也应该知道如何应对。

        但是,这种方法也有其局限性,它不能在构建时重新生成 print_info.c 的自动依赖项。换句话说,如果在配置之后删除生成的 print_info.c ,则不会重新生成该文件,构建也会失败。要正确地模拟 configure_file() , 需要使用 add_custom_command() 和 add_custom_target()。

(1)模拟configure_file

#configurator.py
def configure_file(input_file, output_file, vars_dict):

    with input_file.open('r') as f:
        template = f.read()

    for var in vars_dict:
        template = template.replace('@' + var + '@', vars_dict[var])

    with output_file.open('w') as f:
        f.write(template)

(2)CMakeLists.txt

#为项目获取当且使用者的信息
execute_process(
  COMMAND
    whoami
  TIMEOUT
    1
  OUTPUT_VARIABLE
    _user_name
  OUTPUT_STRIP_TRAILING_WHITESPACE
  )
#查询系统信息
cmake_host_system_information(RESULT _host_name QUERY HOSTNAME)
cmake_host_system_information(RESULT _fqdn QUERY FQDN)

#捕获配置时的时间戳,并通过使用字符串操作函数
string(TIMESTAMP _configuration_time "%Y-%m-%d %H:%M:%S [UTC]" UTC)
#构造一个变量 _config_script ,它将包含一个Python脚本
set(_config_script
"
from pathlib import Path
source_dir = Path('${CMAKE_CURRENT_SOURCE_DIR}')
binary_dir = Path('${CMAKE_CURRENT_BINARY_DIR}')
input_file = source_dir / 'print_info.c.in'
output_file = binary_dir / 'print_info.c'

import sys
sys.path.insert(0, str(source_dir))

from configurator import configure_file
vars_dict = {
    '_user_name':             '${_user_name}',
    '_host_name':             '${_host_name}',
    '_fqdn':                  '${_fqdn}',
    '_processor_name':        '${_processor_name}',
    '_processor_description': '${_processor_description}',
    '_os_name':               '${_os_name}',
    '_os_release':            '${_os_release}',
    '_os_version':            '${_os_version}',
    '_os_platform':           '${_os_platform}',
    '_configuration_time':    '${_configuration_time}',
    'CMAKE_VERSION':          '${CMAKE_VERSION}',
    'CMAKE_GENERATOR':        '${CMAKE_GENERATOR}',
    'CMAKE_Fortran_COMPILER': '${CMAKE_Fortran_COMPILER}',
    'CMAKE_C_COMPILER':       '${CMAKE_C_COMPILER}',
}
configure_file(input_file, output_file, vars_dict)
")

find_package(PythonInterp QUIET REQUIRED)

execute_process(
  COMMAND
    ${PYTHON_EXECUTABLE} "-c" ${_config_script}
  )

add_executable(example "")

target_sources(example
  PRIVATE
    example.f90
    ${CMAKE_CURRENT_BINARY_DIR}/print_info.c
  )

        我们执行了一个Python脚本生成print_info.c。运行Python脚本前,首先检测Python解释器,并构造Python脚本。Python脚本导入 configure_file 函数,我们在 configuration.py中定义了这个函数。为它提供用于读写的文件位置,并将其值作为键值对。