1#!/bin/bash
2#
3# Copyright 2020 The Android Open Source Project
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# Generate Android.bp for AOSP blob self-extractors.
18#
19# For example, a blob package may contain:
20#   ./vendor
21#   └── qcom
22#       └── coral
23#           └── proprietary
24#               ├── lib64
25#               |   ├── libfoo.so
26#               |   └── libbar.so
27#               ├── libfoo.so
28#               └── libbar.so
29#
30# Generate prebuilt modules for these blobs:
31# $ export SYSTEM_EXT_SPECIFIC=true # If installing prebuilts to system_ext/ partition
32# $ export OWNER=qcom # Owner is relevant if PRODUCT_RESTRICT_VENDOR_FILES is set
33# $ ./generate-android-bp-for-blobs.sh ./vendor/qcom/coral/proprietary > Android.bp.txt
34# $ mv Android.bp.txt ${ANDROID_BUILD_TOP}/device/google/coral/self-extractors/qcom/staging/
35#
36# You may need to review the contents of Android.bp.txt as some of the blobs may
37# have unsatisfied dependencies. Add `check_elf_files: false` to bypass this
38# kind of build errors.
39
40set -e
41
42readonly PREBUILT_DIR="$1"
43
44readonly elf_files=$(
45  for file in $(find "$PREBUILT_DIR" -type f); do
46    if readelf -h "$file" 2>/dev/null 1>&2; then
47      basename "$file"
48    fi
49  done | sort | uniq | xargs
50)
51
52echo "// Copyright (C) $(date +%Y) The Android Open Source Project"
53echo "//"
54echo "// Licensed under the Apache License, Version 2.0 (the \"License\");"
55echo "// you may not use this file except in compliance with the License."
56echo "// You may obtain a copy of the License at"
57echo "//"
58echo "//      http://www.apache.org/licenses/LICENSE-2.0"
59echo "//"
60echo "// Unless required by applicable law or agreed to in writing, software"
61echo "// distributed under the License is distributed on an \"AS IS\" BASIS,"
62echo "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied."
63echo "// See the License for the specific language governing permissions and"
64echo "// limitations under the License."
65echo ""
66echo "soong_namespace {"
67echo "}"
68
69for file in $elf_files; do
70  file32=$(find "$PREBUILT_DIR" -type f -name "$file" | grep -v 'lib64' | head)
71  file64=$(find "$PREBUILT_DIR" -type f -name "$file" | grep 'lib64' | head)
72  if [[ -n "$file32" ]] && [[ -n "$file64" ]]; then
73    multilib="both"
74  elif [[ -n "$file32" ]]; then
75    multilib="32"
76  else
77    multilib="64"
78  fi
79
80echo ""
81echo "cc_prebuilt_library_shared {"
82echo "    name: \"${file%.so}\","
83echo "    arch: {"
84
85  if [[ -f "$file32" ]]; then
86  NEEDED=$(readelf -d "$file32" | sed -n -E 's/^.*\(NEEDED\).*\[(.+)\]$/\1/p' | xargs)
87echo "        arm: {"
88echo "            srcs: [\"$(realpath --relative-to="$PREBUILT_DIR" "$file32")\"],"
89    if [[ -n "$NEEDED" ]]; then
90echo "            shared_libs: ["
91      for entry in $NEEDED; do
92echo "                \"${entry%.so}\","
93      done
94echo "            ],"
95    fi
96echo "        },"
97  fi
98
99  if [[ -f "$file64" ]]; then
100  NEEDED=$(readelf -d "$file64" | sed -n -E 's/^.*\(NEEDED\).*\[(.+)\]$/\1/p' | xargs)
101echo "        arm64: {"
102echo "            srcs: [\"$(realpath --relative-to="$PREBUILT_DIR" "$file64")\"],"
103    if [[ -n "$NEEDED" ]]; then
104echo "            shared_libs: ["
105      for entry in $NEEDED; do
106echo "                \"${entry%.so}\","
107      done
108echo "            ],"
109    fi
110echo "        },"
111  fi
112
113echo "    },"
114echo "    compile_multilib: \"$multilib\","
115  if [[ -n "$SYSTEM_EXT_SPECIFIC" ]]; then
116echo "    system_ext_specific: true,"
117  fi
118  if [[ -n "$OWNER" ]]; then
119echo "    owner: \"${OWNER}\","
120  fi
121echo "    strip: {"
122echo "        none: true,"
123echo "    },"
124echo "}"
125
126done
127