return.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. return
  2. ------
  3. Return from a file, directory or function.
  4. .. code-block:: cmake
  5. return([PROPAGATE <var-name>...])
  6. Returns from a file, directory or function. When this command is
  7. encountered in an included file (via :command:`include` or
  8. :command:`find_package`), it causes processing of the current file to stop
  9. and control is returned to the including file. If it is encountered in a
  10. file which is not included by another file, e.g. a ``CMakeLists.txt``,
  11. deferred calls scheduled by :command:`cmake_language(DEFER)` are invoked and
  12. control is returned to the parent directory if there is one. If return is
  13. called in a function, control is returned to the caller of the function.
  14. ``PROPAGATE``
  15. .. versionadded:: 3.25
  16. This option set or unset the specified variables in the parent directory or
  17. function caller scope. This is equivalent to :command:`set(PARENT_SCOPE)` or
  18. :command:`unset(PARENT_SCOPE)` commands.
  19. The option ``PROPAGATE`` can be very useful in conjunction with the
  20. :command:`block` command because the :command:`return` will cross over
  21. various scopes created by the :command:`block` commands.
  22. .. code-block:: cmake
  23. function(MULTI_SCOPES RESULT_VARIABLE)
  24. block(SCOPE_FOR VARIABLES)
  25. # here set(PARENT_SCOPE) is not usable because it will not set the
  26. # variable in the caller scope but in the parent scope of the block()
  27. set(${RESULT_VARIABLE} "new-value")
  28. return(PROPAGATE ${RESULT_VARIABLE})
  29. endblock()
  30. endfunction()
  31. set(MY_VAR "initial-value")
  32. multi_scopes(MY_VAR)
  33. # here MY_VAR will holds "new-value"
  34. Policy :policy:`CMP0140` controls the behavior regarding the arguments of the
  35. command.
  36. Note that a :command:`macro <macro>`, unlike a :command:`function <function>`,
  37. is expanded in place and therefore cannot handle ``return()``.
  38. See Also
  39. ^^^^^^^^
  40. * :command:`block`