Skip to content

Commit 4484410

Browse files
authored
CMake.ctest() runner new tools.cmake:ctest_args conf (#19198)
* CMake.ctest() runner new tools.cmake:ctest_args conf * remove --quiet when necessary * remove managemnt of --quiet * fix test
1 parent ca3a88f commit 4484410

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

conan/internal/model/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"tools.cmake.cmake_layout:test_folder": "(Experimental) Allow configuring the base folder of the build for test_package",
9494
"tools.cmake:cmake_program": "Path to CMake executable",
9595
"tools.cmake.cmakedeps:new": "Use the new CMakeDeps generator",
96+
"tools.cmake:ctest_args": "To inject list of arguments to CMake.ctest() runner",
9697
"tools.cmake:install_strip": "(Deprecated) Add --strip to cmake.install(). Use tools.build:install_strip instead",
9798
"tools.deployer:symlinks": "Set to False to disable deployers copying symlinks",
9899
"tools.files.download:retry": "Number of retries in case of failure when downloading",

conan/tools/cmake/cmake.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def configure(self, variables=None, build_script_folder=None, cli_args=None,
6868
:ref:`CMakeToolchain<conan-cmake-toolchain>` to get:
6969
7070
- The generator, to append ``-G="xxx"``.
71-
- The path to the toolchain and append ``-DCMAKE_TOOLCHAIN_FILE=/path/conan_toolchain.cmake``
72-
- The declared ``cache variables`` and append ``-Dxxx``.
71+
- Toolchain path to append ``-DCMAKE_TOOLCHAIN_FILE=/path/conan_toolchain.cmake``
72+
- The declared ``cache variables`` to append ``-Dxxx``.
7373
7474
and call ``cmake``.
7575
@@ -83,9 +83,9 @@ def configure(self, variables=None, build_script_folder=None, cli_args=None,
8383
``self.folders.source`` at the ``layout()`` method.
8484
:param cli_args: List of arguments ``[arg1, arg2, ...]`` that will be passed
8585
as extra CLI arguments to pass to cmake invocation
86-
:param subfolder: (Experimental): The name of a subfolder to be created inside the ``build_folder``
87-
and the ``package_folder``. If not provided, files will be placed
88-
in the ``build_folder`` and the ``package_folder`` root.
86+
:param subfolder: (Experimental): subfolder to be created inside the ``build_folder`` and
87+
the ``package_folder``. If not provided, files will be
88+
placed in the ``build_folder`` and ``package_folder`` root.
8989
:param stdout: Use it to redirect stdout to this stream
9090
:param stderr: Use it to redirect stderr to this stream
9191
"""
@@ -186,23 +186,25 @@ def build(self, build_type=None, target=None, cli_args=None, build_tool_args=Non
186186
for a multi-configuration generator (e.g. Visual Studio, XCode).
187187
This value will be ignored for single-configuration generators, they will
188188
use the one defined in the toolchain file during the install step.
189-
:param target: The name of a single build target as a string, or names of multiple build targets in a
190-
list of strings to be passed to the ``--target`` argument.
189+
:param target: The name of a single build target as a string, or names of multiple build
190+
targets in a list of strings to be passed to the ``--target`` argument.
191191
:param cli_args: A list of arguments ``[arg1, arg2, ...]`` that will be passed to the
192192
``cmake --build ... arg1 arg2`` command directly.
193193
:param build_tool_args: A list of arguments ``[barg1, barg2, ...]`` for the underlying
194194
build system that will be passed to the command
195195
line after the ``--`` indicator: ``cmake --build ... -- barg1 barg2``
196-
:param subfolder: (Experimental): The name of a subfolder to be created inside the ``build_folder``
197-
and the ``package_folder``. If not provided, files will be placed
198-
in the ``build_folder`` and the ``package_folder`` root.
196+
:param subfolder: (Experimental): subfolder to be created inside the ``build_folder`` and
197+
the ``package_folder``. If not provided, files will be
198+
placed in the ``build_folder`` and ``package_folder`` root.
199199
:param stdout: Use it to redirect stdout to this stream
200200
:param stderr: Use it to redirect stderr to this stream
201201
"""
202202
self._conanfile.output.info("Running CMake.build()")
203-
self._build(build_type, target, cli_args, build_tool_args, subfolder=subfolder, stdout=stdout, stderr=stderr)
203+
self._build(build_type, target, cli_args, build_tool_args, subfolder=subfolder,
204+
stdout=stdout, stderr=stderr)
204205

205-
def install(self, build_type=None, component=None, cli_args=None, stdout=None, stderr=None, subfolder=None):
206+
def install(self, build_type=None, component=None, cli_args=None, stdout=None, stderr=None,
207+
subfolder=None):
206208
"""
207209
Equivalent to running ``cmake --install``
208210
@@ -213,9 +215,9 @@ def install(self, build_type=None, component=None, cli_args=None, stdout=None, s
213215
not build type.
214216
:param cli_args: A list of arguments ``[arg1, arg2, ...]`` for the underlying build system
215217
that will be passed to the command line: ``cmake --install ... arg1 arg2``
216-
:param subfolder: (Experimental): The name of a subfolder to be created inside the ``build_folder``
217-
and the ``package_folder``. If not provided, files will be placed
218-
in the ``build_folder`` and the ``package_folder`` root.
218+
:param subfolder: (Experimental): subfolder to be created inside the ``build_folder`` and
219+
the ``package_folder``. If not provided, files will be
220+
placed in the ``build_folder`` and ``package_folder`` root.
219221
:param stdout: Use it to redirect stdout to this stream
220222
:param stderr: Use it to redirect stderr to this stream
221223
"""
@@ -236,10 +238,12 @@ def install(self, build_type=None, component=None, cli_args=None, stdout=None, s
236238
arg_list.extend(["--component", component])
237239
arg_list.extend(self._compilation_verbosity_arg)
238240

239-
deprecated_install_strip = self._conanfile.conf.get("tools.cmake:install_strip", check_type=bool)
241+
deprecated_install_strip = self._conanfile.conf.get("tools.cmake:install_strip",
242+
check_type=bool)
240243
if deprecated_install_strip:
241-
self._conanfile.output.warning("The 'tools.cmake:install_strip' configuration is deprecated, "
242-
"use 'tools.build:install_strip' instead.", warn_tag="deprecated")
244+
self._conanfile.output.warning("The 'tools.cmake:install_strip' configuration is "
245+
"deprecated, use 'tools.build:install_strip' instead.",
246+
warn_tag="deprecated")
243247

244248
do_strip = self._conanfile.conf.get("tools.build:install_strip", check_type=bool)
245249
if do_strip or deprecated_install_strip:
@@ -287,7 +291,8 @@ def ctest(self, cli_args=None, env="", stdout=None, stderr=None):
287291
"""
288292
Equivalent to running ctest ...
289293
290-
:param cli_args: List of arguments ``[arg1, arg2, ...]`` to be passed as extra ctest command line arguments
294+
:param cli_args: List of arguments ``[arg1, arg2, ...]`` to be passed as extra ctest
295+
command line arguments
291296
:param env: the environment files to activate, by default conanbuild + conanrun
292297
:param stdout: Use it to redirect stdout to this stream
293298
:param stderr: Use it to redirect stderr to this stream
@@ -304,11 +309,19 @@ def ctest(self, cli_args=None, env="", stdout=None, stderr=None):
304309
if njobs:
305310
arg_list.append(f"--parallel {njobs}")
306311

312+
arg_list.extend(cli_args or [])
313+
307314
verbosity = self._conanfile.conf.get("tools.build:verbosity", choices=("quiet", "verbose"))
308315
if verbosity:
316+
# https://cmake.org/cmake/help/latest/manual/ctest.1.html
317+
# Options such as --verbose, --extra-verbose, and --debug are
318+
# ignored if --quiet is specified.
309319
arg_list.append(f"--{verbosity}")
310320

311-
arg_list.extend(cli_args or [])
321+
extra_args = self._conanfile.conf.get("tools.cmake:ctest_args", check_type=list)
322+
if extra_args:
323+
arg_list.extend(extra_args)
324+
312325
arg_list = " ".join(filter(None, arg_list))
313326
command = f"ctest {arg_list}"
314327

test/unittests/tools/cmake/test_cmake_install.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from conan.test.utils.tools import redirect_output
1111

1212

13-
1413
def test_run_install_component():
1514
"""
1615
Testing that the proper component is installed.
@@ -80,6 +79,7 @@ def test_run_install_strip(config, deprecated):
8079
assert "tools.cmake:install_strip" not in stderr
8180
assert "--strip" in conanfile.command
8281

82+
8383
def test_run_install_cli_args():
8484
"""
8585
Testing that the passing cli_args to install works

test/unittests/tools/cmake/test_cmake_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,27 @@ def test_cli_args_configure():
5858
cmake = CMake(conanfile)
5959
cmake.configure(cli_args=["--graphviz=foo.dot"])
6060
assert "--graphviz=foo.dot" in conanfile.command
61+
62+
63+
def test_run_ctest():
64+
settings = Settings.loads(default_settings_yml)
65+
settings.os = "Windows"
66+
settings.arch = "x86"
67+
settings.build_type = "Release"
68+
settings.compiler = "msvc"
69+
settings.compiler.runtime = "dynamic"
70+
settings.compiler.version = "193"
71+
72+
conanfile = ConanFileMock()
73+
conanfile.conf = Conf()
74+
conanfile.conf.define("tools.cmake:ctest_args", ["--debug", "--output-junit myfile"])
75+
conanfile.conf.define("tools.build:verbosity", "verbose")
76+
77+
conanfile.folders.generators = "."
78+
conanfile.folders.set_base_generators(temp_folder())
79+
conanfile.settings = settings
80+
81+
write_cmake_presets(conanfile, "toolchain", "Ninja", {})
82+
cmake = CMake(conanfile)
83+
cmake.ctest(cli_args=["--schedule-random", "--quiet"])
84+
assert "--schedule-random --quiet --verbose --debug --output-junit myfile" in conanfile.command

0 commit comments

Comments
 (0)