Add SYSTEM target property

If it is ON, treat INTERFACE_INCLUDE_DIRECTORIES as system include directories.

Issue: #18040

Signed-off-by: Da Quexian <daquexian566@gmail.com>
This commit is contained in:
Da Quexian 2022-06-01 16:36:59 +08:00
parent 85dc7c763a
commit 69beee5314
7 changed files with 92 additions and 4 deletions

View File

@ -375,6 +375,7 @@ Properties on Targets
/prop_tgt/Swift_LANGUAGE_VERSION
/prop_tgt/Swift_MODULE_DIRECTORY
/prop_tgt/Swift_MODULE_NAME
/prop_tgt/SYSTEM
/prop_tgt/TYPE
/prop_tgt/UNITY_BUILD
/prop_tgt/UNITY_BUILD_BATCH_SIZE

View File

@ -7,7 +7,8 @@ Specifies that an :ref:`Imported Target <Imported Targets>` is not
a ``SYSTEM`` library. This has the following effects:
* Entries of :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` are not treated
as ``SYSTEM`` include directories when compiling consumers, as they
as ``SYSTEM`` include directories when compiling consumers (regardless of
the value of the consumed target's :prop_tgt:`SYSTEM` property), as they
would be by default. Entries of
:prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES` are not affected,
and will always be treated as ``SYSTEM`` include directories.

16
Help/prop_tgt/SYSTEM.rst Normal file
View File

@ -0,0 +1,16 @@
SYSTEM
------
.. versionadded:: 3.25
Specifies that a target is a ``SYSTEM`` library. This has the following effects:
* Entries of :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` are treated as ``SYSTEM``
include directories when compiling consumers.
Entries of :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES` are not affected,
and will always be treated as ``SYSTEM`` include directories.
For imported targets, this property has a default value `ON`, which means that their
`INTERFACE_INCLUDE_DIRECTORIES` are treated as ``SYSTEM`` by default. If their
`SYSTEM` property is `OFF`, then their `INTERFACE_INCLUDE_DIRECTORIES` will not be
treated as ``SYSTEM``, regardless of the value of `IMPORTED_NO_SYSTEM` property.

View File

@ -0,0 +1,7 @@
system
------
* The :prop_tgt:`SYSTEM` target property was added to specify
that a target should be treated as a system library (i.e.
its include directories are automatically ``SYSTEM`` when
compiling consumers).

View File

