OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 # TODO (qinmin): Need to refactor this file as base should not know about | 6 # TODO (qinmin): Need to refactor this file as base should not know about |
7 # higher level concepts. Currently this file has knowledge about higher level | 7 # higher level concepts. Currently this file has knowledge about higher level |
8 # java classes. | 8 # java classes. |
9 | 9 |
10 """Extracts native methods from a Java file and generates the JNI bindings. | 10 """Extracts native methods from a Java file and generates the JNI bindings. |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 natives = ExtractNatives(contents) | 444 natives = ExtractNatives(contents) |
445 called_by_natives = ExtractCalledByNatives(contents) | 445 called_by_natives = ExtractCalledByNatives(contents) |
446 if len(natives) == 0 and len(called_by_natives) == 0: | 446 if len(natives) == 0 and len(called_by_natives) == 0: |
447 raise SyntaxError('Unable to find any JNI methods for %s.' % | 447 raise SyntaxError('Unable to find any JNI methods for %s.' % |
448 fully_qualified_class) | 448 fully_qualified_class) |
449 inl_header_file_generator = InlHeaderFileGenerator( | 449 inl_header_file_generator = InlHeaderFileGenerator( |
450 jni_namespace, fully_qualified_class, natives, called_by_natives) | 450 jni_namespace, fully_qualified_class, natives, called_by_natives) |
451 self.content = inl_header_file_generator.GetContent() | 451 self.content = inl_header_file_generator.GetContent() |
452 | 452 |
453 def _RemoveComments(self, contents): | 453 def _RemoveComments(self, contents): |
454 ret = [] | 454 # We need to support both inline and block comments, and we need to handle |
455 for c in [c.strip() for c in contents.split('\n')]: | 455 # strings that contain '//' or '/*'. Rather than trying to do all that with |
456 if not c.startswith('//'): | 456 # regexps, we just pipe the contents through the C preprocessor. We tell cpp |
457 ret += [c] | 457 # the file has already been preprocessed, so it just removes comments and |
458 return '\n'.join(ret) | 458 # doesn't try to parse #include, #pragma etc. |
| 459 # |
| 460 # TODO(husky): This is a bit hacky. It would be cleaner to use a real Java |
| 461 # parser. Maybe we could ditch JNIFromJavaSource and just always use |
| 462 # JNIFromJavaP; or maybe we could rewrite this script in Java and use APT. |
| 463 # http://code.google.com/p/chromium/issues/detail?id=138941 |
| 464 p = subprocess.Popen(args=['cpp', '-fpreprocessed'], |
| 465 stdin=subprocess.PIPE, |
| 466 stdout=subprocess.PIPE, |
| 467 stderr=subprocess.PIPE) |
| 468 stdout, _ = p.communicate(contents) |
| 469 return stdout |
459 | 470 |
460 def GetContent(self): | 471 def GetContent(self): |
461 return self.content | 472 return self.content |
462 | 473 |
463 @staticmethod | 474 @staticmethod |
464 def CreateFromFile(java_file_name): | 475 def CreateFromFile(java_file_name): |
465 contents = file(java_file_name).read() | 476 contents = file(java_file_name).read() |
466 fully_qualified_class = ExtractFullyQualifiedJavaClassName(java_file_name, | 477 fully_qualified_class = ExtractFullyQualifiedJavaClassName(java_file_name, |
467 contents) | 478 contents) |
468 return JNIFromJavaSource(contents, fully_qualified_class) | 479 return JNIFromJavaSource(contents, fully_qualified_class) |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 input_file = options.input_file | 974 input_file = options.input_file |
964 output_file = None | 975 output_file = None |
965 if options.output_dir: | 976 if options.output_dir: |
966 root_name = os.path.splitext(os.path.basename(input_file))[0] | 977 root_name = os.path.splitext(os.path.basename(input_file))[0] |
967 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 978 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
968 GenerateJNIHeader(input_file, output_file, options.namespace) | 979 GenerateJNIHeader(input_file, output_file, options.namespace) |
969 | 980 |
970 | 981 |
971 if __name__ == '__main__': | 982 if __name__ == '__main__': |
972 sys.exit(main(sys.argv)) | 983 sys.exit(main(sys.argv)) |
OLD | NEW |