foreach.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.
  46. The following example shows how the ``LISTS`` option is
  47. processed:
  48. .. code-block:: cmake
  49. set(A 0;1)
  50. set(B 2 3)
  51. set(C "4 5")
  52. set(D 6;7 8)
  53. set(E "")
  54. foreach(X IN LISTS A B C D E)
  55. message(STATUS "X=${X}")
  56. endforeach()
  57. yields
  58. ::
  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 the only ``loop_var`` 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 passed, their count should match
  78. the lists variables count;
  79. - if any of the lists are shorter, the corresponding iteration
  80. variable is not defined for the current iteration.
  81. .. code-block:: cmake
  82. list(APPEND English one two three four)
  83. list(APPEND Bahasa satu dua tiga)
  84. foreach(num IN ZIP_LISTS English Bahasa)
  85. message(STATUS "num_0=${num_0}, num_1=${num_1}")
  86. endforeach()
  87. foreach(en ba IN ZIP_LISTS English Bahasa)
  88. message(STATUS "en=${en}, ba=${ba}")
  89. endforeach()
  90. yields
  91. ::
  92. -- num_0=one, num_1=satu
  93. -- num_0=two, num_1=dua
  94. -- num_0=three, num_1=tiga
  95. -- num_0=four, num_1=
  96. -- en=one, ba=satu
  97. -- en=two, ba=dua
  98. -- en=three, ba=tiga
  99. -- en=four, ba=