3
3
# SPDX-License-Identifier: Apache-2.0
4
4
5
5
import argparse
6
+ import contextlib
6
7
import os
7
8
import pathlib
8
9
import shlex
9
10
import sys
10
- import yaml
11
11
12
+ import yaml
13
+ from build_helpers import FIND_BUILD_DIR_DESCRIPTION , find_build_dir , is_zephyr_build , load_domains
12
14
from west .commands import Verbosity
13
15
from west .configuration import config
14
16
from west .util import west_topdir
15
17
from west .version import __version__
16
- from zcmake import DEFAULT_CMAKE_GENERATOR , run_cmake , run_build , CMakeCache
17
- from build_helpers import is_zephyr_build , find_build_dir , load_domains , \
18
- FIND_BUILD_DIR_DESCRIPTION
19
-
18
+ from zcmake import DEFAULT_CMAKE_GENERATOR , CMakeCache , run_build , run_cmake
20
19
from zephyr_ext_common import Forceable
21
20
22
21
_ARG_SEPARATOR = '--'
@@ -69,7 +68,7 @@ def __call__(self, parser, namespace, values, option_string=None):
69
68
class Build (Forceable ):
70
69
71
70
def __init__ (self ):
72
- super (Build , self ).__init__ (
71
+ super ().__init__ (
73
72
'build' ,
74
73
# Keep this in sync with the string in west-commands.yml.
75
74
'compile a Zephyr application' ,
@@ -189,7 +188,7 @@ def do_add_parser(self, parser_adder):
189
188
def do_run (self , args , remainder ):
190
189
self .args = args # Avoid having to pass them around
191
190
self .config_board = config_get ('board' , None )
192
- self .dbg ('args: {} remainder: {}' . format ( args , remainder ) ,
191
+ self .dbg (f 'args: { args } remainder: { remainder } ' ,
193
192
level = Verbosity .DBG_EXTREME )
194
193
# Store legacy -s option locally
195
194
source_dir = self .args .source_dir
@@ -211,11 +210,10 @@ def do_run(self, args, remainder):
211
210
212
211
if source_dir :
213
212
if self .args .source_dir :
214
- self .die ("source directory specified twice:({} and {})" . format (
215
- source_dir , self .args .source_dir ) )
213
+ self .die (
214
+ f"source directory specified twice:( { source_dir } and { self .args .source_dir } )" )
216
215
self .args .source_dir = source_dir
217
- self .dbg ('source_dir: {} cmake_opts: {}' .format (self .args .source_dir ,
218
- self .args .cmake_opts ),
216
+ self .dbg (f'source_dir: { self .args .source_dir } cmake_opts: { self .args .cmake_opts } ' ,
219
217
level = Verbosity .DBG_EXTREME )
220
218
self ._sanity_precheck ()
221
219
self ._setup_build_dir ()
@@ -227,13 +225,11 @@ def do_run(self, args, remainder):
227
225
pristine = config_get ('pristine' , 'never' )
228
226
if pristine not in ['auto' , 'always' , 'never' ]:
229
227
self .wrn (
230
- 'treating unknown build.pristine value "{}" as "never"' .
231
- format (pristine ))
228
+ f'treating unknown build.pristine value "{ pristine } " as "never"' )
232
229
pristine = 'never'
233
230
self .auto_pristine = pristine == 'auto'
234
231
235
- self .dbg ('pristine: {} auto_pristine: {}' .format (pristine ,
236
- self .auto_pristine ),
232
+ self .dbg (f'pristine: { pristine } auto_pristine: { self .auto_pristine } ' ,
237
233
level = Verbosity .DBG_MORE )
238
234
if is_zephyr_build (self .build_dir ):
239
235
if pristine == 'always' :
@@ -322,7 +318,7 @@ def _parse_test_item(self, test_item):
322
318
if not os .path .exists (yf ):
323
319
continue
324
320
found_test_metadata = True
325
- with open (yf , 'r' ) as stream :
321
+ with open (yf ) as stream :
326
322
try :
327
323
y = yaml .safe_load (stream )
328
324
except yaml .YAMLError as exc :
@@ -369,17 +365,21 @@ def _parse_test_item(self, test_item):
369
365
# conditional configs (xxx:yyy:CONFIG_FOO=bar)
370
366
# are not supported by 'west build'
371
367
self .wrn ('"west build" does not support '
372
- 'conditional config "{}". Add "-D{}" '
368
+ f 'conditional config "{ arg } ". Add "-D{ arg [ colon + 1 :] } " '
373
369
'to the supplied CMake arguments if '
374
- 'desired.' . format ( arg , arg [ colon + 1 :]) )
370
+ 'desired.' )
375
371
continue
376
372
args .append ("-D{}" .format (arg .replace ('"' , '\" ' )))
377
373
elif data == 'extra_args' :
378
374
# Retain quotes around config options
379
375
config_options = [arg for arg in arg_list if arg .startswith ("CONFIG_" )]
380
- non_config_options = [arg for arg in arg_list if not arg .startswith ("CONFIG_" )]
376
+ non_config_options = [
377
+ arg for arg in arg_list if not arg .startswith ("CONFIG_" )
378
+ ]
381
379
args = ["-D{}" .format (a .replace ('"' , '\" ' )) for a in config_options ]
382
- args .extend (["-D{}" .format (arg .replace ('"' , '' )) for arg in non_config_options ])
380
+ args .extend ([
381
+ "-D{}" .format (arg .replace ('"' , '' )) for arg in non_config_options
382
+ ])
383
383
elif data == 'extra_conf_files' :
384
384
extra_conf_files .extend (arg_list )
385
385
continue
@@ -429,16 +429,14 @@ def _sanity_precheck(self):
429
429
if app :
430
430
self .check_force (
431
431
os .path .isdir (app ),
432
- 'source directory {} does not exist' . format ( app ) )
432
+ f 'source directory { app } does not exist' )
433
433
self .check_force (
434
434
'CMakeLists.txt' in os .listdir (app ),
435
- "{ } doesn't contain a CMakeLists.txt". format ( app ) )
435
+ f" { app } doesn't contain a CMakeLists.txt" )
436
436
437
437
def _update_cache (self ):
438
- try :
438
+ with contextlib . suppress ( FileNotFoundError ) :
439
439
self .cmake_cache = CMakeCache .from_build_dir (self .build_dir )
440
- except FileNotFoundError :
441
- pass
442
440
443
441
def _setup_build_dir (self ):
444
442
# Initialize build_dir and created_build_dir attributes.
@@ -456,8 +454,7 @@ def _setup_build_dir(self):
456
454
457
455
if os .path .exists (build_dir ):
458
456
if not os .path .isdir (build_dir ):
459
- self .die ('build directory {} exists and is not a directory' .
460
- format (build_dir ))
457
+ self .die (f'build directory { build_dir } exists and is not a directory' )
461
458
else :
462
459
os .makedirs (build_dir , exist_ok = False )
463
460
self .created_build_dir = True
@@ -494,23 +491,20 @@ def _find_source_dir(self):
494
491
def _sanity_check_source_dir (self ):
495
492
if self .source_dir == self .build_dir :
496
493
# There's no forcing this.
497
- self .die ('source and build directory {} cannot be the same; '
498
- 'use --build-dir {} to specify a build directory' .
499
- format (self .source_dir , self .build_dir ))
494
+ self .die (f'source and build directory { self .source_dir } cannot be the same; '
495
+ f'use --build-dir { self .build_dir } to specify a build directory' )
500
496
501
497
srcrel = os .path .relpath (self .source_dir )
502
498
self .check_force (
503
499
not is_zephyr_build (self .source_dir ),
504
- 'it looks like {srcrel} is a build directory: '
505
- 'did you mean --build-dir {srcrel} instead?' .
506
- format (srcrel = srcrel ))
500
+ f'it looks like { srcrel } is a build directory: '
501
+ f'did you mean --build-dir { srcrel } instead?' )
507
502
self .check_force (
508
503
'CMakeLists.txt' in os .listdir (self .source_dir ),
509
- 'source directory "{srcrel}" does not contain '
504
+ f 'source directory "{ srcrel } " does not contain '
510
505
'a CMakeLists.txt; is this really what you '
511
506
'want to build? (Use -s SOURCE_DIR to specify '
512
- 'the application source directory)' .
513
- format (srcrel = srcrel ))
507
+ 'the application source directory)' )
514
508
515
509
def _sanity_check (self ):
516
510
# Sanity check the build configuration.
@@ -548,10 +542,9 @@ def _sanity_check(self):
548
542
549
543
self .check_force (
550
544
not apps_mismatched or self .auto_pristine ,
551
- 'Build directory "{}" is for application "{}", but source '
552
- 'directory "{}" was specified; please clean it, use --pristine, '
553
- 'or use --build-dir to set another build directory' .
554
- format (self .build_dir , cached_abs , source_abs ))
545
+ f'Build directory "{ self .build_dir } " is for application "{ cached_abs } ", but source '
546
+ f'directory "{ source_abs } " was specified; please clean it, use --pristine, '
547
+ 'or use --build-dir to set another build directory' )
555
548
556
549
if apps_mismatched :
557
550
self .run_cmake = True # If they insist, we need to re-run cmake.
@@ -577,10 +570,10 @@ def _sanity_check(self):
577
570
self .args .board != cached_board )
578
571
self .check_force (
579
572
not boards_mismatched or self .auto_pristine ,
580
- 'Build directory {} targets board {}, but board {} was specified. '
573
+ f'Build directory { self .build_dir } targets board { cached_board } , '
574
+ 'but board {self.args.board} was specified. '
581
575
'(Clean the directory, use --pristine, or use --build-dir to '
582
- 'specify a different one.)' .
583
- format (self .build_dir , cached_board , self .args .board ))
576
+ 'specify a different one.)' )
584
577
585
578
if self .auto_pristine and (apps_mismatched or boards_mismatched ):
586
579
self ._run_pristine ()
@@ -610,7 +603,7 @@ def _run_cmake(self, board, origin, cmake_opts):
610
603
self ._banner ('generating a build system' )
611
604
612
605
if board is not None and origin != 'CMakeCache.txt' :
613
- cmake_opts = ['-DBOARD={}' . format ( board ) ]
606
+ cmake_opts = [f '-DBOARD={ board } ' ]
614
607
else :
615
608
cmake_opts = []
616
609
if self .args .cmake_opts :
@@ -633,28 +626,27 @@ def _run_cmake(self, board, origin, cmake_opts):
633
626
634
627
config_sysbuild = config_getboolean ('sysbuild' , False )
635
628
if self .args .sysbuild or (config_sysbuild and not self .args .no_sysbuild ):
636
- cmake_opts .extend (['-S{}' . format ( SYSBUILD_PROJ_DIR ) ,
637
- '-DAPP_DIR:PATH={}' . format ( self .source_dir ) ])
629
+ cmake_opts .extend ([f '-S{ SYSBUILD_PROJ_DIR } ' ,
630
+ f '-DAPP_DIR:PATH={ self .source_dir } ' ])
638
631
else :
639
632
# self.args.no_sysbuild == True or config sysbuild False
640
- cmake_opts .extend (['-S{}' . format ( self .source_dir ) ])
633
+ cmake_opts .extend ([f '-S{ self .source_dir } ' ])
641
634
642
635
# Invoke CMake from the current working directory using the
643
636
# -S and -B options (officially introduced in CMake 3.13.0).
644
637
# This is important because users expect invocations like this
645
638
# to Just Work:
646
639
#
647
640
# west build -- -DOVERLAY_CONFIG=relative-path.conf
648
- final_cmake_args = ['-DWEST_PYTHON={}' .format (pathlib .Path (sys .executable ).as_posix ()),
649
- '-B{}' .format (self .build_dir ),
650
- '-G{}' .format (config_get ('generator' ,
651
- DEFAULT_CMAKE_GENERATOR ))]
641
+ final_cmake_args = [f'-DWEST_PYTHON={ pathlib .Path (sys .executable ).as_posix ()} ' ,
642
+ f'-B{ self .build_dir } ' ,
643
+ f'-G{ config_get ("generator" , DEFAULT_CMAKE_GENERATOR )} ' ]
652
644
if cmake_opts :
653
645
final_cmake_args .extend (cmake_opts )
654
646
run_cmake (final_cmake_args , dry_run = self .args .dry_run )
655
647
656
648
def _run_pristine (self ):
657
- self ._banner ('making build dir {} pristine' . format ( self . build_dir ) )
649
+ self ._banner (f 'making build dir { self . build_dir } pristine' )
658
650
if not is_zephyr_build (self .build_dir ):
659
651
self .die ('Refusing to run pristine on a folder that is not a '
660
652
'Zephyr build system' )
@@ -671,7 +663,7 @@ def _run_pristine(self):
671
663
672
664
def _run_build (self , target , domain ):
673
665
if target :
674
- self ._banner ('running target {}' . format ( target ) )
666
+ self ._banner (f 'running target { target } ' )
675
667
elif self .run_cmake :
676
668
self ._banner ('building application' )
677
669
extra_args = ['--target' , target ] if target else []
0 commit comments