Kenneth Waters | 1545e11 | 2010-01-14 13:20:44 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2009 The Chromium OS 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 | |
| 6 | """Produces a stub implementation for OpenGLES functions.""" |
| 7 | |
| 8 | __authors__ = ['"The Chromium OS Authors" <chromium-os-dev@googlegroups.com>'] |
| 9 | |
| 10 | import re |
| 11 | import string |
| 12 | import sys |
| 13 | |
| 14 | def Indent(s, limit=79): |
| 15 | """Indent a code line splitting at ",".""" |
| 16 | parts = re.split(r',\s*', s) |
| 17 | open_col = 0 |
| 18 | |
| 19 | lines = [''] |
| 20 | for i, part in enumerate(re.split(r',\s*', s)): |
| 21 | if i: |
| 22 | canidate = '%s, %s' % (lines[-1], part) |
| 23 | else: |
| 24 | canidate = part |
| 25 | if len(canidate) < limit: |
| 26 | lines[-1] = canidate |
| 27 | else: |
| 28 | lines[-1] += ',' |
| 29 | try: |
| 30 | open_col = lines[-1].rindex('(') + 1 |
| 31 | except ValueError: |
| 32 | pass |
| 33 | lines.append(open_col * ' ' + part) |
| 34 | |
| 35 | return '\n'.join(lines) |
| 36 | |
| 37 | |
| 38 | class EntryPoint(object): |
| 39 | def __init__(self, type, name, args, filter): |
| 40 | self.type = type |
| 41 | self.name = name |
| 42 | self.args = args |
| 43 | self.filter = filter |
| 44 | self.typed_arg_string = ', '.join('%s %s' % arg for arg in self.args) |
| 45 | |
| 46 | def GenericStub(self): |
| 47 | s = [] |
| 48 | s.append(Indent('%s %s(%s) {' % (self.type, self.name, |
| 49 | self.typed_arg_string))) |
| 50 | if self.type != 'void': |
| 51 | s.append(' return (%s)0;' % self.type) |
| 52 | s.append('}') |
| 53 | return '\n'.join(s) |
| 54 | |
| 55 | def GenStub(self): |
| 56 | assert len(self.args) == 2 |
| 57 | |
| 58 | s = [] |
| 59 | s.append(Indent('%s %s(%s) {' % (self.type, self.name, |
| 60 | self.typed_arg_string))) |
| 61 | s.append(' GlContext* gl = GL_CONTEXT();') |
| 62 | s.append(' GLsizei i;') |
| 63 | s.append(' for (i = 0; i < %s; ++i)' % self.args[0][1]) |
| 64 | s.append(' %s[i] = gl->next_name_++;' % self.args[1][1]) |
| 65 | s.append('}') |
| 66 | return '\n'.join(s) |
| 67 | |
| 68 | def ManualStub(self): |
| 69 | s = [] |
| 70 | s.append(Indent('%s %s(%s) {' % (self.type, self.name, |
| 71 | self.typed_arg_string))) |
| 72 | |
| 73 | s.append(' GlContext* gl = GL_CONTEXT();') |
| 74 | s.append('}') |
| 75 | return '\n'.join(s) |
| 76 | |
| 77 | def Stub(self): |
| 78 | functions = { |
| 79 | 'STUB': self.GenericStub, |
| 80 | 'GEN': self.GenStub, |
| 81 | 'MANUAL': self.ManualStub, |
| 82 | } |
| 83 | return functions[self.filter]() |
| 84 | |
| 85 | |
| 86 | def Parse(header): |
| 87 | """Ad-hoc parser for the entry_points definition""" |
| 88 | function_re = re.compile( |
| 89 | r'^\s*([A-Z]+)\(([A-Za-z_0-9* ]+?)\s*([A-Za-z_0-9]+)\s*\(([^)]*)\)\s*\)\s*$') |
| 90 | arg_re = re.compile(r'^\s*([A-Za-z_0-9* ]+?)\s*([A-Za-z_0-9]+)\s*$') |
| 91 | |
| 92 | entries = [] |
| 93 | for line in header: |
| 94 | match = function_re.match(line) |
| 95 | if match: |
| 96 | filter, type, name, arg_string = match.groups() |
| 97 | args = [] |
| 98 | if arg_string != 'void': |
| 99 | for arg in arg_string.split(','): |
| 100 | match = arg_re.match(arg) |
| 101 | assert match |
| 102 | arg_type, arg_name = match.groups() |
| 103 | args.append((arg_type, arg_name)) |
| 104 | entries.append(EntryPoint(type, name, args, filter)) |
| 105 | return entries |
| 106 | |
| 107 | |
| 108 | def MakeFiles(in_files, subs): |
| 109 | """Make template substitutions for every file name in in_files""" |
| 110 | for in_file in in_files: |
| 111 | out_file = re.sub(r'\.in$', '', in_file) |
| 112 | assert in_file != out_file |
| 113 | template = string.Template(file(in_file).read()) |
| 114 | out = file(out_file, 'w') |
| 115 | out.write('/* Auto-Generated from "%s". DO NOT EDIT! */\n' % in_file) |
| 116 | out.write(template.substitute(subs)) |
| 117 | out.close() |
| 118 | |
| 119 | |
| 120 | def CreateSubs(entries): |
| 121 | """Create a dictionary containing the template substitutions for the |
| 122 | OpenGL-ES stub. |
| 123 | |
| 124 | """ |
| 125 | subs = {} |
| 126 | subs['ManualStubs'] = '\n\n'.join(entry.Stub() for entry in entries |
| 127 | if entry.filter == 'MANUAL') |
| 128 | subs['AutoStubs'] = '\n\n'.join(entry.Stub() for entry in entries |
| 129 | if entry.filter != 'MANUAL') |
| 130 | return subs |
| 131 | |
| 132 | |
| 133 | def main(args): |
| 134 | templates = args[1:] |
| 135 | |
| 136 | entries = Parse(file('entry_points', 'r')) |
| 137 | subs = CreateSubs(entries) |
| 138 | MakeFiles(templates, subs) |
| 139 | |
| 140 | |
| 141 | if __name__ == '__main__': |
| 142 | main(sys.argv) |
| 143 | |