Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / telemetry / build_scripts / gen_process_data.py
blob2a494689adde378a51a86afbb44759e523819831
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 # Write out processes data for C++. The processes are defined
6 # in a file provided as a command-line argument.
8 import collections
9 import sys
11 from mozparsers.shared_telemetry_utils import ParserError, load_yaml_file
13 # The banner/text at the top of the generated file.
14 banner = """/* This file is auto-generated from Telemetry build scripts,
15 see gen_process_data.py. */
16 """
18 file_header = """\
19 #ifndef mozilla_TelemetryProcessData_h
20 #define mozilla_TelemetryProcessData_h
22 #include "mozilla/TelemetryProcessEnums.h"
24 namespace mozilla {
25 namespace Telemetry {
26 """
28 file_footer = """
29 } // namespace Telemetry
30 } // namespace mozilla
31 #endif // mozilla_TelemetryProcessData_h"""
34 def to_enum_label(name):
35 return name.title().replace("_", "")
38 def write_processes_data(processes, output):
39 def p(line):
40 print(line, file=output)
42 processes = collections.OrderedDict(processes)
44 p("static GeckoProcessType ProcessIDToGeckoProcessType[%d] = {" % len(processes))
45 for i, (name, value) in enumerate(sorted(processes.items())):
47 " /* %d: ProcessID::%s = */ %s,"
48 % (i, to_enum_label(name), value["gecko_enum"])
50 p("};")
51 p("")
52 p("#if defined(_MSC_VER) && !defined(__clang__)")
53 p("static const char* const ProcessIDToString[%d] = {" % len(processes))
54 p("#else")
55 p("static constexpr const char* ProcessIDToString[%d] = {" % len(processes))
56 p("#endif")
57 for i, (name, value) in enumerate(sorted(processes.items())):
58 p(' /* %d: ProcessID::%s = */ "%s",' % (i, to_enum_label(name), name))
59 p("};")
62 def main(output, *filenames):
63 if len(filenames) > 1:
64 raise Exception("We don't support loading from more than one file.")
66 try:
67 processes = load_yaml_file(filenames[0])
69 # Write the process data file.
70 print(banner, file=output)
71 print(file_header, file=output)
72 write_processes_data(processes, output)
73 print(file_footer, file=output)
74 except ParserError as ex:
75 print("\nError generating processes data:\n" + str(ex) + "\n")
76 sys.exit(1)
79 if __name__ == "__main__":
80 main(sys.stdout, *sys.argv[1:])