CMake中的configure_file命令用于将一个文件拷贝到另一个位置并修改其内容,其格式如下:
- configure_file(
- [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
- FILE_PERMISSIONS
...] - [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
- [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
将文件拷贝到文件。每个变量引用将被替换为变量的当前值,如果未定义该变量则替换为空字符串。
在#cmakedefine01 VAR形式的行中,VAR本身将扩展为VAR 0 或 VAR 1,如果定义了VAR,将被替换为:#define VAR 1
如果输入文件被修改,构建系统(build system)将重新运行CMake来重新配置文件并再次生成构建系统。只有当生成的文件内容发生改变时,生成的文件才会被修改,并在后续的cmake运行中更新它的时间戳。
参数:
(1).:输入文件的路径。相对路径是根据CMAKE_CURRENT_SOURCE_DIR的值来处理的。输入路径必须是文件,而不能是目录。
(2).
执行上述测试代码需要4个文件:build.sh, CMakeLists.txt, test_configure_file.cmake, include/foo.h.in
build.sh内容如下:
- #! /bin/bash
-
- # supported input parameters(cmake commands)
- params=(function macro cmake_parse_arguments \
- find_library find_path find_file find_program find_package \
- cmake_policy cmake_minimum_required project include \
- string list set foreach message option if while return \
- math file configure_file \
- include_directories)
-
- usage()
- {
- echo "Error: $0 needs to have an input parameter"
-
- echo "supported input parameters:"
- for param in ${params[@]}; do
- echo " $0 ${param}"
- done
-
- exit -1
- }
-
- if [ $# != 1 ]; then
- usage
- fi
-
- flag=0
- for param in ${params[@]}; do
- if [ $1 == ${param} ]; then
- flag=1
- break
- fi
- done
-
- if [ ${flag} == 0 ]; then
- echo "Error: parameter \"$1\" is not supported"
- usage
- exit -1
- fi
-
- if [[ ! -d "build" ]]; then
- mkdir build
- cd build
- else
- cd build
- fi
-
- echo "==== test $1 ===="
-
- # test_set.cmake: cmake -DTEST_CMAKE_FEATURE=$1 --log-level=verbose ..
- # test_option.cmake: cmake -DTEST_CMAKE_FEATURE=$1 -DBUILD_PYTORCH=ON ..
- cmake -DTEST_CMAKE_FEATURE=$1 ..
- # It can be executed directly on the terminal, no need to execute build.sh, for example: cmake -P test_set.cmake
- make
CMakeLists.txt内容如下:
- cmake_minimum_required(VERSION 3.22)
- project(cmake_feature_usage)
-
- message("#### current cmake version: ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")
- include(test_${TEST_CMAKE_FEATURE}.cmake)
- message("==== test finish ====")
test_configure_file.cmake内容如下:
- message("#### test_${TEST_CMAKE_FEATURE}.cmake ####")
-
- set(FLAG 2 CACHE STRING "Values that can be specified: [1, 2]" FORCE) # 设置FLAG,用来指定测试哪个代码段
-
- message("CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
- message("CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}")
-
- if(${FLAG} STREQUAL "1")
- # 注意:FOO_ENABLE的值会写入CMakeCache.txt
- option(FOO_ENABLE "Enable Foo" ON)
- if(FOO_ENABLE)
- set(FOO_STRING "foo")
- endif()
-
- set(VAR 1)
-
- configure_file(include/foo.h.in foo.h @ONLY) # 生成的foo.h在build目录下
- # foo.h内容:
- # #define FOO_ENABLE
- # #define FOO_STRING "foo"
-
- # #define VAR 1
- elseif(${FLAG} STREQUAL "2")
- option(FOO_ENABLE "Enable Foo" OFF)
- if(FOO_ENABLE)
- set(FOO_STRING "foo")
- endif()
-
- set(VAR 0)
- if(DEFINED VAR)
- message("defined VAR") # print
- endif()
-
- configure_file(include/foo.h.in foo.h @ONLY) # 生成的foo.h在build目录下
- # foo.h内容:
- # /* #undef FOO_ENABLE */
- # /* #undef FOO_STRING */
-
- # #define VAR 0
- endif()
include/foo.h.in内容如下:
- #cmakedefine FOO_ENABLE
- #cmakedefine FOO_STRING "@FOO_STRING@"
-
- #cmakedefine01 VAR
可能的执行结果如下图所示: