function.rst 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. function
  2. --------
  3. Start recording a function for later invocation as a command.
  4. ::
  5. function(<name> [arg1 [arg2 [arg3 ...]]])
  6. COMMAND1(ARGS ...)
  7. COMMAND2(ARGS ...)
  8. ...
  9. endfunction(<name>)
  10. Define a function named <name> that takes arguments named arg1 arg2
  11. arg3 (...). Commands listed after function, but before the matching
  12. endfunction, are not invoked until the function is invoked. When it
  13. is invoked, the commands recorded in the function are first modified
  14. by replacing formal parameters (${arg1}) with the arguments passed,
  15. and then invoked as normal commands. In addition to referencing the
  16. formal parameters you can reference the variable ARGC which will be
  17. set to the number of arguments passed into the function as well as
  18. ARGV0 ARGV1 ARGV2 ... which will have the actual values of the
  19. arguments passed in. This facilitates creating functions with
  20. optional arguments. Additionally ARGV holds the list of all arguments
  21. given to the function and ARGN holds the list of arguments past the
  22. last expected argument.
  23. A function opens a new scope: see set(var PARENT_SCOPE) for details.
  24. See the cmake_policy() command documentation for the behavior of
  25. policies inside functions.