build_workaround_header.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. # Copyright (c) 2018 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """code generator for gpu workaround definitions"""
  6. import os
  7. import os.path
  8. import sys
  9. from optparse import OptionParser
  10. _LICENSE = """// Copyright 2018 The Chromium Authors. All rights reserved.
  11. // Use of this source code is governed by a BSD-style license that can be
  12. // found in the LICENSE file.
  13. """
  14. _DO_NOT_EDIT_WARNING = ("// This file is auto-generated from " +
  15. os.path.basename(__file__) + "\n" +
  16. "// DO NOT EDIT!\n\n")
  17. def merge_files_into_workarounds(files):
  18. workarounds = set()
  19. for filename in files:
  20. with open(filename, 'r') as f:
  21. workarounds.update([workaround.strip() for workaround in f])
  22. return sorted(list(workarounds))
  23. def write_header(filename, workarounds):
  24. max_workaround_len = len(max(workarounds, key=len))
  25. with open(filename, 'w') as f:
  26. f.write(_LICENSE)
  27. f.write(_DO_NOT_EDIT_WARNING)
  28. indent = ' '
  29. macro = 'GPU_OP'
  30. # length of max string passed to write + 1
  31. max_len = len(indent) + len(macro) + 1 + max_workaround_len + 1 + 1
  32. write = lambda line: f.write(line + ' ' * (max_len - len(line)) + '\\\n')
  33. write('#define GPU_DRIVER_BUG_WORKAROUNDS(GPU_OP)')
  34. for w in workarounds:
  35. write(indent + macro + '(' + w.upper() + ',')
  36. write(indent + ' ' * (len(macro) + 1) + w + ')')
  37. # one extra line to consume the the last \
  38. f.write('// The End\n')
  39. def main(argv):
  40. usage = "usage: %prog [options] file1 file2 file3 etc"
  41. parser = OptionParser(usage=usage)
  42. parser.add_option(
  43. "--output-file",
  44. dest="output_file",
  45. default="gpu_driver_bug_workaround_autogen.h",
  46. help="the name of the header file to write")
  47. (options, _) = parser.parse_args(args=argv)
  48. workarounds = merge_files_into_workarounds(parser.largs)
  49. write_header(options.output_file, workarounds)
  50. if __name__ == '__main__':
  51. sys.exit(main(sys.argv[1:]))