4 Evaluate a group of commands for each value in a list.
8 foreach(<loop_var> <items>)
12 where ``<items>`` is a list of items that are separated by
13 semicolon or whitespace.
14 All commands between ``foreach`` and the matching ``endforeach`` are recorded
15 without being invoked. Once the ``endforeach`` is evaluated, the recorded
16 list of commands is invoked once for each item in ``<items>``.
17 At the beginning of each iteration the variable ``<loop_var>`` will be set
18 to the value of the current item.
20 The scope of ``<loop_var>`` is restricted to the loop scope. See policy
21 :policy:`CMP0124` for details.
23 The commands :command:`break` and :command:`continue` provide means to
24 escape from the normal control flow.
26 Per legacy, the :command:`endforeach` command admits
27 an optional ``<loop_var>`` argument.
28 If used, it must be a verbatim
29 repeat of the argument of the opening
34 foreach(<loop_var> RANGE <stop>)
36 In this variant, ``foreach`` iterates over the numbers
37 0, 1, ... up to (and including) the nonnegative integer ``<stop>``.
41 foreach(<loop_var> RANGE <start> <stop> [<step>])
43 In this variant, ``foreach`` iterates over the numbers from
44 ``<start>`` up to at most ``<stop>`` in steps of ``<step>``.
45 If ``<step>`` is not specified, then the step size is 1.
46 The three arguments ``<start>`` ``<stop>`` ``<step>`` must
47 all be nonnegative integers, and ``<stop>`` must not be
48 smaller than ``<start>``; otherwise you enter the danger zone
49 of undocumented behavior that may change in future releases.
53 foreach(<loop_var> IN [LISTS [<lists>]] [ITEMS [<items>]])
55 In this variant, ``<lists>`` is a whitespace or semicolon
56 separated list of list-valued variables. The ``foreach``
57 command iterates over each item in each given list.
58 The ``<items>`` following the ``ITEMS`` keyword are processed
59 as in the first variant of the ``foreach`` command.
60 The forms ``LISTS A`` and ``ITEMS ${A}`` are
63 The following example shows how the ``LISTS`` option is
73 foreach(X IN LISTS A B C D E)
74 message(STATUS "X=${X}")
92 foreach(<loop_var>... IN ZIP_LISTS <lists>)
94 .. versionadded:: 3.17
96 In this variant, ``<lists>`` is a whitespace or semicolon
97 separated list of list-valued variables. The ``foreach``
98 command iterates over each list simultaneously setting the
99 iteration variables as follows:
101 - if the only ``loop_var`` given, then it sets a series of
102 ``loop_var_N`` variables to the current item from the
104 - if multiple variable names passed, their count should match
105 the lists variables count;
106 - if any of the lists are shorter, the corresponding iteration
107 variable is not defined for the current iteration.
109 .. code-block:: cmake
111 list(APPEND English one two three four)
112 list(APPEND Bahasa satu dua tiga)
114 foreach(num IN ZIP_LISTS English Bahasa)
115 message(STATUS "num_0=${num_0}, num_1=${num_1}")
118 foreach(en ba IN ZIP_LISTS English Bahasa)
119 message(STATUS "en=${en}, ba=${ba}")
125 -- num_0=one, num_1=satu
126 -- num_0=two, num_1=dua
127 -- num_0=three, num_1=tiga
128 -- num_0=four, num_1=