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.
Change-Id: I347e218cb66386665bd15b72612dbe8e1e4fc7cf
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/dump_types.cpp b/dump_types.cpp
deleted file mode 100644
index 817f823..0000000
--- a/dump_types.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-#include "dump_types.hpp"
-namespace phosphor
-{
-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"}},
-    {"xyz.openbmc_project.Dump.Create.DumpType.ErrorLog",
-     {DumpTypes::ELOG, "elog"}}};
-
-DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {
-    {DumpTypes::USER, "user"},
-    {DumpTypes::CORE, "core"},
-    {DumpTypes::RAMOOPS, "ramoops"},
-    {DumpTypes::ELOG, "elog"},
-};
-} // namespace dump
-} // namespace phosphor
diff --git a/dump_types.mako.cpp b/dump_types.mako.cpp
new file mode 100644
index 0000000..a2b02f5
--- /dev/null
+++ b/dump_types.mako.cpp
@@ -0,0 +1,27 @@
+## 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"
+namespace phosphor
+{
+namespace dump
+{
+DUMP_TYPE_TABLE dumpTypeTable = {
+% 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 = {
+% for item in DUMP_TYPE_TABLE:
+  % for key, values in item.items():
+    {DumpTypes::${values[0].upper()}, "${values[0]}"},
+  % endfor
+% endfor
+};
+
+} // namespace dump
+} // namespace phosphor
+
diff --git a/dump_types.hpp b/dump_types.mako.hpp
similarity index 90%
rename from dump_types.hpp
rename to dump_types.mako.hpp
index 219b10d..994ecb7 100644
--- a/dump_types.hpp
+++ b/dump_types.mako.hpp
@@ -19,12 +19,12 @@
 using DUMP_COLLECTION_TYPE = std::string;
 
 // Dump types
-enum class DumpTypes
-{
-    USER,
-    CORE,
-    RAMOOPS,
-    ELOG,
+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 47aaff4..c35f26a 100644
--- a/meson.build
+++ b/meson.build
@@ -118,24 +118,75 @@
 conf_data.set('BMC_DUMP_ROTATE_CONFIG', get_option('dump_rotate_config').enabled(),
                description : 'Turn on rotate config for bmc dump'
              )
+conf_data.set('DUMP_TYPES_YAML', get_option('DUMP_TYPES_YAML'),
+               description : 'YAML filepath containing dump types'
+             )
 
 configure_file(configuration : conf_data,
                output : 'config.h'
               )
 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',
+                        get_option('DUMP_TYPES_YAML'),
+                        '-t',
+                        'dump_types.mako.hpp',
+                        '-v',
+                        'DUMP_TYPE_TABLE',
+                        '-o',
+                        'dump_types.hpp'
+                    ],
+                    depend_files : [ 'dump_types.mako.hpp',
+                                     'map_gen.py',
+                                     get_option('DUMP_TYPES_YAML')
+                                   ],
+                    output : 'dump_types.hpp'
+                 )
+
+dump_types_cpp = custom_target(
+                    'dump_types.cpp',
+                    command : [
+                        python,
+                        map_gen_file_loc,
+                        '-i',
+                        get_option('DUMP_TYPES_YAML'),
+                        '-t',
+                        'dump_types.mako.cpp',
+			                  '-v',
+			                  'DUMP_TYPE_TABLE',
+                        '-o',
+                        'dump_types.cpp'
+                    ],
+                    depend_files : [ 'dump_types.mako.cpp',
+                                     'map_gen.py',
+                                     get_option('DUMP_TYPES_YAML')
+                                   ],
+                    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'
@@ -149,13 +200,14 @@
         'dump_serialize.cpp',
         'elog_watch.cpp',
         errors_map_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 = [
diff --git a/meson_options.txt b/meson_options.txt
index 0fe8e95..07b6cbc 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -76,6 +76,11 @@
         description : 'YAML filepath containing error object paths'
       )
 
+option('DUMP_TYPES_YAML', type : 'string',
+        value : 'example_dump_types.yaml',
+        description : 'YAML filepath containing error dump types'
+      )
+
 option('host-transport', type : 'string',
         value : 'default',
         description : 'To specify the host dump transport protocol')