Skip to content

Commit 88aa873

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
twister: improve logging of cmake commands
With verbose output enabled, twister logs the cmake arguments as a python array whenever it invokes cmake to generate a build system. This is clunky output for manual reproduction, because it forces the developer to manually join the arguments together for consumption by the shell. As an enhancement for this use case, use the standard library shlex.join() function to produce a string that can be copy/pasted directly into the shell on POSIX platforms. We should use this function instead of str.join, because it correctly handles things like shell quoting for options with spaces in them. This function is not available on Windows, so we fall back on behavior that is similar to the old one there. The main difference on Windows is that the output now includes the path to cmake as well, and not just its arguments. (This is the same on POSIX, and is intended to help debug if multiple cmakes are installed on the same machine.) We are running cmake from a couple of different places, so doing this cleanly requires adding a shared module with the platform abstraction. Signed-off-by: Martí Bolívar <[email protected]>
1 parent 66d47ca commit 88aa873

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

scripts/pylib/twister/twisterlib/environment.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
logger.setLevel(logging.DEBUG)
1919

2020
from twisterlib.error import TwisterRuntimeError
21+
from twisterlib.log_helper import log_command
2122

2223
ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
2324
if not ZEPHYR_BASE:
@@ -705,19 +706,20 @@ def check_zephyr_version(self):
705706

706707
@staticmethod
707708
def run_cmake_script(args=[]):
709+
script = os.fspath(args[0])
708710

709-
logger.debug("Running cmake script %s" % (args[0]))
711+
logger.debug("Running cmake script %s", script)
710712

711713
cmake_args = ["-D{}".format(a.replace('"', '')) for a in args[1:]]
712-
cmake_args.extend(['-P', args[0]])
714+
cmake_args.extend(['-P', script])
713715

714-
logger.debug("Calling cmake with arguments: {}".format(cmake_args))
715716
cmake = shutil.which('cmake')
716717
if not cmake:
717718
msg = "Unable to find `cmake` in path"
718719
logger.error(msg)
719720
raise Exception(msg)
720721
cmd = [cmake] + cmake_args
722+
log_command(logger, "Calling cmake", cmd)
721723

722724
kwargs = dict()
723725
kwargs['stdout'] = subprocess.PIPE
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2022 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
'''
5+
Common code used when logging that is needed by multiple modules.
6+
'''
7+
8+
import platform
9+
import shlex
10+
11+
_WINDOWS = (platform.system() == 'Windows')
12+
13+
def log_command(logger, msg, args):
14+
'''Platform-independent helper for logging subprocess invocations.
15+
Will log a command string that can be copy/pasted into a POSIX
16+
shell on POSIX platforms. This is not available on Windows, so
17+
the entire args array is logged instead.
18+
19+
:param logger: logging.Logger to use
20+
:param msg: message to associate with the command
21+
:param args: argument list as passed to subprocess module
22+
'''
23+
msg = f'{msg}: %s'
24+
if _WINDOWS:
25+
logger.debug(msg, str(args))
26+
else:
27+
logger.debug(msg, shlex.join(args))

scripts/pylib/twister/twisterlib/runner.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from multiprocessing.managers import BaseManager
2020
from twisterlib.cmakecache import CMakeCache
2121
from twisterlib.environment import canonical_zephyr_base
22+
from twisterlib.log_helper import log_command
2223

2324
logger = logging.getLogger('twister')
2425
logger.setLevel(logging.DEBUG)
@@ -262,12 +263,12 @@ def run_cmake(self, args=""):
262263
cmake_opts = ['-DBOARD={}'.format(self.platform.name)]
263264
cmake_args.extend(cmake_opts)
264265

265-
266-
logger.debug("Calling cmake with arguments: {}".format(cmake_args))
267266
cmake = shutil.which('cmake')
268267
cmd = [cmake] + cmake_args
269268
kwargs = dict()
270269

270+
log_command(logger, "Calling cmake", cmd)
271+
271272
if self.capture_output:
272273
kwargs['stdout'] = subprocess.PIPE
273274
# CMake sends the output of message() to stderr unless it's STATUS

0 commit comments

Comments
 (0)