CMake中的foreach命令为list中的每个值评估一组命令(Evaluate a group of commands for each value in a list),其格式如下:其中
- foreach(
) -
- endforeach()
-
- foreach(
RANGE ) # 1 - foreach(
RANGE []) # 2 - foreach(
IN [LISTS []] [ITEMS []]) # 3 - foreach(
... IN ZIP_LISTS ) # 4
foreach和匹配的endforeach之间的所有命令都被记录下来而不被调用。一旦评估了endforeach,记录的命令列表就会为
break和continue命令提供了从正常控制流退出的方法。
endforeach命令接受一个可选的
- foreach(var 1 2)
- message("var: ${var}") # var: 1
- # var: 2
- endforeach()
-
- if(DEFINED var)
- message("defined var") # won't print
- endif()
1.在这个变体中,foreach迭代数字0,1,...直至(包括)非负整数
- foreach(var RANGE 2)
- message("var: ${var}") # var: 0
- # var: 1
- # var: 2
- endforeach()
2.在这个变体中,foreach以
- foreach(var RANGE 1 5 2)
- message("var: ${var}") # var: 1
- # var: 3
- # var: 5
- endforeach()
3.在这个变体中,
- set(A 0;1)
- set(B 2 3)
- set(C "4 5")
- set(D 6;7 8)
- set(E "")
- foreach(X IN LISTS A B C D E)
- message("X=${X}") # X=0
- # X=1
- # X=2
- # X=3
- # X=4 5
- # X=6
- # X=7
- # X=8
- endforeach()
-
- foreach(X IN ITEMS a b)
- message("X=${X}") # X=a
- # X=b
- endforeach()
4.在这个变体中,
(1).如果只给出了loop_var,那么它将一系列loop_var_N变量设置为相应list中的当前项。
(2).如果传递了多个变量名,则它们的计数(count)应与list变量计数匹配。
(3).如果任何list较短,则未为当前迭代定义相应的迭代变量。
- list(APPEND English one two three four)
- list(APPEND Bahasa satu dua tiga)
-
- foreach(num IN ZIP_LISTS English Bahasa)
- message("num_0=${num_0}, num_1=${num_1}") # num_0=one, num_1=satu
- # num_0=two, num_1=dua
- # num_0=three, num_1=tiga
- # num_0=four, num_1=
- endforeach()
-
- foreach(en ba IN ZIP_LISTS English Bahasa)
- message("en=${en}, ba=${ba}") # en=one, ba=satu
- # en=two, ba=dua
- # en=three, ba=tiga
- # en=four, ba=
- endforeach()
执行上述测试代码需要3个文件:build.sh, CMakeLists.txt, test_foreach.cmake
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)
-
- 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
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_foreach.cmake:为上面的所有示例代码
可能的执行结果如下图所示: