OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 import sys | 7 import sys |
8 | 8 |
9 from idl_log import ErrOut, InfoOut, WarnOut | 9 from idl_log import ErrOut, InfoOut, WarnOut |
10 from idl_option import GetOption, Option, ParseOptions | 10 from idl_option import GetOption, Option, ParseOptions |
11 | 11 |
12 GeneratorList = [] | 12 GeneratorList = [] |
13 | 13 |
14 Option('release', 'Which release to generate.', default='M14') | 14 Option('release', 'Which release to generate.', default='M14') |
15 Option('range', 'Which release ranges in the form of MIN,MAX.', default='') | 15 Option('range', 'Which release ranges in the form of MIN,MAX.', default='') |
16 | 16 |
17 | 17 |
18 # | 18 # |
19 # Generator | 19 # Generator |
20 # | 20 # |
21 # Base class for generators. This class provides a mechanism for | 21 # Base class for generators. This class provides a mechanism for |
22 # adding new generator objects to the IDL driver. To use this class | 22 # adding new generator objects to the IDL driver. To use this class |
23 # override the GenerateVersion and GenerateRange members, and | 23 # override the GenerateRelease and GenerateRange members, and |
24 # instantiate one copy of the class in the same module which defines it to | 24 # instantiate one copy of the class in the same module which defines it to |
25 # register the generator. After the AST is generated, call the static Run | 25 # register the generator. After the AST is generated, call the static Run |
26 # member which will check every registered generator to see which ones have | 26 # member which will check every registered generator to see which ones have |
27 # been enabled through command-line options. To enable a generator use the | 27 # been enabled through command-line options. To enable a generator use the |
28 # switches: | 28 # switches: |
29 # --<sname> : To enable with defaults | 29 # --<sname> : To enable with defaults |
30 # --<sname>_opt=<XXX,YYY=y> : To enable with generator specific options. | 30 # --<sname>_opt=<XXX,YYY=y> : To enable with generator specific options. |
31 # | 31 # |
32 # NOTE: Generators still have access to global options | 32 # NOTE: Generators still have access to global options |
33 | 33 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 68 |
69 # Check for a range option which over-rides a release option | 69 # Check for a range option which over-rides a release option |
70 if rangestr: | 70 if rangestr: |
71 range_list = rangestr.split(',') | 71 range_list = rangestr.split(',') |
72 if len(range_list) != 2: | 72 if len(range_list) != 2: |
73 self.Error('Failed to generate for %s, incorrect range: "%s"' % | 73 self.Error('Failed to generate for %s, incorrect range: "%s"' % |
74 (self.name, rangestr)) | 74 (self.name, rangestr)) |
75 else: | 75 else: |
76 vmin = range_list[0] | 76 vmin = range_list[0] |
77 vmax = range_list[1] | 77 vmax = range_list[1] |
78 ret = self.GenerateRange(ast, vmin, vmax, options) | 78 vmin = ast.releases.index(vmin) |
79 if not ret: | 79 vmax = ast.releases.index(vmax) + 1 |
| 80 ret = self.GenerateRange(ast, ast.releases[vmin:vmax], options) |
| 81 if ret < 0: |
80 self.Error('Failed to generate range %s : %s.' %(vmin, vmax)) | 82 self.Error('Failed to generate range %s : %s.' %(vmin, vmax)) |
81 # Otherwise this should be a single release generation | 83 # Otherwise this should be a single release generation |
82 else: | 84 else: |
83 if releasestr: | 85 if releasestr: |
84 ret = self.GenerateVersion(ast, releasestr, options) | 86 ret = self.GenerateRelease(ast, releasestr, options) |
85 if ret < 0: | 87 if ret < 0: |
86 self.Error('Failed to generate release %s.' % releasestr) | 88 self.Error('Failed to generate release %s.' % releasestr) |
87 else: | 89 else: |
88 InfoOut.Log('%s wrote %d files.' % (self.name, ret)) | 90 InfoOut.Log('%s wrote %d files.' % (self.name, ret)) |
89 | 91 |
90 else: | 92 else: |
91 self.Error('No range or release specified for %s.' % releasestr) | 93 self.Error('No range or release specified for %s.' % releasestr) |
92 return self.errors | 94 return self.errors |
93 | 95 |
94 def GenerateVersion(self, ast, release, options): | 96 def GenerateRelease(self, ast, release, options): |
95 __pychecker__ = 'unusednames=ast,release,options' | 97 __pychecker__ = 'unusednames=ast,release,options' |
96 self.Error("Undefined release generator.") | 98 self.Error("Undefined release generator.") |
97 return 0 | 99 return 0 |
98 | 100 |
99 def GenerateRange(self, ast, vmin, vmax, options): | 101 def GenerateRange(self, ast, vmin, vmax, options): |
100 __pychecker__ = 'unusednames=ast,vmin,vmax,options' | 102 __pychecker__ = 'unusednames=ast,vmin,vmax,options' |
101 self.Error("Undefined range generator.") | 103 self.Error("Undefined range generator.") |
102 return 0 | 104 return 0 |
103 | 105 |
104 @staticmethod | 106 @staticmethod |
105 def Run(ast): | 107 def Run(ast): |
106 fail_count = 0 | 108 fail_count = 0 |
107 | 109 |
108 # Check all registered generators if they should run. | 110 # Check all registered generators if they should run. |
109 for gen in GeneratorList: | 111 for gen in GeneratorList: |
110 options = gen.GetRunOptions() | 112 options = gen.GetRunOptions() |
111 if options is not None: | 113 if options is not None: |
112 if gen.Generate(ast, options): | 114 if gen.Generate(ast, options): |
113 fail_count += 1 | 115 fail_count += 1 |
114 return fail_count | 116 return fail_count |
115 | 117 |
116 | 118 |
| 119 # |
| 120 # GeneratorByFile |
| 121 # |
| 122 # A subclass of Generator for use of generators which have a one to one |
| 123 # mapping between IDL sources and output files. To use, derive a new class |
| 124 # which defines: |
| 125 # |
| 126 # GetOutFile - Returns an IDLOutFile based on filenode (name) and options |
| 127 # GenerateHead - Writes the first part of the file (includes, etc...) |
| 128 # GenerateBody - Writes the body of the file (definitions) |
| 129 # GenerateTail - Writes the end of the file (closing include guard, etc...) |
| 130 # |
| 131 class GeneratorByFile(Generator): |
| 132 def GenerateRelease(self, ast, release, options): |
| 133 return self.GenerateRange(ast, [release], options) |
| 134 |
| 135 def GenerateRange(self, ast, releases, options): |
| 136 # Get list of out files |
| 137 outlist = GetOption('out') |
| 138 if outlist: outlist = outlist.split(',') |
| 139 |
| 140 skipList = [] |
| 141 cnt = 0 |
| 142 for filenode in ast.GetListOf('File'): |
| 143 # Skip this file if not required |
| 144 if outlist and name not in outlist: |
| 145 continue |
| 146 |
| 147 # If this file has errors, skip it |
| 148 if filenode.GetProperty('ERRORS') > 0: |
| 149 skipList.append(filenode) |
| 150 continue |
| 151 |
| 152 # Create output file |
| 153 out = self.GetOutFile(filenode, options) |
| 154 self.GenerateHead(out, filenode, releases, options) |
| 155 self.GenerateBody(out, filenode, releases, options) |
| 156 self.GenerateTail(out, filenode, releases, options) |
| 157 |
| 158 if out.Close(): cnt = cnt + 1 |
| 159 |
| 160 for filenode in skipList: |
| 161 errcnt = filenode.GetProperty('ERRORS') |
| 162 ErrOut.Log('%s : Skipped because of %d errors.' % ( |
| 163 filenode.GetName(), errcnt)) |
| 164 |
| 165 if skipList: |
| 166 return -len(skipList) |
| 167 |
| 168 if GetOption('diff'): |
| 169 return -cnt |
| 170 return cnt |
| 171 |
| 172 |
117 check_release = 0 | 173 check_release = 0 |
118 check_range = 0 | 174 check_range = 0 |
119 | 175 |
120 class GeneratorVersionTest(Generator): | 176 class GeneratorReleaseTest(Generator): |
121 def GenerateVersion(self, ast, release, options = {}): | 177 def GenerateRelease(self, ast, release, options = {}): |
122 __pychecker__ = 'unusednames=ast,release,options' | 178 __pychecker__ = 'unusednames=ast,release,options' |
123 global check_release | 179 global check_release |
124 check_map = { | 180 check_map = { |
125 'so_long': True, | 181 'so_long': True, |
126 'MyOpt': 'XYZ', | 182 'MyOpt': 'XYZ', |
127 'goodbye': True | 183 'goodbye': True |
128 } | 184 } |
129 check_release = 1 | 185 check_release = 1 |
130 for item in check_map: | 186 for item in check_map: |
131 check_item = check_map[item] | 187 check_item = check_map[item] |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 def Main(args): | 231 def Main(args): |
176 if not args: return Test() | 232 if not args: return Test() |
177 | 233 |
178 filenames = ParseOptions(args) | 234 filenames = ParseOptions(args) |
179 ast = ParseFiles(filenames) | 235 ast = ParseFiles(filenames) |
180 | 236 |
181 return Generator.Run(ast) | 237 return Generator.Run(ast) |
182 | 238 |
183 | 239 |
184 if __name__ == '__main__': | 240 if __name__ == '__main__': |
185 GeneratorVersionTest('Test Gen', 'testgen', 'Generator Class Test.') | 241 GeneratorReleaseTest('Test Gen', 'testgen', 'Generator Class Test.') |
186 sys.exit(Main(sys.argv[1:])) | 242 sys.exit(Main(sys.argv[1:])) |
187 | 243 |
OLD | NEW |