From 1dbcd35a01645cd611f0ce43c8f4448cc813483b Mon Sep 17 00:00:00 2001 From: Mikhail Koviazin Date: Sun, 30 Jul 2023 13:22:59 +0300 Subject: [PATCH] cmake: add support for cli_args in install() This commit fixes #14235 --- conan/tools/cmake/cmake.py | 13 ++++- .../tools/cmake/test_cmake_install.py | 56 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/conan/tools/cmake/cmake.py b/conan/tools/cmake/cmake.py index 39a326becb3..ae23ad60f24 100644 --- a/conan/tools/cmake/cmake.py +++ b/conan/tools/cmake/cmake.py @@ -163,7 +163,7 @@ def build(self, build_type=None, target=None, cli_args=None, build_tool_args=Non self._conanfile.output.info("Running CMake.build()") self._build(build_type, target, cli_args, build_tool_args) - def install(self, build_type=None, component=None): + def install(self, build_type=None, component=None, cli_args=None): """ Equivalent to run ``cmake --build . --target=install`` @@ -172,6 +172,9 @@ def install(self, build_type=None, component=None): It can fail if the build is single configuration (e.g. Unix Makefiles), as in that case the build type must be specified at configure time, not build type. + :param cli_args: A list of arguments ``[arg1, arg2, ...]`` for the underlying + build system that will be passed to the command line: + ``cmake --install ... arg1 arg2`` """ self._conanfile.output.info("Running CMake.install()") mkdir(self._conanfile, self._conanfile.package_folder) @@ -193,6 +196,14 @@ def install(self, build_type=None, component=None): if do_strip: arg_list.append("--strip") + if cli_args: + if "--install" in cli_args: + raise ConanException("Do not pass '--install' argument to 'install()'") + if "--strip" in cli_args: + self._conanfile.output.warning("Do not pass '--strip' argument to 'install()', " + "use 'tools.cmake.install_strip=True' instead") + arg_list.extend(cli_args) + arg_list = " ".join(filter(None, arg_list)) command = "%s %s" % (self._cmake_program, arg_list) self._conanfile.run(command) diff --git a/conans/test/unittests/tools/cmake/test_cmake_install.py b/conans/test/unittests/tools/cmake/test_cmake_install.py index 9d411f817f9..ab411cc3f2e 100644 --- a/conans/test/unittests/tools/cmake/test_cmake_install.py +++ b/conans/test/unittests/tools/cmake/test_cmake_install.py @@ -63,3 +63,59 @@ def test_run_install_strip(): cmake = CMake(conanfile) cmake.install() assert "--strip" in conanfile.command + + +def test_run_install_cli_args(): + """ + Testing that the passing cli_args to install works + Issue related: https://github.com/conan-io/conan/issues/14235 + """ + + settings = Settings.loads(get_default_settings_yml()) + settings.os = "Linux" + settings.arch = "x86_64" + settings.build_type = "Release" + settings.compiler = "gcc" + settings.compiler.version = "11" + + conanfile = ConanFileMock() + + conanfile.conf = Conf() + + conanfile.folders.generators = "." + conanfile.folders.set_base_generators(temp_folder()) + conanfile.settings = settings + conanfile.folders.set_base_package(temp_folder()) + + write_cmake_presets(conanfile, "toolchain", "Unix Makefiles", {}) + cmake = CMake(conanfile) + cmake.install(cli_args=["--prefix=/tmp"]) + assert "--prefix=/tmp" in conanfile.command + + +def test_run_install_cli_args_strip(): + """ + Testing that the install/strip rule is called when using cli_args + Issue related: https://github.com/conan-io/conan/issues/14235 + """ + + settings = Settings.loads(get_default_settings_yml()) + settings.os = "Linux" + settings.arch = "x86_64" + settings.build_type = "Release" + settings.compiler = "gcc" + settings.compiler.version = "11" + + conanfile = ConanFileMock() + + conanfile.conf = Conf() + + conanfile.folders.generators = "." + conanfile.folders.set_base_generators(temp_folder()) + conanfile.settings = settings + conanfile.folders.set_base_package(temp_folder()) + + write_cmake_presets(conanfile, "toolchain", "Unix Makefiles", {}) + cmake = CMake(conanfile) + cmake.install(cli_args=["--strip"]) + assert "--strip" in conanfile.command