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 """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 optparse | 10 import optparse |
(...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
950 out_dir = os.path.join(out_dir, os.path.dirname(input_file)) | 950 out_dir = os.path.join(out_dir, os.path.dirname(input_file)) |
951 if not os.path.exists(out_dir): | 951 if not os.path.exists(out_dir): |
952 os.makedirs(out_dir) | 952 os.makedirs(out_dir) |
953 extracted_file_name = os.path.join(out_dir, os.path.basename(input_file)) | 953 extracted_file_name = os.path.join(out_dir, os.path.basename(input_file)) |
954 with open(extracted_file_name, 'w') as outfile: | 954 with open(extracted_file_name, 'w') as outfile: |
955 outfile.write(jar_file.read(input_file)) | 955 outfile.write(jar_file.read(input_file)) |
956 | 956 |
957 return extracted_file_name | 957 return extracted_file_name |
958 | 958 |
959 | 959 |
960 def GenerateJNIHeader(input_file, output_file, namespace): | 960 def GenerateJNIHeader(input_file, output_file, namespace, skip_if_same): |
961 try: | 961 try: |
962 if os.path.splitext(input_file)[1] == '.class': | 962 if os.path.splitext(input_file)[1] == '.class': |
963 jni_from_javap = JNIFromJavaP.CreateFromClass(input_file, namespace) | 963 jni_from_javap = JNIFromJavaP.CreateFromClass(input_file, namespace) |
964 content = jni_from_javap.GetContent() | 964 content = jni_from_javap.GetContent() |
965 else: | 965 else: |
966 jni_from_java_source = JNIFromJavaSource.CreateFromFile(input_file) | 966 jni_from_java_source = JNIFromJavaSource.CreateFromFile(input_file) |
967 content = jni_from_java_source.GetContent() | 967 content = jni_from_java_source.GetContent() |
968 except ParseError, e: | 968 except ParseError, e: |
969 print e | 969 print e |
970 sys.exit(1) | 970 sys.exit(1) |
971 if output_file: | 971 if output_file: |
972 if not os.path.exists(os.path.dirname(os.path.abspath(output_file))): | 972 if not os.path.exists(os.path.dirname(os.path.abspath(output_file))): |
973 os.makedirs(os.path.dirname(os.path.abspath(output_file))) | 973 os.makedirs(os.path.dirname(os.path.abspath(output_file))) |
| 974 if skip_if_same and os.path.exists(output_file): |
| 975 with file(output_file, 'r') as f: |
| 976 existing_content = f.read() |
| 977 if existing_content == content: |
| 978 return |
974 with file(output_file, 'w') as f: | 979 with file(output_file, 'w') as f: |
975 f.write(content) | 980 f.write(content) |
976 else: | 981 else: |
977 print output | 982 print output |
978 | 983 |
979 | 984 |
980 def main(argv): | 985 def main(argv): |
981 usage = """usage: %prog [OPTIONS] | 986 usage = """usage: %prog [OPTIONS] |
982 This script will parse the given java source code extracting the native | 987 This script will parse the given java source code extracting the native |
983 declarations and print the header file to stdout (or a file). | 988 declarations and print the header file to stdout (or a file). |
984 See SampleForTests.java for more details. | 989 See SampleForTests.java for more details. |
985 """ | 990 """ |
986 option_parser = optparse.OptionParser(usage=usage) | 991 option_parser = optparse.OptionParser(usage=usage) |
987 option_parser.add_option('-j', dest='jar_file', | 992 option_parser.add_option('-j', dest='jar_file', |
988 help='Extract the list of input files from' | 993 help='Extract the list of input files from' |
989 ' a specified jar file.' | 994 ' a specified jar file.' |
990 ' Uses javap to extract the methods from a' | 995 ' Uses javap to extract the methods from a' |
991 ' pre-compiled class. --input should point' | 996 ' pre-compiled class. --input should point' |
992 ' to pre-compiled Java .class files.') | 997 ' to pre-compiled Java .class files.') |
993 option_parser.add_option('-n', dest='namespace', | 998 option_parser.add_option('-n', dest='namespace', |
994 help='Uses as a namespace in the generated header,' | 999 help='Uses as a namespace in the generated header,' |
995 ' instead of the javap class name.') | 1000 ' instead of the javap class name.') |
996 option_parser.add_option('--input_file', | 1001 option_parser.add_option('--input_file', |
997 help='Single input file name. The output file name ' | 1002 help='Single input file name. The output file name ' |
998 'will be derived from it. Must be used with ' | 1003 'will be derived from it. Must be used with ' |
999 '--output_dir.') | 1004 '--output_dir.') |
1000 option_parser.add_option('--output_dir', | 1005 option_parser.add_option('--output_dir', |
1001 help='The output directory. Must be used with ' | 1006 help='The output directory. Must be used with ' |
1002 '--input') | 1007 '--input') |
| 1008 option_parser.add_option('--optimize_generation', type="int", |
| 1009 default=0, help='Whether we should optimize JNI ' |
| 1010 'generation by not regenerating files if they have ' |
| 1011 'not changed.') |
1003 options, args = option_parser.parse_args(argv) | 1012 options, args = option_parser.parse_args(argv) |
1004 if options.jar_file: | 1013 if options.jar_file: |
1005 input_file = ExtractJarInputFile(options.jar_file, options.input_file, | 1014 input_file = ExtractJarInputFile(options.jar_file, options.input_file, |
1006 options.output_dir) | 1015 options.output_dir) |
1007 else: | 1016 else: |
1008 input_file = options.input_file | 1017 input_file = options.input_file |
1009 output_file = None | 1018 output_file = None |
1010 if options.output_dir: | 1019 if options.output_dir: |
1011 root_name = os.path.splitext(os.path.basename(input_file))[0] | 1020 root_name = os.path.splitext(os.path.basename(input_file))[0] |
1012 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 1021 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
1013 GenerateJNIHeader(input_file, output_file, options.namespace) | 1022 GenerateJNIHeader(input_file, output_file, options.namespace, |
| 1023 options.optimize_generation) |
1014 | 1024 |
1015 | 1025 |
1016 if __name__ == '__main__': | 1026 if __name__ == '__main__': |
1017 sys.exit(main(sys.argv)) | 1027 sys.exit(main(sys.argv)) |
OLD | NEW |