west: sign.py: generate platf.toml from platf.toml.h with cc -E

Allow using the C pre-processor to generate a
`rimage/config/platform.toml` file from a "source"
`rimage/config/platform.toml.h` file.

This is optional and fully backwards compatible.

To use, do not use `-c` and point west sign at a configuration directory
instead or let it use the default `rimage/config/` directory and change
the files there.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
diff --git a/scripts/west_commands/sign.py b/scripts/west_commands/sign.py
index 4e5e459..8d96100 100644
--- a/scripts/west_commands/sign.py
+++ b/scripts/west_commands/sign.py
@@ -434,6 +434,18 @@
         self.command.dbg(f'rimage config directory={conf_dir}')
         return conf_dir
 
+    def preprocess_toml(self, config_dir, toml_basename, subdir):
+        'Runs the C pre-processor on config_dir/toml_basename.h'
+
+        compiler_path = self.cmake_cache.get("CMAKE_C_COMPILER")
+        preproc_cmd = [compiler_path, '-P', '-E', str(config_dir / (toml_basename + '.h'))]
+        preproc_cmd += ['-I', str(self.sof_src_dir / 'src')]
+        preproc_cmd += ['-imacros',
+                        str(pathlib.Path('zephyr') / 'include' / 'generated' / 'autoconf.h')]
+        preproc_cmd += ['-o', str(subdir / toml_basename)]
+        self.command.inf(quote_sh_list(preproc_cmd))
+        subprocess.run(preproc_cmd, check=True, cwd=self.build_dir)
+
     def sign(self, command, build_dir, build_conf, formats):
         self.command = command
         args = command.args
@@ -555,7 +567,19 @@
 
         if '-c' not in sign_config_extra_args + args.tool_args:
             conf_dir = self.rimage_config_dir()
-            extra_ri_args += ['-c', str(conf_dir / (target + '.toml'))]
+            toml_basename = target + '.toml'
+            if ((conf_dir / toml_basename).exists() and
+               (conf_dir / (toml_basename + '.h')).exists()):
+                command.die(f"Cannot have both {toml_basename + '.h'} and {toml_basename} in {conf_dir}")
+
+            if (conf_dir / (toml_basename + '.h')).exists():
+                toml_subdir = pathlib.Path('zephyr') / 'misc' / 'generated'
+                self.preprocess_toml(conf_dir, toml_basename, toml_subdir)
+                toml_dir = b / toml_subdir
+            else:
+                toml_dir = conf_dir
+
+            extra_ri_args += ['-c', str(toml_dir / toml_basename)]
 
         # Warning: while not officially supported (yet?), the rimage --option that is last
         # on the command line currently wins in case of duplicate options. So pay