Browse Source

scripts: west_commands: build: Fix linter issues

Fix issues reported by ruff.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
pull/88314/merge
Pieter De Gendt 2 months ago committed by Anas Nashif
parent
commit
166fd3e506
  1. 8
      .ruff-excludes.toml
  2. 98
      scripts/west_commands/build.py

8
.ruff-excludes.toml

@ -1078,14 +1078,6 @@
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation-union "UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation-union
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import "UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
] ]
"./scripts/west_commands/build.py" = [
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"SIM105", # https://docs.astral.sh/ruff/rules/suppressible-exception
"UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
"UP032", # https://docs.astral.sh/ruff/rules/f-string
]
"./scripts/west_commands/build_helpers.py" = [ "./scripts/west_commands/build_helpers.py" = [
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file "E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports

98
scripts/west_commands/build.py

@ -3,20 +3,19 @@
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
import argparse import argparse
import contextlib
import os import os
import pathlib import pathlib
import shlex import shlex
import sys import sys
import yaml
import yaml
from build_helpers import FIND_BUILD_DIR_DESCRIPTION, find_build_dir, is_zephyr_build, load_domains
from west.commands import Verbosity from west.commands import Verbosity
from west.configuration import config from west.configuration import config
from west.util import west_topdir from west.util import west_topdir
from west.version import __version__ from west.version import __version__
from zcmake import DEFAULT_CMAKE_GENERATOR, run_cmake, run_build, CMakeCache from zcmake import DEFAULT_CMAKE_GENERATOR, CMakeCache, run_build, run_cmake
from build_helpers import is_zephyr_build, find_build_dir, load_domains, \
FIND_BUILD_DIR_DESCRIPTION
from zephyr_ext_common import Forceable from zephyr_ext_common import Forceable
_ARG_SEPARATOR = '--' _ARG_SEPARATOR = '--'
@ -69,7 +68,7 @@ class AlwaysIfMissing(argparse.Action):
class Build(Forceable): class Build(Forceable):
def __init__(self): def __init__(self):
super(Build, self).__init__( super().__init__(
'build', 'build',
# Keep this in sync with the string in west-commands.yml. # Keep this in sync with the string in west-commands.yml.
'compile a Zephyr application', 'compile a Zephyr application',
@ -189,7 +188,7 @@ class Build(Forceable):
def do_run(self, args, remainder): def do_run(self, args, remainder):
self.args = args # Avoid having to pass them around self.args = args # Avoid having to pass them around
self.config_board = config_get('board', None) self.config_board = config_get('board', None)
self.dbg('args: {} remainder: {}'.format(args, remainder), self.dbg(f'args: {args} remainder: {remainder}',
level=Verbosity.DBG_EXTREME) level=Verbosity.DBG_EXTREME)
# Store legacy -s option locally # Store legacy -s option locally
source_dir = self.args.source_dir source_dir = self.args.source_dir
@ -211,11 +210,10 @@ class Build(Forceable):
if source_dir: if source_dir:
if self.args.source_dir: if self.args.source_dir:
self.die("source directory specified twice:({} and {})".format( self.die(
source_dir, self.args.source_dir)) f"source directory specified twice:({source_dir} and {self.args.source_dir})")
self.args.source_dir = source_dir self.args.source_dir = source_dir
self.dbg('source_dir: {} cmake_opts: {}'.format(self.args.source_dir, self.dbg(f'source_dir: {self.args.source_dir} cmake_opts: {self.args.cmake_opts}',
self.args.cmake_opts),
level=Verbosity.DBG_EXTREME) level=Verbosity.DBG_EXTREME)
self._sanity_precheck() self._sanity_precheck()
self._setup_build_dir() self._setup_build_dir()
@ -227,13 +225,11 @@ class Build(Forceable):
pristine = config_get('pristine', 'never') pristine = config_get('pristine', 'never')
if pristine not in ['auto', 'always', 'never']: if pristine not in ['auto', 'always', 'never']:
self.wrn( self.wrn(
'treating unknown build.pristine value "{}" as "never"'. f'treating unknown build.pristine value "{pristine}" as "never"')
format(pristine))
pristine = 'never' pristine = 'never'
self.auto_pristine = pristine == 'auto' self.auto_pristine = pristine == 'auto'
self.dbg('pristine: {} auto_pristine: {}'.format(pristine, self.dbg(f'pristine: {pristine} auto_pristine: {self.auto_pristine}',
self.auto_pristine),
level=Verbosity.DBG_MORE) level=Verbosity.DBG_MORE)
if is_zephyr_build(self.build_dir): if is_zephyr_build(self.build_dir):
if pristine == 'always': if pristine == 'always':
@ -322,7 +318,7 @@ class Build(Forceable):
if not os.path.exists(yf): if not os.path.exists(yf):
continue continue
found_test_metadata = True found_test_metadata = True
with open(yf, 'r') as stream: with open(yf) as stream:
try: try:
y = yaml.safe_load(stream) y = yaml.safe_load(stream)
except yaml.YAMLError as exc: except yaml.YAMLError as exc:
@ -369,17 +365,21 @@ class Build(Forceable):
# conditional configs (xxx:yyy:CONFIG_FOO=bar) # conditional configs (xxx:yyy:CONFIG_FOO=bar)
# are not supported by 'west build' # are not supported by 'west build'
self.wrn('"west build" does not support ' self.wrn('"west build" does not support '
'conditional config "{}". Add "-D{}" ' f'conditional config "{arg}". Add "-D{arg[colon+1:]}" '
'to the supplied CMake arguments if ' 'to the supplied CMake arguments if '
'desired.'.format(arg, arg[colon+1:])) 'desired.')
continue continue
args.append("-D{}".format(arg.replace('"', '\"'))) args.append("-D{}".format(arg.replace('"', '\"')))
elif data == 'extra_args': elif data == 'extra_args':
# Retain quotes around config options # Retain quotes around config options
config_options = [arg for arg in arg_list if arg.startswith("CONFIG_")] config_options = [arg for arg in arg_list if arg.startswith("CONFIG_")]
non_config_options = [arg for arg in arg_list if not arg.startswith("CONFIG_")] non_config_options = [
arg for arg in arg_list if not arg.startswith("CONFIG_")
]
args = ["-D{}".format(a.replace('"', '\"')) for a in config_options] args = ["-D{}".format(a.replace('"', '\"')) for a in config_options]
args.extend(["-D{}".format(arg.replace('"', '')) for arg in non_config_options]) args.extend([
"-D{}".format(arg.replace('"', '')) for arg in non_config_options
])
elif data == 'extra_conf_files': elif data == 'extra_conf_files':
extra_conf_files.extend(arg_list) extra_conf_files.extend(arg_list)
continue continue
@ -429,16 +429,14 @@ class Build(Forceable):
if app: if app:
self.check_force( self.check_force(
os.path.isdir(app), os.path.isdir(app),
'source directory {} does not exist'.format(app)) f'source directory {app} does not exist')
self.check_force( self.check_force(
'CMakeLists.txt' in os.listdir(app), 'CMakeLists.txt' in os.listdir(app),
"{} doesn't contain a CMakeLists.txt".format(app)) f"{app} doesn't contain a CMakeLists.txt")
def _update_cache(self): def _update_cache(self):
try: with contextlib.suppress(FileNotFoundError):
self.cmake_cache = CMakeCache.from_build_dir(self.build_dir) self.cmake_cache = CMakeCache.from_build_dir(self.build_dir)
except FileNotFoundError:
pass
def _setup_build_dir(self): def _setup_build_dir(self):
# Initialize build_dir and created_build_dir attributes. # Initialize build_dir and created_build_dir attributes.
@ -456,8 +454,7 @@ class Build(Forceable):
if os.path.exists(build_dir): if os.path.exists(build_dir):
if not os.path.isdir(build_dir): if not os.path.isdir(build_dir):
self.die('build directory {} exists and is not a directory'. self.die(f'build directory {build_dir} exists and is not a directory')
format(build_dir))
else: else:
os.makedirs(build_dir, exist_ok=False) os.makedirs(build_dir, exist_ok=False)
self.created_build_dir = True self.created_build_dir = True
@ -494,23 +491,20 @@ class Build(Forceable):
def _sanity_check_source_dir(self): def _sanity_check_source_dir(self):
if self.source_dir == self.build_dir: if self.source_dir == self.build_dir:
# There's no forcing this. # There's no forcing this.
self.die('source and build directory {} cannot be the same; ' self.die(f'source and build directory {self.source_dir} cannot be the same; '
'use --build-dir {} to specify a build directory'. f'use --build-dir {self.build_dir} to specify a build directory')
format(self.source_dir, self.build_dir))
srcrel = os.path.relpath(self.source_dir) srcrel = os.path.relpath(self.source_dir)
self.check_force( self.check_force(
not is_zephyr_build(self.source_dir), not is_zephyr_build(self.source_dir),
'it looks like {srcrel} is a build directory: ' f'it looks like {srcrel} is a build directory: '
'did you mean --build-dir {srcrel} instead?'. f'did you mean --build-dir {srcrel} instead?')
format(srcrel=srcrel))
self.check_force( self.check_force(
'CMakeLists.txt' in os.listdir(self.source_dir), 'CMakeLists.txt' in os.listdir(self.source_dir),
'source directory "{srcrel}" does not contain ' f'source directory "{srcrel}" does not contain '
'a CMakeLists.txt; is this really what you ' 'a CMakeLists.txt; is this really what you '
'want to build? (Use -s SOURCE_DIR to specify ' 'want to build? (Use -s SOURCE_DIR to specify '
'the application source directory)'. 'the application source directory)')
format(srcrel=srcrel))
def _sanity_check(self): def _sanity_check(self):
# Sanity check the build configuration. # Sanity check the build configuration.
@ -548,10 +542,9 @@ class Build(Forceable):
self.check_force( self.check_force(
not apps_mismatched or self.auto_pristine, not apps_mismatched or self.auto_pristine,
'Build directory "{}" is for application "{}", but source ' f'Build directory "{self.build_dir}" is for application "{cached_abs}", but source '
'directory "{}" was specified; please clean it, use --pristine, ' f'directory "{source_abs}" was specified; please clean it, use --pristine, '
'or use --build-dir to set another build directory'. 'or use --build-dir to set another build directory')
format(self.build_dir, cached_abs, source_abs))
if apps_mismatched: if apps_mismatched:
self.run_cmake = True # If they insist, we need to re-run cmake. self.run_cmake = True # If they insist, we need to re-run cmake.
@ -577,10 +570,10 @@ class Build(Forceable):
self.args.board != cached_board) self.args.board != cached_board)
self.check_force( self.check_force(
not boards_mismatched or self.auto_pristine, not boards_mismatched or self.auto_pristine,
'Build directory {} targets board {}, but board {} was specified. ' f'Build directory {self.build_dir} targets board {cached_board}, '
'but board {self.args.board} was specified. '
'(Clean the directory, use --pristine, or use --build-dir to ' '(Clean the directory, use --pristine, or use --build-dir to '
'specify a different one.)'. 'specify a different one.)')
format(self.build_dir, cached_board, self.args.board))
if self.auto_pristine and (apps_mismatched or boards_mismatched): if self.auto_pristine and (apps_mismatched or boards_mismatched):
self._run_pristine() self._run_pristine()
@ -610,7 +603,7 @@ class Build(Forceable):
self._banner('generating a build system') self._banner('generating a build system')
if board is not None and origin != 'CMakeCache.txt': if board is not None and origin != 'CMakeCache.txt':
cmake_opts = ['-DBOARD={}'.format(board)] cmake_opts = [f'-DBOARD={board}']
else: else:
cmake_opts = [] cmake_opts = []
if self.args.cmake_opts: if self.args.cmake_opts:
@ -633,11 +626,11 @@ class Build(Forceable):
config_sysbuild = config_getboolean('sysbuild', False) config_sysbuild = config_getboolean('sysbuild', False)
if self.args.sysbuild or (config_sysbuild and not self.args.no_sysbuild): if self.args.sysbuild or (config_sysbuild and not self.args.no_sysbuild):
cmake_opts.extend(['-S{}'.format(SYSBUILD_PROJ_DIR), cmake_opts.extend([f'-S{SYSBUILD_PROJ_DIR}',
'-DAPP_DIR:PATH={}'.format(self.source_dir)]) f'-DAPP_DIR:PATH={self.source_dir}'])
else: else:
# self.args.no_sysbuild == True or config sysbuild False # self.args.no_sysbuild == True or config sysbuild False
cmake_opts.extend(['-S{}'.format(self.source_dir)]) cmake_opts.extend([f'-S{self.source_dir}'])
# Invoke CMake from the current working directory using the # Invoke CMake from the current working directory using the
# -S and -B options (officially introduced in CMake 3.13.0). # -S and -B options (officially introduced in CMake 3.13.0).
@ -645,16 +638,15 @@ class Build(Forceable):
# to Just Work: # to Just Work:
# #
# west build -- -DOVERLAY_CONFIG=relative-path.conf # west build -- -DOVERLAY_CONFIG=relative-path.conf
final_cmake_args = ['-DWEST_PYTHON={}'.format(pathlib.Path(sys.executable).as_posix()), final_cmake_args = [f'-DWEST_PYTHON={pathlib.Path(sys.executable).as_posix()}',
'-B{}'.format(self.build_dir), f'-B{self.build_dir}',
'-G{}'.format(config_get('generator', f'-G{config_get("generator", DEFAULT_CMAKE_GENERATOR)}']
DEFAULT_CMAKE_GENERATOR))]
if cmake_opts: if cmake_opts:
final_cmake_args.extend(cmake_opts) final_cmake_args.extend(cmake_opts)
run_cmake(final_cmake_args, dry_run=self.args.dry_run) run_cmake(final_cmake_args, dry_run=self.args.dry_run)
def _run_pristine(self): def _run_pristine(self):
self._banner('making build dir {} pristine'.format(self.build_dir)) self._banner(f'making build dir {self.build_dir} pristine')
if not is_zephyr_build(self.build_dir): if not is_zephyr_build(self.build_dir):
self.die('Refusing to run pristine on a folder that is not a ' self.die('Refusing to run pristine on a folder that is not a '
'Zephyr build system') 'Zephyr build system')
@ -671,7 +663,7 @@ class Build(Forceable):
def _run_build(self, target, domain): def _run_build(self, target, domain):
if target: if target:
self._banner('running target {}'.format(target)) self._banner(f'running target {target}')
elif self.run_cmake: elif self.run_cmake:
self._banner('building application') self._banner('building application')
extra_args = ['--target', target] if target else [] extra_args = ['--target', target] if target else []

Loading…
Cancel
Save