Location>code7788 >text

CMake Global Properties

Popularity:697 ℃/2024-10-09 18:44:57

[Writing in front]

CMake's global properties are settings that take effect for the entire project scope during CMake configuration.

These attributes are different from the Target attribute or the Directory attribute, which take effect only for a specific target or directory.


[Text Begins]

CMake global scope properties are ( CMake 3.30 ):

  • ALLOW_DUPLICATE_CUSTOM_TARGETS
  • AUTOGEN_SOURCE_GROUP
  • AUTOGEN_TARGETS_FOLDER
  • AUTOMOC_SOURCE_GROUP
  • AUTOMOC_TARGETS_FOLDER
  • AUTORCC_SOURCE_GROUP
  • AUTOUIC_SOURCE_GROUP
  • CMAKE_C_KNOWN_FEATURES
  • CMAKE_CUDA_KNOWN_FEATURES
  • CMAKE_CXX_KNOWN_FEATURES
  • CMAKE_HIP_KNOWN_FEATURES
  • CMAKE_ROLE
  • DEBUG_CONFIGURATIONS
  • DISABLED_FEATURES
  • ECLIPSE_EXTRA_CPROJECT_CONTENTS
  • ECLIPSE_EXTRA_NATURES
  • ENABLED_FEATURES
  • ENABLED_LANGUAGES
  • FIND_LIBRARY_USE_LIB32_PATHS
  • FIND_LIBRARY_USE_LIB64_PATHS
  • FIND_LIBRARY_USE_LIBX32_PATHS
  • FIND_LIBRARY_USE_OPENBSD_VERSIONING
  • GENERATOR_IS_MULTI_CONFIG
  • GLOBAL_DEPENDS_DEBUG_MODE
  • GLOBAL_DEPENDS_NO_CYCLES
  • INSTALL_PARALLEL
  • IN_TRY_COMPILE
  • JOB_POOLS
  • PACKAGES_FOUND
  • PACKAGES_NOT_FOUND
  • PREDEFINED_TARGETS_FOLDER
  • PROPAGATE_TOP_LEVEL_INCLUDES_TO_TRY_COMPILE
  • REPORT_UNDEFINED_PROPERTIES
  • RULE_LAUNCH_COMPILE
  • RULE_LAUNCH_CUSTOM
  • RULE_LAUNCH_LINK
  • RULE_MESSAGES
  • TARGET_ARCHIVES_MAY_BE_SHARED_LIBS
  • TARGET_MESSAGES
  • TARGET_SUPPORTS_SHARED_LIBS
  • USE_FOLDERS
  • XCODE_EMIT_EFFECTIVE_PLATFORM_NAME

Define global properties:

define_property(<GLOBAL | DIRECTORY | TARGET | SOURCE |
                 TEST | VARIABLE | CACHED_VARIABLE>
                 PROPERTY <name> [INHERITED]
                 [BRIEF_DOCS <brief-doc> [docs...]]
                 [FULL_DOCS <full-doc> [docs...]]
                 [INITIALIZE_FROM_VARIABLE <variable>])

Define an attribute in the scope forset_property() respond in singingget_property() command. It is primarily used to define how attributes are initialized or inherited. Historically, the command also associated documents with attributes, but this is no longer considered the primary use case.

Example:

# define a global property named GLOBAL_PROPERTY_TEST
define_property(GLOBAL
    # Name of the global property
    PROPERTY GLOBAL_PROPERTY_TEST
    # short documentation description
    BRIEF_DOCS "A global property test"
    # Full documentation description
    FULL_DOCS "A global property test"
)

Sets global properties:

set_property(<GLOBAL                      |
              DIRECTORY [<dir>]           |
              TARGET    [<target1> ...]   |
              SOURCE    [<src1> ...]
                        [DIRECTORY <dirs> ...]
                        [TARGET_DIRECTORY <targets> ...] |
              INSTALL   [<file1> ...]     |
              TEST      [<test1> ...]     |
              CACHE     [<entry1> ...]    >
             [APPEND] [APPEND_STRING]
             PROPERTY <name> [<value1> ...])

