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