| 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 errno | 10 import errno |
| (...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 kmethods = self.GetKMethodsString(clazz) | 882 kmethods = self.GetKMethodsString(clazz) |
| 883 if kmethods: | 883 if kmethods: |
| 884 values = {'JAVA_CLASS': clazz, | 884 values = {'JAVA_CLASS': clazz, |
| 885 'KMETHODS': kmethods} | 885 'KMETHODS': kmethods} |
| 886 ret += [template.substitute(values)] | 886 ret += [template.substitute(values)] |
| 887 if not ret: return '' | 887 if not ret: return '' |
| 888 return '\n' + '\n'.join(ret) | 888 return '\n' + '\n'.join(ret) |
| 889 | 889 |
| 890 def GetJNINativeMethodsString(self): | 890 def GetJNINativeMethodsString(self): |
| 891 """Returns the implementation of the array of native methods.""" | 891 """Returns the implementation of the array of native methods.""" |
| 892 if self.options.native_exports: | 892 if self.options.native_exports and not self.options.native_exports_optional: |
| 893 return '' | 893 return '' |
| 894 template = Template("""\ | 894 template = Template("""\ |
| 895 static const JNINativeMethod kMethods${JAVA_CLASS}[] = { | 895 static const JNINativeMethod kMethods${JAVA_CLASS}[] = { |
| 896 ${KMETHODS} | 896 ${KMETHODS} |
| 897 }; | 897 }; |
| 898 """) | 898 """) |
| 899 return self.SubstituteNativeMethods(template) | 899 return self.SubstituteNativeMethods(template) |
| 900 | 900 |
| 901 def GetRegisterCalledByNativesImplString(self): | 901 def GetRegisterCalledByNativesImplString(self): |
| 902 """Returns the code for registering the called by native methods.""" | 902 """Returns the code for registering the called by native methods.""" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 938 called_by_natives = self.GetRegisterCalledByNativesImplString() | 938 called_by_natives = self.GetRegisterCalledByNativesImplString() |
| 939 values = {'REGISTER_NATIVES_SIGNATURE': signature, | 939 values = {'REGISTER_NATIVES_SIGNATURE': signature, |
| 940 'CLASSES': self.GetFindClasses(), | 940 'CLASSES': self.GetFindClasses(), |
| 941 'NATIVES': natives, | 941 'NATIVES': natives, |
| 942 'CALLED_BY_NATIVES': called_by_natives, | 942 'CALLED_BY_NATIVES': called_by_natives, |
| 943 } | 943 } |
| 944 return template.substitute(values) | 944 return template.substitute(values) |
| 945 | 945 |
| 946 def GetRegisterNativesImplString(self): | 946 def GetRegisterNativesImplString(self): |
| 947 """Returns the shared implementation for RegisterNatives.""" | 947 """Returns the shared implementation for RegisterNatives.""" |
| 948 if self.options.native_exports: | 948 if self.options.native_exports and not self.options.native_exports_optional: |
| 949 return '' | 949 return '' |
| 950 | 950 |
| 951 template = Template("""\ | 951 template = Template("""\ |
| 952 const int kMethods${JAVA_CLASS}Size = arraysize(kMethods${JAVA_CLASS}); | 952 const int kMethods${JAVA_CLASS}Size = arraysize(kMethods${JAVA_CLASS}); |
| 953 | 953 |
| 954 if (env->RegisterNatives(${JAVA_CLASS}_clazz(env), | 954 if (env->RegisterNatives(${JAVA_CLASS}_clazz(env), |
| 955 kMethods${JAVA_CLASS}, | 955 kMethods${JAVA_CLASS}, |
| 956 kMethods${JAVA_CLASS}Size) < 0) { | 956 kMethods${JAVA_CLASS}Size) < 0) { |
| 957 jni_generator::HandleRegistrationError( | 957 jni_generator::HandleRegistrationError( |
| 958 env, ${JAVA_CLASS}_clazz(env), __FILE__); | 958 env, ${JAVA_CLASS}_clazz(env), __FILE__); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 [JavaDataTypeToC(param.datatype) + ' ' + | 1028 [JavaDataTypeToC(param.datatype) + ' ' + |
| 1029 param.name | 1029 param.name |
| 1030 for param in native.params]) | 1030 for param in native.params]) |
| 1031 | 1031 |
| 1032 def GetCalledByNativeParamsInDeclaration(self, called_by_native): | 1032 def GetCalledByNativeParamsInDeclaration(self, called_by_native): |
| 1033 return ',\n '.join([ | 1033 return ',\n '.join([ |
| 1034 JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' + | 1034 JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' + |
| 1035 param.name | 1035 param.name |
| 1036 for param in called_by_native.params]) | 1036 for param in called_by_native.params]) |
| 1037 | 1037 |
| 1038 def GetStubName(self, native): |
| 1039 if self.options.native_exports: |
| 1040 template = Template("Java_${JAVA_NAME}_native${NAME}") |
| 1041 |
| 1042 java_name = JniParams.RemapClassName(self.fully_qualified_class) |
| 1043 java_name = java_name.replace('_', '_1').replace('/', '_') |
| 1044 if native.java_class_name: |
| 1045 java_name += '_00024' + native.java_class_name |
| 1046 |
| 1047 values = {'NAME': native.name, |
| 1048 'JAVA_NAME': java_name} |
| 1049 return template.substitute(values) |
| 1050 else: |
| 1051 return native.name |
| 1052 |
| 1038 def GetForwardDeclaration(self, native): | 1053 def GetForwardDeclaration(self, native): |
| 1039 template_str = """ | 1054 template_str = """ |
| 1040 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); | 1055 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); |
| 1041 """ | 1056 """ |
| 1042 if self.options.native_exports: | 1057 if self.options.native_exports: |
| 1043 template_str += """ | 1058 template_str += """ |
| 1044 __attribute__((visibility("default"), alias("${NAME}"))) | 1059 __attribute__((visibility("default"), alias("${NAME}"))) |
| 1045 ${RETURN} Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, ${PARAMS}); | 1060 ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS}); |
| 1046 """ | 1061 """ |
| 1047 template = Template(template_str) | 1062 template = Template(template_str) |
| 1048 params_in_call = [] | 1063 params_in_call = [] |
| 1049 if not self.options.pure_native_methods: | 1064 if not self.options.pure_native_methods: |
| 1050 params_in_call = ['env', 'jcaller'] | 1065 params_in_call = ['env', 'jcaller'] |
| 1051 params_in_call = ', '.join(params_in_call + [p.name for p in native.params]) | 1066 params_in_call = ', '.join(params_in_call + [p.name for p in native.params]) |
| 1052 | 1067 |
| 1053 java_name = JniParams.RemapClassName(self.fully_qualified_class) | |
| 1054 java_name = java_name.replace('_', '_1').replace('/', '_') | |
| 1055 if native.java_class_name: | |
| 1056 java_name += '_00024' + native.java_class_name | |
| 1057 | |
| 1058 values = {'RETURN': JavaDataTypeToC(native.return_type), | 1068 values = {'RETURN': JavaDataTypeToC(native.return_type), |
| 1059 'NAME': native.name, | 1069 'NAME': native.name, |
| 1060 'JAVA_NAME': java_name, | |
| 1061 'PARAMS': self.GetParamsInDeclaration(native), | 1070 'PARAMS': self.GetParamsInDeclaration(native), |
| 1062 'PARAMS_IN_CALL': params_in_call} | 1071 'PARAMS_IN_CALL': params_in_call, |
| 1072 'STUB_NAME': self.GetStubName(native)} |
| 1063 return template.substitute(values) | 1073 return template.substitute(values) |
| 1064 | 1074 |
| 1065 def GetNativeMethodStubString(self, native): | 1075 def GetNativeMethodStubString(self, native): |
| 1066 """Returns stubs for native methods.""" | 1076 """Returns stubs for native methods.""" |
| 1067 if self.options.native_exports: | 1077 if self.options.native_exports: |
| 1068 template_str = """\ | 1078 template_str = """\ |
| 1069 __attribute__((visibility("default"))) | 1079 __attribute__((visibility("default"))) |
| 1070 ${RETURN} Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, | 1080 ${RETURN} ${STUB_NAME}(JNIEnv* env, |
| 1071 ${PARAMS_IN_DECLARATION}) {""" | 1081 ${PARAMS_IN_DECLARATION}) {""" |
| 1072 else: | 1082 else: |
| 1073 template_str = """\ | 1083 template_str = """\ |
| 1074 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {""" | 1084 static ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {""" |
| 1075 template_str += """ | 1085 template_str += """ |
| 1076 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); | 1086 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); |
| 1077 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); | 1087 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); |
| 1078 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; | 1088 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
| 1079 } | 1089 } |
| 1080 """ | 1090 """ |
| 1081 | 1091 |
| 1082 template = Template(template_str) | 1092 template = Template(template_str) |
| 1083 params = [] | 1093 params = [] |
| 1084 if not self.options.pure_native_methods: | 1094 if not self.options.pure_native_methods: |
| 1085 params = ['env', 'jcaller'] | 1095 params = ['env', 'jcaller'] |
| 1086 params_in_call = ', '.join(params + [p.name for p in native.params[1:]]) | 1096 params_in_call = ', '.join(params + [p.name for p in native.params[1:]]) |
| 1087 | 1097 |
| 1088 return_type = JavaDataTypeToC(native.return_type) | 1098 return_type = JavaDataTypeToC(native.return_type) |
| 1089 optional_error_return = JavaReturnValueToC(native.return_type) | 1099 optional_error_return = JavaReturnValueToC(native.return_type) |
| 1090 if optional_error_return: | 1100 if optional_error_return: |
| 1091 optional_error_return = ', ' + optional_error_return | 1101 optional_error_return = ', ' + optional_error_return |
| 1092 post_call = '' | 1102 post_call = '' |
| 1093 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): | 1103 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): |
| 1094 post_call = '.Release()' | 1104 post_call = '.Release()' |
| 1095 | 1105 |
| 1096 if self.options.native_exports: | |
| 1097 java_name = JniParams.RemapClassName(self.fully_qualified_class) | |
| 1098 java_name = java_name.replace('_', '_1').replace('/', '_') | |
| 1099 if native.java_class_name: | |
| 1100 java_name += '_00024' + native.java_class_name | |
| 1101 else: | |
| 1102 java_name = '' | |
| 1103 | |
| 1104 values = { | 1106 values = { |
| 1105 'RETURN': return_type, | 1107 'RETURN': return_type, |
| 1106 'OPTIONAL_ERROR_RETURN': optional_error_return, | 1108 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| 1107 'JAVA_NAME': java_name, | |
| 1108 'NAME': native.name, | 1109 'NAME': native.name, |
| 1109 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), | 1110 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), |
| 1110 'PARAM0_NAME': native.params[0].name, | 1111 'PARAM0_NAME': native.params[0].name, |
| 1111 'P0_TYPE': native.p0_type, | 1112 'P0_TYPE': native.p0_type, |
| 1112 'PARAMS_IN_CALL': params_in_call, | 1113 'PARAMS_IN_CALL': params_in_call, |
| 1113 'POST_CALL': post_call | 1114 'POST_CALL': post_call, |
| 1115 'STUB_NAME': self.GetStubName(native), |
| 1114 } | 1116 } |
| 1115 return template.substitute(values) | 1117 return template.substitute(values) |
| 1116 | 1118 |
| 1117 def GetArgument(self, param): | 1119 def GetArgument(self, param): |
| 1118 return ('as_jint(' + param.name + ')' | 1120 return ('as_jint(' + param.name + ')' |
| 1119 if param.datatype == 'int' else param.name) | 1121 if param.datatype == 'int' else param.name) |
| 1120 | 1122 |
| 1121 def GetArgumentsInCall(self, params): | 1123 def GetArgumentsInCall(self, params): |
| 1122 """Return a string of arguments to call from native into Java""" | 1124 """Return a string of arguments to call from native into Java""" |
| 1123 return [self.GetArgument(p) for p in params] | 1125 return [self.GetArgument(p) for p in params] |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1219 function_signature_template.substitute(values)) | 1221 function_signature_template.substitute(values)) |
| 1220 if called_by_native.system_class: | 1222 if called_by_native.system_class: |
| 1221 values['FUNCTION_HEADER'] = ( | 1223 values['FUNCTION_HEADER'] = ( |
| 1222 function_header_with_unused_template.substitute(values)) | 1224 function_header_with_unused_template.substitute(values)) |
| 1223 else: | 1225 else: |
| 1224 values['FUNCTION_HEADER'] = function_header_template.substitute(values) | 1226 values['FUNCTION_HEADER'] = function_header_template.substitute(values) |
| 1225 return template.substitute(values) | 1227 return template.substitute(values) |
| 1226 | 1228 |
| 1227 def GetKMethodArrayEntry(self, native): | 1229 def GetKMethodArrayEntry(self, native): |
| 1228 template = Template("""\ | 1230 template = Template("""\ |
| 1229 { "native${NAME}", ${JNI_SIGNATURE}, reinterpret_cast<void*>(${NAME}) },""") | 1231 { "native${NAME}", ${JNI_SIGNATURE}, reinterpret_cast<void*>(${STUB_NAME}) }
,""") |
| 1230 values = {'NAME': native.name, | 1232 values = {'NAME': native.name, |
| 1231 'JNI_SIGNATURE': JniParams.Signature(native.params, | 1233 'JNI_SIGNATURE': JniParams.Signature(native.params, |
| 1232 native.return_type, | 1234 native.return_type, |
| 1233 True)} | 1235 True), |
| 1236 'STUB_NAME': self.GetStubName(native)} |
| 1234 return template.substitute(values) | 1237 return template.substitute(values) |
| 1235 | 1238 |
| 1236 def GetUniqueClasses(self, origin): | 1239 def GetUniqueClasses(self, origin): |
| 1237 ret = {self.class_name: self.fully_qualified_class} | 1240 ret = {self.class_name: self.fully_qualified_class} |
| 1238 for entry in origin: | 1241 for entry in origin: |
| 1239 class_name = self.class_name | 1242 class_name = self.class_name |
| 1240 jni_class_path = self.fully_qualified_class | 1243 jni_class_path = self.fully_qualified_class |
| 1241 if entry.java_class_name: | 1244 if entry.java_class_name: |
| 1242 class_name = entry.java_class_name | 1245 class_name = entry.java_class_name |
| 1243 jni_class_path = self.fully_qualified_class + '$' + class_name | 1246 jni_class_path = self.fully_qualified_class + '$' + class_name |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1493 action='store_true', dest='eager_called_by_natives', | 1496 action='store_true', dest='eager_called_by_natives', |
| 1494 help='When true, the called-by-native methods will ' | 1497 help='When true, the called-by-native methods will ' |
| 1495 'be initialized in a non-atomic way.') | 1498 'be initialized in a non-atomic way.') |
| 1496 option_parser.add_option('--cpp', default='cpp', | 1499 option_parser.add_option('--cpp', default='cpp', |
| 1497 help='The path to cpp command.') | 1500 help='The path to cpp command.') |
| 1498 option_parser.add_option('--javap', default='javap', | 1501 option_parser.add_option('--javap', default='javap', |
| 1499 help='The path to javap command.') | 1502 help='The path to javap command.') |
| 1500 option_parser.add_option('--native_exports', action='store_true', | 1503 option_parser.add_option('--native_exports', action='store_true', |
| 1501 help='Native method registration through .so ' | 1504 help='Native method registration through .so ' |
| 1502 'exports.') | 1505 'exports.') |
| 1506 option_parser.add_option('--native_exports_optional', action='store_true', |
| 1507 help='Support both explicit and native method' |
| 1508 'registration.') |
| 1503 options, args = option_parser.parse_args(argv) | 1509 options, args = option_parser.parse_args(argv) |
| 1510 if options.native_exports_optional: |
| 1511 options.native_exports = True |
| 1504 if options.jar_file: | 1512 if options.jar_file: |
| 1505 input_file = ExtractJarInputFile(options.jar_file, options.input_file, | 1513 input_file = ExtractJarInputFile(options.jar_file, options.input_file, |
| 1506 options.output_dir) | 1514 options.output_dir) |
| 1507 elif options.input_file: | 1515 elif options.input_file: |
| 1508 input_file = options.input_file | 1516 input_file = options.input_file |
| 1509 else: | 1517 else: |
| 1510 option_parser.print_help() | 1518 option_parser.print_help() |
| 1511 print '\nError: Must specify --jar_file or --input_file.' | 1519 print '\nError: Must specify --jar_file or --input_file.' |
| 1512 return 1 | 1520 return 1 |
| 1513 output_file = None | 1521 output_file = None |
| 1514 if options.output_dir: | 1522 if options.output_dir: |
| 1515 root_name = os.path.splitext(os.path.basename(input_file))[0] | 1523 root_name = os.path.splitext(os.path.basename(input_file))[0] |
| 1516 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 1524 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
| 1517 if options.jarjar: | 1525 if options.jarjar: |
| 1518 with open(options.jarjar) as f: | 1526 with open(options.jarjar) as f: |
| 1519 JniParams.SetJarJarMappings(f.read()) | 1527 JniParams.SetJarJarMappings(f.read()) |
| 1520 GenerateJNIHeader(input_file, output_file, options) | 1528 GenerateJNIHeader(input_file, output_file, options) |
| 1521 | 1529 |
| 1522 if options.depfile: | 1530 if options.depfile: |
| 1523 build_utils.WriteDepfile( | 1531 build_utils.WriteDepfile( |
| 1524 options.depfile, | 1532 options.depfile, |
| 1525 build_utils.GetPythonDependencies()) | 1533 build_utils.GetPythonDependencies()) |
| 1526 | 1534 |
| 1527 | 1535 |
| 1528 if __name__ == '__main__': | 1536 if __name__ == '__main__': |
| 1529 sys.exit(main(sys.argv)) | 1537 sys.exit(main(sys.argv)) |
| OLD | NEW |