block.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, and, finally,
  12. the scopes created by ``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 :command:`block` command, this option
  25. set or unset the specified variables in the parent scope. This is equivalent
  26. to :command:`set(PARENT_SCOPE)` or :command:`unset(PARENT_SCOPE)` commands.
  27. .. code-block:: cmake
  28. set(VAR1 "INIT1")
  29. set(VAR2 "INIT2")
  30. block(PROPAGATE VAR1 VAR2)
  31. set(VAR1 "VALUE1")
  32. unset(VAR2)
  33. endblock()
  34. # here, VAR1 holds value VALUE1 and VAR2 is unset
  35. This option is only allowed when a variable scope is created. An error will
  36. be raised in the other cases.
  37. When the ``block`` is local to a :command:`foreach` or :command:`while`
  38. command, the commands :command:`break` and :command:`continue` can be used
  39. inside this block.
  40. .. code-block:: cmake
  41. while(TRUE)
  42. block()
  43. ...
  44. # the break() command will terminate the while() command
  45. break()
  46. endblock()
  47. endwhile()
  48. See Also
  49. ^^^^^^^^
  50. * :command:`endblock`
  51. * :command:`return`
  52. * :command:`cmake_policy`