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