[Writing in front]
CMake's directory attributes are settings that are valid within the scope of a particular directory (and its subdirectories).
These properties differ from global variables or Target properties in that they provide a mechanism that allows developers to define different build behaviors for different parts of the project.
The directory attributes allow you to specify compiler options, include paths, preprocessing definitions, and so on, without having to repeat these settings for each target or file.
[Text Begins]
CMake directory-wide properties are ( CMake 3.30 ):
- ADDITIONAL_CLEAN_FILES
- BINARY_DIR
- BUILDSYSTEM_TARGETS
- CACHE_VARIABLES
- CLEAN_NO_CUSTOM
- CMAKE_CONFIGURE_DEPENDS
- COMPILE_DEFINITIONS
- COMPILE_OPTIONS
- DEFINITIONS
- EXCLUDE_FROM_ALL
- IMPLICIT_DEPENDS_INCLUDE_TRANSFORM
- IMPORTED_TARGETS
- INCLUDE_DIRECTORIES
- INCLUDE_REGULAR_EXPRESSION
- LABELS
- LINK_DIRECTORIES
- LINK_OPTIONS
- LISTFILE_STACK
- MACROS
- PARENT_DIRECTORY
- RULE_LAUNCH_COMPILE
- RULE_LAUNCH_CUSTOM
- RULE_LAUNCH_LINK
- SOURCE_DIR
- SUBDIRECTORIES
- SYSTEM
- TESTS
- TEST_INCLUDE_FILES
- VARIABLES
- VS_GLOBAL_SECTION_POST_
- VS_GLOBAL_SECTION_PRE_
- VS_STARTUP_PROJECT
Define catalog attributes:
define_property( <DIRECTORY>
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() cap (a poem)get_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 directory property named CURRENT_DIRECTORY
define_property(DIRECTORY
# Name of the directory property
PROPERTY CURRENT_DIRECTORY
# A short documentation description
BRIEF_DOCS "The current directory"
# Detailed documentation description
FULL_DOCS "The current directory"
)
Set the catalog properties:
set_property(<[DIRECTORY <dirs> ...] [TARGET_DIRECTORY <targets> ...]>
[APPEND] [APPEND_STRING]
PROPERTY <name> [<value1> ...])
Sets an attribute on zero or more objects in the range.
DIRECTORY
The scope defaults to the current directory, but other directories (which are already handled by CMake) can be named either as full paths or relative paths. Relative paths are considered relative to the current source directory. See alsoset_directory_properties() Command.Added in version 3.19.
<dir>
A binary directory can be referenced.If given
APPEND
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 current directory property to the current source directory
set_property(DIRECTORY PROPERTY CURRENT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
One of the commands is dedicated to setting directory attributes:
set_directory_properties(PROPERTIES prop1 value1 [prop2 value2] ...)
Sets the attributes of the current directory and its subdirectories in a key-value pair.
see alsoset_property(DIRECTORY) Command.
Gets the catalog properties:
get_property(<[DIRECTORY <dir> | TARGET_DIRECTORY <target>]>
PROPERTY <name>
[SET | DEFINED | BRIEF_DOCS | FULL_DOCS])
Gets an attribute from an object in scope.
DIRECTORY
The scope defaults to the current directory, but another directory (already handled by CMake) may be named by a full or relative path to "<dir>
". Relative paths are considered relative to the current source directory. See alsoget_directory_property() Command.Added in version 3.19.
<dir>
A binary directory can be referenced.If given
SET
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 current directory properties
get_property(CURRENT_DIR DIRECTORY PROPERTY CURRENT_DIRECTORY)
One of the commands is dedicated to getting directory attributes:
get_directory_property(<variable> [DIRECTORY <dir>] <prop-name>)
Stores catalog-wide attributes in the named
<variable>
Center.
DIRECTORY
parameter specifies another directory from which to retrieve the attribute value, rather than the current directory. A relative path is considered relative to the current source directory. CMake must already know the directory, or add it or a top-level directory by calling add_subdirectory.Added in version 3.19.
<dir>
A binary directory can be referenced.If the property is not defined for the specified directory range, an empty string is returned. For
INHERITED
attribute, if the attribute is not found in the specified directory scope, the search will link to the parent scope, such asdefine_property() The order states.
get_directory_property(<variable> [DIRECTORY <dir>]
DEFINITION <var-name>)
Get variable definitions from a directory. This form is useful for getting variable definitions from another directory.
Example:
# Add preprocessing definitions, defining the macro TEST_DEFINED with a value of 1
add_compile_definitions(TEST_DEFINED=1)
# get_directory_properties COMPILE_DEFINITIONS for the current directory, i.e., defined macros
get_directory_property(DEFS COMPILE_DEFINITIONS)
# Print the obtained definitions
message("DEFS: ${DEFS}")
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 directory property called CURRENT_DIRECTORY
define_property(DIRECTORY)
# Name of the directory property
PROPERTY CURRENT_DIRECTORY
# A short documentation description
BRIEF_DOCS "The current directory"
# Detailed documentation description
FULL_DOCS "The current directory"
)
# Set the current directory property to the current source directory
set_property(DIRECTORY PROPERTY CURRENT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
# Get current directory properties
get_property(CURRENT_DIR DIRECTORY PROPERTY CURRENT_DIRECTORY)
# Print the current directory information
message("CURRENT_DIR: ${CURRENT_DIR}")
# Add a preprocessing definition that defines the macro TEST_DEFINED with a value of 1
add_compile_definitions(TEST_DEFINED=1)
# Get the current directory's properties COMPILE_DEFINITIONS, i.e., defined macros
get_directory_property(DEFS COMPILE_DEFINITIONS)
# Print the obtained definitions
message("DEFS: ${DEFS}")
# Link the library directory to the lib directory in the current directory
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
# Get the link directory properties
get_property(LINK_DIR DIRECTORY PROPERTY LINK_DIRECTORIES)
# Get source directory properties
get_property(S_DIR DIRECTORY PROPERTY SOURCE_DIR)
# Print the link directory and source directory information
message("LINK_DIR: ${LINK_DIR} S_DIR: ${S_DIR}")
# Set compiler flags for the current directory and its subdirectories
set_directory_properties(PROPERTIES COMPILE_FLAGS "-Wall -Wextra")
# Get compiler flags for the current directory
get_directory_property(COMPILE_FLAGS COMPILE_FLAGS)
# Print the compilation flags obtained
message("COMPILE_FLAGS: ${COMPILE_FLAGS}")
CMake output is as follows:
[Conclusion]
Link to the project (more star ah.... ⭐_⭐):
Github Address:/mengps/LearnCMake