CMake语法—函数(解析参数 PARSE_ARGV)
目录
CMake语法—函数(解析参数 PARSE_ARGV)
1 CMake函数解析参数PARSE_ARGV示例
1.1 CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
# 设置工程名称
set(PROJECT_NAME KAIZEN)
# 设置工程版本号
set(PROJECT_VERSION "1.0.0.10" CACHE STRING "默认版本号")
# 工程定义
project(${PROJECT_NAME}
LANGUAGES CXX C
VERSION ${PROJECT_VERSION}
)
# 打印开始日志
message(STATUS "\n##########BEGIN_TEST")
# 定义函数
function(deploy)
set(options opt1 opt2 opt3)
set(oneValueArgs oneV1 oneV2 oneV3)
set(multiValueArgs multV1 multV2)
message(STATUS "ARGN: ${ARGN}")
message(STATUS "options: ${options}")
message(STATUS "oneValueArgs: ${oneValueArgs}")
message(STATUS "multiValueArgs: ${multiValueArgs}")
message(STATUS "\n----begin----PARSE_ARGV 0")
cmake_parse_arguments(PARSE_ARGV 0 Zero "${options}" "${oneValueArgs}" "${multiValueArgs}")
message(STATUS "Zero_opt1: ${Zero_opt1}")
message(STATUS "Zero_opt2: ${Zero_opt2}")
message(STATUS "Zero_opt3: ${Zero_opt3}")
message(STATUS "Zero_oneV1: ${Zero_oneV1}")
message(STATUS "Zero_oneV2: ${Zero_oneV2}")
message(STATUS "Zero_oneV3: ${Zero_oneV3}")
message(STATUS "Zero_multV1: ${Zero_multV1}")
message(STATUS "Zero_multV2: ${Zero_multV2}")
message(STATUS "Zero_KEYWORDS_MISSING_VALUES: ${Zero_KEYWORDS_MISSING_VALUES}")
message(STATUS "Zero_UNPARSED_ARGUMENTS: ${Zero_UNPARSED_ARGUMENTS}")
message(STATUS "----end----PARSE_ARGV 0")
message(STATUS "\n----begin----PARSE_ARGV 4")
cmake_parse_arguments(PARSE_ARGV 4 Four "${options}" "${oneValueArgs}" "${multiValueArgs}")
message(STATUS "Four_opt1: ${Four_opt1}")
message(STATUS "Four_opt2: ${Four_opt2}")
message(STATUS "Four_opt3: ${Four_opt3}")
message(STATUS "Four_oneV1: ${Four_oneV1}")
message(STATUS "Four_oneV2: ${Four_oneV2}")
message(STATUS "Four_oneV3: ${Four_oneV3}")
message(STATUS "Four_multV1: ${Four_multV1}")
message(STATUS "Four_multV2: ${Four_multV2}")
message(STATUS "Four_KEYWORDS_MISSING_VALUES: ${Four_KEYWORDS_MISSING_VALUES}")
message(STATUS "Four_UNPARSED_ARGUMENTS: ${Four_UNPARSED_ARGUMENTS}")
message(STATUS "----end----PARSE_ARGV 4")
message(STATUS "\n----begin----PARSE_ARGV 7")
cmake_parse_arguments(PARSE_ARGV 7 Seven "${options}" "${oneValueArgs}" "${multiValueArgs}")
message(STATUS "Seven_opt1: ${Seven_opt1}")
message(STATUS "Seven_opt2: ${Seven_opt2}")
message(STATUS "Seven_opt3: ${Seven_opt3}")
message(STATUS "Seven_oneV1: ${Seven_oneV1}")
message(STATUS "Seven_oneV2: ${Seven_oneV2}")
message(STATUS "Seven_oneV3: ${Seven_oneV3}")
message(STATUS "Seven_multV1: ${Seven_multV1}")
message(STATUS "Seven_multV2: ${Seven_multV2}")
message(STATUS "Seven_KEYWORDS_MISSING_VALUES: ${Seven_KEYWORDS_MISSING_VALUES}")
message(STATUS "Seven_UNPARSED_ARGUMENTS: ${Seven_UNPARSED_ARGUMENTS}")
message(STATUS "----end----PARSE_ARGV 7")
endfunction()
# 调用形式一(标准)
message(STATUS "----begin----调用形式一")
deploy(opt1 opt2 opt3 oneV1 abc oneV2 def oneV3 xyz multV1 kaizen baidu git multV2 C++ Java Python)
message(STATUS "----end----调用形式一")
# 调用形式二(标准但顺序不同)
message(STATUS "\n")
message(STATUS "----begin----调用形式二")
deploy(multV1 kaizen baidu git oneV1 abc opt1 oneV2 baidu opt2 oneV3 beijing opt3 multV2 C++ Java)
message(STATUS "----end----调用形式二")
# 打印结束日志
message(STATUS "##########END_TEST\n")
1.2 执行CMake配置脚本
@echo off
set currentDir=%~dp0
set buildDir=%currentDir%
set cmakeOutputDir=%currentDir%\build
cmake -S %buildDir% -B %cmakeOutputDir% -G"Visual Studio 16 2019" -T v140 -A x64
pause
1.3 目录结构
- learn_cmake:为根目录
- build:为CMake配置输出目录(在此例中即生成sln解决方案的地方)
- cmake_config.bat:执行CMake配置过程的脚本(双击直接运行)
- CMakeLists.txt:CMake脚本
2 运行结果
2.1 环境说明
本地安装VS版本:Visual Studio 2019(2015工具集)
CMake版本:3.18.2
F:\learn_cmake
λ cmake --version
cmake version 3.18.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
2.2 运行结果
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
-- The CXX compiler identification is MSVC 19.0.24245.0
-- The C compiler identification is MSVC 19.0.24245.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
--
##########BEGIN_TEST
-- ----begin----调用形式一
-- ARGN: opt1;opt2;opt3;oneV1;abc;oneV2;def;oneV3;xyz;multV1;kaizen;baidu;git;multV2;C++;Java;Python
-- options: opt1;opt2;opt3
-- oneValueArgs: oneV1;oneV2;oneV3
-- multiValueArgs: multV1;multV2
--
----begin----PARSE_ARGV 0
-- Zero_opt1: TRUE
-- Zero_opt2: TRUE
-- Zero_opt3: TRUE
-- Zero_oneV1: abc
-- Zero_oneV2: def
-- Zero_oneV3: xyz
-- Zero_multV1: kaizen;baidu;git
-- Zero_multV2: C++;Java;Python
-- Zero_KEYWORDS_MISSING_VALUES:
-- Zero_UNPARSED_ARGUMENTS:
-- ----end----PARSE_ARGV 0
--
----begin----PARSE_ARGV 4
-- Four_opt1: FALSE
-- Four_opt2: FALSE
-- Four_opt3: FALSE
-- Four_oneV1:
-- Four_oneV2: def
-- Four_oneV3: xyz
-- Four_multV1: kaizen;baidu;git
-- Four_multV2: C++;Java;Python
-- Four_KEYWORDS_MISSING_VALUES:
-- Four_UNPARSED_ARGUMENTS: abc
-- ----end----PARSE_ARGV 4
--
----begin----PARSE_ARGV 7
-- Seven_opt1: FALSE
-- Seven_opt2: FALSE
-- Seven_opt3: FALSE
-- Seven_oneV1:
-- Seven_oneV2:
-- Seven_oneV3: xyz
-- Seven_multV1: kaizen;baidu;git
-- Seven_multV2: C++;Java;Python
-- Seven_KEYWORDS_MISSING_VALUES:
-- Seven_UNPARSED_ARGUMENTS:
-- ----end----PARSE_ARGV 7
-- ----end----调用形式一
--
-- ----begin----调用形式二
-- ARGN: multV1;kaizen;baidu;git;oneV1;abc;opt1;oneV2;baidu;opt2;oneV3;beijing;opt3;multV2;C++;Java
-- options: opt1;opt2;opt3
-- oneValueArgs: oneV1;oneV2;oneV3
-- multiValueArgs: multV1;multV2
--
----begin----PARSE_ARGV 0
-- Zero_opt1: TRUE
-- Zero_opt2: TRUE
-- Zero_opt3: TRUE
-- Zero_oneV1: abc
-- Zero_oneV2: baidu
-- Zero_oneV3: beijing
-- Zero_multV1: kaizen;baidu;git
-- Zero_multV2: C++;Java
-- Zero_KEYWORDS_MISSING_VALUES:
-- Zero_UNPARSED_ARGUMENTS:
-- ----end----PARSE_ARGV 0
--
----begin----PARSE_ARGV 4
-- Four_opt1: TRUE
-- Four_opt2: TRUE
-- Four_opt3: TRUE
-- Four_oneV1: abc
-- Four_oneV2: baidu
-- Four_oneV3: beijing
-- Four_multV1:
-- Four_multV2: C++;Java
-- Four_KEYWORDS_MISSING_VALUES:
-- Four_UNPARSED_ARGUMENTS:
-- ----end----PARSE_ARGV 4
--
----begin----PARSE_ARGV 7
-- Seven_opt1: FALSE
-- Seven_opt2: TRUE
-- Seven_opt3: TRUE
-- Seven_oneV1:
-- Seven_oneV2: baidu
-- Seven_oneV3: beijing
-- Seven_multV1:
-- Seven_multV2: C++;Java
-- Seven_KEYWORDS_MISSING_VALUES:
-- Seven_UNPARSED_ARGUMENTS:
-- ----end----PARSE_ARGV 7
-- ----end----调用形式二
-- ##########END_TEST
-- Configuring done
-- Generating done
-- Build files have been written to: F:/learn_cmake/build
请按任意键继续. . .
2.3 结论
2.3.1 cmake_parse_arguments 命令定义2
cmake_parse_arguments(PARSE_ARGV <N> <prefix> <options>
<one_value_keywords> <multi_value_keywords>)
2.3.2 PARSE_ARGV 这种形式只能应用于函数体中(PS:相对于宏而言,后期会梳理宏与函数的区别)
2.3.3 N 是个无符号整型数值
从示例程序输出结果可知:
当 N==0 时,从第1个实参开始,后面所有实参均视为有效实参,前面的实参全部忽略掉。
当 N==4 时,从第5个实参开始,后面所有实参均视为有效实参,前面的实参全部忽略掉。
当 N==7 时,从第8个实参开始, 后面所有实参均视为有效实参,前面的实参全部忽略掉。