block.rst 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. set(var3 "INIT3")
  33. block(PROPAGATE var1 var2)
  34. set(var1 "VALUE1")
  35. unset(var2)
  36. set(var3 "VALUE3")
  37. endblock()
  38. # Now var1 holds VALUE1, var2 is unset, and var3 holds the initial value INIT3
  39. This option is only allowed when a variable scope is created. An error will
  40. be raised in the other cases.
  41. When the ``block()`` is inside a :command:`foreach` or :command:`while`
  42. command, the :command:`break` and :command:`continue` commands can be used
  43. inside the block.
  44. .. code-block:: cmake
  45. while(TRUE)
  46. block()
  47. ...
  48. # the break() command will terminate the while() command
  49. break()
  50. endblock()
  51. endwhile()
  52. See Also
  53. ^^^^^^^^
  54. * :command:`endblock`
  55. * :command:`return`
  56. * :command:`cmake_policy`