@@ -423,6 +423,75 @@ def edt_flash_params(cmd, flash):
423423
424424class RimageSigner (Signer ):
425425
426+ def check_if_sof_dir (self , maybe_sof_dir ):
427+ '''
428+ Check if a pathlib.Path() object is the root of the SOF tree.
429+
430+ Returns a Path object if it is indeed the SOF tree, or None if not.
431+
432+ This checks if the file "versions.json" exists under the path, as
433+ this file contains the SOF versioning information.
434+ '''
435+ versions_json_file = maybe_sof_dir / 'versions.json'
436+ if versions_json_file .exists ():
437+ return maybe_sof_dir
438+
439+ return None
440+
441+ def get_sof_src_dir (self ):
442+ '''
443+ Find the SOF tree.
444+
445+ Returns a Path object if SOF tree is found, or None if not.
446+ '''
447+ sof_src_dir = None
448+
449+ # Try to find SOF directory via path to rimage config files pointed to
450+ # via CMake cached variable "RIMAGE_CONFIG_PATH" if it exists.
451+ # The config files are inside <sof top>/tools/tools/rimage/config in
452+ # the SOF tree.
453+ conf_dir = self .cmake_cache .get ('RIMAGE_CONFIG_PATH' )
454+ if conf_dir :
455+ # Config path is <sof top>/tools/rimage/config/
456+ # So need to go up 3 levels to get SOF directory.
457+ maybe_sof_dir = pathlib .Path (conf_dir ).parent .parent .parent
458+
459+ # Make sure this is the actual SOF directory as
460+ # RIMAGE_CONFIG_PATH may point to somewhere else.
461+ sof_src_dir = self .check_if_sof_dir (maybe_sof_dir )
462+
463+ # Try to find SOF if it is included via manifest as a project
464+ # under the main manifest (e.g. included via a sub-manifest).
465+ if not sof_src_dir :
466+ try :
467+ sof_proj = self .command .manifest .get_projects (['sof' ], allow_paths = False )
468+ sof_src_dir = pathlib .Path (sof_proj [0 ].abspath )
469+
470+ # Since SOF is pulled in as a project, we assume it is
471+ # the correct one as the manifest has to be modified
472+ # manually.
473+ except ValueError :
474+ pass
475+
476+ # Try to find SOF as SOF is the top level manifest via west init.
477+ if not sof_src_dir :
478+ maybe_sof_dir = pathlib .Path (manifest .manifest_path ()).parent
479+
480+ # Make sure this is the actual SOF directory
481+ # as the top level manifest may not be SOF.
482+ sof_src_dir = self .check_if_sof_dir (maybe_sof_dir )
483+
484+ # If the above all failed to find the SOF tree, see if the path is specified
485+ # via environment variable "SOF_SRC_DIR" as last resort.
486+ if not sof_src_dir and os .getenv ("SOF_SRC_DIR" ):
487+ maybe_sof_dir = pathlib .Path (os .getenv ("SOF_SRC_DIR" ))
488+
489+ # Make sure this is the actual SOF directory
490+ # as the top level manifest may not be SOF.
491+ sof_src_dir = self .check_if_sof_dir (maybe_sof_dir )
492+
493+ return sof_src_dir
494+
426495 def rimage_config_dir (self ):
427496 'Returns the rimage/config/ directory with the highest precedence'
428497 args = self .command .args
@@ -438,7 +507,11 @@ def rimage_config_dir(self):
438507 def generate_uuid_registry (self ):
439508 'Runs the uuid-registry.h generator script'
440509
441- generate_cmd = [sys .executable , str (self .sof_src_dir / 'scripts' / 'gen-uuid-reg.py' ),
510+ uuid_script_path = self .sof_src_dir / 'scripts' / 'gen-uuid-reg.py'
511+ if not uuid_script_path .exists ():
512+ self .command .die (f"{ uuid_script_path } does not exists." )
513+
514+ generate_cmd = [sys .executable , str (uuid_script_path ),
442515 str (self .sof_src_dir / 'uuid-registry.txt' ),
443516 str (pathlib .Path ('zephyr' ) / 'include' / 'generated' / 'uuid-registry.h' )
444517 ]
@@ -554,13 +627,11 @@ def sign(self, command, build_dir, build_conf, formats):
554627 if not args .quiet :
555628 command .inf ('Signing with tool {}' .format (tool_path ))
556629
557- try :
558- sof_proj = command .manifest .get_projects (['sof' ], allow_paths = False )
559- sof_src_dir = pathlib .Path (sof_proj [0 ].abspath )
560- except ValueError : # sof is the manifest
561- sof_src_dir = pathlib .Path (manifest .manifest_path ()).parent
562-
563- self .sof_src_dir = sof_src_dir
630+ self .sof_src_dir = self .get_sof_src_dir ()
631+ if self .sof_src_dir :
632+ command .inf (f"SOF directory: { self .sof_src_dir } " )
633+ else :
634+ command .die ("Cannot find SOF directory." )
564635
565636
566637 command .inf ('Signing for SOC target ' + target )
@@ -605,7 +676,7 @@ def sign(self, command, build_dir, build_conf, formats):
605676 if '-k' not in sign_config_extra_args + args .tool_args :
606677 # rimage requires a key argument even when it does not sign
607678 cmake_default_key = cache .get ('RIMAGE_SIGN_KEY' , 'key placeholder from sign.py' )
608- extra_ri_args += [ '-k' , str (sof_src_dir / 'keys' / cmake_default_key ) ]
679+ extra_ri_args += [ '-k' , str (self . sof_src_dir / 'keys' / cmake_default_key ) ]
609680
610681 if args .tool_data and '-c' in args .tool_args :
611682 command .wrn ('--tool-data ' + args .tool_data + ' ignored! Overridden by: -- -c ... ' )
0 commit comments