return.rst 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. return
  2. ------
  3. Return from a file, directory or function.
  4. .. code-block:: cmake
  5. return([PROPAGATE <var-name>...])
  6. When this command is encountered in an included file (via :command:`include` or
  7. :command:`find_package`), it causes processing of the current file to stop
  8. and control is returned to the including file. If it is encountered in a
  9. file which is not included by another file, e.g. a ``CMakeLists.txt``,
  10. deferred calls scheduled by :command:`cmake_language(DEFER)` are invoked and
  11. control is returned to the parent directory if there is one.
  12. If ``return()`` is called in a function, control is returned to the caller
  13. of that function. Note that a :command:`macro`, unlike a :command:`function`,
  14. is expanded in place and therefore cannot handle ``return()``.
  15. Policy :policy:`CMP0140` controls the behavior regarding the arguments of the
  16. command. All arguments are ignored unless that policy is set to ``NEW``.
  17. ``PROPAGATE``
  18. .. versionadded:: 3.25
  19. This option sets or unsets the specified variables in the parent directory or
  20. function caller scope. This is equivalent to :command:`set(PARENT_SCOPE)` or
  21. :command:`unset(PARENT_SCOPE)` commands, except for the way it interacts
  22. with the :command:`block` command, as described below.
  23. The ``PROPAGATE`` option can be very useful in conjunction with the
  24. :command:`block` command. A ``return`` will propagate the
  25. specified variables through any enclosing block scopes created by the
  26. :command:`block` commands. Inside a function, this ensures the variables
  27. are propagated to the function's caller, regardless of any blocks within
  28. the function. If not inside a function, it ensures the variables are
  29. propagated to the parent file or directory scope. For example:
  30. .. code-block:: cmake
  31. :caption: CMakeLists.txt
  32. cmake_version_required(VERSION 3.25)
  33. project(example)
  34. set(var1 "top-value")
  35. block(SCOPE_FOR VARIABLES)
  36. add_subdirectory(subDir)
  37. # var1 has the value "block-nested"
  38. endblock()
  39. # var1 has the value "top-value"
  40. .. code-block:: cmake
  41. :caption: subDir/CMakeLists.txt
  42. function(multi_scopes result_var1 result_var2)
  43. block(SCOPE_FOR VARIABLES)
  44. # This would only propagate out of the immediate block, not to
  45. # the caller of the function.
  46. #set(${result_var1} "new-value" PARENT_SCOPE)
  47. #unset(${result_var2} PARENT_SCOPE)
  48. # This propagates the variables through the enclosing block and
  49. # out to the caller of the function.
  50. set(${result_var1} "new-value")
  51. unset(${result_var2})
  52. return(PROPAGATE ${result_var1} ${result_var2})
  53. endblock()
  54. endfunction()
  55. set(var1 "some-value")
  56. set(var2 "another-value")
  57. multi_scopes(var1 var2)
  58. # Now var1 will hold "new-value" and var2 will be unset
  59. block(SCOPE_FOR VARIABLES)
  60. # This return() will set var1 in the directory scope that included us
  61. # via add_subdirectory(). The surrounding block() here does not limit
  62. # propagation to the current file, but the block() in the parent
  63. # directory scope does prevent propagation going any further.
  64. set(var1 "block-nested")
  65. return(PROPAGATE var1)
  66. endblock()
  67. See Also
  68. ^^^^^^^^
  69. * :command:`block`
  70. * :command:`function`