maruel@chromium.org | be8446a | 2012-04-21 23:30:11 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 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 |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Extracts native methods from a Java file and generates the JNI bindings. |
| 7 | If you change this, please run and update the tests.""" |
| 8 | |
| 9 | import collections |
torne@chromium.org | 108c37d | 2013-03-08 22:52:50 +0900 | [diff] [blame] | 10 | import errno |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 11 | import optparse |
| 12 | import os |
| 13 | import re |
| 14 | import string |
| 15 | from string import Template |
| 16 | import subprocess |
| 17 | import sys |
| 18 | import textwrap |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 19 | import zipfile |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 20 | |
cjhopman@chromium.org | fb98e33 | 2014-06-25 08:38:17 +0900 | [diff] [blame] | 21 | CHROMIUM_SRC = os.path.join( |
| 22 | os.path.dirname(__file__), os.pardir, os.pardir, os.pardir) |
| 23 | BUILD_ANDROID_GYP = os.path.join( |
| 24 | CHROMIUM_SRC, 'build', 'android', 'gyp') |
| 25 | |
| 26 | sys.path.append(BUILD_ANDROID_GYP) |
| 27 | |
| 28 | from util import build_utils |
| 29 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 30 | |
| 31 | class ParseError(Exception): |
| 32 | """Exception thrown when we can't parse the input file.""" |
| 33 | |
| 34 | def __init__(self, description, *context_lines): |
| 35 | Exception.__init__(self) |
| 36 | self.description = description |
| 37 | self.context_lines = context_lines |
| 38 | |
| 39 | def __str__(self): |
| 40 | context = '\n'.join(self.context_lines) |
| 41 | return '***\nERROR: %s\n\n%s\n***' % (self.description, context) |
| 42 | |
| 43 | |
| 44 | class Param(object): |
| 45 | """Describes a param for a method, either java or native.""" |
| 46 | |
| 47 | def __init__(self, **kwargs): |
| 48 | self.datatype = kwargs['datatype'] |
| 49 | self.name = kwargs['name'] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 50 | |
| 51 | |
| 52 | class NativeMethod(object): |
| 53 | """Describes a C/C++ method that is called by Java code""" |
| 54 | |
| 55 | def __init__(self, **kwargs): |
| 56 | self.static = kwargs['static'] |
| 57 | self.java_class_name = kwargs['java_class_name'] |
| 58 | self.return_type = kwargs['return_type'] |
| 59 | self.name = kwargs['name'] |
| 60 | self.params = kwargs['params'] |
| 61 | if self.params: |
| 62 | assert type(self.params) is list |
| 63 | assert type(self.params[0]) is Param |
| 64 | if (self.params and |
bulach@chromium.org | a022cf6 | 2013-11-05 09:54:22 +0900 | [diff] [blame] | 65 | self.params[0].datatype == kwargs.get('ptr_type', 'int') and |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 66 | self.params[0].name.startswith('native')): |
| 67 | self.type = 'method' |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 68 | self.p0_type = self.params[0].name[len('native'):] |
| 69 | if kwargs.get('native_class_name'): |
| 70 | self.p0_type = kwargs['native_class_name'] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 71 | else: |
| 72 | self.type = 'function' |
| 73 | self.method_id_var_name = kwargs.get('method_id_var_name', None) |
| 74 | |
| 75 | |
| 76 | class CalledByNative(object): |
| 77 | """Describes a java method exported to c/c++""" |
| 78 | |
| 79 | def __init__(self, **kwargs): |
| 80 | self.system_class = kwargs['system_class'] |
| 81 | self.unchecked = kwargs['unchecked'] |
| 82 | self.static = kwargs['static'] |
| 83 | self.java_class_name = kwargs['java_class_name'] |
| 84 | self.return_type = kwargs['return_type'] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 85 | self.name = kwargs['name'] |
| 86 | self.params = kwargs['params'] |
| 87 | self.method_id_var_name = kwargs.get('method_id_var_name', None) |
bulach@chromium.org | 37fc911 | 2013-10-26 01:27:03 +0900 | [diff] [blame] | 88 | self.signature = kwargs.get('signature') |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 89 | self.is_constructor = kwargs.get('is_constructor', False) |
| 90 | self.env_call = GetEnvCall(self.is_constructor, self.static, |
| 91 | self.return_type) |
| 92 | self.static_cast = GetStaticCastForReturnType(self.return_type) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 93 | |
| 94 | |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 95 | class ConstantField(object): |
| 96 | def __init__(self, **kwargs): |
| 97 | self.name = kwargs['name'] |
| 98 | self.value = kwargs['value'] |
| 99 | |
| 100 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 101 | def JavaDataTypeToC(java_type): |
| 102 | """Returns a C datatype for the given java type.""" |
| 103 | java_pod_type_map = { |
| 104 | 'int': 'jint', |
| 105 | 'byte': 'jbyte', |
digit@chromium.org | 9d7eab0 | 2012-12-21 05:33:03 +0900 | [diff] [blame] | 106 | 'char': 'jchar', |
| 107 | 'short': 'jshort', |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 108 | 'boolean': 'jboolean', |
| 109 | 'long': 'jlong', |
| 110 | 'double': 'jdouble', |
| 111 | 'float': 'jfloat', |
| 112 | } |
| 113 | java_type_map = { |
| 114 | 'void': 'void', |
| 115 | 'String': 'jstring', |
torne | c912423 | 2015-08-27 23:57:06 +0900 | [diff] [blame] | 116 | 'Throwable': 'jthrowable', |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 117 | 'java/lang/String': 'jstring', |
dtrainor@chromium.org | f7e9977 | 2012-12-06 08:27:41 +0900 | [diff] [blame] | 118 | 'java/lang/Class': 'jclass', |
torne | c912423 | 2015-08-27 23:57:06 +0900 | [diff] [blame] | 119 | 'java/lang/Throwable': 'jthrowable', |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 120 | } |
dtrainor@chromium.org | f7e9977 | 2012-12-06 08:27:41 +0900 | [diff] [blame] | 121 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 122 | if java_type in java_pod_type_map: |
| 123 | return java_pod_type_map[java_type] |
| 124 | elif java_type in java_type_map: |
| 125 | return java_type_map[java_type] |
| 126 | elif java_type.endswith('[]'): |
| 127 | if java_type[:-2] in java_pod_type_map: |
| 128 | return java_pod_type_map[java_type[:-2]] + 'Array' |
| 129 | return 'jobjectArray' |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 130 | elif java_type.startswith('Class'): |
| 131 | # Checking just the start of the name, rather than a direct comparison, |
| 132 | # in order to handle generics. |
| 133 | return 'jclass' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 134 | else: |
| 135 | return 'jobject' |
| 136 | |
| 137 | |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 138 | def WrapCTypeForDeclaration(c_type): |
| 139 | """Wrap the C datatype in a JavaRef if required.""" |
| 140 | if re.match(RE_SCOPED_JNI_TYPES, c_type): |
torne | 9854c72 | 2016-08-05 00:44:06 +0900 | [diff] [blame] | 141 | return 'const base::android::JavaParamRef<' + c_type + '>&' |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 142 | else: |
| 143 | return c_type |
| 144 | |
| 145 | |
| 146 | def JavaDataTypeToCForDeclaration(java_type): |
| 147 | """Returns a JavaRef-wrapped C datatype for the given java type.""" |
| 148 | return WrapCTypeForDeclaration(JavaDataTypeToC(java_type)) |
| 149 | |
| 150 | |
anton@chromium.org | 9d3b13b | 2014-05-07 23:14:10 +0900 | [diff] [blame] | 151 | def JavaDataTypeToCForCalledByNativeParam(java_type): |
| 152 | """Returns a C datatype to be when calling from native.""" |
| 153 | if java_type == 'int': |
| 154 | return 'JniIntWrapper' |
| 155 | else: |
torne | 28061e9 | 2016-08-09 01:29:18 +0900 | [diff] [blame] | 156 | c_type = JavaDataTypeToC(java_type) |
| 157 | if re.match(RE_SCOPED_JNI_TYPES, c_type): |
| 158 | return 'const base::android::JavaRefOrBare<' + c_type + '>&' |
| 159 | else: |
| 160 | return c_type |
anton@chromium.org | 9d3b13b | 2014-05-07 23:14:10 +0900 | [diff] [blame] | 161 | |
| 162 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 163 | def JavaReturnValueToC(java_type): |
| 164 | """Returns a valid C return value for the given java type.""" |
| 165 | java_pod_type_map = { |
| 166 | 'int': '0', |
| 167 | 'byte': '0', |
| 168 | 'char': '0', |
| 169 | 'short': '0', |
| 170 | 'boolean': 'false', |
| 171 | 'long': '0', |
| 172 | 'double': '0', |
| 173 | 'float': '0', |
| 174 | 'void': '' |
| 175 | } |
| 176 | return java_pod_type_map.get(java_type, 'NULL') |
| 177 | |
| 178 | |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 179 | class JniParams(object): |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 180 | """Get JNI related parameters.""" |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 181 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 182 | def __init__(self, fully_qualified_class): |
| 183 | self._fully_qualified_class = 'L' + fully_qualified_class |
| 184 | self._package = '/'.join(fully_qualified_class.split('/')[:-1]) |
| 185 | self._imports = [] |
| 186 | self._inner_classes = [] |
| 187 | self._implicit_imports = [] |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 188 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 189 | def ExtractImportsAndInnerClasses(self, contents): |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 190 | contents = contents.replace('\n', '') |
| 191 | re_import = re.compile(r'import.*?(?P<class>\S*?);') |
| 192 | for match in re.finditer(re_import, contents): |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 193 | self._imports += ['L' + match.group('class').replace('.', '/')] |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 194 | |
| 195 | re_inner = re.compile(r'(class|interface)\s+?(?P<name>\w+?)\W') |
| 196 | for match in re.finditer(re_inner, contents): |
| 197 | inner = match.group('name') |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 198 | if not self._fully_qualified_class.endswith(inner): |
| 199 | self._inner_classes += [self._fully_qualified_class + '$' + |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 200 | inner] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 201 | |
qsr@chromium.org | 47bd346 | 2014-05-19 23:26:49 +0900 | [diff] [blame] | 202 | re_additional_imports = re.compile( |
qsr@chromium.org | 74f541e | 2014-05-23 23:44:37 +0900 | [diff] [blame] | 203 | r'@JNIAdditionalImport\(\s*{?(?P<class_names>.*?)}?\s*\)') |
qsr@chromium.org | 47bd346 | 2014-05-19 23:26:49 +0900 | [diff] [blame] | 204 | for match in re.finditer(re_additional_imports, contents): |
qsr@chromium.org | 74f541e | 2014-05-23 23:44:37 +0900 | [diff] [blame] | 205 | for class_name in match.group('class_names').split(','): |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 206 | self._AddAdditionalImport(class_name.strip()) |
qsr@chromium.org | 47bd346 | 2014-05-19 23:26:49 +0900 | [diff] [blame] | 207 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 208 | def JavaToJni(self, param): |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 209 | """Converts a java param into a JNI signature type.""" |
| 210 | pod_param_map = { |
| 211 | 'int': 'I', |
| 212 | 'boolean': 'Z', |
digit@chromium.org | 9d7eab0 | 2012-12-21 05:33:03 +0900 | [diff] [blame] | 213 | 'char': 'C', |
| 214 | 'short': 'S', |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 215 | 'long': 'J', |
| 216 | 'double': 'D', |
| 217 | 'float': 'F', |
| 218 | 'byte': 'B', |
| 219 | 'void': 'V', |
| 220 | } |
| 221 | object_param_list = [ |
| 222 | 'Ljava/lang/Boolean', |
| 223 | 'Ljava/lang/Integer', |
| 224 | 'Ljava/lang/Long', |
| 225 | 'Ljava/lang/Object', |
| 226 | 'Ljava/lang/String', |
dtrainor@chromium.org | f7e9977 | 2012-12-06 08:27:41 +0900 | [diff] [blame] | 227 | 'Ljava/lang/Class', |
aurimas | 9e29f73 | 2015-06-25 07:53:10 +0900 | [diff] [blame] | 228 | 'Ljava/lang/CharSequence', |
| 229 | 'Ljava/lang/Runnable', |
| 230 | 'Ljava/lang/Throwable', |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 231 | ] |
bulach@chromium.org | 6993130 | 2014-05-08 04:16:23 +0900 | [diff] [blame] | 232 | |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 233 | prefix = '' |
| 234 | # Array? |
digit@chromium.org | 9d7eab0 | 2012-12-21 05:33:03 +0900 | [diff] [blame] | 235 | while param[-2:] == '[]': |
| 236 | prefix += '[' |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 237 | param = param[:-2] |
| 238 | # Generic? |
| 239 | if '<' in param: |
| 240 | param = param[:param.index('<')] |
| 241 | if param in pod_param_map: |
| 242 | return prefix + pod_param_map[param] |
| 243 | if '/' in param: |
| 244 | # Coming from javap, use the fully qualified param directly. |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 245 | return prefix + 'L' + param + ';' |
bulach@chromium.org | 4a130cc | 2014-03-28 20:15:06 +0900 | [diff] [blame] | 246 | |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 247 | for qualified_name in (object_param_list + |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 248 | [self._fully_qualified_class] + self._inner_classes): |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 249 | if (qualified_name.endswith('/' + param) or |
| 250 | qualified_name.endswith('$' + param.replace('.', '$')) or |
| 251 | qualified_name == 'L' + param): |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 252 | return prefix + qualified_name + ';' |
bulach@chromium.org | 7156c76 | 2012-11-13 23:35:00 +0900 | [diff] [blame] | 253 | |
| 254 | # Is it from an import? (e.g. referecing Class from import pkg.Class; |
| 255 | # note that referencing an inner class Inner from import pkg.Class.Inner |
| 256 | # is not supported). |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 257 | for qualified_name in self._imports: |
bulach@chromium.org | 7156c76 | 2012-11-13 23:35:00 +0900 | [diff] [blame] | 258 | if qualified_name.endswith('/' + param): |
| 259 | # Ensure it's not an inner class. |
| 260 | components = qualified_name.split('/') |
| 261 | if len(components) > 2 and components[-2][0].isupper(): |
| 262 | raise SyntaxError('Inner class (%s) can not be imported ' |
| 263 | 'and used by JNI (%s). Please import the outer ' |
| 264 | 'class and use Outer.Inner instead.' % |
| 265 | (qualified_name, param)) |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 266 | return prefix + qualified_name + ';' |
bulach@chromium.org | 7156c76 | 2012-11-13 23:35:00 +0900 | [diff] [blame] | 267 | |
| 268 | # Is it an inner class from an outer class import? (e.g. referencing |
| 269 | # Class.Inner from import pkg.Class). |
| 270 | if '.' in param: |
| 271 | components = param.split('.') |
| 272 | outer = '/'.join(components[:-1]) |
| 273 | inner = components[-1] |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 274 | for qualified_name in self._imports: |
bulach@chromium.org | 7156c76 | 2012-11-13 23:35:00 +0900 | [diff] [blame] | 275 | if qualified_name.endswith('/' + outer): |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 276 | return (prefix + qualified_name + '$' + inner + ';') |
bulach@chromium.org | 4a130cc | 2014-03-28 20:15:06 +0900 | [diff] [blame] | 277 | raise SyntaxError('Inner class (%s) can not be ' |
| 278 | 'used directly by JNI. Please import the outer ' |
| 279 | 'class, probably:\n' |
| 280 | 'import %s.%s;' % |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 281 | (param, self._package.replace('/', '.'), |
bulach@chromium.org | 4a130cc | 2014-03-28 20:15:06 +0900 | [diff] [blame] | 282 | outer.replace('/', '.'))) |
bulach@chromium.org | 7156c76 | 2012-11-13 23:35:00 +0900 | [diff] [blame] | 283 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 284 | self._CheckImplicitImports(param) |
bulach@chromium.org | 6993130 | 2014-05-08 04:16:23 +0900 | [diff] [blame] | 285 | |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 286 | # Type not found, falling back to same package as this class. |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 287 | return (prefix + 'L' + self._package + '/' + param + ';') |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 288 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 289 | def _AddAdditionalImport(self, class_name): |
| 290 | assert class_name.endswith('.class') |
| 291 | raw_class_name = class_name[:-len('.class')] |
| 292 | if '.' in raw_class_name: |
| 293 | raise SyntaxError('%s cannot be used in @JNIAdditionalImport. ' |
| 294 | 'Only import unqualified outer classes.' % class_name) |
| 295 | new_import = 'L%s/%s' % (self._package, raw_class_name) |
| 296 | if new_import in self._imports: |
| 297 | raise SyntaxError('Do not use JNIAdditionalImport on an already ' |
| 298 | 'imported class: %s' % (new_import.replace('/', '.'))) |
| 299 | self._imports += [new_import] |
| 300 | |
| 301 | def _CheckImplicitImports(self, param): |
bulach@chromium.org | 6993130 | 2014-05-08 04:16:23 +0900 | [diff] [blame] | 302 | # Ensure implicit imports, such as java.lang.*, are not being treated |
| 303 | # as being in the same package. |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 304 | if not self._implicit_imports: |
bulach@chromium.org | 6993130 | 2014-05-08 04:16:23 +0900 | [diff] [blame] | 305 | # This file was generated from android.jar and lists |
| 306 | # all classes that are implicitly imported. |
| 307 | with file(os.path.join(os.path.dirname(sys.argv[0]), |
| 308 | 'android_jar.classes'), 'r') as f: |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 309 | self._implicit_imports = f.readlines() |
| 310 | for implicit_import in self._implicit_imports: |
bulach@chromium.org | 6993130 | 2014-05-08 04:16:23 +0900 | [diff] [blame] | 311 | implicit_import = implicit_import.strip().replace('.class', '') |
| 312 | implicit_import = implicit_import.replace('/', '.') |
| 313 | if implicit_import.endswith('.' + param): |
| 314 | raise SyntaxError('Ambiguous class (%s) can not be used directly ' |
| 315 | 'by JNI.\nPlease import it, probably:\n\n' |
| 316 | 'import %s;' % |
| 317 | (param, implicit_import)) |
| 318 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 319 | def Signature(self, params, returns, wrap): |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 320 | """Returns the JNI signature for the given datatypes.""" |
| 321 | items = ['('] |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 322 | items += [self.JavaToJni(param.datatype) for param in params] |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 323 | items += [')'] |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 324 | items += [self.JavaToJni(returns)] |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 325 | if wrap: |
| 326 | return '\n' + '\n'.join(['"' + item + '"' for item in items]) |
| 327 | else: |
| 328 | return '"' + ''.join(items) + '"' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 329 | |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 330 | @staticmethod |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 331 | def ParseJavaPSignature(signature_line): |
| 332 | prefix = 'Signature: ' |
| 333 | index = signature_line.find(prefix) |
| 334 | if index == -1: |
| 335 | prefix = 'descriptor: ' |
| 336 | index = signature_line.index(prefix) |
| 337 | return '"%s"' % signature_line[index + len(prefix):] |
| 338 | |
| 339 | @staticmethod |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 340 | def Parse(params): |
| 341 | """Parses the params into a list of Param objects.""" |
| 342 | if not params: |
| 343 | return [] |
| 344 | ret = [] |
| 345 | for p in [p.strip() for p in params.split(',')]: |
| 346 | items = p.split(' ') |
bauerb | b7c5386 | 2016-08-25 22:41:41 +0900 | [diff] [blame] | 347 | |
| 348 | # Remove @Annotations from parameters. |
| 349 | while items[0].startswith('@'): |
| 350 | del items[0] |
| 351 | |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 352 | if 'final' in items: |
| 353 | items.remove('final') |
bauerb | b7c5386 | 2016-08-25 22:41:41 +0900 | [diff] [blame] | 354 | |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 355 | param = Param( |
| 356 | datatype=items[0], |
| 357 | name=(items[1] if len(items) > 1 else 'p%s' % len(ret)), |
| 358 | ) |
| 359 | ret += [param] |
| 360 | return ret |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 361 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 362 | |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 363 | def ExtractJNINamespace(contents): |
| 364 | re_jni_namespace = re.compile('.*?@JNINamespace\("(.*?)"\)') |
| 365 | m = re.findall(re_jni_namespace, contents) |
| 366 | if not m: |
| 367 | return '' |
| 368 | return m[0] |
| 369 | |
| 370 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 371 | def ExtractFullyQualifiedJavaClassName(java_file_name, contents): |
| 372 | re_package = re.compile('.*?package (.*?);') |
| 373 | matches = re.findall(re_package, contents) |
| 374 | if not matches: |
| 375 | raise SyntaxError('Unable to find "package" line in %s' % java_file_name) |
| 376 | return (matches[0].replace('.', '/') + '/' + |
| 377 | os.path.splitext(os.path.basename(java_file_name))[0]) |
| 378 | |
| 379 | |
bulach@chromium.org | a022cf6 | 2013-11-05 09:54:22 +0900 | [diff] [blame] | 380 | def ExtractNatives(contents, ptr_type): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 381 | """Returns a list of dict containing information about a native method.""" |
| 382 | contents = contents.replace('\n', '') |
| 383 | natives = [] |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 384 | re_native = re.compile(r'(@NativeClassQualifiedName' |
bulach@chromium.org | 86eae28 | 2014-05-10 21:15:07 +0900 | [diff] [blame] | 385 | '\(\"(?P<native_class_name>.*?)\"\)\s+)?' |
| 386 | '(@NativeCall(\(\"(?P<java_class_name>.*?)\"\))\s+)?' |
| 387 | '(?P<qualifiers>\w+\s\w+|\w+|\s+)\s*native ' |
| 388 | '(?P<return_type>\S*) ' |
| 389 | '(?P<name>native\w+)\((?P<params>.*?)\);') |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 390 | for match in re.finditer(re_native, contents): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 391 | native = NativeMethod( |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 392 | static='static' in match.group('qualifiers'), |
| 393 | java_class_name=match.group('java_class_name'), |
| 394 | native_class_name=match.group('native_class_name'), |
bulach@chromium.org | a6e185e | 2013-03-26 16:32:39 +0900 | [diff] [blame] | 395 | return_type=match.group('return_type'), |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 396 | name=match.group('name').replace('native', ''), |
bulach@chromium.org | a022cf6 | 2013-11-05 09:54:22 +0900 | [diff] [blame] | 397 | params=JniParams.Parse(match.group('params')), |
| 398 | ptr_type=ptr_type) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 399 | natives += [native] |
| 400 | return natives |
| 401 | |
| 402 | |
estevenson | 2498e5d | 2017-02-01 05:34:56 +0900 | [diff] [blame] | 403 | def IsMainDexJavaClass(contents): |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 404 | """Returns True if the class is annotated with "@MainDex", False if not. |
estevenson | 2498e5d | 2017-02-01 05:34:56 +0900 | [diff] [blame] | 405 | |
| 406 | JNI registration doesn't always need to be completed for non-browser processes |
| 407 | since most Java code is only used by the browser process. Classes that are |
| 408 | needed by non-browser processes must explicitly be annotated with @MainDex |
| 409 | to force JNI registration. |
| 410 | """ |
Jay Civelli | 7645519 | 2017-06-10 06:57:19 +0900 | [diff] [blame] | 411 | re_maindex = re.compile(r'@MainDex[\s\S]*class({|[\s\S]*{)') |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 412 | return bool(re.search(re_maindex, contents)) |
| 413 | |
| 414 | |
| 415 | def GetBinaryClassName(fully_qualified_class): |
| 416 | """Returns a string concatenating the Java package and class.""" |
| 417 | return fully_qualified_class.replace('_', '_1').replace('/', '_') |
| 418 | |
| 419 | |
| 420 | def GetRegistrationFunctionName(fully_qualified_class): |
| 421 | """Returns the register name with a given class.""" |
| 422 | return 'RegisterNative_' + GetBinaryClassName(fully_qualified_class) |
estevenson | 2498e5d | 2017-02-01 05:34:56 +0900 | [diff] [blame] | 423 | |
| 424 | |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 425 | def GetStaticCastForReturnType(return_type): |
digit@chromium.org | 9d7eab0 | 2012-12-21 05:33:03 +0900 | [diff] [blame] | 426 | type_map = { 'String' : 'jstring', |
| 427 | 'java/lang/String' : 'jstring', |
torne | c912423 | 2015-08-27 23:57:06 +0900 | [diff] [blame] | 428 | 'Throwable': 'jthrowable', |
| 429 | 'java/lang/Throwable': 'jthrowable', |
digit@chromium.org | 9d7eab0 | 2012-12-21 05:33:03 +0900 | [diff] [blame] | 430 | 'boolean[]': 'jbooleanArray', |
| 431 | 'byte[]': 'jbyteArray', |
| 432 | 'char[]': 'jcharArray', |
| 433 | 'short[]': 'jshortArray', |
| 434 | 'int[]': 'jintArray', |
| 435 | 'long[]': 'jlongArray', |
skhatri@nvidia.com | f5bf8c4 | 2014-06-02 21:23:12 +0900 | [diff] [blame] | 436 | 'float[]': 'jfloatArray', |
digit@chromium.org | 9d7eab0 | 2012-12-21 05:33:03 +0900 | [diff] [blame] | 437 | 'double[]': 'jdoubleArray' } |
| 438 | ret = type_map.get(return_type, None) |
| 439 | if ret: |
| 440 | return ret |
| 441 | if return_type.endswith('[]'): |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 442 | return 'jobjectArray' |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 443 | return None |
| 444 | |
| 445 | |
| 446 | def GetEnvCall(is_constructor, is_static, return_type): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 447 | """Maps the types availabe via env->Call__Method.""" |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 448 | if is_constructor: |
| 449 | return 'NewObject' |
| 450 | env_call_map = {'boolean': 'Boolean', |
| 451 | 'byte': 'Byte', |
| 452 | 'char': 'Char', |
| 453 | 'short': 'Short', |
| 454 | 'int': 'Int', |
| 455 | 'long': 'Long', |
| 456 | 'float': 'Float', |
| 457 | 'void': 'Void', |
| 458 | 'double': 'Double', |
| 459 | 'Object': 'Object', |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 460 | } |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 461 | call = env_call_map.get(return_type, 'Object') |
| 462 | if is_static: |
| 463 | call = 'Static' + call |
| 464 | return 'Call' + call + 'Method' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 465 | |
| 466 | |
bulach@chromium.org | 0c6805b | 2012-09-28 21:34:33 +0900 | [diff] [blame] | 467 | def GetMangledParam(datatype): |
| 468 | """Returns a mangled identifier for the datatype.""" |
| 469 | if len(datatype) <= 2: |
| 470 | return datatype.replace('[', 'A') |
| 471 | ret = '' |
| 472 | for i in range(1, len(datatype)): |
| 473 | c = datatype[i] |
| 474 | if c == '[': |
| 475 | ret += 'A' |
| 476 | elif c.isupper() or datatype[i - 1] in ['/', 'L']: |
| 477 | ret += c.upper() |
| 478 | return ret |
| 479 | |
| 480 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 481 | def GetMangledMethodName(jni_params, name, params, return_type): |
bulach@chromium.org | 0c6805b | 2012-09-28 21:34:33 +0900 | [diff] [blame] | 482 | """Returns a mangled method name for the given signature. |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 483 | |
| 484 | The returned name can be used as a C identifier and will be unique for all |
| 485 | valid overloads of the same method. |
| 486 | |
| 487 | Args: |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 488 | jni_params: JniParams object. |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 489 | name: string. |
bulach@chromium.org | 0c6805b | 2012-09-28 21:34:33 +0900 | [diff] [blame] | 490 | params: list of Param. |
| 491 | return_type: string. |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 492 | |
| 493 | Returns: |
| 494 | A mangled name. |
| 495 | """ |
bulach@chromium.org | 0c6805b | 2012-09-28 21:34:33 +0900 | [diff] [blame] | 496 | mangled_items = [] |
| 497 | for datatype in [return_type] + [x.datatype for x in params]: |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 498 | mangled_items += [GetMangledParam(jni_params.JavaToJni(datatype))] |
bulach@chromium.org | 0c6805b | 2012-09-28 21:34:33 +0900 | [diff] [blame] | 499 | mangled_name = name + '_'.join(mangled_items) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 500 | assert re.match(r'[0-9a-zA-Z_]+', mangled_name) |
| 501 | return mangled_name |
| 502 | |
| 503 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 504 | def MangleCalledByNatives(jni_params, called_by_natives): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 505 | """Mangles all the overloads from the call_by_natives list.""" |
| 506 | method_counts = collections.defaultdict( |
| 507 | lambda: collections.defaultdict(lambda: 0)) |
| 508 | for called_by_native in called_by_natives: |
| 509 | java_class_name = called_by_native.java_class_name |
| 510 | name = called_by_native.name |
| 511 | method_counts[java_class_name][name] += 1 |
| 512 | for called_by_native in called_by_natives: |
| 513 | java_class_name = called_by_native.java_class_name |
| 514 | method_name = called_by_native.name |
| 515 | method_id_var_name = method_name |
| 516 | if method_counts[java_class_name][method_name] > 1: |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 517 | method_id_var_name = GetMangledMethodName(jni_params, method_name, |
bulach@chromium.org | 0c6805b | 2012-09-28 21:34:33 +0900 | [diff] [blame] | 518 | called_by_native.params, |
| 519 | called_by_native.return_type) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 520 | called_by_native.method_id_var_name = method_id_var_name |
| 521 | return called_by_natives |
| 522 | |
| 523 | |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 524 | # Regex to match the JNI types that should be wrapped in a JavaRef. |
| 525 | RE_SCOPED_JNI_TYPES = re.compile('jobject|jclass|jstring|jthrowable|.*Array') |
| 526 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 527 | |
| 528 | # Regex to match a string like "@CalledByNative public void foo(int bar)". |
| 529 | RE_CALLED_BY_NATIVE = re.compile( |
| 530 | '@CalledByNative(?P<Unchecked>(Unchecked)*?)(?:\("(?P<annotation>.*)"\))?' |
| 531 | '\s+(?P<prefix>[\w ]*?)' |
estevenson | 0621dbe | 2016-10-01 05:52:49 +0900 | [diff] [blame] | 532 | '(:?\s*@\w+)?' # Ignore annotations in return types. |
bulach@chromium.org | a6e185e | 2013-03-26 16:32:39 +0900 | [diff] [blame] | 533 | '\s*(?P<return_type>\S+?)' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 534 | '\s+(?P<name>\w+)' |
| 535 | '\s*\((?P<params>[^\)]*)\)') |
| 536 | |
| 537 | |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 538 | # Removes empty lines that are indented (i.e. start with 2x spaces). |
| 539 | def RemoveIndentedEmptyLines(string): |
| 540 | return re.sub('^(?: {2})+$\n', '', string, flags=re.MULTILINE) |
| 541 | |
| 542 | |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 543 | def ExtractCalledByNatives(jni_params, contents): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 544 | """Parses all methods annotated with @CalledByNative. |
| 545 | |
| 546 | Args: |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 547 | jni_params: JniParams object. |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 548 | contents: the contents of the java file. |
| 549 | |
| 550 | Returns: |
| 551 | A list of dict with information about the annotated methods. |
| 552 | TODO(bulach): return a CalledByNative object. |
| 553 | |
| 554 | Raises: |
| 555 | ParseError: if unable to parse. |
| 556 | """ |
| 557 | called_by_natives = [] |
| 558 | for match in re.finditer(RE_CALLED_BY_NATIVE, contents): |
| 559 | called_by_natives += [CalledByNative( |
| 560 | system_class=False, |
| 561 | unchecked='Unchecked' in match.group('Unchecked'), |
| 562 | static='static' in match.group('prefix'), |
| 563 | java_class_name=match.group('annotation') or '', |
| 564 | return_type=match.group('return_type'), |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 565 | name=match.group('name'), |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 566 | params=JniParams.Parse(match.group('params')))] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 567 | # Check for any @CalledByNative occurrences that weren't matched. |
| 568 | unmatched_lines = re.sub(RE_CALLED_BY_NATIVE, '', contents).split('\n') |
| 569 | for line1, line2 in zip(unmatched_lines, unmatched_lines[1:]): |
| 570 | if '@CalledByNative' in line1: |
| 571 | raise ParseError('could not parse @CalledByNative method signature', |
| 572 | line1, line2) |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 573 | return MangleCalledByNatives(jni_params, called_by_natives) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 574 | |
| 575 | |
| 576 | class JNIFromJavaP(object): |
| 577 | """Uses 'javap' to parse a .class file and generate the JNI header file.""" |
| 578 | |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 579 | def __init__(self, contents, options): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 580 | self.contents = contents |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 581 | self.namespace = options.namespace |
bulach@chromium.org | 07a9ba4 | 2014-02-28 03:23:11 +0900 | [diff] [blame] | 582 | for line in contents: |
| 583 | class_name = re.match( |
| 584 | '.*?(public).*?(class|interface) (?P<class_name>\S+?)( |\Z)', |
| 585 | line) |
| 586 | if class_name: |
| 587 | self.fully_qualified_class = class_name.group('class_name') |
| 588 | break |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 589 | self.fully_qualified_class = self.fully_qualified_class.replace('.', '/') |
simonb@opera.com | 521714e | 2013-09-04 06:22:48 +0900 | [diff] [blame] | 590 | # Java 7's javap includes type parameters in output, like HashSet<T>. Strip |
| 591 | # away the <...> and use the raw class name that Java 6 would've given us. |
| 592 | self.fully_qualified_class = self.fully_qualified_class.split('<', 1)[0] |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 593 | self.jni_params = JniParams(self.fully_qualified_class) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 594 | self.java_class_name = self.fully_qualified_class.split('/')[-1] |
| 595 | if not self.namespace: |
| 596 | self.namespace = 'JNI_' + self.java_class_name |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 597 | re_method = re.compile('(?P<prefix>.*?)(?P<return_type>\S+?) (?P<name>\w+?)' |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 598 | '\((?P<params>.*?)\)') |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 599 | self.called_by_natives = [] |
bulach@chromium.org | 37fc911 | 2013-10-26 01:27:03 +0900 | [diff] [blame] | 600 | for lineno, content in enumerate(contents[2:], 2): |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 601 | match = re.match(re_method, content) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 602 | if not match: |
| 603 | continue |
| 604 | self.called_by_natives += [CalledByNative( |
| 605 | system_class=True, |
| 606 | unchecked=False, |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 607 | static='static' in match.group('prefix'), |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 608 | java_class_name='', |
bulach@chromium.org | 0bafda7 | 2012-11-08 04:06:51 +0900 | [diff] [blame] | 609 | return_type=match.group('return_type').replace('.', '/'), |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 610 | name=match.group('name'), |
bulach@chromium.org | 37fc911 | 2013-10-26 01:27:03 +0900 | [diff] [blame] | 611 | params=JniParams.Parse(match.group('params').replace('.', '/')), |
| 612 | signature=JniParams.ParseJavaPSignature(contents[lineno + 1]))] |
| 613 | re_constructor = re.compile('(.*?)public ' + |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 614 | self.fully_qualified_class.replace('/', '.') + |
| 615 | '\((?P<params>.*?)\)') |
bulach@chromium.org | 37fc911 | 2013-10-26 01:27:03 +0900 | [diff] [blame] | 616 | for lineno, content in enumerate(contents[2:], 2): |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 617 | match = re.match(re_constructor, content) |
| 618 | if not match: |
| 619 | continue |
| 620 | self.called_by_natives += [CalledByNative( |
bulach@chromium.org | 208f05b | 2012-10-03 06:13:55 +0900 | [diff] [blame] | 621 | system_class=True, |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 622 | unchecked=False, |
| 623 | static=False, |
| 624 | java_class_name='', |
| 625 | return_type=self.fully_qualified_class, |
| 626 | name='Constructor', |
bulach@chromium.org | c6b7584 | 2012-11-01 07:58:15 +0900 | [diff] [blame] | 627 | params=JniParams.Parse(match.group('params').replace('.', '/')), |
bulach@chromium.org | 37fc911 | 2013-10-26 01:27:03 +0900 | [diff] [blame] | 628 | signature=JniParams.ParseJavaPSignature(contents[lineno + 1]), |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 629 | is_constructor=True)] |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 630 | self.called_by_natives = MangleCalledByNatives(self.jni_params, |
| 631 | self.called_by_natives) |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 632 | self.constant_fields = [] |
bulach@chromium.org | 07a9ba4 | 2014-02-28 03:23:11 +0900 | [diff] [blame] | 633 | re_constant_field = re.compile('.*?public static final int (?P<name>.*?);') |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 634 | re_constant_field_value = re.compile( |
bulach@chromium.org | 07a9ba4 | 2014-02-28 03:23:11 +0900 | [diff] [blame] | 635 | '.*?Constant(Value| value): int (?P<value>(-*[0-9]+)?)') |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 636 | for lineno, content in enumerate(contents[2:], 2): |
| 637 | match = re.match(re_constant_field, content) |
| 638 | if not match: |
| 639 | continue |
| 640 | value = re.match(re_constant_field_value, contents[lineno + 2]) |
bulach@chromium.org | 07a9ba4 | 2014-02-28 03:23:11 +0900 | [diff] [blame] | 641 | if not value: |
| 642 | value = re.match(re_constant_field_value, contents[lineno + 3]) |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 643 | if value: |
| 644 | self.constant_fields.append( |
| 645 | ConstantField(name=match.group('name'), |
| 646 | value=value.group('value'))) |
| 647 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 648 | self.inl_header_file_generator = InlHeaderFileGenerator( |
estevenson | 2498e5d | 2017-02-01 05:34:56 +0900 | [diff] [blame] | 649 | self.namespace, self.fully_qualified_class, [], self.called_by_natives, |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 650 | self.constant_fields, self.jni_params, options) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 651 | |
| 652 | def GetContent(self): |
| 653 | return self.inl_header_file_generator.GetContent() |
| 654 | |
| 655 | @staticmethod |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 656 | def CreateFromClass(class_file, options): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 657 | class_name = os.path.splitext(os.path.basename(class_file))[0] |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 658 | p = subprocess.Popen(args=[options.javap, '-c', '-verbose', |
| 659 | '-s', class_name], |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 660 | cwd=os.path.dirname(class_file), |
| 661 | stdout=subprocess.PIPE, |
| 662 | stderr=subprocess.PIPE) |
| 663 | stdout, _ = p.communicate() |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 664 | jni_from_javap = JNIFromJavaP(stdout.split('\n'), options) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 665 | return jni_from_javap |
| 666 | |
| 667 | |
| 668 | class JNIFromJavaSource(object): |
| 669 | """Uses the given java source file to generate the JNI header file.""" |
| 670 | |
torne@chromium.org | 202d267 | 2014-04-15 02:29:59 +0900 | [diff] [blame] | 671 | # Match single line comments, multiline comments, character literals, and |
| 672 | # double-quoted strings. |
| 673 | _comment_remover_regex = re.compile( |
| 674 | r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', |
| 675 | re.DOTALL | re.MULTILINE) |
| 676 | |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 677 | def __init__(self, contents, fully_qualified_class, options): |
torne@chromium.org | 202d267 | 2014-04-15 02:29:59 +0900 | [diff] [blame] | 678 | contents = self._RemoveComments(contents) |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 679 | self.jni_params = JniParams(fully_qualified_class) |
| 680 | self.jni_params.ExtractImportsAndInnerClasses(contents) |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 681 | jni_namespace = ExtractJNINamespace(contents) or options.namespace |
bulach@chromium.org | a022cf6 | 2013-11-05 09:54:22 +0900 | [diff] [blame] | 682 | natives = ExtractNatives(contents, options.ptr_type) |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 683 | called_by_natives = ExtractCalledByNatives(self.jni_params, contents) |
bulach@chromium.org | e2530d0 | 2012-07-03 01:23:35 +0900 | [diff] [blame] | 684 | if len(natives) == 0 and len(called_by_natives) == 0: |
| 685 | raise SyntaxError('Unable to find any JNI methods for %s.' % |
| 686 | fully_qualified_class) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 687 | inl_header_file_generator = InlHeaderFileGenerator( |
estevenson | 2498e5d | 2017-02-01 05:34:56 +0900 | [diff] [blame] | 688 | jni_namespace, fully_qualified_class, natives, called_by_natives, [], |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 689 | self.jni_params, options) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 690 | self.content = inl_header_file_generator.GetContent() |
| 691 | |
torne@chromium.org | 202d267 | 2014-04-15 02:29:59 +0900 | [diff] [blame] | 692 | @classmethod |
| 693 | def _RemoveComments(cls, contents): |
husky@chromium.org | 8c438d7 | 2012-07-31 02:27:48 +0900 | [diff] [blame] | 694 | # We need to support both inline and block comments, and we need to handle |
torne@chromium.org | 202d267 | 2014-04-15 02:29:59 +0900 | [diff] [blame] | 695 | # strings that contain '//' or '/*'. |
| 696 | # TODO(bulach): This is a bit hacky. It would be cleaner to use a real Java |
husky@chromium.org | 8c438d7 | 2012-07-31 02:27:48 +0900 | [diff] [blame] | 697 | # parser. Maybe we could ditch JNIFromJavaSource and just always use |
| 698 | # JNIFromJavaP; or maybe we could rewrite this script in Java and use APT. |
| 699 | # http://code.google.com/p/chromium/issues/detail?id=138941 |
torne@chromium.org | 202d267 | 2014-04-15 02:29:59 +0900 | [diff] [blame] | 700 | def replacer(match): |
| 701 | # Replace matches that are comments with nothing; return literals/strings |
| 702 | # unchanged. |
| 703 | s = match.group(0) |
| 704 | if s.startswith('/'): |
| 705 | return '' |
| 706 | else: |
| 707 | return s |
| 708 | return cls._comment_remover_regex.sub(replacer, contents) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 709 | |
| 710 | def GetContent(self): |
| 711 | return self.content |
| 712 | |
| 713 | @staticmethod |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 714 | def CreateFromFile(java_file_name, options): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 715 | contents = file(java_file_name).read() |
| 716 | fully_qualified_class = ExtractFullyQualifiedJavaClassName(java_file_name, |
| 717 | contents) |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 718 | return JNIFromJavaSource(contents, fully_qualified_class, options) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 719 | |
| 720 | |
| 721 | class InlHeaderFileGenerator(object): |
| 722 | """Generates an inline header file for JNI integration.""" |
| 723 | |
| 724 | def __init__(self, namespace, fully_qualified_class, natives, |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 725 | called_by_natives, constant_fields, jni_params, options): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 726 | self.namespace = namespace |
| 727 | self.fully_qualified_class = fully_qualified_class |
| 728 | self.class_name = self.fully_qualified_class.split('/')[-1] |
| 729 | self.natives = natives |
| 730 | self.called_by_natives = called_by_natives |
| 731 | self.header_guard = fully_qualified_class.replace('/', '_') + '_JNI' |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 732 | self.constant_fields = constant_fields |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 733 | self.jni_params = jni_params |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 734 | self.options = options |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 735 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 736 | |
| 737 | def GetContent(self): |
| 738 | """Returns the content of the JNI binding file.""" |
| 739 | template = Template("""\ |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 740 | // Copyright 2014 The Chromium Authors. All rights reserved. |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 741 | // Use of this source code is governed by a BSD-style license that can be |
| 742 | // found in the LICENSE file. |
| 743 | |
| 744 | |
| 745 | // This file is autogenerated by |
| 746 | // ${SCRIPT_NAME} |
| 747 | // For |
| 748 | // ${FULLY_QUALIFIED_CLASS} |
| 749 | |
| 750 | #ifndef ${HEADER_GUARD} |
| 751 | #define ${HEADER_GUARD} |
| 752 | |
| 753 | #include <jni.h> |
| 754 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 755 | ${INCLUDES} |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 756 | |
anton@chromium.org | 9d3b13b | 2014-05-07 23:14:10 +0900 | [diff] [blame] | 757 | #include "base/android/jni_int_wrapper.h" |
| 758 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 759 | // Step 1: forward declarations. |
| 760 | namespace { |
| 761 | $CLASS_PATH_DEFINITIONS |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 762 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 763 | } // namespace |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 764 | |
| 765 | $OPEN_NAMESPACE |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 766 | |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 767 | $CONSTANT_FIELDS |
| 768 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 769 | // Step 2: method stubs. |
| 770 | $METHOD_STUBS |
| 771 | |
bulach@chromium.org | 231cff6 | 2012-10-17 03:35:10 +0900 | [diff] [blame] | 772 | // Step 3: RegisterNatives. |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 773 | $JNI_NATIVE_METHODS |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 774 | $REGISTER_NATIVES_EMPTY |
Yipeng Wang | 97484ae | 2017-07-01 00:27:54 +0900 | [diff] [blame] | 775 | $CLOSE_NAMESPACE |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 776 | $REGISTER_NATIVES |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 777 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 778 | #endif // ${HEADER_GUARD} |
| 779 | """) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 780 | values = { |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 781 | 'SCRIPT_NAME': self.options.script_name, |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 782 | 'FULLY_QUALIFIED_CLASS': self.fully_qualified_class, |
| 783 | 'CLASS_PATH_DEFINITIONS': self.GetClassPathDefinitionsString(), |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 784 | 'CONSTANT_FIELDS': self.GetConstantFieldsString(), |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 785 | 'METHOD_STUBS': self.GetMethodStubsString(), |
| 786 | 'OPEN_NAMESPACE': self.GetOpenNamespaceString(), |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 787 | 'JNI_NATIVE_METHODS': self.GetJNINativeMethodsString(), |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 788 | 'REGISTER_NATIVES_EMPTY': self.GetOriginalRegisterNativesString(), |
Yipeng Wang | 97484ae | 2017-07-01 00:27:54 +0900 | [diff] [blame] | 789 | 'CLOSE_NAMESPACE': self.GetCloseNamespaceString(), |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 790 | 'REGISTER_NATIVES': self.GetRegisterNativesString(), |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 791 | 'HEADER_GUARD': self.header_guard, |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 792 | 'INCLUDES': self.GetIncludesString(), |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 793 | } |
sievers | 2c26d4b | 2016-08-04 08:20:40 +0900 | [diff] [blame] | 794 | assert ((values['JNI_NATIVE_METHODS'] == '') == |
| 795 | (values['REGISTER_NATIVES'] == '')) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 796 | return WrapOutput(template.substitute(values)) |
| 797 | |
| 798 | def GetClassPathDefinitionsString(self): |
| 799 | ret = [] |
| 800 | ret += [self.GetClassPathDefinitions()] |
| 801 | return '\n'.join(ret) |
| 802 | |
bulach@chromium.org | 658dbc0 | 2014-02-27 04:59:00 +0900 | [diff] [blame] | 803 | def GetConstantFieldsString(self): |
| 804 | if not self.constant_fields: |
| 805 | return '' |
| 806 | ret = ['enum Java_%s_constant_fields {' % self.class_name] |
| 807 | for c in self.constant_fields: |
| 808 | ret += [' %s = %s,' % (c.name, c.value)] |
| 809 | ret += ['};'] |
| 810 | return '\n'.join(ret) |
| 811 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 812 | def GetMethodStubsString(self): |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 813 | """Returns the code corresponding to method stubs.""" |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 814 | ret = [] |
| 815 | for native in self.natives: |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 816 | ret += [self.GetNativeStub(native)] |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 817 | ret += self.GetLazyCalledByNativeMethodStubs() |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 818 | return '\n'.join(ret) |
| 819 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 820 | def GetLazyCalledByNativeMethodStubs(self): |
| 821 | return [self.GetLazyCalledByNativeMethodStub(called_by_native) |
| 822 | for called_by_native in self.called_by_natives] |
| 823 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 824 | def GetIncludesString(self): |
| 825 | if not self.options.includes: |
| 826 | return '' |
| 827 | includes = self.options.includes.split(',') |
| 828 | return '\n'.join('#include "%s"' % x for x in includes) |
| 829 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 830 | def GetKMethodsString(self, clazz): |
| 831 | ret = [] |
| 832 | for native in self.natives: |
| 833 | if (native.java_class_name == clazz or |
| 834 | (not native.java_class_name and clazz == self.class_name)): |
| 835 | ret += [self.GetKMethodArrayEntry(native)] |
| 836 | return '\n'.join(ret) |
| 837 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 838 | def SubstituteNativeMethods(self, template): |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 839 | """Substitutes NAMESPACE, JAVA_CLASS and KMETHODS in the provided |
| 840 | template.""" |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 841 | ret = [] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 842 | all_classes = self.GetUniqueClasses(self.natives) |
| 843 | all_classes[self.class_name] = self.fully_qualified_class |
| 844 | for clazz in all_classes: |
| 845 | kmethods = self.GetKMethodsString(clazz) |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 846 | namespace_str = '' |
| 847 | if self.namespace: |
| 848 | namespace_str = self.namespace + '::' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 849 | if kmethods: |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 850 | values = {'NAMESPACE': namespace_str, |
| 851 | 'JAVA_CLASS': clazz, |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 852 | 'KMETHODS': kmethods} |
| 853 | ret += [template.substitute(values)] |
| 854 | if not ret: return '' |
| 855 | return '\n' + '\n'.join(ret) |
| 856 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 857 | def GetJNINativeMethodsString(self): |
| 858 | """Returns the implementation of the array of native methods.""" |
torne | fc32b00 | 2016-07-21 19:41:58 +0900 | [diff] [blame] | 859 | if not self.options.native_exports_optional: |
mkosiba@chromium.org | 56092f0 | 2014-08-11 20:27:12 +0900 | [diff] [blame] | 860 | return '' |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 861 | template = Template("""\ |
| 862 | static const JNINativeMethod kMethods${JAVA_CLASS}[] = { |
| 863 | ${KMETHODS} |
| 864 | }; |
| 865 | """) |
| 866 | return self.SubstituteNativeMethods(template) |
| 867 | |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 868 | # TODO(agrieve): Remove this function when deleting original registers. |
| 869 | # https://crbug.com/683256. |
| 870 | def GetOriginalRegisterNativesString(self): |
| 871 | """Return the code for original RegisterNatives""" |
| 872 | natives = self.GetRegisterNativesImplString() |
| 873 | if not natives: |
| 874 | return '' |
| 875 | |
| 876 | return """ |
| 877 | // TODO(agrieve): Remove these empty registration functions and functions |
| 878 | // calling them. https://crbug.com/683256. |
| 879 | static bool RegisterNativesImpl(JNIEnv* env) { |
| 880 | return true; |
| 881 | } |
| 882 | """ |
| 883 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 884 | def GetRegisterNativesString(self): |
| 885 | """Returns the code for RegisterNatives.""" |
sievers | 2c26d4b | 2016-08-04 08:20:40 +0900 | [diff] [blame] | 886 | natives = self.GetRegisterNativesImplString() |
| 887 | if not natives: |
| 888 | return '' |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 889 | template = Template("""\ |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 890 | JNI_REGISTRATION_EXPORT bool ${REGISTER_NAME}(JNIEnv* env) { |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 891 | ${NATIVES} |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 892 | return true; |
| 893 | } |
| 894 | """) |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 895 | values = { |
| 896 | 'REGISTER_NAME': |
| 897 | GetRegistrationFunctionName(self.fully_qualified_class), |
| 898 | 'NATIVES': natives |
| 899 | } |
sievers | 2c26d4b | 2016-08-04 08:20:40 +0900 | [diff] [blame] | 900 | return template.substitute(values) |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 901 | |
| 902 | def GetRegisterNativesImplString(self): |
| 903 | """Returns the shared implementation for RegisterNatives.""" |
torne | fc32b00 | 2016-07-21 19:41:58 +0900 | [diff] [blame] | 904 | if not self.options.native_exports_optional: |
mkosiba@chromium.org | 56092f0 | 2014-08-11 20:27:12 +0900 | [diff] [blame] | 905 | return '' |
| 906 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 907 | template = Template("""\ |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 908 | const int kMethods${JAVA_CLASS}Size = |
| 909 | arraysize(${NAMESPACE}kMethods${JAVA_CLASS}); |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 910 | |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 911 | if (env->RegisterNatives(${JAVA_CLASS}_clazz(env), |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 912 | ${NAMESPACE}kMethods${JAVA_CLASS}, |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 913 | kMethods${JAVA_CLASS}Size) < 0) { |
| 914 | jni_generator::HandleRegistrationError( |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 915 | env, ${JAVA_CLASS}_clazz(env), __FILE__); |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 916 | return false; |
| 917 | } |
| 918 | """) |
| 919 | return self.SubstituteNativeMethods(template) |
| 920 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 921 | def GetOpenNamespaceString(self): |
| 922 | if self.namespace: |
bulach@chromium.org | 16dde20 | 2012-07-24 00:31:35 +0900 | [diff] [blame] | 923 | all_namespaces = ['namespace %s {' % ns |
| 924 | for ns in self.namespace.split('::')] |
| 925 | return '\n'.join(all_namespaces) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 926 | return '' |
| 927 | |
| 928 | def GetCloseNamespaceString(self): |
| 929 | if self.namespace: |
bulach@chromium.org | 16dde20 | 2012-07-24 00:31:35 +0900 | [diff] [blame] | 930 | all_namespaces = ['} // namespace %s' % ns |
| 931 | for ns in self.namespace.split('::')] |
| 932 | all_namespaces.reverse() |
| 933 | return '\n'.join(all_namespaces) + '\n' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 934 | return '' |
| 935 | |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 936 | def GetJNIFirstParamType(self, native): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 937 | if native.type == 'method': |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 938 | return 'jobject' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 939 | elif native.type == 'function': |
| 940 | if native.static: |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 941 | return 'jclass' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 942 | else: |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 943 | return 'jobject' |
| 944 | |
| 945 | def GetJNIFirstParam(self, native, for_declaration): |
| 946 | c_type = self.GetJNIFirstParamType(native) |
| 947 | if for_declaration: |
| 948 | c_type = WrapCTypeForDeclaration(c_type) |
| 949 | return [c_type + ' jcaller'] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 950 | |
| 951 | def GetParamsInDeclaration(self, native): |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 952 | """Returns the params for the forward declaration. |
| 953 | |
| 954 | Args: |
| 955 | native: the native dictionary describing the method. |
| 956 | |
| 957 | Returns: |
| 958 | A string containing the params. |
| 959 | """ |
| 960 | return ',\n '.join(self.GetJNIFirstParam(native, True) + |
| 961 | [JavaDataTypeToCForDeclaration(param.datatype) + ' ' + |
| 962 | param.name |
| 963 | for param in native.params]) |
| 964 | |
| 965 | def GetParamsInStub(self, native): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 966 | """Returns the params for the stub declaration. |
| 967 | |
| 968 | Args: |
| 969 | native: the native dictionary describing the method. |
| 970 | |
| 971 | Returns: |
| 972 | A string containing the params. |
| 973 | """ |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 974 | return ',\n '.join(self.GetJNIFirstParam(native, False) + |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 975 | [JavaDataTypeToC(param.datatype) + ' ' + |
| 976 | param.name |
| 977 | for param in native.params]) |
| 978 | |
| 979 | def GetCalledByNativeParamsInDeclaration(self, called_by_native): |
anton@chromium.org | 9d3b13b | 2014-05-07 23:14:10 +0900 | [diff] [blame] | 980 | return ',\n '.join([ |
| 981 | JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' + |
| 982 | param.name |
| 983 | for param in called_by_native.params]) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 984 | |
torne | 94bda72 | 2015-02-20 21:37:52 +0900 | [diff] [blame] | 985 | def GetStubName(self, native): |
| 986 | """Return the name of the stub function for this native method. |
| 987 | |
| 988 | Args: |
| 989 | native: the native dictionary describing the method. |
| 990 | |
| 991 | Returns: |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 992 | A string with the stub function name (used by the JVM). |
torne | 94bda72 | 2015-02-20 21:37:52 +0900 | [diff] [blame] | 993 | """ |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 994 | template = Template("Java_${JAVA_NAME}_native${NAME}") |
torne | 94bda72 | 2015-02-20 21:37:52 +0900 | [diff] [blame] | 995 | |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 996 | java_name = GetBinaryClassName(self.fully_qualified_class) |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 997 | if native.java_class_name: |
| 998 | java_name += '_00024' + native.java_class_name |
torne | 94bda72 | 2015-02-20 21:37:52 +0900 | [diff] [blame] | 999 | |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1000 | values = {'NAME': native.name, |
| 1001 | 'JAVA_NAME': java_name} |
| 1002 | return template.substitute(values) |
| 1003 | |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 1004 | def GetJavaParamRefForCall(self, c_type, name): |
torne | 9854c72 | 2016-08-05 00:44:06 +0900 | [diff] [blame] | 1005 | return Template( |
| 1006 | 'base::android::JavaParamRef<${TYPE}>(env, ${NAME})').substitute({ |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 1007 | 'TYPE': c_type, |
| 1008 | 'NAME': name, |
| 1009 | }) |
| 1010 | |
| 1011 | def GetJNIFirstParamForCall(self, native): |
| 1012 | c_type = self.GetJNIFirstParamType(native) |
| 1013 | return [self.GetJavaParamRefForCall(c_type, 'jcaller')] |
| 1014 | |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1015 | def GetNativeStub(self, native): |
| 1016 | is_method = native.type == 'method' |
| 1017 | |
| 1018 | if is_method: |
| 1019 | params = native.params[1:] |
torne | 94bda72 | 2015-02-20 21:37:52 +0900 | [diff] [blame] | 1020 | else: |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1021 | params = native.params |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 1022 | params_in_call = ['env'] + self.GetJNIFirstParamForCall(native) |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 1023 | for p in params: |
| 1024 | c_type = JavaDataTypeToC(p.datatype) |
| 1025 | if re.match(RE_SCOPED_JNI_TYPES, c_type): |
| 1026 | params_in_call.append(self.GetJavaParamRefForCall(c_type, p.name)) |
| 1027 | else: |
| 1028 | params_in_call.append(p.name) |
| 1029 | params_in_call = ', '.join(params_in_call) |
mkosiba@chromium.org | 56092f0 | 2014-08-11 20:27:12 +0900 | [diff] [blame] | 1030 | |
torne | 4149526 | 2015-08-26 23:07:32 +0900 | [diff] [blame] | 1031 | return_type = return_declaration = JavaDataTypeToC(native.return_type) |
| 1032 | post_call = '' |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 1033 | if re.match(RE_SCOPED_JNI_TYPES, return_type): |
torne | 4149526 | 2015-08-26 23:07:32 +0900 | [diff] [blame] | 1034 | post_call = '.Release()' |
torne | 9854c72 | 2016-08-05 00:44:06 +0900 | [diff] [blame] | 1035 | return_declaration = ('base::android::ScopedJavaLocalRef<' + return_type + |
| 1036 | '>') |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1037 | profiling_entered_native = '' |
| 1038 | if self.options.enable_profiling: |
| 1039 | profiling_entered_native = 'JNI_LINK_SAVED_FRAME_POINTER;' |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1040 | values = { |
| 1041 | 'RETURN': return_type, |
torne | 4149526 | 2015-08-26 23:07:32 +0900 | [diff] [blame] | 1042 | 'RETURN_DECLARATION': return_declaration, |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1043 | 'NAME': native.name, |
| 1044 | 'PARAMS': self.GetParamsInDeclaration(native), |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 1045 | 'PARAMS_IN_STUB': self.GetParamsInStub(native), |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1046 | 'PARAMS_IN_CALL': params_in_call, |
torne | 4149526 | 2015-08-26 23:07:32 +0900 | [diff] [blame] | 1047 | 'POST_CALL': post_call, |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1048 | 'STUB_NAME': self.GetStubName(native), |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1049 | 'PROFILING_ENTERED_NATIVE': profiling_entered_native, |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | if is_method: |
| 1053 | optional_error_return = JavaReturnValueToC(native.return_type) |
| 1054 | if optional_error_return: |
| 1055 | optional_error_return = ', ' + optional_error_return |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1056 | values.update({ |
| 1057 | 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| 1058 | 'PARAM0_NAME': native.params[0].name, |
| 1059 | 'P0_TYPE': native.p0_type, |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1060 | }) |
| 1061 | template = Template("""\ |
torne | 17bc964 | 2016-12-02 02:11:54 +0900 | [diff] [blame] | 1062 | JNI_GENERATOR_EXPORT ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_STUB}) { |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1063 | ${PROFILING_ENTERED_NATIVE} |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1064 | ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1065 | CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); |
| 1066 | return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1067 | } |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1068 | """) |
| 1069 | else: |
| 1070 | template = Template(""" |
torne | 4149526 | 2015-08-26 23:07:32 +0900 | [diff] [blame] | 1071 | static ${RETURN_DECLARATION} ${NAME}(JNIEnv* env, ${PARAMS}); |
mkosiba@chromium.org | 56092f0 | 2014-08-11 20:27:12 +0900 | [diff] [blame] | 1072 | |
torne | 17bc964 | 2016-12-02 02:11:54 +0900 | [diff] [blame] | 1073 | JNI_GENERATOR_EXPORT ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_STUB}) { |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1074 | ${PROFILING_ENTERED_NATIVE} |
torne | 4149526 | 2015-08-26 23:07:32 +0900 | [diff] [blame] | 1075 | return ${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
Torne (Richard Coles) | 0715a9e | 2015-08-14 21:41:14 +0900 | [diff] [blame] | 1076 | } |
| 1077 | """) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1078 | |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1079 | return RemoveIndentedEmptyLines(template.substitute(values)) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1080 | |
anton@chromium.org | 9d3b13b | 2014-05-07 23:14:10 +0900 | [diff] [blame] | 1081 | def GetArgument(self, param): |
torne | 28061e9 | 2016-08-09 01:29:18 +0900 | [diff] [blame] | 1082 | if param.datatype == 'int': |
| 1083 | return 'as_jint(' + param.name + ')' |
| 1084 | elif re.match(RE_SCOPED_JNI_TYPES, JavaDataTypeToC(param.datatype)): |
| 1085 | return param.name + '.obj()' |
| 1086 | else: |
| 1087 | return param.name |
anton@chromium.org | 9d3b13b | 2014-05-07 23:14:10 +0900 | [diff] [blame] | 1088 | |
| 1089 | def GetArgumentsInCall(self, params): |
| 1090 | """Return a string of arguments to call from native into Java""" |
| 1091 | return [self.GetArgument(p) for p in params] |
| 1092 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1093 | def GetCalledByNativeValues(self, called_by_native): |
| 1094 | """Fills in necessary values for the CalledByNative methods.""" |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1095 | java_class = called_by_native.java_class_name or self.class_name |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1096 | if called_by_native.static or called_by_native.is_constructor: |
| 1097 | first_param_in_declaration = '' |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1098 | first_param_in_call = ('%s_clazz(env)' % java_class) |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1099 | else: |
torne | 28061e9 | 2016-08-09 01:29:18 +0900 | [diff] [blame] | 1100 | first_param_in_declaration = ( |
| 1101 | ', const base::android::JavaRefOrBare<jobject>& obj') |
| 1102 | first_param_in_call = 'obj.obj()' |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1103 | params_in_declaration = self.GetCalledByNativeParamsInDeclaration( |
| 1104 | called_by_native) |
| 1105 | if params_in_declaration: |
| 1106 | params_in_declaration = ', ' + params_in_declaration |
anton@chromium.org | 9d3b13b | 2014-05-07 23:14:10 +0900 | [diff] [blame] | 1107 | params_in_call = ', '.join(self.GetArgumentsInCall(called_by_native.params)) |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1108 | if params_in_call: |
| 1109 | params_in_call = ', ' + params_in_call |
| 1110 | pre_call = '' |
| 1111 | post_call = '' |
| 1112 | if called_by_native.static_cast: |
| 1113 | pre_call = 'static_cast<%s>(' % called_by_native.static_cast |
| 1114 | post_call = ')' |
| 1115 | check_exception = '' |
| 1116 | if not called_by_native.unchecked: |
| 1117 | check_exception = 'jni_generator::CheckException(env);' |
| 1118 | return_type = JavaDataTypeToC(called_by_native.return_type) |
| 1119 | optional_error_return = JavaReturnValueToC(called_by_native.return_type) |
| 1120 | if optional_error_return: |
| 1121 | optional_error_return = ', ' + optional_error_return |
| 1122 | return_declaration = '' |
| 1123 | return_clause = '' |
| 1124 | if return_type != 'void': |
| 1125 | pre_call = ' ' + pre_call |
| 1126 | return_declaration = return_type + ' ret =' |
torne | c510c59 | 2015-09-04 20:16:35 +0900 | [diff] [blame] | 1127 | if re.match(RE_SCOPED_JNI_TYPES, return_type): |
torne | 9854c72 | 2016-08-05 00:44:06 +0900 | [diff] [blame] | 1128 | return_type = 'base::android::ScopedJavaLocalRef<' + return_type + '>' |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1129 | return_clause = 'return ' + return_type + '(env, ret);' |
| 1130 | else: |
| 1131 | return_clause = 'return ret;' |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1132 | profiling_leaving_native = '' |
| 1133 | if self.options.enable_profiling: |
| 1134 | profiling_leaving_native = 'JNI_SAVE_FRAME_POINTER;' |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1135 | return { |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1136 | 'JAVA_CLASS': java_class, |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1137 | 'RETURN_TYPE': return_type, |
| 1138 | 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| 1139 | 'RETURN_DECLARATION': return_declaration, |
| 1140 | 'RETURN_CLAUSE': return_clause, |
| 1141 | 'FIRST_PARAM_IN_DECLARATION': first_param_in_declaration, |
| 1142 | 'PARAMS_IN_DECLARATION': params_in_declaration, |
| 1143 | 'PRE_CALL': pre_call, |
| 1144 | 'POST_CALL': post_call, |
| 1145 | 'ENV_CALL': called_by_native.env_call, |
| 1146 | 'FIRST_PARAM_IN_CALL': first_param_in_call, |
| 1147 | 'PARAMS_IN_CALL': params_in_call, |
| 1148 | 'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name, |
| 1149 | 'CHECK_EXCEPTION': check_exception, |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1150 | 'GET_METHOD_ID_IMPL': self.GetMethodIDImpl(called_by_native), |
| 1151 | 'PROFILING_LEAVING_NATIVE': profiling_leaving_native, |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1152 | } |
| 1153 | |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1154 | |
| 1155 | def GetLazyCalledByNativeMethodStub(self, called_by_native): |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1156 | """Returns a string.""" |
| 1157 | function_signature_template = Template("""\ |
bulach@chromium.org | 0c6805b | 2012-09-28 21:34:33 +0900 | [diff] [blame] | 1158 | static ${RETURN_TYPE} Java_${JAVA_CLASS}_${METHOD_ID_VAR_NAME}(\ |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1159 | JNIEnv* env${FIRST_PARAM_IN_DECLARATION}${PARAMS_IN_DECLARATION})""") |
| 1160 | function_header_template = Template("""\ |
| 1161 | ${FUNCTION_SIGNATURE} {""") |
| 1162 | function_header_with_unused_template = Template("""\ |
| 1163 | ${FUNCTION_SIGNATURE} __attribute__ ((unused)); |
| 1164 | ${FUNCTION_SIGNATURE} {""") |
| 1165 | template = Template(""" |
bulach@chromium.org | 231cff6 | 2012-10-17 03:35:10 +0900 | [diff] [blame] | 1166 | static base::subtle::AtomicWord g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = 0; |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1167 | ${FUNCTION_HEADER} |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1168 | CHECK_CLAZZ(env, ${FIRST_PARAM_IN_CALL}, |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1169 | ${JAVA_CLASS}_clazz(env)${OPTIONAL_ERROR_RETURN}); |
bulach@chromium.org | 231cff6 | 2012-10-17 03:35:10 +0900 | [diff] [blame] | 1170 | jmethodID method_id = |
| 1171 | ${GET_METHOD_ID_IMPL} |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1172 | ${PROFILING_LEAVING_NATIVE} |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1173 | ${RETURN_DECLARATION} |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1174 | ${PRE_CALL}env->${ENV_CALL}(${FIRST_PARAM_IN_CALL}, |
| 1175 | method_id${PARAMS_IN_CALL})${POST_CALL}; |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1176 | ${CHECK_EXCEPTION} |
| 1177 | ${RETURN_CLAUSE} |
| 1178 | }""") |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1179 | values = self.GetCalledByNativeValues(called_by_native) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1180 | values['FUNCTION_SIGNATURE'] = ( |
| 1181 | function_signature_template.substitute(values)) |
| 1182 | if called_by_native.system_class: |
| 1183 | values['FUNCTION_HEADER'] = ( |
| 1184 | function_header_with_unused_template.substitute(values)) |
| 1185 | else: |
| 1186 | values['FUNCTION_HEADER'] = function_header_template.substitute(values) |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1187 | return RemoveIndentedEmptyLines(template.substitute(values)) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1188 | |
| 1189 | def GetKMethodArrayEntry(self, native): |
torne | 94bda72 | 2015-02-20 21:37:52 +0900 | [diff] [blame] | 1190 | template = Template(' { "native${NAME}", ${JNI_SIGNATURE}, ' + |
| 1191 | 'reinterpret_cast<void*>(${STUB_NAME}) },') |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 1192 | values = { |
| 1193 | 'NAME': native.name, |
| 1194 | 'JNI_SIGNATURE': self.jni_params.Signature(native.params, |
| 1195 | native.return_type, True), |
| 1196 | 'STUB_NAME': self.GetStubName(native) |
| 1197 | } |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1198 | return template.substitute(values) |
| 1199 | |
| 1200 | def GetUniqueClasses(self, origin): |
| 1201 | ret = {self.class_name: self.fully_qualified_class} |
| 1202 | for entry in origin: |
| 1203 | class_name = self.class_name |
| 1204 | jni_class_path = self.fully_qualified_class |
| 1205 | if entry.java_class_name: |
| 1206 | class_name = entry.java_class_name |
| 1207 | jni_class_path = self.fully_qualified_class + '$' + class_name |
| 1208 | ret[class_name] = jni_class_path |
| 1209 | return ret |
| 1210 | |
| 1211 | def GetClassPathDefinitions(self): |
| 1212 | """Returns the ClassPath constants.""" |
| 1213 | ret = [] |
| 1214 | template = Template("""\ |
bulach@chromium.org | 1a8e155 | 2012-03-02 00:51:51 +0900 | [diff] [blame] | 1215 | const char k${JAVA_CLASS}ClassPath[] = "${JNI_CLASS_PATH}";""") |
torne | fc32b00 | 2016-07-21 19:41:58 +0900 | [diff] [blame] | 1216 | all_classes = self.GetUniqueClasses(self.called_by_natives) |
| 1217 | if self.options.native_exports_optional: |
| 1218 | all_classes.update(self.GetUniqueClasses(self.natives)) |
mkosiba@chromium.org | 56092f0 | 2014-08-11 20:27:12 +0900 | [diff] [blame] | 1219 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1220 | for clazz in all_classes: |
| 1221 | values = { |
| 1222 | 'JAVA_CLASS': clazz, |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 1223 | 'JNI_CLASS_PATH': all_classes[clazz], |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1224 | } |
| 1225 | ret += [template.substitute(values)] |
| 1226 | ret += '' |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1227 | |
torne | fc32b00 | 2016-07-21 19:41:58 +0900 | [diff] [blame] | 1228 | template = Template("""\ |
ygorshenin@chromium.org | 25171e5 | 2014-08-20 20:44:05 +0900 | [diff] [blame] | 1229 | // Leaking this jclass as we cannot use LazyInstance from some threads. |
torne | 09434e5 | 2015-01-09 04:48:31 +0900 | [diff] [blame] | 1230 | base::subtle::AtomicWord g_${JAVA_CLASS}_clazz __attribute__((unused)) = 0; |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1231 | #define ${JAVA_CLASS}_clazz(env) \ |
| 1232 | base::android::LazyGetClass(env, k${JAVA_CLASS}ClassPath, \ |
| 1233 | &g_${JAVA_CLASS}_clazz)""") |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1234 | |
Torne (Richard Coles) | 42ef704 | 2016-07-19 20:36:09 +0900 | [diff] [blame] | 1235 | for clazz in all_classes: |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1236 | values = { |
| 1237 | 'JAVA_CLASS': clazz, |
| 1238 | } |
ygorshenin@chromium.org | 25171e5 | 2014-08-20 20:44:05 +0900 | [diff] [blame] | 1239 | ret += [template.substitute(values)] |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1240 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1241 | return '\n'.join(ret) |
| 1242 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1243 | def GetMethodIDImpl(self, called_by_native): |
| 1244 | """Returns the implementation of GetMethodID.""" |
torne | b66773d | 2016-05-10 02:08:20 +0900 | [diff] [blame] | 1245 | template = Template("""\ |
bulach@chromium.org | 231cff6 | 2012-10-17 03:35:10 +0900 | [diff] [blame] | 1246 | base::android::MethodID::LazyGet< |
| 1247 | base::android::MethodID::TYPE_${STATIC}>( |
mkosiba@chromium.org | 3307517 | 2014-08-22 01:37:32 +0900 | [diff] [blame] | 1248 | env, ${JAVA_CLASS}_clazz(env), |
bulach@chromium.org | 231cff6 | 2012-10-17 03:35:10 +0900 | [diff] [blame] | 1249 | "${JNI_NAME}", |
| 1250 | ${JNI_SIGNATURE}, |
| 1251 | &g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME}); |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1252 | """) |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 1253 | jni_name = called_by_native.name |
| 1254 | jni_return_type = called_by_native.return_type |
| 1255 | if called_by_native.is_constructor: |
| 1256 | jni_name = '<init>' |
| 1257 | jni_return_type = 'void' |
bulach@chromium.org | 37fc911 | 2013-10-26 01:27:03 +0900 | [diff] [blame] | 1258 | if called_by_native.signature: |
| 1259 | signature = called_by_native.signature |
| 1260 | else: |
Yipeng Wang | 60438ac | 2017-06-17 06:08:33 +0900 | [diff] [blame] | 1261 | signature = self.jni_params.Signature(called_by_native.params, |
| 1262 | jni_return_type, True) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1263 | values = { |
| 1264 | 'JAVA_CLASS': called_by_native.java_class_name or self.class_name, |
bulach@chromium.org | 31af753 | 2012-09-24 20:01:41 +0900 | [diff] [blame] | 1265 | 'JNI_NAME': jni_name, |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1266 | 'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name, |
bulach@chromium.org | 231cff6 | 2012-10-17 03:35:10 +0900 | [diff] [blame] | 1267 | 'STATIC': 'STATIC' if called_by_native.static else 'INSTANCE', |
bulach@chromium.org | 37fc911 | 2013-10-26 01:27:03 +0900 | [diff] [blame] | 1268 | 'JNI_SIGNATURE': signature, |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1269 | } |
| 1270 | return template.substitute(values) |
| 1271 | |
| 1272 | |
| 1273 | def WrapOutput(output): |
| 1274 | ret = [] |
| 1275 | for line in output.splitlines(): |
tedchoc@chromium.org | 7299218 | 2012-08-03 11:00:17 +0900 | [diff] [blame] | 1276 | # Do not wrap lines under 80 characters or preprocessor directives. |
| 1277 | if len(line) < 80 or line.lstrip()[:1] == '#': |
bulach@chromium.org | 1c77575 | 2012-06-22 19:03:16 +0900 | [diff] [blame] | 1278 | stripped = line.rstrip() |
| 1279 | if len(ret) == 0 or len(ret[-1]) or len(stripped): |
| 1280 | ret.append(stripped) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1281 | else: |
| 1282 | first_line_indent = ' ' * (len(line) - len(line.lstrip())) |
| 1283 | subsequent_indent = first_line_indent + ' ' * 4 |
| 1284 | if line.startswith('//'): |
| 1285 | subsequent_indent = '//' + subsequent_indent |
| 1286 | wrapper = textwrap.TextWrapper(width=80, |
| 1287 | subsequent_indent=subsequent_indent, |
| 1288 | break_long_words=False) |
| 1289 | ret += [wrapped.rstrip() for wrapped in wrapper.wrap(line)] |
| 1290 | ret += [''] |
| 1291 | return '\n'.join(ret) |
| 1292 | |
| 1293 | |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1294 | def ExtractJarInputFile(jar_file, input_file, out_dir): |
| 1295 | """Extracts input file from jar and returns the filename. |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1296 | |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1297 | The input file is extracted to the same directory that the generated jni |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1298 | headers will be placed in. This is passed as an argument to script. |
| 1299 | |
| 1300 | Args: |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1301 | jar_file: the jar file containing the input files to extract. |
| 1302 | input_files: the list of files to extract from the jar file. |
| 1303 | out_dir: the name of the directories to extract to. |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1304 | |
| 1305 | Returns: |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1306 | the name of extracted input file. |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1307 | """ |
| 1308 | jar_file = zipfile.ZipFile(jar_file) |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1309 | |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1310 | out_dir = os.path.join(out_dir, os.path.dirname(input_file)) |
torne@chromium.org | 108c37d | 2013-03-08 22:52:50 +0900 | [diff] [blame] | 1311 | try: |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1312 | os.makedirs(out_dir) |
torne@chromium.org | 108c37d | 2013-03-08 22:52:50 +0900 | [diff] [blame] | 1313 | except OSError as e: |
| 1314 | if e.errno != errno.EEXIST: |
| 1315 | raise |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1316 | extracted_file_name = os.path.join(out_dir, os.path.basename(input_file)) |
| 1317 | with open(extracted_file_name, 'w') as outfile: |
| 1318 | outfile.write(jar_file.read(input_file)) |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1319 | |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1320 | return extracted_file_name |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1321 | |
| 1322 | |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1323 | def GenerateJNIHeader(input_file, output_file, options): |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1324 | try: |
| 1325 | if os.path.splitext(input_file)[1] == '.class': |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1326 | jni_from_javap = JNIFromJavaP.CreateFromClass(input_file, options) |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1327 | content = jni_from_javap.GetContent() |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1328 | else: |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1329 | jni_from_java_source = JNIFromJavaSource.CreateFromFile( |
| 1330 | input_file, options) |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1331 | content = jni_from_java_source.GetContent() |
| 1332 | except ParseError, e: |
| 1333 | print e |
| 1334 | sys.exit(1) |
| 1335 | if output_file: |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 1336 | WriteOutput(output_file, content) |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1337 | else: |
paulmiller | 4d5b53e | 2015-05-30 02:24:37 +0900 | [diff] [blame] | 1338 | print content |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1339 | |
| 1340 | |
Yipeng Wang | fba87a3 | 2017-07-01 03:16:41 +0900 | [diff] [blame^] | 1341 | def WriteOutput(output_file, content): |
| 1342 | if os.path.exists(output_file): |
| 1343 | with open(output_file) as f: |
| 1344 | existing_content = f.read() |
| 1345 | if existing_content == content: |
| 1346 | return |
| 1347 | with open(output_file, 'w') as f: |
| 1348 | f.write(content) |
| 1349 | |
| 1350 | |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1351 | def GetScriptName(): |
| 1352 | script_components = os.path.abspath(sys.argv[0]).split(os.path.sep) |
| 1353 | base_index = 0 |
| 1354 | for idx, value in enumerate(script_components): |
| 1355 | if value == 'base' or value == 'third_party': |
| 1356 | base_index = idx |
| 1357 | break |
| 1358 | return os.sep.join(script_components[base_index:]) |
| 1359 | |
| 1360 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1361 | def main(argv): |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1362 | usage = """usage: %prog [OPTIONS] |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1363 | This script will parse the given java source code extracting the native |
| 1364 | declarations and print the header file to stdout (or a file). |
| 1365 | See SampleForTests.java for more details. |
| 1366 | """ |
| 1367 | option_parser = optparse.OptionParser(usage=usage) |
cjhopman@chromium.org | fb98e33 | 2014-06-25 08:38:17 +0900 | [diff] [blame] | 1368 | build_utils.AddDepfileOption(option_parser) |
| 1369 | |
cjhopman@chromium.org | e607d25 | 2014-06-06 04:47:30 +0900 | [diff] [blame] | 1370 | option_parser.add_option('-j', '--jar_file', dest='jar_file', |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1371 | help='Extract the list of input files from' |
bulach@chromium.org | 7f85d46 | 2012-05-30 18:56:02 +0900 | [diff] [blame] | 1372 | ' a specified jar file.' |
| 1373 | ' Uses javap to extract the methods from a' |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1374 | ' pre-compiled class. --input should point' |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1375 | ' to pre-compiled Java .class files.') |
| 1376 | option_parser.add_option('-n', dest='namespace', |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1377 | help='Uses as a namespace in the generated header ' |
| 1378 | 'instead of the javap class name, or when there is ' |
| 1379 | 'no JNINamespace annotation in the java source.') |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1380 | option_parser.add_option('--input_file', |
| 1381 | help='Single input file name. The output file name ' |
| 1382 | 'will be derived from it. Must be used with ' |
| 1383 | '--output_dir.') |
| 1384 | option_parser.add_option('--output_dir', |
| 1385 | help='The output directory. Must be used with ' |
| 1386 | '--input') |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1387 | option_parser.add_option('--script_name', default=GetScriptName(), |
| 1388 | help='The name of this script in the generated ' |
| 1389 | 'header.') |
bulach@chromium.org | 51dd749 | 2014-01-08 23:41:13 +0900 | [diff] [blame] | 1390 | option_parser.add_option('--includes', |
| 1391 | help='The comma-separated list of header files to ' |
| 1392 | 'include in the generated header.') |
bulach@chromium.org | a022cf6 | 2013-11-05 09:54:22 +0900 | [diff] [blame] | 1393 | option_parser.add_option('--ptr_type', default='int', |
| 1394 | type='choice', choices=['int', 'long'], |
| 1395 | help='The type used to represent native pointers in ' |
| 1396 | 'Java code. For 32-bit, use int; ' |
| 1397 | 'for 64-bit, use long.') |
bulach@chromium.org | 92d0dac | 2014-01-17 01:08:05 +0900 | [diff] [blame] | 1398 | option_parser.add_option('--cpp', default='cpp', |
| 1399 | help='The path to cpp command.') |
| 1400 | option_parser.add_option('--javap', default='javap', |
| 1401 | help='The path to javap command.') |
torne | 0dd0b0d | 2015-02-21 08:06:33 +0900 | [diff] [blame] | 1402 | option_parser.add_option('--native_exports_optional', action='store_true', |
| 1403 | help='Support both explicit and native method' |
| 1404 | 'registration.') |
dskiba | b7cdc89 | 2016-10-26 08:39:11 +0900 | [diff] [blame] | 1405 | option_parser.add_option('--enable_profiling', action='store_true', |
| 1406 | help='Add additional profiling instrumentation.') |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1407 | options, args = option_parser.parse_args(argv) |
yfriedman@chromium.org | 28e6a04 | 2012-05-24 02:53:53 +0900 | [diff] [blame] | 1408 | if options.jar_file: |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1409 | input_file = ExtractJarInputFile(options.jar_file, options.input_file, |
| 1410 | options.output_dir) |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1411 | elif options.input_file: |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1412 | input_file = options.input_file |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1413 | else: |
| 1414 | option_parser.print_help() |
| 1415 | print '\nError: Must specify --jar_file or --input_file.' |
| 1416 | return 1 |
bulach@chromium.org | 4c248ed | 2012-07-20 05:02:55 +0900 | [diff] [blame] | 1417 | output_file = None |
| 1418 | if options.output_dir: |
| 1419 | root_name = os.path.splitext(os.path.basename(input_file))[0] |
| 1420 | output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
bulach@chromium.org | 7cd403c | 2013-10-03 03:11:24 +0900 | [diff] [blame] | 1421 | GenerateJNIHeader(input_file, output_file, options) |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1422 | |
cjhopman@chromium.org | fb98e33 | 2014-06-25 08:38:17 +0900 | [diff] [blame] | 1423 | if options.depfile: |
agrieve | 92aed19 | 2016-09-14 11:04:21 +0900 | [diff] [blame] | 1424 | build_utils.WriteDepfile(options.depfile, output_file) |
cjhopman@chromium.org | fb98e33 | 2014-06-25 08:38:17 +0900 | [diff] [blame] | 1425 | |
bulach@chromium.org | 6079a07 | 2012-02-24 09:09:38 +0900 | [diff] [blame] | 1426 | |
| 1427 | if __name__ == '__main__': |
| 1428 | sys.exit(main(sys.argv)) |