OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 os.path | 5 import os.path |
6 import sys | 6 import sys |
7 import string | 7 import string |
8 import optparse | 8 import optparse |
9 import re | 9 import re |
10 try: | 10 try: |
(...skipping 15 matching lines...) Expand all Loading... |
26 module_path, os.pardir, os.pardir, os.pardir, os.pardir)) | 26 module_path, os.pardir, os.pardir, os.pardir, os.pardir)) |
27 | 27 |
28 # jinja2 is in chromium's third_party directory. | 28 # jinja2 is in chromium's third_party directory. |
29 # Insert at 1 so at front to override system libraries, and | 29 # Insert at 1 so at front to override system libraries, and |
30 # after path[0] == invoking script dir | 30 # after path[0] == invoking script dir |
31 | 31 |
32 sys.path.insert(1, third_party_dir) | 32 sys.path.insert(1, third_party_dir) |
33 import jinja2 | 33 import jinja2 |
34 | 34 |
35 cmdline_parser = optparse.OptionParser() | 35 cmdline_parser = optparse.OptionParser() |
36 cmdline_parser.add_option("--domains") | 36 cmdline_parser.add_option("--protocol") |
| 37 cmdline_parser.add_option("--include") |
| 38 cmdline_parser.add_option("--string_type") |
| 39 cmdline_parser.add_option("--export_macro") |
37 cmdline_parser.add_option("--output_dir") | 40 cmdline_parser.add_option("--output_dir") |
38 cmdline_parser.add_option("--output_package") | 41 cmdline_parser.add_option("--output_package") |
39 cmdline_parser.add_option("--string_type") | |
40 cmdline_parser.add_option("--export_macro") | |
41 | |
42 generate_domains = set() | |
43 | 42 |
44 try: | 43 try: |
45 arg_options, arg_values = cmdline_parser.parse_args() | 44 arg_options, arg_values = cmdline_parser.parse_args() |
46 if (len(arg_values) == 0): | 45 protocol_file = arg_options.protocol |
47 raise Exception("At least one plain argument expected (found %s)" % len(
arg_values)) | 46 if not protocol_file: |
| 47 raise Exception("Protocol directory must be specified") |
| 48 include_file = arg_options.include |
48 output_dirname = arg_options.output_dir | 49 output_dirname = arg_options.output_dir |
49 if not output_dirname: | 50 if not output_dirname: |
50 raise Exception("Output directory must be specified") | 51 raise Exception("Output directory must be specified") |
51 output_package = arg_options.output_package | 52 output_package = arg_options.output_package |
52 if not output_package: | 53 if not output_package: |
53 raise Exception("Output package must be specified") | 54 raise Exception("Output package must be specified") |
54 string_type = arg_options.string_type | 55 string_type = arg_options.string_type |
55 if not string_type: | 56 if not string_type: |
56 raise Exception("String type must be specified") | 57 raise Exception("String type must be specified") |
57 export_macro = arg_options.export_macro | 58 export_macro = arg_options.export_macro |
58 if not export_macro: | 59 if not export_macro: |
59 raise Exception("Export macro must be specified") | 60 raise Exception("Export macro must be specified") |
60 output_domains = arg_options.domains | |
61 if output_domains and len(output_domains): | |
62 for domain in output_domains.split(","): | |
63 generate_domains.add(domain) | |
64 except Exception: | 61 except Exception: |
65 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html | 62 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html |
66 exc = sys.exc_info()[1] | 63 exc = sys.exc_info()[1] |
67 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) | 64 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) |
68 sys.stderr.write("Usage: <script> --output_dir <output_dir> protocol.json ..
.\n") | |
69 exit(1) | 65 exit(1) |
70 | 66 |
71 json_api = {"domains": []} | |
72 | 67 |
73 json_timestamp = 0 | 68 input_file = open(protocol_file, "r") |
| 69 json_string = input_file.read() |
| 70 parsed_json = json.loads(json_string) |
74 | 71 |
75 for filename in arg_values: | 72 |
76 json_timestamp = max(os.path.getmtime(filename), json_timestamp) | 73 # Make gyp / make generatos happy, otherwise make rebuilds world. |
77 input_file = open(filename, "r") | 74 def up_to_date(): |
78 json_string = input_file.read() | 75 template_ts = max( |
79 parsed_json = json.loads(json_string) | 76 os.path.getmtime(__file__), |
80 json_api["domains"] += parsed_json["domains"] | 77 os.path.getmtime(os.path.join(templates_dir, "TypeBuilder_h.template")), |
| 78 os.path.getmtime(os.path.join(templates_dir, "TypeBuilder_cpp.template")
), |
| 79 os.path.getmtime(protocol_file)) |
| 80 |
| 81 for domain in parsed_json["domains"]: |
| 82 name = domain["domain"] |
| 83 h_path = os.path.join(output_dirname, name) |
| 84 cpp_path = os.path.join(output_dirname, name) |
| 85 if not os.path.exists(h_path) or not os.path.exists(cpp_path): |
| 86 return False |
| 87 generated_ts = max(os.path.getmtime(h_path), os.path.getmtime(cpp_path)) |
| 88 if generated_ts < template_ts: |
| 89 return False |
| 90 return True |
| 91 |
| 92 |
| 93 if up_to_date(): |
| 94 sys.exit() |
| 95 |
81 | 96 |
82 def to_title_case(name): | 97 def to_title_case(name): |
83 return name[:1].upper() + name[1:] | 98 return name[:1].upper() + name[1:] |
84 | 99 |
85 | 100 |
86 def dash_to_camelcase(word): | 101 def dash_to_camelcase(word): |
87 return ''.join(to_title_case(x) or '-' for x in word.split('-')) | 102 return ''.join(to_title_case(x) or '-' for x in word.split('-')) |
88 | 103 |
89 | 104 |
90 def initialize_jinja_env(cache_dir): | 105 def initialize_jinja_env(cache_dir): |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 "raw_return_type": typedefs[type], | 220 "raw_return_type": typedefs[type], |
206 } | 221 } |
207 | 222 |
208 type_definitions = {} | 223 type_definitions = {} |
209 type_definitions["number"] = create_primitive_type_definition("number") | 224 type_definitions["number"] = create_primitive_type_definition("number") |
210 type_definitions["integer"] = create_primitive_type_definition("integer") | 225 type_definitions["integer"] = create_primitive_type_definition("integer") |
211 type_definitions["boolean"] = create_primitive_type_definition("boolean") | 226 type_definitions["boolean"] = create_primitive_type_definition("boolean") |
212 type_definitions["object"] = create_object_type_definition() | 227 type_definitions["object"] = create_object_type_definition() |
213 type_definitions["any"] = create_any_type_definition() | 228 type_definitions["any"] = create_any_type_definition() |
214 | 229 |
| 230 |
215 def wrap_array_definition(type): | 231 def wrap_array_definition(type): |
216 return { | 232 return { |
217 "return_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"]
, | 233 "return_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"]
, |
218 "pass_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], | 234 "pass_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], |
219 "to_raw_type": "%s.get()", | 235 "to_raw_type": "%s.get()", |
220 "to_pass_type": "std::move(%s)", | 236 "to_pass_type": "std::move(%s)", |
221 "to_rvalue": "std::move(%s)", | 237 "to_rvalue": "std::move(%s)", |
222 "type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], | 238 "type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], |
223 "raw_type": "protocol::Array<%s>" % type["raw_type"], | 239 "raw_type": "protocol::Array<%s>" % type["raw_type"], |
224 "raw_pass_type": "protocol::Array<%s>*" % type["raw_type"], | 240 "raw_pass_type": "protocol::Array<%s>*" % type["raw_type"], |
(...skipping 12 matching lines...) Expand all Loading... |
237 if type["type"] == "object": | 253 if type["type"] == "object": |
238 type_definitions[domain["domain"] + "." + type["id"]] = create_u
ser_type_definition(domain["domain"], type) | 254 type_definitions[domain["domain"] + "." + type["id"]] = create_u
ser_type_definition(domain["domain"], type) |
239 elif type["type"] == "array": | 255 elif type["type"] == "array": |
240 items_type = type["items"]["type"] | 256 items_type = type["items"]["type"] |
241 type_definitions[domain["domain"] + "." + type["id"]] = wrap_arr
ay_definition(type_definitions[items_type]) | 257 type_definitions[domain["domain"] + "." + type["id"]] = wrap_arr
ay_definition(type_definitions[items_type]) |
242 elif type["type"] == domain["domain"] + ".string": | 258 elif type["type"] == domain["domain"] + ".string": |
243 type_definitions[domain["domain"] + "." + type["id"]] = create_s
tring_type_definition(domain["domain"]) | 259 type_definitions[domain["domain"] + "." + type["id"]] = create_s
tring_type_definition(domain["domain"]) |
244 else: | 260 else: |
245 type_definitions[domain["domain"] + "." + type["id"]] = create_p
rimitive_type_definition(type["type"]) | 261 type_definitions[domain["domain"] + "." + type["id"]] = create_p
rimitive_type_definition(type["type"]) |
246 | 262 |
247 patch_full_qualified_refs() | |
248 create_type_definitions() | |
249 | |
250 | 263 |
251 def type_definition(name): | 264 def type_definition(name): |
252 return type_definitions[name] | 265 return type_definitions[name] |
253 | 266 |
254 | 267 |
255 def resolve_type(property): | 268 def resolve_type(property): |
256 if "$ref" in property: | 269 if "$ref" in property: |
257 return type_definitions[property["$ref"]] | 270 return type_definitions[property["$ref"]] |
258 if property["type"] == "array": | 271 if property["type"] == "array": |
259 return wrap_array_definition(resolve_type(property["items"])) | 272 return wrap_array_definition(resolve_type(property["items"])) |
260 return type_definitions[property["type"]] | 273 return type_definitions[property["type"]] |
261 | 274 |
262 | 275 |
263 def join_arrays(dict, keys): | 276 def join_arrays(dict, keys): |
264 result = [] | 277 result = [] |
265 for key in keys: | 278 for key in keys: |
266 if key in dict: | 279 if key in dict: |
267 result += dict[key] | 280 result += dict[key] |
268 return result | 281 return result |
269 | 282 |
270 | 283 |
271 def has_disable(commands): | 284 def has_disable(commands): |
272 for command in commands: | 285 for command in commands: |
273 if command["name"] == "disable": | 286 if command["name"] == "disable": |
274 return True | 287 return True |
275 return False | 288 return False |
276 | 289 |
277 | 290 |
278 if os.path.exists(__file__): | 291 generate_domains = [] |
279 current_script_timestamp = os.path.getmtime(__file__) | 292 json_api = {} |
280 else: | 293 json_api["domains"] = parsed_json["domains"] |
281 current_script_timestamp = 0 | 294 |
| 295 for domain in parsed_json["domains"]: |
| 296 generate_domains.append(domain["domain"]) |
| 297 |
| 298 if include_file: |
| 299 input_file = open(include_file, "r") |
| 300 json_string = input_file.read() |
| 301 parsed_json = json.loads(json_string) |
| 302 json_api["domains"] += parsed_json["domains"] |
282 | 303 |
283 | 304 |
284 def is_up_to_date(file, template): | 305 patch_full_qualified_refs() |
285 if not os.path.exists(file): | 306 create_type_definitions() |
286 return False | |
287 timestamp = os.path.getmtime(file) | |
288 return timestamp > max(os.path.getmtime(module_path + template), | |
289 current_script_timestamp, json_timestamp) | |
290 | 307 |
291 if not os.path.exists(output_dirname): | 308 if not os.path.exists(output_dirname): |
292 os.mkdir(output_dirname) | 309 os.mkdir(output_dirname) |
293 jinja_env = initialize_jinja_env(output_dirname) | 310 jinja_env = initialize_jinja_env(output_dirname) |
294 | 311 |
295 h_template_name = "/TypeBuilder_h.template" | 312 h_template_name = "/TypeBuilder_h.template" |
296 cpp_template_name = "/TypeBuilder_cpp.template" | 313 cpp_template_name = "/TypeBuilder_cpp.template" |
297 h_template = jinja_env.get_template(h_template_name) | 314 h_template = jinja_env.get_template(h_template_name) |
298 cpp_template = jinja_env.get_template(cpp_template_name) | 315 cpp_template = jinja_env.get_template(cpp_template_name) |
299 | 316 |
300 | 317 |
301 def generate(domain): | 318 def generate(domain): |
302 class_name = domain["domain"] | 319 class_name = domain["domain"] |
303 h_file_name = output_dirname + "/" + class_name + ".h" | 320 h_file_name = output_dirname + "/" + class_name + ".h" |
304 cpp_file_name = output_dirname + "/" + class_name + ".cpp" | 321 cpp_file_name = output_dirname + "/" + class_name + ".cpp" |
305 | 322 |
306 if (is_up_to_date(cpp_file_name, cpp_template_name) and | |
307 is_up_to_date(h_file_name, h_template_name)): | |
308 return | |
309 | |
310 template_context = { | 323 template_context = { |
311 "domain": domain, | 324 "domain": domain, |
312 "join_arrays": join_arrays, | 325 "join_arrays": join_arrays, |
313 "resolve_type": resolve_type, | 326 "resolve_type": resolve_type, |
314 "type_definition": type_definition, | 327 "type_definition": type_definition, |
315 "has_disable": has_disable, | 328 "has_disable": has_disable, |
316 "export_macro": export_macro, | 329 "export_macro": export_macro, |
317 "output_package": output_package, | 330 "output_package": output_package, |
318 } | 331 } |
319 h_file = output_file(h_file_name) | 332 h_file = output_file(h_file_name) |
320 cpp_file = output_file(cpp_file_name) | 333 cpp_file = output_file(cpp_file_name) |
321 h_file.write(h_template.render(template_context)) | 334 h_file.write(h_template.render(template_context)) |
322 cpp_file.write(cpp_template.render(template_context)) | 335 cpp_file.write(cpp_template.render(template_context)) |
323 h_file.close() | 336 h_file.close() |
324 cpp_file.close() | 337 cpp_file.close() |
325 | 338 |
326 | 339 |
327 for domain in json_api["domains"]: | 340 for domain in json_api["domains"]: |
328 if domain["domain"] in generate_domains: | 341 if domain["domain"] in generate_domains: |
329 generate(domain) | 342 generate(domain) |
OLD | NEW |