Skip to content

Commit

Permalink
cmake: add support for cli_args in install() (#14397)
Browse files Browse the repository at this point in the history
This commit fixes #14235
  • Loading branch information
mkmkme authored Jul 30, 2023
1 parent aca81fc commit c21df11
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
10 changes: 9 additions & 1 deletion conan/tools/cmake/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand All @@ -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)
Expand All @@ -193,6 +196,11 @@ 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()'")
arg_list.extend(cli_args)

arg_list = " ".join(filter(None, arg_list))
command = "%s %s" % (self._cmake_program, arg_list)
self._conanfile.run(command)
Expand Down
56 changes: 56 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmake_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c21df11

Please sign in to comment.