define_property.rst 4.3 KB

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