Sets an attribute on zero or more objects in the range.

GLOBAL The scope is unique and does not accept names.

If givenAPPEND option, the list will be appended to any existing property values (except ignore and do not append null values). If theAPPEND_STRING option, the string will be appended as a string to any existing attribute values, i.e. it produces a longer string instead of a list of strings. When using theAPPEND maybeAPPEND_STRING as well as defined to supportINHERITED behavior (see :command:).define_property), no inheritance occurs when the initial value to be attached to is found. If the property has not already been set directly within the specified range, the command behaves as if it had not given theAPPEND maybeAPPEND_STRING Same.

Example:

# Set the global property GLOBAL_PROPERTY_TEST to ON.
set_property(GLOBAL PROPERTY GLOBAL_PROPERTY_TEST ON)

Gets the global properties:

get_property(<variable>
             <GLOBAL             |
              DIRECTORY [<dir>]  |
              TARGET    <target> |
              SOURCE    <source>
                        [DIRECTORY <dir> | TARGET_DIRECTORY <target>] |
              INSTALL   <file>   |
              TEST      <test>   |
              CACHE     <entry>  |
              VARIABLE           >
             PROPERTY <name>
             [SET | DEFINED | BRIEF_DOCS | FULL_DOCS])

Gets an attribute from an object in scope.

GLOBAL The scope is unique and does not accept names.

If givenSET option, the variable will be set to a boolean value indicating whether the attribute has been set. If theDEFINED option, the variable will be set to a boolean value indicating whether or not the attribute has been defined, such as with thedefine_property command. If theBRIEF_DOCS maybeFULL_DOCS, then the variable will be set to a string containing the documentation for the requested property. If documentation is requested for an attribute that has not yet been defined, it returns "NOTFOUND”。

Example:

# Get the value of the global property GLOBAL_PROPERTY_TEST and store the result in the variable IS_GLOBAL
get_property(IS_GLOBAL GLOBAL PROPERTY GLOBAL_PROPERTY_TEST)

One of them is dedicated to getting theCMake Global Properties Command:

get_cmake_property(<var> <property>)

Get global properties from a CMake instance.<property> The value is stored in the variable<var> in. If the property is not found, the<var> will be set toNOTFOUND. For available properties, seecmake-properties(7) Manual.

In addition to global attributes, this command (for historical reasons) supports theVARIABLES respond in singingMACROS directory attribute. It also supports a specialCOMPONENTS Global property that lists the properties provided to theinstall() component of the command.

Example:

# Get the current CMake role and store the result in the variable ROLE
get_cmake_property(ROLE CMAKE_ROLE)

Test it all over again in its entirety at the end:

# Require a minimum CMake version of 3.16.
cmake_minimum_required(VERSION 3.16)

# Define a global property called GLOBAL_PROPERTY_TEST
define_property(GLOBAL)
    # Name of the global property
    PROPERTY GLOBAL_PROPERTY_TEST
    # short documentation description
    BRIEF_DOCS "A global property test"
    # Full documentation description
    FULL_DOCS "A global property test"
)

# Set global property GLOBAL_PROPERTY_TEST to ON
set_property(GLOBAL PROPERTY GLOBAL_PROPERTY_TEST ON)

# Get the value of the global property GLOBAL_PROPERTY_TEST and store the result in the variable IS_GLOBAL
get_property(IS_GLOBAL GLOBAL PROPERTY GLOBAL_PROPERTY_TEST)

# Print the value of the variable IS_GLOBAL to verify that the global property is set.
message("IS_GLOBAL: ${IS_GLOBAL}")

# Get the current CMake role and store the result in the variable ROLE
get_cmake_property(ROLE CMAKE_ROLE)

# Print the value of the variable ROLE, which is used to display the current CMake role.
message("ROLE: ${ROLE}")

CMake output is as follows:

image


[Conclusion]

Link to the project (more star ah.... ⭐_⭐):

Github Address:/mengps/LearnCMake