Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: tools/json_schema_compiler/cpp_bundle_generator.py

Issue 12181005: generated_api.h should have its body generated into generated_api.cc (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Latest master Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import code 5 import code
6 import cpp_util 6 import cpp_util
7 from model import Platforms 7 from model import Platforms
8 from schema_util import CapitalizeFirstLetter 8 from schema_util import CapitalizeFirstLetter
9 from schema_util import JsFunctionNameToClassName 9 from schema_util import JsFunctionNameToClassName
10 10
(...skipping 24 matching lines...) Expand all
35 """This class contains methods to generate code based on multiple schemas. 35 """This class contains methods to generate code based on multiple schemas.
36 """ 36 """
37 37
38 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace): 38 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace):
39 self._root = root; 39 self._root = root;
40 self._model = model 40 self._model = model
41 self._api_defs = api_defs 41 self._api_defs = api_defs
42 self._cpp_type_generator = cpp_type_generator 42 self._cpp_type_generator = cpp_type_generator
43 self._cpp_namespace = cpp_namespace 43 self._cpp_namespace = cpp_namespace
44 44
45 self.api_cc_generator = _APICCGenerator(self)
45 self.api_h_generator = _APIHGenerator(self) 46 self.api_h_generator = _APIHGenerator(self)
46 self.schemas_cc_generator = _SchemasCCGenerator(self) 47 self.schemas_cc_generator = _SchemasCCGenerator(self)
47 self.schemas_h_generator = _SchemasHGenerator(self) 48 self.schemas_h_generator = _SchemasHGenerator(self)
48 49
49 def GenerateHeader(self, file_base, body_code): 50 def _GenerateHeader(self, file_base, body_code):
50 """Generates a code.Code object for a header file 51 """Generates a code.Code object for a header file
51 52
52 Parameters: 53 Parameters:
53 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') 54 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h')
54 - |body_code| - the code to put in between the multiple inclusion guards""" 55 - |body_code| - the code to put in between the multiple inclusion guards"""
55 c = code.Code() 56 c = code.Code()
56 c.Append(cpp_util.CHROMIUM_LICENSE) 57 c.Append(cpp_util.CHROMIUM_LICENSE)
57 c.Append() 58 c.Append()
58 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) 59 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH)
59 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base) 60 ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH, file_base)
(...skipping 21 matching lines...) Expand all
81 raise ValueError("Unsupported platform ifdef: %s" % platform.name) 82 raise ValueError("Unsupported platform ifdef: %s" % platform.name)
82 return ' and '.join(ifdefs) 83 return ' and '.join(ifdefs)
83 84
84 def _GetNamespaceFunctions(self, namespace): 85 def _GetNamespaceFunctions(self, namespace):
85 functions = list(namespace.functions.values()) 86 functions = list(namespace.functions.values())
86 if namespace.compiler_options.get("generate_type_functions", False): 87 if namespace.compiler_options.get("generate_type_functions", False):
87 for type_ in namespace.types.values(): 88 for type_ in namespace.types.values():
88 functions += list(type_.functions.values()) 89 functions += list(type_.functions.values())
89 return functions 90 return functions
90 91
91 def GenerateFunctionRegistry(self): 92 def _GenerateFunctionRegistryRegisterAll(self):
92 c = code.Code() 93 c = code.Code()
93 c.Sblock("class GeneratedFunctionRegistry {") 94 c.Append('// static')
94 c.Append(" public:") 95 c.Sblock('void GeneratedFunctionRegistry::RegisterAll('
95 c.Sblock("static void RegisterAll(ExtensionFunctionRegistry* registry) {") 96 'ExtensionFunctionRegistry* registry) {')
96 for namespace in self._model.namespaces.values(): 97 for namespace in self._model.namespaces.values():
97 namespace_ifdefs = self._GetPlatformIfdefs(namespace) 98 namespace_ifdefs = self._GetPlatformIfdefs(namespace)
98 if namespace_ifdefs is not None: 99 if namespace_ifdefs is not None:
99 c.Append("#if %s" % namespace_ifdefs, indent_level=0) 100 c.Append("#if %s" % namespace_ifdefs, indent_level=0)
100 101
101 namespace_name = CapitalizeFirstLetter(namespace.name.replace( 102 namespace_name = CapitalizeFirstLetter(namespace.name.replace(
102 "experimental.", "")) 103 "experimental.", ""))
103 for function in self._GetNamespaceFunctions(namespace): 104 for function in self._GetNamespaceFunctions(namespace):
104 if function.nocompile: 105 if function.nocompile:
105 continue 106 continue
106 function_ifdefs = self._GetPlatformIfdefs(function) 107 function_ifdefs = self._GetPlatformIfdefs(function)
107 if function_ifdefs is not None: 108 if function_ifdefs is not None:
108 c.Append("#if %s" % function_ifdefs, indent_level=0) 109 c.Append("#if %s" % function_ifdefs, indent_level=0)
109 110
110 function_name = JsFunctionNameToClassName(namespace.name, function.name) 111 function_name = JsFunctionNameToClassName(namespace.name, function.name)
111 c.Append("registry->RegisterFunction<%sFunction>();" % ( 112 c.Append("registry->RegisterFunction<%sFunction>();" % (
112 function_name)) 113 function_name))
113 114
114 if function_ifdefs is not None: 115 if function_ifdefs is not None:
115 c.Append("#endif // %s" % function_ifdefs, indent_level=0) 116 c.Append("#endif // %s" % function_ifdefs, indent_level=0)
116 117
117 if namespace_ifdefs is not None: 118 if namespace_ifdefs is not None:
118 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) 119 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0)
119 c.Eblock("}") 120 c.Eblock("}")
120 c.Eblock("};")
121 c.Append()
122 return c 121 return c
123 122
124 class _APIHGenerator(object): 123 class _APIHGenerator(object):
125 """Generates the header for API registration / declaration""" 124 """Generates the header for API registration / declaration"""
126 def __init__(self, cpp_bundle): 125 def __init__(self, cpp_bundle):
127 self._bundle = cpp_bundle 126 self._bundle = cpp_bundle
128 127
129 def Generate(self, namespace): 128 def Generate(self, namespace):
130 c = code.Code() 129 c = code.Code()
131 130
132 c.Append('#include <string>') 131 c.Append('#include <string>')
133 c.Append() 132 c.Append()
134 c.Append('#include "base/basictypes.h"') 133 c.Append('#include "base/basictypes.h"')
134 c.Append()
135 c.Append("class ExtensionFunctionRegistry;")
136 c.Append()
137 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
138 c.Append()
139 c.Sblock('class GeneratedFunctionRegistry {')
140 c.AdjustIndentLevel(-1) # Decrement indent level by 1.
141 c.Append('public:')
142 c.AdjustIndentLevel(1) # Increment indent level by 1.
not at google - send to devlin 2013/02/10 16:03:44 There is no need for this, just don't use Sblock('
Joe Thomas 2013/02/11 22:58:23 Done.
143 c.Append('static void RegisterAll('
144 'ExtensionFunctionRegistry* registry);')
145 c.Eblock('};');
146 c.Append()
147 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
148 return self._bundle._GenerateHeader('generated_api', c)
135 149
150 class _APICCGenerator(object):
151 """Generates a code.Code object for the generated API .cc file"""
152
153 def __init__(self, cpp_bundle):
154 self._bundle = cpp_bundle
155
156 def Generate(self, namespace):
157 c = code.Code()
158 c.Append(cpp_util.CHROMIUM_LICENSE)
159 c.Append()
160 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH,
161 'generated_api.h')))
162 c.Append()
136 for namespace in self._bundle._model.namespaces.values(): 163 for namespace in self._bundle._model.namespaces.values():
137 namespace_name = namespace.unix_name.replace("experimental_", "") 164 namespace_name = namespace.unix_name.replace("experimental_", "")
138 implementation_header = namespace.compiler_options.get( 165 implementation_header = namespace.compiler_options.get(
139 "implemented_in", 166 "implemented_in",
140 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, 167 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name,
141 namespace_name)) 168 namespace_name))
142 if not os.path.exists( 169 if not os.path.exists(
143 os.path.join(self._bundle._root, 170 os.path.join(self._bundle._root,
144 os.path.normpath(implementation_header))): 171 os.path.normpath(implementation_header))):
145 if "implemented_in" in namespace.compiler_options: 172 if "implemented_in" in namespace.compiler_options:
146 raise ValueError('Header file for namespace "%s" specified in ' 173 raise ValueError('Header file for namespace "%s" specified in '
147 'compiler_options not found: %s' % 174 'compiler_options not found: %s' %
148 (namespace.unix_name, implementation_header)) 175 (namespace.unix_name, implementation_header))
149 continue 176 continue
150 ifdefs = self._bundle._GetPlatformIfdefs(namespace) 177 ifdefs = self._bundle._GetPlatformIfdefs(namespace)
151 if ifdefs is not None: 178 if ifdefs is not None:
152 c.Append("#if %s" % ifdefs, indent_level=0) 179 c.Append("#if %s" % ifdefs, indent_level=0)
153 180
154 c.Append('#include "%s"' % implementation_header) 181 c.Append('#include "%s"' % implementation_header)
155 182
156 if ifdefs is not None: 183 if ifdefs is not None:
157 c.Append("#endif // %s" % ifdefs, indent_level=0) 184 c.Append("#endif // %s" % ifdefs, indent_level=0)
158
159 c.Append() 185 c.Append()
160 c.Append("class ExtensionFunctionRegistry;") 186 c.Append('#include '
187 '"chrome/browser/extensions/extension_function_registry.h"')
161 c.Append() 188 c.Append()
162
163 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 189 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
164 for namespace in self._bundle._model.namespaces.values():
165 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name))
166 c.Append() 190 c.Append()
167 c.Concat(self._bundle.GenerateFunctionRegistry()) 191 c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll())
192 c.Append()
168 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 193 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
169 c.Append() 194 c.Append()
170 return self._bundle.GenerateHeader('generated_api', c) 195 return c
171 196
172 class _SchemasHGenerator(object): 197 class _SchemasHGenerator(object):
173 """Generates a code.Code object for the generated schemas .h file""" 198 """Generates a code.Code object for the generated schemas .h file"""
174 def __init__(self, cpp_bundle): 199 def __init__(self, cpp_bundle):
175 self._bundle = cpp_bundle 200 self._bundle = cpp_bundle
176 201
177 def Generate(self, namespace): 202 def Generate(self, namespace):
178 c = code.Code() 203 c = code.Code()
179 c.Append('#include <map>') 204 c.Append('#include <map>')
180 c.Append('#include <string>') 205 c.Append('#include <string>')
181 c.Append(); 206 c.Append();
182 c.Append('#include "base/string_piece.h"') 207 c.Append('#include "base/string_piece.h"')
183 c.Append() 208 c.Append()
184 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 209 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
185 c.Append() 210 c.Append()
186 c.Sblock('class GeneratedSchemas {') 211 c.Sblock('class GeneratedSchemas {')
187 c.Append(' public:') 212 c.AdjustIndentLevel(-1) # Decrement indent level by 1.
not at google - send to devlin 2013/02/10 16:03:44 ditto
Joe Thomas 2013/02/11 22:58:23 Done.
213 c.Append('public:')
214 c.AdjustIndentLevel(1) # Increment indent level by 1.
188 c.Append('// Puts all API schemas in |schemas|.') 215 c.Append('// Puts all API schemas in |schemas|.')
189 c.Append('static void Get(' 216 c.Append('static void Get('
190 'std::map<std::string, base::StringPiece>* schemas);') 217 'std::map<std::string, base::StringPiece>* schemas);')
191 c.Eblock('};'); 218 c.Eblock('};');
192 c.Append() 219 c.Append()
193 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 220 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
194 c.Append() 221 return self._bundle._GenerateHeader('generated_schemas', c)
195 return self._bundle.GenerateHeader('generated_schemas', c)
196 222
197 class _SchemasCCGenerator(object): 223 class _SchemasCCGenerator(object):
198 """Generates a code.Code object for the generated schemas .cc file""" 224 """Generates a code.Code object for the generated schemas .cc file"""
199 225
200 def __init__(self, cpp_bundle): 226 def __init__(self, cpp_bundle):
201 self._bundle = cpp_bundle 227 self._bundle = cpp_bundle
202 228
203 def Generate(self, namespace): 229 def Generate(self, namespace):
204 c = code.Code() 230 c = code.Code()
205 c.Append(cpp_util.CHROMIUM_LICENSE) 231 c.Append(cpp_util.CHROMIUM_LICENSE)
(...skipping 13 matching lines...) Expand all
219 separators=(',', ':')) 245 separators=(',', ':'))
220 # Escape all double-quotes and backslashes. For this to output a valid 246 # Escape all double-quotes and backslashes. For this to output a valid
221 # JSON C string, we need to escape \ and ". 247 # JSON C string, we need to escape \ and ".
222 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') 248 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"')
223 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) 249 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content))
224 c.Eblock('}') 250 c.Eblock('}')
225 c.Append() 251 c.Append()
226 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 252 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
227 c.Append() 253 c.Append()
228 return c 254 return c
OLDNEW
« tools/json_schema_compiler/code.py ('K') | « tools/json_schema_compiler/compiler.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698