From a121419908728facbaf8f31288ba76fd91ebe688 Mon Sep 17 00:00:00 2001 From: Arha Gatram Date: Wed, 24 Jun 2026 10:35:20 -0700 Subject: [PATCH] ALIAS: Add support for set_property and set_target_properties commands Currently, getting properties from an ALIAS target retrieves the property from the target which the alias references while setting properties results in an error. This relaxes set_property and set_target_properties to accept ALIAS targets and act on the referenced target, mirroring the get behavior. An error is raised when setting ALIAS_GLOBAL or ALIASED_TARGET on an ALIAS target. Closes: #19445 --- Help/command/add_executable.rst | 11 ++++++++--- Help/command/add_library.rst | 13 +++++++++---- Help/command/set_property.rst | 4 +++- Help/command/set_target_properties.rst | 4 +++- Help/manual/cmake-buildsystem.7.rst | 13 ++++++++++--- Help/release/dev/alias-transparency.rst | 6 ++++++ Source/cmSetPropertyCommand.cxx | 11 +++++++---- Source/cmSetTargetPropertiesCommand.cxx | 12 ++++++++---- Tests/RunCMake/alias_targets/RunCMakeTest.cmake | 2 ++ ...ult.txt => set_property-ALIAS_GLOBAL-result.txt} | 0 .../set_property-ALIAS_GLOBAL-stderr.txt | 2 ++ .../alias_targets/set_property-ALIAS_GLOBAL.cmake | 6 ++++++ .../RunCMake/alias_targets/set_property-stderr.txt | 4 ---- Tests/RunCMake/alias_targets/set_property.cmake | 5 +++++ ...> set_target_properties-ALIAS_GLOBAL-result.txt} | 0 .../set_target_properties-ALIAS_GLOBAL-stderr.txt | 3 +++ .../set_target_properties-ALIAS_GLOBAL.cmake | 6 ++++++ .../alias_targets/set_target_properties-stderr.txt | 4 ---- .../alias_targets/set_target_properties.cmake | 5 +++++ 19 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 Help/release/dev/alias-transparency.rst rename Tests/RunCMake/alias_targets/{set_property-result.txt => set_property-ALIAS_GLOBAL-result.txt} (100%) create mode 100644 Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL-stderr.txt create mode 100644 Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL.cmake delete mode 100644 Tests/RunCMake/alias_targets/set_property-stderr.txt rename Tests/RunCMake/alias_targets/{set_target_properties-result.txt => set_target_properties-ALIAS_GLOBAL-result.txt} (100%) create mode 100644 Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL-stderr.txt create mode 100644 Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL.cmake delete mode 100644 Tests/RunCMake/alias_targets/set_target_properties-stderr.txt diff --git a/Help/command/add_executable.rst b/Help/command/add_executable.rst index bab371485b..ae6356334d 100644 --- a/Help/command/add_executable.rst +++ b/Help/command/add_executable.rst @@ -112,11 +112,16 @@ Alias Executables ``ALIAS`` targets can be used as targets to read properties from, executables for custom commands and custom targets. They can also be tested for existence with the regular :command:`if(TARGET)` subcommand. -The ```` may not be used to modify properties of ````, that -is, it may not be used as the operand of :command:`set_property`, -:command:`set_target_properties`, :command:`target_link_libraries` etc. An ``ALIAS`` target may not be installed or exported. +.. versionchanged:: 4.5 + The ```` may be used as the operand of :command:`set_property` and + :command:`set_target_properties` to modify properties of ````. It + may not be used with the commands :command:`target_link_libraries`, + :command:`target_compile_definitions` etc. CMake 4.4 and earlier did not + allow the ```` to modify properties of ```` with + :command:`set_property` and :command:`set_target_properties`. + See Also ^^^^^^^^ diff --git a/Help/command/add_library.rst b/Help/command/add_library.rst index 66ce32717f..9272f522e8 100644 --- a/Help/command/add_library.rst +++ b/Help/command/add_library.rst @@ -317,12 +317,17 @@ Alias Libraries ``ALIAS`` targets can be used as linkable targets and as targets to read properties from. They can also be tested for existence with the -regular :command:`if(TARGET)` subcommand. The ```` may not be used -to modify properties of ````, that is, it may not be used as the -operand of :command:`set_property`, :command:`set_target_properties`, -:command:`target_link_libraries` etc. An ``ALIAS`` target may not be +regular :command:`if(TARGET)` subcommand. An ``ALIAS`` target may not be installed or exported. +.. versionchanged:: 4.5 + The ```` may be used as the operand of :command:`set_property` and + :command:`set_target_properties` to modify properties of ````. It + may not be used with the commands :command:`target_link_libraries`, + :command:`target_compile_definitions` etc. CMake 4.4 and earlier did not + allow the ```` to modify properties of ```` with + :command:`set_property` and :command:`set_target_properties`. + See Also ^^^^^^^^ diff --git a/Help/command/set_property.rst b/Help/command/set_property.rst index e560f1843a..5fe7825460 100644 --- a/Help/command/set_property.rst +++ b/Help/command/set_property.rst @@ -40,7 +40,9 @@ It must be one of the following: Scope may name zero or more existing targets. See also the :command:`set_target_properties` command. - :ref:`Alias Targets` do not support setting target properties. + .. versionchanged:: 4.5 + If ```` is an :ref:`Alias Target `, properties are + set on the target which the alias references. ``FILE_SET`` .. versionadded:: 4.3 diff --git a/Help/command/set_target_properties.rst b/Help/command/set_target_properties.rst index 5357575ba2..d172eaabe4 100644 --- a/Help/command/set_target_properties.rst +++ b/Help/command/set_target_properties.rst @@ -15,7 +15,9 @@ set next. You can use any prop value pair you want and extract it later with the :command:`get_property` or :command:`get_target_property` command. -:ref:`Alias Targets` do not support setting target properties. +.. versionchanged:: 4.5 + If ```` contains :ref:`Alias Targets`, properties are set on the + corresponding target referenced by each alias. See Also ^^^^^^^^ diff --git a/Help/manual/cmake-buildsystem.7.rst b/Help/manual/cmake-buildsystem.7.rst index e9b90fb17c..b6849becb2 100644 --- a/Help/manual/cmake-buildsystem.7.rst +++ b/Help/manual/cmake-buildsystem.7.rst @@ -1429,9 +1429,16 @@ target, which may be an :prop_tgt:`IMPORTED` target from a package, or an target_link_libraries(exe1 Upstream::lib1) ``ALIAS`` targets are not mutable, installable or exportable. They are -entirely local to the buildsystem description. A name can be tested for -whether it is an ``ALIAS`` name by reading the :prop_tgt:`ALIASED_TARGET` -property from it: +entirely local to the buildsystem description. + +.. versionchanged:: 4.5 + ``ALIAS`` targets may be used as the operand for :command:`set_property` and + similar commands used to modify properties of targets. The commands operate + on the target which the alias references. CMake 4.4 and earlier did not + allow modifying targets via an ``ALIAS``. + +A name can be tested for whether it is an ``ALIAS`` name by reading the +:prop_tgt:`ALIASED_TARGET` property from it: .. code-block:: cmake diff --git a/Help/release/dev/alias-transparency.rst b/Help/release/dev/alias-transparency.rst new file mode 100644 index 0000000000..402e42fd71 --- /dev/null +++ b/Help/release/dev/alias-transparency.rst @@ -0,0 +1,6 @@ +alias-transparency +------------------ + +* The :command:`set_property` and :command:`set_target_properties` commands + now accept :ref:`Alias Targets`. Properties are set on the target which + the alias references. diff --git a/Source/cmSetPropertyCommand.cxx b/Source/cmSetPropertyCommand.cxx index 292a308b27..749b591d06 100644 --- a/Source/cmSetPropertyCommand.cxx +++ b/Source/cmSetPropertyCommand.cxx @@ -709,11 +709,14 @@ bool HandleTargetMode(cmExecutionStatus& status, bool appendMode, bool remove) { for (std::string const& name : names) { - if (status.GetMakefile().IsAlias(name)) { - status.SetError("can not be used on an ALIAS target."); - return false; + // Better error message for alias properties set on an alias target. + if (propertyName == "ALIASED_TARGET" || propertyName == "ALIAS_GLOBAL") { + if (status.GetMakefile().IsAlias(name)) { + status.SetError(cmStrCat("can not set property ", propertyName, + " on an ALIAS target: ", name)); + return false; + } } - if (cmTarget* target = status.GetMakefile().FindTargetToUse(name)) { if (target->IsSymbolic()) { status.SetError("can not be used on a SYMBOLIC target."); diff --git a/Source/cmSetTargetPropertiesCommand.cxx b/Source/cmSetTargetPropertiesCommand.cxx index 0009bd2d50..e3a7f93067 100644 --- a/Source/cmSetTargetPropertiesCommand.cxx +++ b/Source/cmSetTargetPropertiesCommand.cxx @@ -35,10 +35,6 @@ bool cmSetTargetPropertiesCommand(std::vector const& args, // loop over all the targets for (std::string const& tname : cmStringRange{ args.begin(), propsIter }) { - if (mf.IsAlias(tname)) { - status.SetError("can not be used on an ALIAS target."); - return false; - } if (cmTarget* target = mf.FindTargetToUse(tname)) { if (target->IsSymbolic()) { status.SetError("can not be used on a SYMBOLIC target."); @@ -46,6 +42,14 @@ bool cmSetTargetPropertiesCommand(std::vector const& args, } // loop through all the props and set them for (auto k = propsIter + 1; k != args.end(); k += 2) { + // Better error message for alias properties set on an alias target. + if (*k == "ALIASED_TARGET" || *k == "ALIAS_GLOBAL") { + if (mf.IsAlias(tname)) { + status.SetError(cmStrCat("can not set property ", *k, + " on an ALIAS target: ", tname)); + return false; + } + } target->SetProperty(*k, *(k + 1)); target->CheckProperty(*k, &mf); } diff --git a/Tests/RunCMake/alias_targets/RunCMakeTest.cmake b/Tests/RunCMake/alias_targets/RunCMakeTest.cmake index 4be1b9d8d4..c5bdd3ee72 100644 --- a/Tests/RunCMake/alias_targets/RunCMakeTest.cmake +++ b/Tests/RunCMake/alias_targets/RunCMakeTest.cmake @@ -12,8 +12,10 @@ run_cmake(imported-global-target) run_cmake(imported-target) run_cmake(alias-target) run_cmake(set_property) +run_cmake(set_property-ALIAS_GLOBAL) run_cmake(get_property) run_cmake(set_target_properties) +run_cmake(set_target_properties-ALIAS_GLOBAL) run_cmake(target_link_libraries) run_cmake(target_include_directories) run_cmake(export) diff --git a/Tests/RunCMake/alias_targets/set_property-result.txt b/Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL-result.txt similarity index 100% rename from Tests/RunCMake/alias_targets/set_property-result.txt rename to Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL-result.txt diff --git a/Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL-stderr.txt b/Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL-stderr.txt new file mode 100644 index 0000000000..02dc9d7c8a --- /dev/null +++ b/Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL-stderr.txt @@ -0,0 +1,2 @@ +CMake Error at set_property-ALIAS_GLOBAL\.cmake:[0-9]+ \(set_property\): + set_property can not set property ALIAS_GLOBAL on an ALIAS target: alias diff --git a/Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL.cmake b/Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL.cmake new file mode 100644 index 0000000000..f4c3207822 --- /dev/null +++ b/Tests/RunCMake/alias_targets/set_property-ALIAS_GLOBAL.cmake @@ -0,0 +1,6 @@ +enable_language(CXX) + +add_library(foo empty.cpp) +add_library(alias ALIAS foo) + +set_property(TARGET alias PROPERTY ALIAS_GLOBAL something) diff --git a/Tests/RunCMake/alias_targets/set_property-stderr.txt b/Tests/RunCMake/alias_targets/set_property-stderr.txt deleted file mode 100644 index 67efae024b..0000000000 --- a/Tests/RunCMake/alias_targets/set_property-stderr.txt +++ /dev/null @@ -1,4 +0,0 @@ -CMake Error at set_property\.cmake:8 \(set_property\): - set_property can not be used on an ALIAS target\. -Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\) diff --git a/Tests/RunCMake/alias_targets/set_property.cmake b/Tests/RunCMake/alias_targets/set_property.cmake index 1f5e475c4a..310bf87227 100644 --- a/Tests/RunCMake/alias_targets/set_property.cmake +++ b/Tests/RunCMake/alias_targets/set_property.cmake @@ -6,3 +6,8 @@ add_library(foo empty.cpp) add_library(alias ALIAS foo) set_property(TARGET alias PROPERTY ANYTHING 1) + +get_target_property(val foo ANYTHING) +if(NOT val STREQUAL "1") + message(SEND_ERROR "set_property(): Target property 'ANYTHING' set on ALIAS 'alias' has wrong value on aliased target 'foo': '${val}' instead of '1'.") +endif() diff --git a/Tests/RunCMake/alias_targets/set_target_properties-result.txt b/Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL-result.txt similarity index 100% rename from Tests/RunCMake/alias_targets/set_target_properties-result.txt rename to Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL-result.txt diff --git a/Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL-stderr.txt b/Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL-stderr.txt new file mode 100644 index 0000000000..449c969750 --- /dev/null +++ b/Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL-stderr.txt @@ -0,0 +1,3 @@ +CMake Error at set_target_properties-ALIAS_GLOBAL\.cmake:[0-9]+ \(set_target_properties\): + set_target_properties can not set property ALIAS_GLOBAL on an ALIAS target: + alias diff --git a/Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL.cmake b/Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL.cmake new file mode 100644 index 0000000000..2d25a5790d --- /dev/null +++ b/Tests/RunCMake/alias_targets/set_target_properties-ALIAS_GLOBAL.cmake @@ -0,0 +1,6 @@ +enable_language(CXX) + +add_library(foo empty.cpp) +add_library(alias ALIAS foo) + +set_target_properties(alias PROPERTIES ALIAS_GLOBAL something) diff --git a/Tests/RunCMake/alias_targets/set_target_properties-stderr.txt b/Tests/RunCMake/alias_targets/set_target_properties-stderr.txt deleted file mode 100644 index 5ca552b1cf..0000000000 --- a/Tests/RunCMake/alias_targets/set_target_properties-stderr.txt +++ /dev/null @@ -1,4 +0,0 @@ -CMake Error at set_target_properties\.cmake:8 \(set_target_properties\): - set_target_properties can not be used on an ALIAS target\. -Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\) diff --git a/Tests/RunCMake/alias_targets/set_target_properties.cmake b/Tests/RunCMake/alias_targets/set_target_properties.cmake index ee44d4595a..9957a2c779 100644 --- a/Tests/RunCMake/alias_targets/set_target_properties.cmake +++ b/Tests/RunCMake/alias_targets/set_target_properties.cmake @@ -6,3 +6,8 @@ add_library(foo empty.cpp) add_library(alias ALIAS foo) set_target_properties(alias PROPERTIES ANYTHING 1) + +get_target_property(val foo ANYTHING) +if(NOT val STREQUAL "1") + message(SEND_ERROR "set_target_properties(): Target property 'ANYTHING' set on ALIAS 'alias' has wrong value on aliased target 'foo': '${val}' instead of '1'.") +endif()