| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 # On Android we build unit test bundles as shared libraries. To run | 6 # On Android we build unit test bundles as shared libraries. To run |
| 7 # tests, we launch a special "test runner" apk which loads the library | 7 # tests, we launch a special "test runner" apk which loads the library |
| 8 # then jumps into it. Since java is required for many tests | 8 # then jumps into it. Since java is required for many tests |
| 9 # (e.g. PathUtils.java), a "pure native" test bundle is inadequate. | 9 # (e.g. PathUtils.java), a "pure native" test bundle is inadequate. |
| 10 # | 10 # |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 [strip, '--strip-unneeded', self._native_library, '-o', dest]) | 129 [strip, '--strip-unneeded', self._native_library, '-o', dest]) |
| 130 if self._jars: | 130 if self._jars: |
| 131 destdir = os.path.join(self._output_directory, 'libs') | 131 destdir = os.path.join(self._output_directory, 'libs') |
| 132 if not os.path.exists(destdir): | 132 if not os.path.exists(destdir): |
| 133 os.makedirs(destdir) | 133 os.makedirs(destdir) |
| 134 for jar in self._jars: | 134 for jar in self._jars: |
| 135 dest = os.path.join(destdir, os.path.basename(jar)) | 135 dest = os.path.join(destdir, os.path.basename(jar)) |
| 136 logging.warn('%s --> %s' % (jar, dest)) | 136 logging.warn('%s --> %s' % (jar, dest)) |
| 137 shutil.copyfile(jar, dest) | 137 shutil.copyfile(jar, dest) |
| 138 | 138 |
| 139 def CreateBundle(self): | 139 def CreateBundle(self, ant_compile): |
| 140 """Create the apk bundle source and assemble components.""" | 140 """Create the apk bundle source and assemble components.""" |
| 141 if not ant_compile: |
| 142 self._SOURCE_FILES.append('Android.mk') |
| 143 self._REPLACEME_FILES.append('Android.mk') |
| 141 self._CopyTemplateFiles() | 144 self._CopyTemplateFiles() |
| 142 self._ReplaceStrings() | 145 self._ReplaceStrings() |
| 143 self._CopyLibraryAndJars() | 146 self._CopyLibraryAndJars() |
| 144 | 147 |
| 145 def Compile(self, ant_args): | 148 def Compile(self, ant_args): |
| 146 """Build the generated apk with ant. | 149 """Build the generated apk with ant. |
| 147 | 150 |
| 148 Args: | 151 Args: |
| 149 ant_args: extra args to pass to ant | 152 ant_args: extra args to pass to ant |
| 150 """ | 153 """ |
| 151 cmd = ['ant'] | 154 cmd = ['ant'] |
| 152 if ant_args: | 155 if ant_args: |
| 153 cmd.append(ant_args) | 156 cmd.append(ant_args) |
| 154 cmd.extend(['-buildfile', | 157 cmd.extend(['-buildfile', |
| 155 os.path.join(self._output_directory, 'native_test_apk.xml')]) | 158 os.path.join(self._output_directory, 'native_test_apk.xml')]) |
| 156 logging.warn(cmd) | 159 logging.warn(cmd) |
| 157 p = subprocess.Popen(cmd, stderr=subprocess.STDOUT) | 160 p = subprocess.Popen(cmd, stderr=subprocess.STDOUT) |
| 158 (stdout, _) = p.communicate() | 161 (stdout, _) = p.communicate() |
| 159 logging.warn(stdout) | 162 logging.warn(stdout) |
| 160 if p.returncode != 0: | 163 if p.returncode != 0: |
| 161 logging.error('Ant return code %d' % p.returncode) | 164 logging.error('Ant return code %d' % p.returncode) |
| 162 sys.exit(p.returncode) | 165 sys.exit(p.returncode) |
| 163 | 166 |
| 167 def CompileAndroidMk(self): |
| 168 """Build the generated apk within Android source tree using Android.mk.""" |
| 169 try: |
| 170 import compile_android_mk # pylint: disable=F0401 |
| 171 except: |
| 172 raise AssertionError('Not in Android source tree. ' |
| 173 'Please use --ant-compile.') |
| 174 compile_android_mk.CompileAndroidMk(self._native_library, |
| 175 self._output_directory) |
| 176 |
| 164 | 177 |
| 165 def main(argv): | 178 def main(argv): |
| 166 parser = optparse.OptionParser() | 179 parser = optparse.OptionParser() |
| 167 parser.add_option('--verbose', | 180 parser.add_option('--verbose', |
| 168 help='Be verbose') | 181 help='Be verbose') |
| 169 parser.add_option('--native_library', | 182 parser.add_option('--native_library', |
| 170 help='Full name of native shared library test bundle') | 183 help='Full name of native shared library test bundle') |
| 171 parser.add_option('--jar', action='append', | 184 parser.add_option('--jar', action='append', |
| 172 help='Include this jar; can be specified multiple times') | 185 help='Include this jar; can be specified multiple times') |
| 173 parser.add_option('--output', | 186 parser.add_option('--output', |
| 174 help='Output directory for generated files.') | 187 help='Output directory for generated files.') |
| 175 parser.add_option('--ant-compile', action='store_true', | 188 parser.add_option('--ant-compile', action='store_true', |
| 176 help='If specified, build the generated apk with ant') | 189 help='If specified, build the generated apk with ant. ' |
| 190 'Otherwise assume compiling within the Android ' |
| 191 'source tree using Android.mk.') |
| 177 parser.add_option('--ant-args', | 192 parser.add_option('--ant-args', |
| 178 help='extra args for ant') | 193 help='extra args for ant') |
| 179 | 194 |
| 180 options, _ = parser.parse_args(argv) | 195 options, _ = parser.parse_args(argv) |
| 181 | 196 |
| 182 # It is not an error to specify no native library; the apk should | 197 # It is not an error to specify no native library; the apk should |
| 183 # still be generated and build. It will, however, print | 198 # still be generated and build. It will, however, print |
| 184 # NATIVE_LOADER_FAILED when run. | 199 # NATIVE_LOADER_FAILED when run. |
| 185 if not options.output: | 200 if not options.output: |
| 186 raise Exception('No output directory specified for generated files') | 201 raise Exception('No output directory specified for generated files') |
| 187 | 202 |
| 188 if options.verbose: | 203 if options.verbose: |
| 189 logging.basicConfig(level=logging.DEBUG, format=' %(message)s') | 204 logging.basicConfig(level=logging.DEBUG, format=' %(message)s') |
| 190 | 205 |
| 206 # Ignore --ant-compile when building with Android source. |
| 207 if 'ANDROID_BUILD_TOP' in os.environ: |
| 208 options.ant_compile = False |
| 209 |
| 191 ntag = NativeTestApkGenerator(native_library=options.native_library, | 210 ntag = NativeTestApkGenerator(native_library=options.native_library, |
| 192 jars=options.jar, | 211 jars=options.jar, |
| 193 output_directory=options.output) | 212 output_directory=options.output) |
| 194 ntag.CreateBundle() | 213 ntag.CreateBundle(options.ant_compile) |
| 214 |
| 195 if options.ant_compile: | 215 if options.ant_compile: |
| 196 ntag.Compile(options.ant_args) | 216 ntag.Compile(options.ant_args) |
| 217 else: |
| 218 ntag.CompileAndroidMk() |
| 197 | 219 |
| 198 logging.warn('COMPLETE.') | 220 logging.warn('COMPLETE.') |
| 199 | 221 |
| 200 if __name__ == '__main__': | 222 if __name__ == '__main__': |
| 201 sys.exit(main(sys.argv)) | 223 sys.exit(main(sys.argv)) |
| OLD | NEW |