| OLD | NEW |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 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 optparse | 5 import optparse |
| 6 import os.path | 6 import os.path |
| 7 import re | 7 import re |
| 8 import sys | 8 import sys |
| 9 | 9 |
| 10 # Path handling for libraries and templates | 10 # Path handling for libraries and templates |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 def to_lower_case(name): | 29 def to_lower_case(name): |
| 30 return name[:1].lower() + name[1:] | 30 return name[:1].lower() + name[1:] |
| 31 | 31 |
| 32 | 32 |
| 33 def agent_name_to_class(agent_name): | 33 def agent_name_to_class(agent_name): |
| 34 if agent_name == "Performance": | 34 if agent_name == "Performance": |
| 35 return "PerformanceMonitor" | 35 return "PerformanceMonitor" |
| 36 elif agent_name == "TraceEvents": | 36 elif agent_name == "TraceEvents": |
| 37 return "InspectorTraceEvents" | 37 return "InspectorTraceEvents" |
| 38 elif agent_name == "PlatformTraceEvents": |
| 39 return "PlatformTraceEventsAgent" |
| 38 else: | 40 else: |
| 39 return "Inspector%sAgent" % agent_name | 41 return "Inspector%sAgent" % agent_name |
| 40 | 42 |
| 41 | 43 |
| 42 def initialize_jinja_env(cache_dir): | 44 def initialize_jinja_env(cache_dir): |
| 43 jinja_env = jinja2.Environment( | 45 jinja_env = jinja2.Environment( |
| 44 loader=jinja2.FileSystemLoader(templates_dir), | 46 loader=jinja2.FileSystemLoader(templates_dir), |
| 45 # Bytecode cache is not concurrency-safe unless pre-cached: | 47 # Bytecode cache is not concurrency-safe unless pre-cached: |
| 46 # if pre-cached this is read-only, but writing creates a race condition. | 48 # if pre-cached this is read-only, but writing creates a race condition. |
| 47 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 49 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
| (...skipping 26 matching lines...) Expand all Loading... |
| 74 sys.stderr.write("Cannot parse %s\n" % source[:100]) | 76 sys.stderr.write("Cannot parse %s\n" % source[:100]) |
| 75 sys.exit(1) | 77 sys.exit(1) |
| 76 model.append(File(match.group(1), match.group(2))) | 78 model.append(File(match.group(1), match.group(2))) |
| 77 return model | 79 return model |
| 78 | 80 |
| 79 | 81 |
| 80 class File(object): | 82 class File(object): |
| 81 def __init__(self, name, source): | 83 def __init__(self, name, source): |
| 82 self.name = name | 84 self.name = name |
| 83 self.header_name = self.name + "Inl" | 85 self.header_name = self.name + "Inl" |
| 84 self.includes = [include_inspector_header("InspectorInstrumentation")] | 86 self.includes = [include_inspector_header(base_name)] |
| 85 self.forward_declarations = [] | 87 self.forward_declarations = [] |
| 86 self.declarations = [] | 88 self.declarations = [] |
| 89 self.defines = [] |
| 87 for line in map(str.strip, source.split("\n")): | 90 for line in map(str.strip, source.split("\n")): |
| 88 line = re.sub(r"\s{2,}", " ", line).strip() # Collapse whitespace | 91 line = re.sub(r"\s{2,}", " ", line).strip() # Collapse whitespace |
| 89 if len(line) == 0: | 92 if len(line) == 0: |
| 90 continue | 93 continue |
| 91 if line[0] == "#": | 94 if line.startswith("#define"): |
| 95 self.defines.append(line) |
| 96 elif line.startswith("#include"): |
| 92 self.includes.append(line) | 97 self.includes.append(line) |
| 93 elif line.startswith("class "): | 98 elif line.startswith("class ") or line.startswith("struct "): |
| 94 self.forward_declarations.append(line) | 99 self.forward_declarations.append(line) |
| 95 else: | 100 else: |
| 96 self.declarations.append(Method(line)) | 101 self.declarations.append(Method(line)) |
| 97 self.includes.sort() | 102 self.includes.sort() |
| 98 self.forward_declarations.sort() | 103 self.forward_declarations.sort() |
| 99 | 104 |
| 100 | 105 |
| 101 def include_header(name): | 106 def include_header(name): |
| 102 return "#include \"%s.h\"" % name | 107 return "#include \"%s.h\"" % name |
| 103 | 108 |
| 104 | 109 |
| 105 def include_inspector_header(name): | 110 def include_inspector_header(name): |
| 106 if name == "PerformanceMonitor": | 111 if name == "PerformanceMonitor": |
| 107 return include_header("core/frame/" + name) | 112 return include_header("core/frame/" + name) |
| 113 if name == "PlatformInstrumentation": |
| 114 return include_header("platform/instrumentation/" + name) |
| 108 return include_header("core/inspector/" + name) | 115 return include_header("core/inspector/" + name) |
| 109 | 116 |
| 110 | 117 |
| 111 class Method(object): | 118 class Method(object): |
| 112 def __init__(self, source): | 119 def __init__(self, source): |
| 113 match = re.match(r"(\[[\w|,|=|\s]*\])?\s?(\w*\*?) (\w*)\((.*)\)\s?;", so
urce) | 120 match = re.match(r"(\[[\w|,|=|\s]*\])?\s?(\w*\*?) (\w*)\((.*)\)\s?;", so
urce) |
| 114 if not match: | 121 if not match: |
| 115 sys.stderr.write("Cannot parse %s\n" % source) | 122 sys.stderr.write("Cannot parse %s\n" % source) |
| 116 sys.exit(1) | 123 sys.exit(1) |
| 117 | 124 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 if not output_dirpath: | 200 if not output_dirpath: |
| 194 raise Exception("Output directory must be specified") | 201 raise Exception("Output directory must be specified") |
| 195 except Exception: | 202 except Exception: |
| 196 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html | 203 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html |
| 197 exc = sys.exc_info()[1] | 204 exc = sys.exc_info()[1] |
| 198 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) | 205 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) |
| 199 sys.stderr.write("Usage: <script> --output_dir <output_dir> InstrumentingPro
bes.idl\n") | 206 sys.stderr.write("Usage: <script> --output_dir <output_dir> InstrumentingPro
bes.idl\n") |
| 200 exit(1) | 207 exit(1) |
| 201 | 208 |
| 202 jinja_env = initialize_jinja_env(output_dirpath) | 209 jinja_env = initialize_jinja_env(output_dirpath) |
| 210 all_agents = set() |
| 211 all_defines = [] |
| 212 base_name = os.path.splitext(os.path.basename(input_path))[0] |
| 213 |
| 203 fin = open(input_path, "r") | 214 fin = open(input_path, "r") |
| 204 files = load_model_from_idl(fin.read()) | 215 files = load_model_from_idl(fin.read()) |
| 205 fin.close() | 216 fin.close() |
| 206 all_agents = set() | |
| 207 | 217 |
| 208 for f in files: | 218 for f in files: |
| 209 for declaration in f.declarations: | 219 for declaration in f.declarations: |
| 210 for agent in declaration.agents: | 220 for agent in declaration.agents: |
| 211 all_agents.add(agent) | 221 all_agents.add(agent) |
| 222 all_defines += f.defines |
| 212 | 223 |
| 213 template_context = { | 224 template_context = { |
| 214 "files": files, | 225 "files": files, |
| 215 "agents": all_agents, | 226 "agents": all_agents, |
| 227 "defines": all_defines, |
| 228 "name": base_name, |
| 216 "input_file": os.path.basename(input_path) | 229 "input_file": os.path.basename(input_path) |
| 217 } | 230 } |
| 218 cpp_template = jinja_env.get_template("/InstrumentingProbesImpl_cpp.template") | 231 cpp_template = jinja_env.get_template("/InstrumentingProbesImpl_cpp.template") |
| 219 cpp_file = open(output_dirpath + "/InstrumentingProbesImpl.cpp", "w") | 232 cpp_file = open(output_dirpath + "/" + base_name + "Impl.cpp", "w") |
| 220 cpp_file.write(cpp_template.render(template_context)) | 233 cpp_file.write(cpp_template.render(template_context)) |
| 221 cpp_file.close() | 234 cpp_file.close() |
| 222 | 235 |
| 223 agents_h_template = jinja_env.get_template("/InstrumentingAgents_h.template") | 236 agents_h_template = jinja_env.get_template("/InstrumentingAgents_h.template") |
| 224 agents_h_file = open(output_dirpath + "/InstrumentingAgents.h", "w") | 237 agents_h_file = open(output_dirpath + "/" + base_name + "Agents.h", "w") |
| 225 agents_h_file.write(agents_h_template.render(template_context)) | 238 agents_h_file.write(agents_h_template.render(template_context)) |
| 226 agents_h_file.close() | 239 agents_h_file.close() |
| 227 | 240 |
| 228 for f in files: | 241 for f in files: |
| 229 template_context["file"] = f | 242 template_context["file"] = f |
| 230 h_template = jinja_env.get_template("/InstrumentingProbesImpl_h.template") | 243 h_template = jinja_env.get_template("/InstrumentingProbesImpl_h.template") |
| 231 h_file = open(output_dirpath + "/" + f.name + "Inl.h", "w") | 244 h_file = open(output_dirpath + "/" + f.header_name + ".h", "w") |
| 232 h_file.write(h_template.render(template_context)) | 245 h_file.write(h_template.render(template_context)) |
| 233 h_file.close() | 246 h_file.close() |
| OLD | NEW |