laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 1 | # Lint as: python2, python3 |
Laszlo Csomor | 527ff1f | 2017-10-24 14:37:07 +0200 | [diff] [blame] | 2 | # pylint: disable=g-direct-third-party-import |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 3 | # Copyright 2016 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """A tool for extracting native libs from an AAR into a zip. |
| 18 | |
| 19 | The native libs for the requested cpu will be extracted into a zip. The paths |
| 20 | are converted from the AAR directory structure of /jni/<cpu>/foo.so to the APK |
| 21 | directory structure of /lib/<cpu>/foo.so. |
| 22 | """ |
| 23 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 24 | from __future__ import absolute_import |
| 25 | from __future__ import division |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 26 | from __future__ import print_function |
| 27 | |
Laszlo Csomor | 527ff1f | 2017-10-24 14:37:07 +0200 | [diff] [blame] | 28 | import os |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 29 | import re |
| 30 | import sys |
| 31 | import zipfile |
| 32 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 33 | # Do not edit this line. Copybara replaces it with PY2 migration helper. |
| 34 | from absl import app |
| 35 | from absl import flags |
| 36 | import six |
| 37 | |
Laszlo Csomor | 527ff1f | 2017-10-24 14:37:07 +0200 | [diff] [blame] | 38 | from tools.android import junction |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 39 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 40 | FLAGS = flags.FLAGS |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 41 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 42 | flags.DEFINE_string("input_aar", None, "Input AAR") |
| 43 | flags.mark_flag_as_required("input_aar") |
| 44 | flags.DEFINE_string("cpu", None, "CPU architecture to include") |
| 45 | flags.mark_flag_as_required("cpu") |
| 46 | flags.DEFINE_string("output_zip", None, "Output ZIP of native libs") |
| 47 | flags.mark_flag_as_required("output_zip") |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class UnsupportedArchitectureException(Exception): |
Laszlo Csomor | 527ff1f | 2017-10-24 14:37:07 +0200 | [diff] [blame] | 51 | """Exception thrown when an AAR does not support the requested CPU.""" |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 52 | pass |
| 53 | |
| 54 | |
| 55 | def CreateNativeLibsZip(aar, cpu, native_libs_zip): |
| 56 | native_lib_pattern = re.compile("^jni/.+/.+\\.so$") |
| 57 | if any(native_lib_pattern.match(filename) for filename in aar.namelist()): |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 58 | cpu_pattern = re.compile("^jni/" + six.ensure_str(cpu) + "/.+\\.so$") |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 59 | libs = [name for name in aar.namelist() if cpu_pattern.match(name)] |
| 60 | if not libs: |
| 61 | raise UnsupportedArchitectureException() |
| 62 | for lib in libs: |
| 63 | # Only replaces the first instance of jni, in case the AAR contains |
| 64 | # something like /jni/x86/jni.so. |
| 65 | new_filename = lib.replace("jni", "lib", 1) |
| 66 | native_libs_zip.writestr(new_filename, aar.read(lib)) |
| 67 | |
| 68 | |
Laszlo Csomor | 527ff1f | 2017-10-24 14:37:07 +0200 | [diff] [blame] | 69 | def Main(input_aar_path, output_zip_path, cpu, input_aar_path_for_error_msg): |
| 70 | with zipfile.ZipFile(input_aar_path, "r") as input_aar: |
| 71 | with zipfile.ZipFile(output_zip_path, "w") as native_libs_zip: |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 72 | try: |
Laszlo Csomor | 527ff1f | 2017-10-24 14:37:07 +0200 | [diff] [blame] | 73 | CreateNativeLibsZip(input_aar, cpu, native_libs_zip) |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 74 | except UnsupportedArchitectureException: |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 75 | print("AAR " + six.ensure_str(input_aar_path_for_error_msg) + |
| 76 | " missing native libs for requested architecture: " + |
| 77 | six.ensure_str(cpu)) |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 78 | sys.exit(1) |
| 79 | |
| 80 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 81 | def main(unused_argv): |
Laszlo Csomor | 527ff1f | 2017-10-24 14:37:07 +0200 | [diff] [blame] | 82 | if os.name == "nt": |
| 83 | with junction.TempJunction(os.path.dirname(FLAGS.input_aar)) as j_in: |
| 84 | with junction.TempJunction(os.path.dirname(FLAGS.output_zip)) as j_out: |
| 85 | Main( |
| 86 | os.path.join(j_in, os.path.basename(FLAGS.input_aar)), |
| 87 | os.path.join(j_out, os.path.basename(FLAGS.output_zip)), FLAGS.cpu, |
| 88 | FLAGS.input_aar) |
| 89 | else: |
| 90 | Main(FLAGS.input_aar, FLAGS.output_zip, FLAGS.cpu, FLAGS.input_aar) |
| 91 | |
| 92 | |
Adam Michael | 6a87bd4 | 2016-10-25 19:37:41 +0000 | [diff] [blame] | 93 | if __name__ == "__main__": |
| 94 | FLAGS(sys.argv) |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 95 | app.run(main) |