block.rst 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. block
  2. -----
  3. .. versionadded:: 3.25
  4. Evaluate a group of commands with a dedicated variable and/or policy scope.
  5. .. code-block:: cmake
  6. block([SCOPE_FOR [POLICIES] [VARIABLES] ] [PROPAGATE <var-name>...])
  7. <commands>
  8. endblock()
  9. All commands between ``block()`` and the matching :command:`endblock` are
  10. recorded without being invoked. Once the :command:`endblock` is evaluated, the
  11. recorded list of commands is invoked inside the requested scopes, then the
  12. scopes created by the ``block()`` command are removed.
  13. ``SCOPE_FOR``
  14. Specify which scopes must be created.
  15. ``POLICIES``
  16. Create a new policy scope. This is equivalent to
  17. :command:`cmake_policy(PUSH)`.
  18. ``VARIABLES``
  19. Create a new variable scope.
  20. If ``SCOPE_FOR`` is not specified, this is equivalent to:
  21. .. code-block:: cmake
  22. block(SCOPE_FOR VARIABLES POLICIES)
  23. ``PROPAGATE``
  24. When a variable scope is created by the :command:`block` command, this
  25. option sets or unsets the specified variables in the parent scope. This is
  26. equivalent to :command:`set(PARENT_SCOPE)` or :command:`unset(PARENT_SCOPE)`
  27. commands.
  28. .. code-block:: cmake
  29. set(var1 "INIT1")
  30. set(var2 "INIT2")
  31. block(PROPAGATE var1 var2)
  32. set(var1 "VALUE1")
  33. unset(var2)
  34. endblock()
  35. # Now var1 holds VALUE1, and var2 is unset
  36. This option is only allowed when a variable scope is created. An error will
  37. be raised in the other cases.
  38. When the ``block()`` is inside a :command:`foreach` or :command:`while`
  39. command, the :command:`break` and :command:`continue` commands can be used
  40. inside the block.
  41. .. code-block:: cmake
  42. while(TRUE)
  43. block()
  44. ...
  45. # the break() command will terminate the while() command
  46. break()
  47. endblock()
  48. endwhile()
  49. See Also
  50. ^^^^^^^^
  51. * :command:`endblock`
  52. * :command:`return`
  53. * :command:`cmake_policy`