Chromium Code Reviews| Index: base/android/jni_generator/jni_generator.py |
| diff --git a/base/android/jni_generator/jni_generator.py b/base/android/jni_generator/jni_generator.py |
| index 27335834953a3e006647d27d98b301deb5b1fbf2..160b0d2441328a8d64fe778c209e31d735d55b63 100755 |
| --- a/base/android/jni_generator/jni_generator.py |
| +++ b/base/android/jni_generator/jni_generator.py |
| @@ -730,6 +730,8 @@ jmethodID g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = NULL;""") |
| for native in self.natives: |
| if native.type != 'method': |
| ret += [self.GetForwardDeclaration(native)] |
| + if self.options.native_exports and ret != []: |
| + return '\nextern "C" {\n' + "\n".join(ret) + "\n};" |
|
bulach
2014/04/14 13:16:02
nit: use '\n' instead of "\n" in the second string
ostap
2014/04/15 23:30:20
Done.
|
| return '\n'.join(ret) |
| def GetConstantFieldsString(self): |
| @@ -751,6 +753,9 @@ jmethodID g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = NULL;""") |
| ret += self.GetEagerCalledByNativeMethodStubs() |
| else: |
| ret += self.GetLazyCalledByNativeMethodStubs() |
| + |
| + if self.options.native_exports and ret != []: |
| + return '\nextern "C" {\n' + "\n".join(ret) + "\n};" |
| return '\n'.join(ret) |
| def GetLazyCalledByNativeMethodStubs(self): |
| @@ -796,6 +801,8 @@ jmethodID g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = NULL;""") |
| def GetJNINativeMethodsString(self): |
| """Returns the implementation of the array of native methods.""" |
| + if self.options.native_exports: |
| + return '' |
| template = Template("""\ |
| static const JNINativeMethod kMethods${JAVA_CLASS}[] = { |
| ${KMETHODS} |
| @@ -850,6 +857,9 @@ ${CALLED_BY_NATIVES} |
| def GetRegisterNativesImplString(self): |
| """Returns the shared implementation for RegisterNatives.""" |
| + if self.options.native_exports: |
| + return '' |
| + |
| template = Template("""\ |
| const int kMethods${JAVA_CLASS}Size = arraysize(kMethods${JAVA_CLASS}); |
| @@ -865,7 +875,7 @@ ${CALLED_BY_NATIVES} |
| def GetJNIRegisterNativesString(self): |
| """Returns the implementation for the JNI registration of native methods.""" |
| - if not self.init_native: |
| + if self.options.native_exports or not self.init_native: |
| return '' |
| template = Template("""\ |
| @@ -931,23 +941,42 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| for param in called_by_native.params]) |
| def GetForwardDeclaration(self, native): |
| - template = Template(""" |
| + template_str = """ |
| static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); |
| -""") |
| +""" |
| + if self.options.native_exports: |
| + template_str += """ |
| +__attribute__((alias("${NAME}"), visibility("default"))) ${RETURN} |
|
bulach
2014/04/14 13:16:02
nit: move the ${RETURN} to the next line and put i
ostap
2014/04/15 23:30:20
Done.
|
| + Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, ${PARAMS}); |
| +""" |
| + template = Template(template_str) |
| + |
| + java_name = JniParams.RemapClassName(self.fully_qualified_class).replace('_', '_1').replace('/', '_') |
|
bulach
2014/04/14 13:16:02
nit: >80cols, suggest:
java_name = java_name = Jni
ostap
2014/04/15 23:30:20
Done.
|
| + if native.java_class_name: |
| + java_name += "_00024" + native.java_class_name |
|
bulach
2014/04/14 13:16:02
nit: s/"/'/
ostap
2014/04/15 23:30:20
Done.
|
| + |
| values = {'RETURN': JavaDataTypeToC(native.return_type), |
| 'NAME': native.name, |
| + 'JAVA_NAME': java_name, |
| 'PARAMS': self.GetParamsInDeclaration(native)} |
| return template.substitute(values) |
| def GetNativeMethodStubString(self, native): |
| """Returns stubs for native methods.""" |
| - template = Template("""\ |
| + template_str = """\ |
| static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) { |
| ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); |
| CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); |
| return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
| } |
| -""") |
| +""" |
| + if self.options.native_exports: |
| + template_str += """ |
| +__attribute__((alias("${NAME}"), visibility("default"))) |
| + ${RETURN} Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, |
|
bulach
2014/04/14 13:16:02
nit: unindent this line
ostap
2014/04/15 23:30:20
Done.
|
| + ${PARAMS_IN_DECLARATION}); |
| +""" |
| + template = Template(template_str) |
| params = [] |
| if not self.options.pure_native_methods: |
| params = ['env', 'jcaller'] |
| @@ -960,9 +989,18 @@ static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) { |
| post_call = '' |
| if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): |
| post_call = '.Release()' |
| + |
| + if self.options.native_exports: |
| + java_name = JniParams.RemapClassName(self.fully_qualified_class).replace('_', '_1').replace('/', '_') |
|
bulach
2014/04/14 13:16:02
nit: as above, >80 cols
ostap
2014/04/15 23:30:20
Done.
|
| + if native.java_class_name: |
| + java_name += "_00024" + native.java_class_name |
|
bulach
2014/04/14 13:16:02
nit: s/"/'/ and below in 998
ostap
2014/04/15 23:30:20
Done.
|
| + else: |
| + java_name = "" |
| + |
| values = { |
| 'RETURN': return_type, |
| 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| + 'JAVA_NAME': java_name, |
| 'NAME': native.name, |
| 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), |
| 'PARAM0_NAME': native.params[0].name, |
| @@ -1102,8 +1140,14 @@ ${FUNCTION_HEADER} |
| const char k${JAVA_CLASS}ClassPath[] = "${JNI_CLASS_PATH}";""") |
| native_classes = self.GetUniqueClasses(self.natives) |
| called_by_native_classes = self.GetUniqueClasses(self.called_by_natives) |
| - all_classes = native_classes |
| - all_classes.update(called_by_native_classes) |
| + if self.options.native_exports: |
| + all_classes = called_by_native_classes |
| + loop_classes = all_classes |
| + else: |
| + all_classes = native_classes |
| + all_classes.update(called_by_native_classes) |
| + loop_classes = called_by_native_classes |
| + |
| for clazz in all_classes: |
| values = { |
| 'JAVA_CLASS': clazz, |
| @@ -1111,7 +1155,7 @@ const char k${JAVA_CLASS}ClassPath[] = "${JNI_CLASS_PATH}";""") |
| } |
| ret += [template.substitute(values)] |
| ret += '' |
| - for clazz in called_by_native_classes: |
| + for clazz in loop_classes: |
| template = Template("""\ |
| // Leaking this jclass as we cannot use LazyInstance from some threads. |
| jclass g_${JAVA_CLASS}_clazz = NULL;""") |
| @@ -1322,6 +1366,10 @@ See SampleForTests.java for more details. |
| help='The path to cpp command.') |
| option_parser.add_option('--javap', default='javap', |
| help='The path to javap command.') |
| + option_parser.add_option('--native_exports', |
| + action='store_true', dest='native_exports', |
|
bulach
2014/04/14 13:16:02
nit: no need for dest
ostap
2014/04/15 23:30:20
Done.
|
| + help='Native method registration through .so ' |
| + 'exports.') |
| options, args = option_parser.parse_args(argv) |
| if options.jar_file: |
| input_file = ExtractJarInputFile(options.jar_file, options.input_file, |