block.rst 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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)` with an automatic
  18. :command:`cmake_policy(POP)` when leaving the block scope.
  19. ``VARIABLES``
  20. Create a new variable scope.
  21. If ``SCOPE_FOR`` is not specified, this is equivalent to:
  22. .. code-block:: cmake
  23. block(SCOPE_FOR VARIABLES POLICIES)
  24. ``PROPAGATE``
  25. When a variable scope is created by the :command:`block` command, this
  26. option sets or unsets the specified variables in the parent scope. This is
  27. equivalent to :command:`set(PARENT_SCOPE)` or :command:`unset(PARENT_SCOPE)`
  28. commands.
  29. .. code-block:: cmake
  30. set(var1 "INIT1")
  31. set(var2 "INIT2")
  32. block(PROPAGATE var1 var2)
  33. set(var1 "VALUE1")
  34. unset(var2)
  35. endblock()
  36. # Now var1 holds VALUE1, and var2 is unset
  37. This option is only allowed when a variable scope is created. An error will
  38. be raised in the other cases.
  39. When the ``block()`` is inside a :command:`foreach` or :command:`while`
  40. command, the :command:`break` and :command:`continue` commands can be used
  41. inside the block.
  42. .. code-block:: cmake
  43. while(TRUE)
  44. block()
  45. ...
  46. # the break() command will terminate the while() command
  47. break()
  48. endblock()
  49. endwhile()
  50. See Also
  51. ^^^^^^^^
  52. * :command:`endblock`
  53. * :command:`return`
  54. * :command:`cmake_policy`