CMP0124.rst 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. CMP0124
  2. -------
  3. .. versionadded:: 3.21
  4. :command:`foreach` loop variables are only available in the loop scope.
  5. CMake 3.20 and below always leave the loop variable set at the end of the
  6. loop, either to the value it had before the loop, if any, or to the empty
  7. string. CMake 3.21 and above prefer to leave the loop variable in the
  8. state it had before the loop started, either set or unset. This policy
  9. provides compatibility for projects that expect the loop variable to always
  10. be left set.
  11. The ``OLD`` behavior for this policy is to set the loop variable at the
  12. end of the loop, either to its original value, or to an empty value.
  13. The ``NEW`` behavior for this policy is to restore the loop variable to
  14. the state it had before the loop started, either set or unset.
  15. For example:
  16. .. code-block:: cmake
  17. set(items a b c)
  18. set(var1 "value")
  19. unset(var2)
  20. foreach(var1 IN LISTS items)
  21. endforeach()
  22. foreach(var2 IN LISTS items)
  23. endforeach()
  24. if(DEFINED var1)
  25. message("var1: ${var1}")
  26. endif()
  27. if(DEFINED var2)
  28. message("var2: ${var2}")
  29. endif()
  30. Under the ``OLD`` behavior, this code prints ``var1: value`` and ``var2:``.
  31. Under the ``NEW`` behavior, this code prints only ``var1: value``.
  32. .. |INTRODUCED_IN_CMAKE_VERSION| replace:: 3.21
  33. .. |WARNS_OR_DOES_NOT_WARN| replace:: does *not* warn
  34. .. include:: STANDARD_ADVICE.txt
  35. .. include:: DEPRECATED.txt