Generate dump types table

This commit refines the map generation process in
phosphor-debug-collector. The existing Python script is enhanced to
accept varying templates and variable names, A new Mako template is
introduced for generating dump types. This enhancement allows different
architectures to support various types of dumps without requiring major
changes.

Tests:
Successfully tested the creation of different types of
BMC dumps.

Built with master and p10bmc

Change-Id: I347e218cb66386665bd15b72612dbe8e1e4fc7cf
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/dump_types.cpp b/dump_types.mako.cpp
similarity index 80%
rename from dump_types.cpp
rename to dump_types.mako.cpp
index ac3b31c..251599f 100644
--- a/dump_types.cpp
+++ b/dump_types.mako.cpp
@@ -1,3 +1,6 @@
+## This file is a template.  The comment below is emitted
+## into the rendered file; feel free to edit this file.
+// !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
 #include "dump_types.hpp"
 
 #include <phosphor-logging/elog-errors.hpp>
@@ -9,18 +12,21 @@
 {
 namespace dump
 {
+
 DUMP_TYPE_TABLE dumpTypeTable = {
-    {"xyz.openbmc_project.Dump.Create.DumpType.UserRequested",
-     {DumpTypes::USER, "BMC_DUMP"}},
-    {"xyz.openbmc_project.Dump.Create.DumpType.ApplicationCored",
-     {DumpTypes::CORE, "BMC_DUMP"}},
-    {"xyz.openbmc_project.Dump.Create.DumpType.Ramoops",
-     {DumpTypes::RAMOOPS, "BMC_DUMP"}}};
+% for item in DUMP_TYPE_TABLE:
+  % for key, values in item.items():
+    {"${key}", {DumpTypes::${values[0].upper()}, "${values[1]}"}},
+  % endfor
+% endfor
+};
 
 DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {
-    {DumpTypes::USER, "user"},
-    {DumpTypes::CORE, "core"},
-    {DumpTypes::RAMOOPS, "ramoops"},
+% for item in DUMP_TYPE_TABLE:
+  % for key, values in item.items():
+    {DumpTypes::${values[0].upper()}, "${values[0]}"},
+  % endfor
+% endfor
 };
 
 std::optional<std::string> dumpTypeToString(const DumpTypes& dumpType)
diff --git a/dump_types.hpp b/dump_types.mako.hpp
similarity index 86%
rename from dump_types.hpp
rename to dump_types.mako.hpp
index 11c500d..c46bcdc 100644
--- a/dump_types.hpp
+++ b/dump_types.mako.hpp
@@ -1,3 +1,6 @@
+## This file is a template.  The comment below is emitted
+## into the rendered file; feel free to edit this file.
+// !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
 #pragma once
 
 #include <optional>
@@ -19,11 +22,12 @@
 using DUMP_COLLECTION_TYPE = std::string;
 
 // Dump types
-enum class DumpTypes
-{
-    USER,
-    CORE,
-    RAMOOPS,
+enum class DumpTypes {
+% for item in DUMP_TYPE_TABLE:
+  % for key, values in item.items():
+    ${values[0].upper()},
+  % endfor
+% endfor
 };
 
 // A table of dump types
diff --git a/errors_map_gen.py b/errors_map_gen.py
deleted file mode 100755
index 9fafae9..0000000
--- a/errors_map_gen.py
+++ /dev/null
@@ -1,44 +0,0 @@
-#!/usr/bin/env python3
-
-import argparse
-import os
-
-import yaml
-from mako.template import Template
-
-
-def main():
-    parser = argparse.ArgumentParser(
-        description="OpenPOWER error map code generator"
-    )
-
-    parser.add_argument(
-        "-i",
-        "--errors_map_yaml",
-        dest="errors_map_yaml",
-        default="errors_watch.yaml",
-        help="input errors watch yaml file to parse",
-    )
-
-    parser.add_argument(
-        "-c",
-        "--cpp_file",
-        dest="cpp_file",
-        default="errors_map.cpp",
-        help="output cpp file",
-    )
-    args = parser.parse_args()
-
-    with open(os.path.join(script_dir, args.errors_map_yaml), "r") as fd:
-        yamlDict = yaml.safe_load(fd)
-
-        # Render the mako template for cpp file
-        template = os.path.join(script_dir, "errors_map.mako.cpp")
-        t = Template(filename=template)
-        with open(args.cpp_file, "w") as fd:
-            fd.write(t.render(errDict=yamlDict))
-
-
-if __name__ == "__main__":
-    script_dir = os.path.dirname(os.path.realpath(__file__))
-    main()
diff --git a/example_dump_types.yaml b/example_dump_types.yaml
new file mode 100644
index 0000000..feb650c
--- /dev/null
+++ b/example_dump_types.yaml
@@ -0,0 +1,12 @@
+- xyz.openbmc_project.Dump.Create.DumpType.UserRequested:
+      - user
+      - BMC_DUMP
+- xyz.openbmc_project.Dump.Create.DumpType.ApplicationCored:
+      - core
+      - BMC_DUMP
+- xyz.openbmc_project.Dump.Create.DumpType.Ramoops:
+      - ramoops
+      - BMC_DUMP
+- xyz.openbmc_project.Dump.Create.DumpType.ErrorLog:
+      - elog
+      - BMC_DUMP
diff --git a/map_gen.py b/map_gen.py
new file mode 100755
index 0000000..39215d1
--- /dev/null
+++ b/map_gen.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+import argparse
+import os
+
+import yaml
+from mako.template import Template
+
+
+def main():
+    parser = argparse.ArgumentParser(
+        description="OpenPOWER map code generator"
+    )
+
+    parser.add_argument(
+        "-i",
+        "--input_yaml",
+        dest="input_yaml",
+        default="example.yaml",
+        help="input yaml file to parse",
+    )
+
+    parser.add_argument(
+        "-t",
+        "--template",
+        dest="template",
+        default="template.mako.cpp",
+        help="mako template file to use",
+    )
+
+    parser.add_argument(
+        "-o",
+        "--output_file",
+        dest="output_file",
+        default="output.cpp",
+        help="output cpp file",
+    )
+
+    parser.add_argument(
+        "-v",
+        "--var_name",
+        dest="var_name",
+        default="mapping",
+        help="variable name to use in the template",
+    )
+
+    args = parser.parse_args()
+
+    with open(os.path.join(script_dir, args.input_yaml), "r") as fd:
+        yaml_dict = yaml.safe_load(fd)
+
+    template = os.path.join(script_dir, args.template)
+    t = Template(filename=template)
+    with open(args.output_file, "w") as fd:
+        if args.var_name == "errDict":
+            fd.write(t.render(errDict=yaml_dict))
+        else:
+            fd.write(t.render(DUMP_TYPE_TABLE=yaml_dict))
+
+
+if __name__ == "__main__":
+    script_dir = os.path.dirname(os.path.realpath(__file__))
+    main()
diff --git a/meson.build b/meson.build
index 5696b45..54a233c 100644
--- a/meson.build
+++ b/meson.build
@@ -124,20 +124,88 @@
               )
 subdir('xyz/openbmc_project/Dump/Internal/Create')
 
+dump_types_yaml_files = []
+
+# Dump types YAML file
+dump_types_yaml_files += {'input': 'example_dump_types.yaml',
+         'output': 'dump_types.yaml'}
+
+# Copy and combine YAML files
+concatenate_command = 'cat '
+combined_yaml_file = 'combined_dump_types.yaml'
+
+foreach yaml_file : dump_types_yaml_files
+    configure_file(input : yaml_file.get('input'),
+                   output : yaml_file.get('output'),
+                   copy : true)
+    concatenate_command += meson.build_root() + '/' + yaml_file.get('output') + ' '
+endforeach
+
+concatenate_command += '> ' + meson.build_root() + '/' + combined_yaml_file
+run_command('sh', '-c', concatenate_command)
+
 python = find_program('python3')
-errors_map_gen_file_loc = meson.project_source_root()
-errors_map_gen_file_loc += '/errors_map_gen.py'
+map_gen_file_loc = meson.project_source_root()
+map_gen_file_loc += '/map_gen.py'
+
+dump_types_hpp = custom_target(
+                    'dump_types.hpp',
+                    command : [
+                        python,
+                        map_gen_file_loc,
+                        '-i',
+                        meson.build_root() + '/' +  combined_yaml_file,
+                        '-t',
+                        'dump_types.mako.hpp',
+                        '-v',
+                        'DUMP_TYPE_TABLE',
+                        '-o',
+                        'dump_types.hpp'
+                    ],
+                    depend_files : [ 'dump_types.mako.hpp',
+                                     'map_gen.py',
+                                     meson.build_root() + '/' +  combined_yaml_file
+                                   ],
+                    output : 'dump_types.hpp'
+                 )
+
+dump_types_cpp = custom_target(
+                    'dump_types.cpp',
+                    command : [
+                        python,
+                        map_gen_file_loc,
+                        '-i',
+                        meson.build_root() + '/' +  combined_yaml_file,
+                        '-t',
+                        'dump_types.mako.cpp',
+                        '-v',
+                        'DUMP_TYPE_TABLE',
+                        '-o',
+                        'dump_types.cpp'
+                    ],
+                    depend_files : [ 'dump_types.mako.cpp',
+                                     'map_gen.py',
+                                     meson.build_root() + '/' +  combined_yaml_file
+                                   ],
+                    output : 'dump_types.cpp'
+                 )
 
 errors_map_cpp = custom_target(
                     'errors_map.cpp',
                     command : [
                         python,
-                        errors_map_gen_file_loc,
+                        map_gen_file_loc,
                         '-i',
-                        get_option('ERROR_MAP_YAML')
+                        get_option('ERROR_MAP_YAML'),
+                        '-t',
+                        'errors_map.mako.cpp',
+                        '-v',
+                        'errDict',
+                        '-o',
+                        'errors_map.cpp'
                     ],
                     depend_files : [ 'errors_map.mako.cpp',
-                                     'errors_map_gen.py',
+                                     'map_gen.py',
                                      get_option('ERROR_MAP_YAML')
                                    ],
                     output : 'errors_map.cpp'
@@ -154,13 +222,14 @@
         common_hpp,
         server_hpp,
         server_cpp,
+        dump_types_hpp,
+        dump_types_cpp,
         'watch.cpp',
         'bmc_dump_entry.cpp',
         'dump_utils.cpp',
         'dump_offload.cpp',
         'dump_manager_faultlog.cpp',
-        'faultlog_dump_entry.cpp',
-        'dump_types.cpp'
+        'faultlog_dump_entry.cpp'
     ]
 
 phosphor_dump_manager_dependency = [
@@ -184,6 +253,7 @@
 subdir('dump-extensions')
 
 phosphor_dump_monitor_sources = [
+        dump_types_hpp,
         'core_manager.cpp',
         'core_manager_main.cpp',
         'watch.cpp'
@@ -199,6 +269,7 @@
 phosphor_dump_monitor_incdir = []
 
 phosphor_ramoops_monitor_sources = [
+        dump_types_hpp,
         'ramoops_manager.cpp',
         'ramoops_manager_main.cpp',
         'watch.cpp'