OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """Generates java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
6 | 6 |
7 import argparse | 7 import argparse |
8 import ast | 8 import ast |
9 import contextlib | 9 import contextlib |
10 import os | 10 import os |
11 import re | 11 import re |
12 import shutil | 12 import shutil |
| 13 import sys |
13 import tempfile | 14 import tempfile |
14 import zipfile | |
15 | 15 |
16 from jinja2 import contextfilter | 16 from jinja2 import contextfilter |
17 | 17 |
18 import mojom.fileutil as fileutil | 18 import mojom.fileutil as fileutil |
19 import mojom.generate.generator as generator | 19 import mojom.generate.generator as generator |
20 import mojom.generate.module as mojom | 20 import mojom.generate.module as mojom |
21 from mojom.generate.template_expander import UseJinja | 21 from mojom.generate.template_expander import UseJinja |
22 | 22 |
| 23 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, |
| 24 os.pardir, os.pardir, os.pardir, os.pardir, |
| 25 'build', 'android', 'gyp')) |
| 26 from util import build_utils |
| 27 |
23 | 28 |
24 GENERATOR_PREFIX = 'java' | 29 GENERATOR_PREFIX = 'java' |
25 | 30 |
26 _spec_to_java_type = { | 31 _spec_to_java_type = { |
27 mojom.BOOL.spec: 'boolean', | 32 mojom.BOOL.spec: 'boolean', |
28 mojom.DCPIPE.spec: 'org.chromium.mojo.system.DataPipe.ConsumerHandle', | 33 mojom.DCPIPE.spec: 'org.chromium.mojo.system.DataPipe.ConsumerHandle', |
29 mojom.DOUBLE.spec: 'double', | 34 mojom.DOUBLE.spec: 'double', |
30 mojom.DPPIPE.spec: 'org.chromium.mojo.system.DataPipe.ProducerHandle', | 35 mojom.DPPIPE.spec: 'org.chromium.mojo.system.DataPipe.ProducerHandle', |
31 mojom.FLOAT.spec: 'float', | 36 mojom.FLOAT.spec: 'float', |
32 mojom.HANDLE.spec: 'org.chromium.mojo.system.UntypedHandle', | 37 mojom.HANDLE.spec: 'org.chromium.mojo.system.UntypedHandle', |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 return False | 395 return False |
391 | 396 |
392 @contextlib.contextmanager | 397 @contextlib.contextmanager |
393 def TempDir(): | 398 def TempDir(): |
394 dirname = tempfile.mkdtemp() | 399 dirname = tempfile.mkdtemp() |
395 try: | 400 try: |
396 yield dirname | 401 yield dirname |
397 finally: | 402 finally: |
398 shutil.rmtree(dirname) | 403 shutil.rmtree(dirname) |
399 | 404 |
400 def ZipContentInto(root, zip_filename): | |
401 with zipfile.ZipFile(zip_filename, 'w') as zip_file: | |
402 for dirname, _, files in os.walk(root): | |
403 for filename in files: | |
404 path = os.path.join(dirname, filename) | |
405 path_in_archive = os.path.relpath(path, root) | |
406 zip_file.write(path, path_in_archive) | |
407 | |
408 class Generator(generator.Generator): | 405 class Generator(generator.Generator): |
409 | 406 |
410 java_filters = { | 407 java_filters = { |
411 'array_expected_length': GetArrayExpectedLength, | 408 'array_expected_length': GetArrayExpectedLength, |
412 'array': GetArrayKind, | 409 'array': GetArrayKind, |
413 'constant_value': ConstantValue, | 410 'constant_value': ConstantValue, |
414 'decode_method': DecodeMethod, | 411 'decode_method': DecodeMethod, |
415 'default_value': DefaultValue, | 412 'default_value': DefaultValue, |
416 'encode_method': EncodeMethod, | 413 'encode_method': EncodeMethod, |
417 'expression_to_text': ExpressionToText, | 414 'expression_to_text': ExpressionToText, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 args = parser.parse_args(unparsed_args) | 523 args = parser.parse_args(unparsed_args) |
527 package_path = GetPackage(self.module).replace('.', '/') | 524 package_path = GetPackage(self.module).replace('.', '/') |
528 | 525 |
529 # Generate the java files in a temporary directory and place a single | 526 # Generate the java files in a temporary directory and place a single |
530 # srcjar in the output directory. | 527 # srcjar in the output directory. |
531 basename = self.MatchMojomFilePath("%s.srcjar" % self.module.name) | 528 basename = self.MatchMojomFilePath("%s.srcjar" % self.module.name) |
532 zip_filename = os.path.join(self.output_dir, basename) | 529 zip_filename = os.path.join(self.output_dir, basename) |
533 with TempDir() as temp_java_root: | 530 with TempDir() as temp_java_root: |
534 self.output_dir = os.path.join(temp_java_root, package_path) | 531 self.output_dir = os.path.join(temp_java_root, package_path) |
535 self.DoGenerateFiles(); | 532 self.DoGenerateFiles(); |
536 ZipContentInto(temp_java_root, zip_filename) | 533 build_utils.ZipDir(zip_filename, temp_java_root) |
537 | 534 |
538 if args.java_output_directory: | 535 if args.java_output_directory: |
539 # If requested, generate the java files directly into indicated directory. | 536 # If requested, generate the java files directly into indicated directory. |
540 self.output_dir = os.path.join(args.java_output_directory, package_path) | 537 self.output_dir = os.path.join(args.java_output_directory, package_path) |
541 self.DoGenerateFiles(); | 538 self.DoGenerateFiles(); |
542 | 539 |
543 def GetJinjaParameters(self): | 540 def GetJinjaParameters(self): |
544 return { | 541 return { |
545 'lstrip_blocks': True, | 542 'lstrip_blocks': True, |
546 'trim_blocks': True, | 543 'trim_blocks': True, |
547 } | 544 } |
548 | 545 |
549 def GetGlobals(self): | 546 def GetGlobals(self): |
550 return { | 547 return { |
551 'namespace': self.module.namespace, | 548 'namespace': self.module.namespace, |
552 'module': self.module, | 549 'module': self.module, |
553 } | 550 } |
OLD | NEW |