From 77156c92ebced1d5b9762647d9ec130884d9e52c Mon Sep 17 00:00:00 2001 From: Arha Gatram Date: Mon, 22 Jun 2026 14:40:56 -0700 Subject: [PATCH] ALIAS: Add support for install and export commands The install({TARGETS, EXPORT}) versions of the command now accept ALIAS targets. The behavior is identical to what would happen if the name of the target that the ALIAS references were put instead. The export(TARGETS) command also has the same support and behavior for ALIAS targets now. Closes: #20979 --- Help/command/add_executable.rst | 8 +++++--- Help/command/add_library.rst | 10 ++++++---- Help/manual/cmake-buildsystem.7.rst | 8 +++++--- Help/release/dev/alias-transparency-install.rst | 6 ++++++ Source/cmExportCommand.cxx | 9 ++------- Source/cmInstallCommand.cxx | 13 +------------ Tests/RunCMake/alias_targets/RunCMakeTest.cmake | 1 + ...esult.txt => export-alias-and-target-result.txt} | 0 .../export-alias-and-target-stderr.txt | 2 ++ .../alias_targets/export-alias-and-target.cmake | 8 ++++++++ Tests/RunCMake/alias_targets/export-check.cmake | 9 +++++++++ Tests/RunCMake/alias_targets/export-stderr.txt | 4 ---- .../alias_targets/install-export-check.cmake | 10 ++++++++++ .../alias_targets/install-export-result.txt | 1 - .../alias_targets/install-export-stderr.txt | 4 ---- 15 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 Help/release/dev/alias-transparency-install.rst rename Tests/RunCMake/alias_targets/{export-result.txt => export-alias-and-target-result.txt} (100%) create mode 100644 Tests/RunCMake/alias_targets/export-alias-and-target-stderr.txt create mode 100644 Tests/RunCMake/alias_targets/export-alias-and-target.cmake create mode 100644 Tests/RunCMake/alias_targets/export-check.cmake delete mode 100644 Tests/RunCMake/alias_targets/export-stderr.txt create mode 100644 Tests/RunCMake/alias_targets/install-export-check.cmake delete mode 100644 Tests/RunCMake/alias_targets/install-export-result.txt delete mode 100644 Tests/RunCMake/alias_targets/install-export-stderr.txt diff --git a/Help/command/add_executable.rst b/Help/command/add_executable.rst index b2eb25ae26..db868bb844 100644 --- a/Help/command/add_executable.rst +++ b/Help/command/add_executable.rst @@ -112,13 +112,15 @@ 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. -An ``ALIAS`` target may not be installed or exported. .. versionchanged:: 4.5 The ```` may be used as the operand of :command:`set_property`, :command:`set_target_properties`, :command:`target_link_libraries`, etc. to - modify properties of ````. CMake 4.4 and earlier did not allow the - ```` to modify properties of ````. + modify properties of ````. If an ``ALIAS`` executable is passed to + an :command:`install` or :command:`export` command, the executable that the + alias references is installed or exported. CMake 4.4 and earlier did not + allow using the ```` to modify properties of ````, or using + the ```` in :command:`install` and :command:`export` commands. See Also ^^^^^^^^ diff --git a/Help/command/add_library.rst b/Help/command/add_library.rst index e9f2dc9836..43d2e31ed4 100644 --- a/Help/command/add_library.rst +++ b/Help/command/add_library.rst @@ -317,14 +317,16 @@ 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. An ``ALIAS`` target may not be -installed or exported. +regular :command:`if(TARGET)` subcommand. .. versionchanged:: 4.5 The ```` may be used as the operand of :command:`set_property`, :command:`set_target_properties`, :command:`target_link_libraries`, etc. to - modify properties of ````. CMake 4.4 and earlier did not allow the - ```` to modify properties of ````. + modify properties of ````. If an ``ALIAS`` target is passed to an + :command:`install` or :command:`export` command, the target that the alias + references is installed or exported. CMake 4.4 and earlier did not allow + using the ```` to modify properties of ````, or using the + ```` in :command:`install` and :command:`export` commands. See Also ^^^^^^^^ diff --git a/Help/manual/cmake-buildsystem.7.rst b/Help/manual/cmake-buildsystem.7.rst index b6849becb2..967c3e31ac 100644 --- a/Help/manual/cmake-buildsystem.7.rst +++ b/Help/manual/cmake-buildsystem.7.rst @@ -1433,9 +1433,11 @@ 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``. + similar commands used to modify properties of targets. The + :command:`install` and :command:`export` commands may also use an ``ALIAS`` + target as the operand. The commands operate on the target which the alias + references. CMake 4.4 and earlier did not allow modifying, installing, + or exporting 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: diff --git a/Help/release/dev/alias-transparency-install.rst b/Help/release/dev/alias-transparency-install.rst new file mode 100644 index 0000000000..6fe4358eba --- /dev/null +++ b/Help/release/dev/alias-transparency-install.rst @@ -0,0 +1,6 @@ +alias-transparency-install +-------------------------- + +* The :command:`export` and :command:`install` commands now + accept :ref:`Alias Targets`. The commands operate on the underlying + aliased target. diff --git a/Source/cmExportCommand.cxx b/Source/cmExportCommand.cxx index f3e98d32ae..415b5521d5 100644 --- a/Source/cmExportCommand.cxx +++ b/Source/cmExportCommand.cxx @@ -89,15 +89,10 @@ static void AddExportGenerator( makefile.AddExportBuildFileGenerator(std::move(exportGenerator)); } -static bool ValidateExportableTarget(std::string const& name, cmMakefile& mf, +static bool ValidateExportableTarget(std::string const& name, cmGlobalGenerator* gg, cmExecutionStatus& status) { - if (mf.IsAlias(name)) { - status.SetError(cmStrCat("given ALIAS target \"", name, - "\" which may not be exported.")); - return false; - } cmTarget const* target = gg->FindTarget(name); if (!target) { status.SetError(cmStrCat("given target \"", name, @@ -187,7 +182,7 @@ static bool HandleTargetsMode(std::vector const& args, cmGlobalGenerator* gg = mf.GetGlobalGenerator(); for (std::string const& currentTarget : *arguments.Targets) { - if (!ValidateExportableTarget(currentTarget, mf, gg, status)) { + if (!ValidateExportableTarget(currentTarget, gg, status)) { return false; } targets.emplace_back(currentTarget, std::string{}); diff --git a/Source/cmInstallCommand.cxx b/Source/cmInstallCommand.cxx index ea02df85dd..0e950037b4 100644 --- a/Source/cmInstallCommand.cxx +++ b/Source/cmInstallCommand.cxx @@ -704,18 +704,12 @@ bool HandleTargetsMode(std::vector const& args, for (std::string const& tgt : targetList) { - if (helper.Makefile->IsAlias(tgt)) { - status.SetError( - cmStrCat("TARGETS given target \"", tgt, "\" which is an alias.")); - return false; - } // Lookup this target in the current directory. cmTarget* target = helper.Makefile->FindLocalNonAliasTarget(tgt); if (!target) { // If no local target has been found, find it in the global scope. cmTarget* const globalTarget = - helper.Makefile->GetGlobalGenerator()->FindTarget( - tgt, { cm::TargetDomain::NATIVE }); + helper.Makefile->GetGlobalGenerator()->FindTarget(tgt); if (globalTarget && !globalTarget->IsImported()) { target = globalTarget; } @@ -1369,11 +1363,6 @@ bool HandleImportedRuntimeArtifactsMode(std::vector const& args, } for (std::string const& tgt : targetList) { - if (helper.Makefile->IsAlias(tgt)) { - status.SetError(cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given target \"", - tgt, "\" which is an alias.")); - return false; - } // Lookup this target in the current directory. cmTarget* target = helper.Makefile->FindTargetToUse(tgt); if (!target || !target->IsImported()) { diff --git a/Tests/RunCMake/alias_targets/RunCMakeTest.cmake b/Tests/RunCMake/alias_targets/RunCMakeTest.cmake index 4ceb9f5972..b5d7755901 100644 --- a/Tests/RunCMake/alias_targets/RunCMakeTest.cmake +++ b/Tests/RunCMake/alias_targets/RunCMakeTest.cmake @@ -27,6 +27,7 @@ run_cmake(target_link_options) run_cmake(target_precompile_headers) run_cmake(target_sources) run_cmake(export) +run_cmake(export-alias-and-target) run_cmake(install-export) run_cmake(name-conflict) run_cmake(add_dependencies) diff --git a/Tests/RunCMake/alias_targets/export-result.txt b/Tests/RunCMake/alias_targets/export-alias-and-target-result.txt similarity index 100% rename from Tests/RunCMake/alias_targets/export-result.txt rename to Tests/RunCMake/alias_targets/export-alias-and-target-result.txt diff --git a/Tests/RunCMake/alias_targets/export-alias-and-target-stderr.txt b/Tests/RunCMake/alias_targets/export-alias-and-target-stderr.txt new file mode 100644 index 0000000000..beb72f422b --- /dev/null +++ b/Tests/RunCMake/alias_targets/export-alias-and-target-stderr.txt @@ -0,0 +1,2 @@ +CMake Error in CMakeLists\.txt: + given target "foo" more than once\. diff --git a/Tests/RunCMake/alias_targets/export-alias-and-target.cmake b/Tests/RunCMake/alias_targets/export-alias-and-target.cmake new file mode 100644 index 0000000000..ae437e3c40 --- /dev/null +++ b/Tests/RunCMake/alias_targets/export-alias-and-target.cmake @@ -0,0 +1,8 @@ + +enable_language(CXX) + +add_library(foo empty.cpp) + +add_library(alias ALIAS foo) + +export(TARGETS foo alias FILE anotherFile.cmake) diff --git a/Tests/RunCMake/alias_targets/export-check.cmake b/Tests/RunCMake/alias_targets/export-check.cmake new file mode 100644 index 0000000000..795a708525 --- /dev/null +++ b/Tests/RunCMake/alias_targets/export-check.cmake @@ -0,0 +1,9 @@ +file(READ "${RunCMake_TEST_BINARY_DIR}/someFile.cmake" content) + +if(NOT "${content}" MATCHES "add_library\\(foo ") + set(RunCMake_TEST_FAILED "export(): Exported file does not define aliased target 'foo'.\n") +endif() + +if("${content}" MATCHES "add_library\\(alias ") + set(RunCMake_TEST_FAILED "export(): Exported file lists 'alias' instead of the aliased target 'foo'.\n") +endif() diff --git a/Tests/RunCMake/alias_targets/export-stderr.txt b/Tests/RunCMake/alias_targets/export-stderr.txt deleted file mode 100644 index fa3733c9e6..0000000000 --- a/Tests/RunCMake/alias_targets/export-stderr.txt +++ /dev/null @@ -1,4 +0,0 @@ -CMake Error at export\.cmake:8 \(export\): - export given ALIAS target "alias" which may not be exported\. -Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\) diff --git a/Tests/RunCMake/alias_targets/install-export-check.cmake b/Tests/RunCMake/alias_targets/install-export-check.cmake new file mode 100644 index 0000000000..3d5867c6a7 --- /dev/null +++ b/Tests/RunCMake/alias_targets/install-export-check.cmake @@ -0,0 +1,10 @@ +string(MD5 _hash "lib/cmake") +file(READ "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/Export/${_hash}/theTargets.cmake" content) + +if(NOT "${content}" MATCHES "add_library\\(foo ") + set(RunCMake_TEST_FAILED "install(): Aliased target 'foo' was not registered in the export set.\n") +endif() + +if("${content}" MATCHES "add_library\\(alias ") + set(RunCMake_TEST_FAILED "install(): 'alias' was registered in the export set instead of the aliased target 'foo'.\n") +endif() diff --git a/Tests/RunCMake/alias_targets/install-export-result.txt b/Tests/RunCMake/alias_targets/install-export-result.txt deleted file mode 100644 index d00491fd7e..0000000000 --- a/Tests/RunCMake/alias_targets/install-export-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/alias_targets/install-export-stderr.txt b/Tests/RunCMake/alias_targets/install-export-stderr.txt deleted file mode 100644 index 313b226066..0000000000 --- a/Tests/RunCMake/alias_targets/install-export-stderr.txt +++ /dev/null @@ -1,4 +0,0 @@ -CMake Error at install-export\.cmake:8 \(install\): - install TARGETS given target "alias" which is an alias\. -Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\)