From 69beee5314f70b3ddee8029f6ab3d684035005e5 Mon Sep 17 00:00:00 2001 From: Da Quexian Date: Wed, 1 Jun 2022 16:36:59 +0800 Subject: Add SYSTEM target property If it is ON, treat INTERFACE_INCLUDE_DIRECTORIES as system include directories. Issue: #18040 Signed-off-by: Da Quexian --- Help/manual/cmake-properties.7.rst | 1 + Help/prop_tgt/IMPORTED_NO_SYSTEM.rst | 3 +- Help/prop_tgt/SYSTEM.rst | 16 ++++++++++ Help/release/dev/system.rst | 7 +++++ Source/cmGeneratorTarget.cxx | 11 +++++-- Source/cmTarget.cxx | 4 +++ Tests/IncludeDirectories/CMakeLists.txt | 54 +++++++++++++++++++++++++++++++++ 7 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 Help/prop_tgt/SYSTEM.rst create mode 100644 Help/release/dev/system.rst diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst index d88322c..3f061fe 100644 --- a/Help/manual/cmake-properties.7.rst +++ b/Help/manual/cmake-properties.7.rst @@ -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 diff --git a/Help/prop_tgt/IMPORTED_NO_SYSTEM.rst b/Help/prop_tgt/IMPORTED_NO_SYSTEM.rst index ee22d6f..49228ad 100644 --- a/Help/prop_tgt/IMPORTED_NO_SYSTEM.rst +++ b/Help/prop_tgt/IMPORTED_NO_SYSTEM.rst @@ -7,7 +7,8 @@ Specifies that an :ref:`Imported Target ` 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. diff --git a/Help/prop_tgt/SYSTEM.rst b/Help/prop_tgt/SYSTEM.rst new file mode 100644 index 0000000..2db6aed --- /dev/null +++ b/Help/prop_tgt/SYSTEM.rst @@ -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. diff --git a/Help/release/dev/system.rst b/Help/release/dev/system.rst new file mode 100644 index 0000000..c6d1bd2 --- /dev/null +++ b/Help/release/dev/system.rst @@ -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). diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 8a7215b..9147f8a 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -807,11 +807,16 @@ void handleSystemIncludesDep(cmLocalGenerator* lg, dagChecker, depTgt, language), result); } - if (!depTgt->IsImported() || excludeImported) { + if (!depTgt->GetPropertyAsBool("SYSTEM")) { return; } - if (depTgt->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) { - return; + if (depTgt->IsImported()) { + if (excludeImported) { + return; + } + if (depTgt->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) { + return; + } } if (cmValue dirs = depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) { diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 83dc1c2..d5b9b06 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -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()) { diff --git a/Tests/IncludeDirectories/CMakeLists.txt b/Tests/IncludeDirectories/CMakeLists.txt index bb4b92c..e82cea2 100644 --- a/Tests/IncludeDirectories/CMakeLists.txt +++ b/Tests/IncludeDirectories/CMakeLists.txt @@ -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) -- cgit v0.12