OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """The frontend for the Mojo bindings system.""" | 6 """The frontend for the Mojo bindings system.""" |
7 | 7 |
8 | 8 |
9 import argparse | 9 import argparse |
10 import imp | 10 import imp |
11 import os | 11 import os |
12 import pprint | 12 import pprint |
13 import sys | 13 import sys |
14 | 14 |
15 # Disable lint check for finding modules: | 15 # Disable lint check for finding modules: |
16 # pylint: disable=F0401 | 16 # pylint: disable=F0401 |
17 | 17 |
18 script_dir = os.path.dirname(os.path.abspath(__file__)) | 18 def _GetDirAbove(dirname): |
19 sys.path.insert(0, os.path.join(script_dir, "pylib")) | 19 """Returns the directory "above" this file containing |dirname| (which must |
| 20 also be "above" this file).""" |
| 21 path = os.path.abspath(__file__) |
| 22 while True: |
| 23 path, tail = os.path.split(path) |
| 24 assert tail |
| 25 if tail == dirname: |
| 26 return path |
| 27 |
| 28 # Manually check for the command-line flag. (This isn't quite right, since it |
| 29 # ignores, e.g., "--", but it's close enough.) |
| 30 if "--use_chromium_bundled_pylibs" in sys.argv[1:]: |
| 31 sys.path.insert(0, os.path.join(_GetDirAbove("mojo"), "third_party")) |
| 32 |
| 33 sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 34 "pylib")) |
20 | 35 |
21 from mojom.error import Error | 36 from mojom.error import Error |
22 from mojom.generate.data import OrderedModuleFromData | 37 from mojom.generate.data import OrderedModuleFromData |
23 from mojom.parse.parser import Parse | 38 from mojom.parse.parser import Parse |
24 from mojom.parse.translate import Translate | 39 from mojom.parse.translate import Translate |
25 | 40 |
26 | 41 |
27 def LoadGenerators(generators_string): | 42 def LoadGenerators(generators_string): |
28 if not generators_string: | 43 if not generators_string: |
29 return [] # No generators. | 44 return [] # No generators. |
30 | 45 |
| 46 script_dir = os.path.dirname(os.path.abspath(__file__)) |
31 generators = [] | 47 generators = [] |
32 for generator_name in [s.strip() for s in generators_string.split(",")]: | 48 for generator_name in [s.strip() for s in generators_string.split(",")]: |
33 # "Built-in" generators: | 49 # "Built-in" generators: |
34 if generator_name.lower() == "c++": | 50 if generator_name.lower() == "c++": |
35 generator_name = os.path.join(script_dir, "generators", | 51 generator_name = os.path.join(script_dir, "generators", |
36 "mojom_cpp_generator.py") | 52 "mojom_cpp_generator.py") |
37 elif generator_name.lower() == "javascript": | 53 elif generator_name.lower() == "javascript": |
38 generator_name = os.path.join(script_dir, "generators", | 54 generator_name = os.path.join(script_dir, "generators", |
39 "mojom_js_generator.py") | 55 "mojom_js_generator.py") |
40 # Specified generator python module: | 56 # Specified generator python module: |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 for generator_module in generator_modules: | 128 for generator_module in generator_modules: |
113 generator = generator_module.Generator(module, args.output_dir) | 129 generator = generator_module.Generator(module, args.output_dir) |
114 generator.GenerateFiles() | 130 generator.GenerateFiles() |
115 | 131 |
116 # Save result. | 132 # Save result. |
117 _processed_files[filename] = module | 133 _processed_files[filename] = module |
118 return module | 134 return module |
119 # pylint: enable=W0102 | 135 # pylint: enable=W0102 |
120 | 136 |
121 | 137 |
122 def Main(): | 138 def main(): |
123 parser = argparse.ArgumentParser( | 139 parser = argparse.ArgumentParser( |
124 description="Generate bindings from mojom files.") | 140 description="Generate bindings from mojom files.") |
125 parser.add_argument("filename", nargs="+", | 141 parser.add_argument("filename", nargs="+", |
126 help="mojom input file") | 142 help="mojom input file") |
127 parser.add_argument("-d", "--depth", dest="depth", default=".", | 143 parser.add_argument("-d", "--depth", dest="depth", default=".", |
128 help="depth from source root") | 144 help="depth from source root") |
129 parser.add_argument("-o", "--output_dir", dest="output_dir", default=".", | 145 parser.add_argument("-o", "--output_dir", dest="output_dir", default=".", |
130 help="output directory for generated files") | 146 help="output directory for generated files") |
131 parser.add_argument("-g", "--generators", dest="generators_string", | 147 parser.add_argument("-g", "--generators", dest="generators_string", |
132 metavar="GENERATORS", default="c++,javascript", | 148 metavar="GENERATORS", default="c++,javascript", |
133 help="comma-separated list of generators") | 149 help="comma-separated list of generators") |
134 parser.add_argument("--debug_print_intermediate", action="store_true", | 150 parser.add_argument("--debug_print_intermediate", action="store_true", |
135 help="print the intermediate representation") | 151 help="print the intermediate representation") |
| 152 parser.add_argument("--use_chromium_bundled_pylibs", action="store_true", |
| 153 help="use Python modules bundled in the Chromium source") |
136 args = parser.parse_args() | 154 args = parser.parse_args() |
137 | 155 |
138 generator_modules = LoadGenerators(args.generators_string) | 156 generator_modules = LoadGenerators(args.generators_string) |
139 | 157 |
140 if not os.path.exists(args.output_dir): | 158 if not os.path.exists(args.output_dir): |
141 os.makedirs(args.output_dir) | 159 os.makedirs(args.output_dir) |
142 | 160 |
143 for filename in args.filename: | 161 for filename in args.filename: |
144 ProcessFile(args, generator_modules, filename) | 162 ProcessFile(args, generator_modules, filename) |
145 | 163 |
146 return 0 | 164 return 0 |
147 | 165 |
148 | 166 |
149 if __name__ == "__main__": | 167 if __name__ == "__main__": |
150 sys.exit(Main()) | 168 sys.exit(main()) |
OLD | NEW |