blob: 59c3d7b8a1bff13d9d632129e9ea0c0970e93eb4 [file] [log] [blame]
laszlocsomord93a1462019-11-04 09:14:39 -08001# Lint as: python2, python3
Laszlo Csomor527ff1f2017-10-24 14:37:07 +02002# pylint: disable=g-direct-third-party-import
Adam Michael6a87bd42016-10-25 19:37:41 +00003# 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
19The native libs for the requested cpu will be extracted into a zip. The paths
20are converted from the AAR directory structure of /jni/<cpu>/foo.so to the APK
21directory structure of /lib/<cpu>/foo.so.
22"""
23
laszlocsomord93a1462019-11-04 09:14:39 -080024from __future__ import absolute_import
25from __future__ import division
Googler246ee562017-12-19 11:04:21 -080026from __future__ import print_function
27
Laszlo Csomor527ff1f2017-10-24 14:37:07 +020028import os
Adam Michael6a87bd42016-10-25 19:37:41 +000029import re
30import sys
31import zipfile
32
laszlocsomord93a1462019-11-04 09:14:39 -080033# Do not edit this line. Copybara replaces it with PY2 migration helper.
34from absl import app
35from absl import flags
36import six
37
Laszlo Csomor527ff1f2017-10-24 14:37:07 +020038from tools.android import junction
Adam Michael6a87bd42016-10-25 19:37:41 +000039
laszlocsomord93a1462019-11-04 09:14:39 -080040FLAGS = flags.FLAGS
Adam Michael6a87bd42016-10-25 19:37:41 +000041
laszlocsomord93a1462019-11-04 09:14:39 -080042flags.DEFINE_string("input_aar", None, "Input AAR")
43flags.mark_flag_as_required("input_aar")
44flags.DEFINE_string("cpu", None, "CPU architecture to include")
45flags.mark_flag_as_required("cpu")
46flags.DEFINE_string("output_zip", None, "Output ZIP of native libs")
47flags.mark_flag_as_required("output_zip")
Adam Michael6a87bd42016-10-25 19:37:41 +000048
49
50class UnsupportedArchitectureException(Exception):
Laszlo Csomor527ff1f2017-10-24 14:37:07 +020051 """Exception thrown when an AAR does not support the requested CPU."""
Adam Michael6a87bd42016-10-25 19:37:41 +000052 pass
53
54
55def 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()):
laszlocsomord93a1462019-11-04 09:14:39 -080058 cpu_pattern = re.compile("^jni/" + six.ensure_str(cpu) + "/.+\\.so$")
Adam Michael6a87bd42016-10-25 19:37:41 +000059 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 Csomor527ff1f2017-10-24 14:37:07 +020069def 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 Michael6a87bd42016-10-25 19:37:41 +000072 try:
Laszlo Csomor527ff1f2017-10-24 14:37:07 +020073 CreateNativeLibsZip(input_aar, cpu, native_libs_zip)
Adam Michael6a87bd42016-10-25 19:37:41 +000074 except UnsupportedArchitectureException:
laszlocsomord93a1462019-11-04 09:14:39 -080075 print("AAR " + six.ensure_str(input_aar_path_for_error_msg) +
76 " missing native libs for requested architecture: " +
77 six.ensure_str(cpu))
Adam Michael6a87bd42016-10-25 19:37:41 +000078 sys.exit(1)
79
80
laszlocsomord93a1462019-11-04 09:14:39 -080081def main(unused_argv):
Laszlo Csomor527ff1f2017-10-24 14:37:07 +020082 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 Michael6a87bd42016-10-25 19:37:41 +000093if __name__ == "__main__":
94 FLAGS(sys.argv)
laszlocsomord93a1462019-11-04 09:14:39 -080095 app.run(main)