define_property.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. define_property
  2. ---------------
  3. Define and document custom properties.
  4. .. code-block:: cmake
  5. define_property(<GLOBAL | DIRECTORY | TARGET | SOURCE |
  6. TEST | VARIABLE | CACHED_VARIABLE>
  7. PROPERTY <name> [INHERITED]
  8. [BRIEF_DOCS <brief-doc> [docs...]]
  9. [FULL_DOCS <full-doc> [docs...]]
  10. [INITIALIZE_FROM_VARIABLE <variable>])
  11. Defines one property in a scope for use with the :command:`set_property` and
  12. :command:`get_property` commands. It is mainly useful for defining the way
  13. a property is initialized or inherited. Historically, the command also
  14. associated documentation with a property, but that is no longer considered a
  15. primary use case.
  16. The first argument determines the kind of scope in which the property should
  17. be used. It must be one of the following:
  18. ::
  19. GLOBAL = associated with the global namespace
  20. DIRECTORY = associated with one directory
  21. TARGET = associated with one target
  22. SOURCE = associated with one source file
  23. TEST = associated with a test named with add_test
  24. VARIABLE = documents a CMake language variable
  25. CACHED_VARIABLE = documents a CMake cache variable
  26. Note that unlike :command:`set_property` and :command:`get_property` no
  27. actual scope needs to be given; only the kind of scope is important.
  28. The required ``PROPERTY`` option is immediately followed by the name of
  29. the property being defined.
  30. If the ``INHERITED`` option is given, then the :command:`get_property` command
  31. will chain up to the next higher scope when the requested property is not set
  32. in the scope given to the command.
  33. * ``DIRECTORY`` scope chains to its parent directory's scope, continuing the
  34. walk up parent directories until a directory has the property set or there
  35. are no more parents. If still not found at the top level directory, it
  36. chains to the ``GLOBAL`` scope.
  37. * ``TARGET``, ``SOURCE`` and ``TEST`` properties chain to ``DIRECTORY`` scope,
  38. including further chaining up the directories, etc. as needed.
  39. Note that this scope chaining behavior only applies to calls to
  40. :command:`get_property`, :command:`get_directory_property`,
  41. :command:`get_target_property`, :command:`get_source_file_property` and
  42. :command:`get_test_property`. There is no inheriting behavior when *setting*
  43. properties, so using ``APPEND`` or ``APPEND_STRING`` with the
  44. :command:`set_property` command will not consider inherited values when working
  45. out the contents to append to.
  46. The ``BRIEF_DOCS`` and ``FULL_DOCS`` options are followed by strings to be
  47. associated with the property as its brief and full documentation.
  48. CMake does not use this documentation other than making it available to the
  49. project via corresponding options to the :command:`get_property` command.
  50. .. versionchanged:: 3.23
  51. The ``BRIEF_DOCS`` and ``FULL_DOCS`` options are optional.
  52. .. versionadded:: 3.23
  53. The ``INITIALIZE_FROM_VARIABLE`` option specifies a variable from which the
  54. property should be initialized. It can only be used with target properties.
  55. The ``<variable>`` name must end with the property name and must not begin
  56. with ``CMAKE_`` or ``_CMAKE_``. The property name must contain at least one
  57. underscore. It is recommended that the property name have a prefix specific
  58. to the project.
  59. Property Redefinition
  60. ^^^^^^^^^^^^^^^^^^^^^
  61. Once a property is defined for a particular type of scope, it cannot be
  62. redefined. Attempts to redefine an existing property by calling
  63. :command:`define_property` with the same scope type and property name
  64. will be silently ignored. Defining the same property name for two different
  65. kinds of scope is valid.
  66. :command:`get_property` can be used to determine whether a property is
  67. already defined for a particular kind of scope, and if so, to examine its
  68. definition. For example:
  69. .. code-block:: cmake
  70. # Initial definition
  71. define_property(TARGET PROPERTY MY_NEW_PROP
  72. BRIEF_DOCS "My new custom property"
  73. )
  74. # Later examination
  75. get_property(my_new_prop_exists
  76. TARGET NONE
  77. PROPERTY MY_NEW_PROP
  78. DEFINED
  79. )
  80. if(my_new_prop_exists)
  81. get_property(my_new_prop_docs
  82. TARGET NONE
  83. PROPERTY MY_NEW_PROP
  84. BRIEF_DOCS
  85. )
  86. # ${my_new_prop_docs} is now set to "My new custom property"
  87. endif()
  88. See Also
  89. ^^^^^^^^
  90. * :command:`get_property`
  91. * :command:`set_property`