Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: base/android/jni_generator/jni_generator.py

Issue 227303003: android: Don't use cpp to strip comments in jni_generator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address nits Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 """Extracts native methods from a Java file and generates the JNI bindings. 6 """Extracts native methods from a Java file and generates the JNI bindings.
7 If you change this, please run and update the tests.""" 7 If you change this, please run and update the tests."""
8 8
9 import collections 9 import collections
10 import errno 10 import errno
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 stdout=subprocess.PIPE, 573 stdout=subprocess.PIPE,
574 stderr=subprocess.PIPE) 574 stderr=subprocess.PIPE)
575 stdout, _ = p.communicate() 575 stdout, _ = p.communicate()
576 jni_from_javap = JNIFromJavaP(stdout.split('\n'), options) 576 jni_from_javap = JNIFromJavaP(stdout.split('\n'), options)
577 return jni_from_javap 577 return jni_from_javap
578 578
579 579
580 class JNIFromJavaSource(object): 580 class JNIFromJavaSource(object):
581 """Uses the given java source file to generate the JNI header file.""" 581 """Uses the given java source file to generate the JNI header file."""
582 582
583 # Match single line comments, multiline comments, character literals, and
584 # double-quoted strings.
585 _comment_remover_regex = re.compile(
586 r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
587 re.DOTALL | re.MULTILINE)
588
583 def __init__(self, contents, fully_qualified_class, options): 589 def __init__(self, contents, fully_qualified_class, options):
584 contents = self._RemoveComments(contents, options) 590 contents = self._RemoveComments(contents)
585 JniParams.SetFullyQualifiedClass(fully_qualified_class) 591 JniParams.SetFullyQualifiedClass(fully_qualified_class)
586 JniParams.ExtractImportsAndInnerClasses(contents) 592 JniParams.ExtractImportsAndInnerClasses(contents)
587 jni_namespace = ExtractJNINamespace(contents) or options.namespace 593 jni_namespace = ExtractJNINamespace(contents) or options.namespace
588 natives = ExtractNatives(contents, options.ptr_type) 594 natives = ExtractNatives(contents, options.ptr_type)
589 called_by_natives = ExtractCalledByNatives(contents) 595 called_by_natives = ExtractCalledByNatives(contents)
590 if len(natives) == 0 and len(called_by_natives) == 0: 596 if len(natives) == 0 and len(called_by_natives) == 0:
591 raise SyntaxError('Unable to find any JNI methods for %s.' % 597 raise SyntaxError('Unable to find any JNI methods for %s.' %
592 fully_qualified_class) 598 fully_qualified_class)
593 inl_header_file_generator = InlHeaderFileGenerator( 599 inl_header_file_generator = InlHeaderFileGenerator(
594 jni_namespace, fully_qualified_class, natives, called_by_natives, 600 jni_namespace, fully_qualified_class, natives, called_by_natives,
595 [], options) 601 [], options)
596 self.content = inl_header_file_generator.GetContent() 602 self.content = inl_header_file_generator.GetContent()
597 603
598 def _RemoveComments(self, contents, options): 604 @classmethod
605 def _RemoveComments(cls, contents):
599 # We need to support both inline and block comments, and we need to handle 606 # We need to support both inline and block comments, and we need to handle
600 # strings that contain '//' or '/*'. Rather than trying to do all that with 607 # strings that contain '//' or '/*'.
601 # regexps, we just pipe the contents through the C preprocessor. We tell cpp 608 # TODO(bulach): This is a bit hacky. It would be cleaner to use a real Java
602 # the file has already been preprocessed, so it just removes comments and
603 # doesn't try to parse #include, #pragma etc.
604 #
605 # TODO(husky): This is a bit hacky. It would be cleaner to use a real Java
606 # parser. Maybe we could ditch JNIFromJavaSource and just always use 609 # parser. Maybe we could ditch JNIFromJavaSource and just always use
607 # JNIFromJavaP; or maybe we could rewrite this script in Java and use APT. 610 # JNIFromJavaP; or maybe we could rewrite this script in Java and use APT.
608 # http://code.google.com/p/chromium/issues/detail?id=138941 611 # http://code.google.com/p/chromium/issues/detail?id=138941
609 p = subprocess.Popen(args=[options.cpp, '-fpreprocessed'], 612 def replacer(match):
610 stdin=subprocess.PIPE, 613 # Replace matches that are comments with nothing; return literals/strings
611 stdout=subprocess.PIPE, 614 # unchanged.
612 stderr=subprocess.PIPE) 615 s = match.group(0)
613 stdout, _ = p.communicate(contents) 616 if s.startswith('/'):
614 return stdout 617 return ''
618 else:
619 return s
620 return cls._comment_remover_regex.sub(replacer, contents)
615 621
616 def GetContent(self): 622 def GetContent(self):
617 return self.content 623 return self.content
618 624
619 @staticmethod 625 @staticmethod
620 def CreateFromFile(java_file_name, options): 626 def CreateFromFile(java_file_name, options):
621 contents = file(java_file_name).read() 627 contents = file(java_file_name).read()
622 fully_qualified_class = ExtractFullyQualifiedJavaClassName(java_file_name, 628 fully_qualified_class = ExtractFullyQualifiedJavaClassName(java_file_name,
623 contents) 629 contents)
624 return JNIFromJavaSource(contents, fully_qualified_class, options) 630 return JNIFromJavaSource(contents, fully_qualified_class, options)
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
1337 root_name = os.path.splitext(os.path.basename(input_file))[0] 1343 root_name = os.path.splitext(os.path.basename(input_file))[0]
1338 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' 1344 output_file = os.path.join(options.output_dir, root_name) + '_jni.h'
1339 if options.jarjar: 1345 if options.jarjar:
1340 with open(options.jarjar) as f: 1346 with open(options.jarjar) as f:
1341 JniParams.SetJarJarMappings(f.read()) 1347 JniParams.SetJarJarMappings(f.read())
1342 GenerateJNIHeader(input_file, output_file, options) 1348 GenerateJNIHeader(input_file, output_file, options)
1343 1349
1344 1350
1345 if __name__ == '__main__': 1351 if __name__ == '__main__':
1346 sys.exit(main(sys.argv)) 1352 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698