From accabeb272e50869c75c6bfaa4ffd1b2b88cb0d7 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 11 Mar 2026 13:15:59 -0400 Subject: [PATCH] install: Add option to use project name as default component Teach `CMAKE_INSTALL_DEFAULT_COMPONENT_NAME` to accept a special `` placeholder. Fixes: #27682 --- ...OMPONENT_NAME-project-name-placeholder.rst | 7 +++++ .../CMAKE_INSTALL_DEFAULT_COMPONENT_NAME.rst | 10 +++++++ Source/cmInstallCommand.cxx | 8 ++++++ Tests/RunCMake/install/RunCMakeTest.cmake | 2 ++ ...eDependencies-Default-COMPONENT-Name.cmake | 21 ++++++++++++++ ...ojectName-Placeholder-COMPONENT-Name.cmake | 28 +++++++++++++++++++ 6 files changed, 76 insertions(+) create mode 100644 Help/release/dev/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME-project-name-placeholder.rst create mode 100644 Tests/RunCMake/install/RuntimeDependencies-Default-COMPONENT-Name.cmake create mode 100644 Tests/RunCMake/install/RuntimeDependencies-ProjectName-Placeholder-COMPONENT-Name.cmake diff --git a/Help/release/dev/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME-project-name-placeholder.rst b/Help/release/dev/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME-project-name-placeholder.rst new file mode 100644 index 0000000000..37dc6264b5 --- /dev/null +++ b/Help/release/dev/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME-project-name-placeholder.rst @@ -0,0 +1,7 @@ +CMAKE_INSTALL_DEFAULT_COMPONENT_NAME-project-name-placeholder +------------------------------------------------------------- + +* :variable:`CMAKE_INSTALL_DEFAULT_COMPONENT_NAME` gained support for a + special ```` placeholder value. It tells each + :command:`install` invocation to take a default component name + from the current :variable:`PROJECT_NAME` variable value. diff --git a/Help/variable/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME.rst b/Help/variable/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME.rst index 57160f1d9a..15ec0cdf82 100644 --- a/Help/variable/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME.rst +++ b/Help/variable/CMAKE_INSTALL_DEFAULT_COMPONENT_NAME.rst @@ -7,3 +7,13 @@ If an :command:`install` command is used without the ``COMPONENT`` argument, these files will be grouped into a default component. The name of this default install component will be taken from this variable. It defaults to ``Unspecified``. + +There is a special component name: + +```` + .. versionadded:: 4.4 + + This literal placeholder will expand to the contents of the + :variable:`PROJECT_NAME` variable when the :command:`install` command is + evaluated. If no :command:`project` command has been called it defaults to + ``Unspecified``. diff --git a/Source/cmInstallCommand.cxx b/Source/cmInstallCommand.cxx index 1d37546a51..c33ef25646 100644 --- a/Source/cmInstallCommand.cxx +++ b/Source/cmInstallCommand.cxx @@ -94,6 +94,14 @@ public: { this->DefaultComponentName = this->Makefile->GetSafeDefinition( "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"); + if (this->DefaultComponentName == "") { + cmValue projectName = this->Makefile->GetDefinition("PROJECT_NAME"); + if (!projectName->empty()) { + this->DefaultComponentName = projectName; + } else { + this->DefaultComponentName = "Unspecified"; + } + } if (this->DefaultComponentName.empty()) { this->DefaultComponentName = "Unspecified"; } diff --git a/Tests/RunCMake/install/RunCMakeTest.cmake b/Tests/RunCMake/install/RunCMakeTest.cmake index fcd1cd503e..ff8e812fba 100644 --- a/Tests/RunCMake/install/RunCMakeTest.cmake +++ b/Tests/RunCMake/install/RunCMakeTest.cmake @@ -215,6 +215,8 @@ if(CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin|Windows)$") unset(RunCMake_TEST_OPTIONS) run_cmake(TARGETS-RUNTIME_DEPENDENCY_SET-RUNTIME_DEPENDENCIES-conflict) run_cmake(RuntimeDependencies-COMPONENTS) + run_cmake(RuntimeDependencies-Default-COMPONENT-Name) + run_cmake(RuntimeDependencies-ProjectName-Placeholder-COMPONENT-Name) else() run_cmake(TARGETS-RUNTIME_DEPENDENCIES-unsupported) run_cmake(TARGETS-RUNTIME_DEPENDENCY_SET-unsupported) diff --git a/Tests/RunCMake/install/RuntimeDependencies-Default-COMPONENT-Name.cmake b/Tests/RunCMake/install/RuntimeDependencies-Default-COMPONENT-Name.cmake new file mode 100644 index 0000000000..47e8f36800 --- /dev/null +++ b/Tests/RunCMake/install/RuntimeDependencies-Default-COMPONENT-Name.cmake @@ -0,0 +1,21 @@ +enable_language(C) + +function(check_components value) + get_cmake_property(comp COMPONENTS) + if(NOT comp STREQUAL value) + message(FATAL_ERROR "Expected value of COMPONENTS:\n ${value}\nActual value of COMPONENTS:\n ${comp}") + endif() +endfunction() + +if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + add_library(tgt MODULE obj1.c) +else() + add_executable(tgt main.c) +endif() + +set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "default_name") +install(TARGETS tgt + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ) +check_components("default_name") diff --git a/Tests/RunCMake/install/RuntimeDependencies-ProjectName-Placeholder-COMPONENT-Name.cmake b/Tests/RunCMake/install/RuntimeDependencies-ProjectName-Placeholder-COMPONENT-Name.cmake new file mode 100644 index 0000000000..6701bcc71d --- /dev/null +++ b/Tests/RunCMake/install/RuntimeDependencies-ProjectName-Placeholder-COMPONENT-Name.cmake @@ -0,0 +1,28 @@ +enable_language(C) + +function(check_components value) + get_cmake_property(comp COMPONENTS) + if(NOT comp STREQUAL value) + message(FATAL_ERROR "Expected value of COMPONENTS:\n ${value}\nActual value of COMPONENTS:\n ${comp}") + endif() +endfunction() + +project(hello_world) +if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + add_library(tgt MODULE obj1.c) + add_library(tgt2 MODULE obj1.c) +else() + add_executable(tgt main.c) + add_executable(tgt2 main.c) +endif() + +set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "") +install(TARGETS tgt + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ) +install(TARGETS tgt2 + RUNTIME DESTINATION bin COMPONENT "other" + LIBRARY DESTINATION lib COMPONENT "other" + ) +check_components("hello_world;other")