@ -807,12 +807,17 @@ void handleSystemIncludesDep(cmLocalGenerator* lg,
dagChecker, depTgt, language),
result);
}
if (!depTgt->IsImported() || excludeImported) {
if (!depTgt->GetPropertyAsBool("SYSTEM")) {
return;
}
if (depTgt->IsImported()) {
if (excludeImported) {
return;
}
if (depTgt->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) {
return;
}
}
if (cmValue dirs = depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
cmExpandList(cmGeneratorExpression::Evaluate(*dirs, lg, config, headTarget,

View File

@ -759,6 +759,10 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type,
}
}
if (this->IsImported()) {
this->SetProperty("SYSTEM", "ON");
}
for (auto const& prop : mf->GetState()->GetPropertyDefinitions().GetMap()) {
if (prop.first.second == cmProperty::TARGET &&
!prop.second.GetInitializeFromVariable().empty()) {

View File

@ -121,12 +121,66 @@ add_library(ordertest ordertest.cpp)
target_include_directories(ordertest SYSTEM PUBLIC SystemIncludeDirectories/systemlib)
target_include_directories(ordertest PUBLIC SystemIncludeDirectories/userlib)
# Test "IMPORTED_NO_SYSTEM" property and its interaction with "SYSTEM"
add_library(ordertest2 ordertest.cpp)
target_include_directories(ordertest2 SYSTEM PRIVATE SystemIncludeDirectories/systemlib)
target_link_libraries(ordertest2 PRIVATE ordertest2_userlib)
add_library(ordertest2_userlib INTERFACE IMPORTED)
target_include_directories(ordertest2_userlib INTERFACE SystemIncludeDirectories/userlib)
set_property(TARGET ordertest2_userlib PROPERTY IMPORTED_NO_SYSTEM 1)
get_property(system_prop_value TARGET ordertest2_userlib PROPERTY SYSTEM)
if (NOT system_prop_value)
message(SEND_ERROR "ordertest2_userlib SYSTEM property should be ON.")
endif()
# Test "SYSTEM" property of non-imported libraries
add_library(ordertest3_systemlib INTERFACE)
target_include_directories(ordertest3_systemlib INTERFACE SystemIncludeDirectories/systemlib)
set_property(TARGET ordertest3_systemlib PROPERTY SYSTEM 1)
add_library(ordertest3_userlib INTERFACE)
target_include_directories(ordertest3_userlib INTERFACE SystemIncludeDirectories/userlib)
add_library(ordertest3 ordertest.cpp)
target_link_libraries(ordertest3 PRIVATE ordertest3_systemlib ordertest3_userlib)
# Test "SYSTEM" property of imported libraries and its interaction with "IMPORTED_NO_SYSTEM"
add_library(ordertest4 ordertest.cpp)
target_include_directories(ordertest4 SYSTEM PRIVATE SystemIncludeDirectories/systemlib)
target_link_libraries(ordertest4 PRIVATE ordertest4_userlib)
add_library(ordertest4_userlib INTERFACE IMPORTED)
target_include_directories(ordertest4_userlib INTERFACE SystemIncludeDirectories/userlib)
set_property(TARGET ordertest4_userlib PROPERTY SYSTEM 0)
get_property(imported_no_system_prop_value TARGET ordertest4_userlib PROPERTY IMPORTED_NO_SYSTEM)
if (imported_no_system_prop_value)
message(SEND_ERROR "ordertest4_userlib IMPORTED_NO_SYSTEM property should be OFF.")
endif()
# Test the interaction between "SYSTEM" and "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES"
add_library(ordertest5_systemlib INTERFACE)
target_include_directories(ordertest5_systemlib SYSTEM INTERFACE SystemIncludeDirectories/systemlib)
# The default value of `SYSTEM` is already `OFF`. Here we explicitly set it again.
set_property(TARGET ordertest5_systemlib PROPERTY SYSTEM 0)
add_library(ordertest5_userlib INTERFACE)
target_include_directories(ordertest5_userlib INTERFACE SystemIncludeDirectories/userlib)
add_library(ordertest5 ordertest.cpp)
target_link_libraries(ordertest5 PRIVATE ordertest5_systemlib ordertest5_userlib)
# Test that the include of imported executable is treated as system by default.
add_executable(ordertest6_systemexe IMPORTED)
target_include_directories(ordertest6_systemexe INTERFACE SystemIncludeDirectories/systemlib)
set_property(TARGET ordertest6_systemexe PROPERTY ENABLE_EXPORTS 1)
add_library(ordertest6_userlib INTERFACE)
target_include_directories(ordertest6_userlib INTERFACE SystemIncludeDirectories/userlib)
add_library(ordertest6 ordertest.cpp)
target_link_libraries(ordertest6 PRIVATE ordertest6_systemexe ordertest6_userlib)
# Test that the include of imported executable is not treated as system if "SYSTEM" property is OFF.
add_library(ordertest7 ordertest.cpp)
target_include_directories(ordertest7 SYSTEM PRIVATE SystemIncludeDirectories/systemlib)
target_link_libraries(ordertest7 PRIVATE ordertest7_userexe)
add_library(ordertest7_userexe INTERFACE IMPORTED)
target_include_directories(ordertest7_userexe INTERFACE SystemIncludeDirectories/userlib)
set_property(TARGET ordertest7_userexe PROPERTY ENABLE_EXPORTS 1)
set_property(TARGET ordertest7_userexe PROPERTY SYSTEM 0)
add_subdirectory(StandardIncludeDirectories)
add_subdirectory(TargetIncludeDirectories)