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