gen-scalar-data.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/.
  4. # Write out scalar information for C++. The scalars are defined
  5. # in a file provided as a command-line argument.
  6. from __future__ import print_function
  7. from shared_telemetry_utils import StringTable, static_assert
  8. import parse_scalars
  9. import sys
  10. # The banner/text at the top of the generated file.
  11. banner = """/* This file is auto-generated, only for internal use in TelemetryScalar.h,
  12. see gen-scalar-data.py. */
  13. """
  14. file_header = """\
  15. #ifndef mozilla_TelemetryScalarData_h
  16. #define mozilla_TelemetryScalarData_h
  17. #include "ScalarInfo.h"
  18. namespace {
  19. """
  20. file_footer = """\
  21. } // namespace
  22. #endif // mozilla_TelemetryScalarData_h
  23. """
  24. def write_scalar_info(scalar, output, name_index, expiration_index):
  25. """Writes a scalar entry to the output file.
  26. :param scalar: a ScalarType instance describing the scalar.
  27. :param output: the output stream.
  28. :param name_index: the index of the scalar name in the strings table.
  29. :param expiration_index: the index of the expiration version in the strings table.
  30. """
  31. cpp_guard = scalar.cpp_guard
  32. if cpp_guard:
  33. print("#if defined(%s)" % cpp_guard, file=output)
  34. print(" {{ {}, {}, {}, {}, {} }},"\
  35. .format(scalar.nsITelemetry_kind,
  36. name_index,
  37. expiration_index,
  38. scalar.dataset,
  39. "true" if scalar.keyed else "false"),
  40. file=output)
  41. if cpp_guard:
  42. print("#endif", file=output)
  43. def write_scalar_tables(scalars, output):
  44. """Writes the scalar and strings tables to an header file.
  45. :param scalars: a list of ScalarType instances describing the scalars.
  46. :param output: the output stream.
  47. """
  48. string_table = StringTable()
  49. print("const ScalarInfo gScalars[] = {", file=output)
  50. for s in scalars:
  51. # We add both the scalar label and the expiration string to the strings
  52. # table.
  53. name_index = string_table.stringIndex(s.label)
  54. exp_index = string_table.stringIndex(s.expires)
  55. # Write the scalar info entry.
  56. write_scalar_info(s, output, name_index, exp_index)
  57. print("};", file=output)
  58. string_table_name = "gScalarsStringTable"
  59. string_table.writeDefinition(output, string_table_name)
  60. static_assert(output, "sizeof(%s) <= UINT32_MAX" % string_table_name,
  61. "index overflow")
  62. def main(output, *filenames):
  63. # Load the scalars first.
  64. if len(filenames) > 1:
  65. raise Exception('We don\'t support loading from more than one file.')
  66. scalars = parse_scalars.load_scalars(filenames[0])
  67. # Write the scalar data file.
  68. print(banner, file=output)
  69. print(file_header, file=output)
  70. write_scalar_tables(scalars, output)
  71. print(file_footer, file=output)
  72. if __name__ == '__main__':
  73. main(sys.stdout, *sys.argv[1:])