foreach.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. foreach
  2. -------
  3. Evaluate a group of commands for each value in a list.
  4. .. code-block:: cmake
  5. foreach(<loop_var> <items>)
  6. <commands>
  7. endforeach()
  8. where ``<items>`` is a list of items that are separated by
  9. semicolon or whitespace.
  10. All commands between ``foreach`` and the matching ``endforeach`` are recorded
  11. without being invoked. Once the ``endforeach`` is evaluated, the recorded
  12. list of commands is invoked once for each item in ``<items>``.
  13. At the beginning of each iteration the variable ``<loop_var>`` will be set
  14. to the value of the current item.
  15. The scope of ``<loop_var>`` is restricted to the loop scope. See policy
  16. :policy:`CMP0124` for details.
  17. The commands :command:`break` and :command:`continue` provide means to
  18. escape from the normal control flow.
  19. Per legacy, the :command:`endforeach` command admits
  20. an optional ``<loop_var>`` argument.
  21. If used, it must be a verbatim
  22. repeat of the argument of the opening
  23. ``foreach`` command.
  24. .. code-block:: cmake
  25. foreach(<loop_var> RANGE <stop>)
  26. In this variant, ``foreach`` iterates over the numbers
  27. 0, 1, ... up to (and including) the nonnegative integer ``<stop>``.
  28. .. code-block:: cmake
  29. foreach(<loop_var> RANGE <start> <stop> [<step>])
  30. In this variant, ``foreach`` iterates over the numbers from
  31. ``<start>`` up to at most ``<stop>`` in steps of ``<step>``.
  32. If ``<step>`` is not specified, then the step size is 1.
  33. The three arguments ``<start>`` ``<stop>`` ``<step>`` must
  34. all be nonnegative integers, and ``<stop>`` must not be
  35. smaller than ``<start>``; otherwise you enter the danger zone
  36. of undocumented behavior that may change in future releases.
  37. .. code-block:: cmake
  38. foreach(<loop_var> IN [LISTS [<lists>]] [ITEMS [<items>]])
  39. In this variant, ``<lists>`` is a whitespace or semicolon
  40. separated list of list-valued variables. The ``foreach``
  41. command iterates over each item in each given list.
  42. The ``<items>`` following the ``ITEMS`` keyword are processed
  43. as in the first variant of the ``foreach`` command.
  44. The forms ``LISTS A`` and ``ITEMS ${A}`` are
  45. equivalent. If no ``<lists>`` or ``<items>`` are given, the body
  46. of the loop will never be executed (i.e., it is processed as empty).
  47. The following example shows how the ``LISTS`` option is
  48. processed:
  49. .. code-block:: cmake
  50. set(A 0;1)
  51. set(B 2 3)
  52. set(C "4 5")
  53. set(D 6;7 8)
  54. set(E "")
  55. foreach(X IN LISTS A B C D E)
  56. message(STATUS "X=${X}")
  57. endforeach()
  58. yields::
  59. -- X=0
  60. -- X=1
  61. -- X=2
  62. -- X=3
  63. -- X=4 5
  64. -- X=6
  65. -- X=7
  66. -- X=8
  67. .. code-block:: cmake
  68. foreach(<loop_var>... IN ZIP_LISTS <lists>)
  69. .. versionadded:: 3.17
  70. In this variant, ``<lists>`` is a whitespace or semicolon
  71. separated list of list-valued variables. The ``foreach``
  72. command iterates over each list simultaneously setting the
  73. iteration variables as follows:
  74. - if a single ``loop_var`` is given, then it sets a series of
  75. ``loop_var_N`` variables to the current item from the
  76. corresponding list;
  77. - if multiple variable names are passed, it sets each variable to the
  78. current item from the corresponding list. The number of iteration
  79. variables must match the number of list variables.
  80. If no ``<lists>`` are given, the body of the loop will never be executed
  81. (i.e., it is processed as empty).
  82. The following example shows how the ``ZIP_LISTS`` option is
  83. processed:
  84. .. noqa: spellcheck off
  85. .. code-block:: cmake
  86. list(APPEND English one two three four)
  87. list(APPEND Bahasa satu dua tiga)
  88. foreach(num IN ZIP_LISTS English Bahasa)
  89. message(STATUS "num_0=${num_0}, num_1=${num_1}")
  90. endforeach()
  91. foreach(en ba IN ZIP_LISTS English Bahasa)
  92. message(STATUS "en=${en}, ba=${ba}")
  93. endforeach()
  94. yields::
  95. -- num_0=one, num_1=satu
  96. -- num_0=two, num_1=dua
  97. -- num_0=three, num_1=tiga
  98. -- num_0=four, num_1=
  99. -- en=one, ba=satu
  100. -- en=two, ba=dua
  101. -- en=three, ba=tiga
  102. -- en=four, ba=
  103. .. noqa: spellcheck on
  104. See Also
  105. ^^^^^^^^
  106. * :command:`break`
  107. * :command:`continue`
  108. * :command:`endforeach`
  109. * :command:`while`