blob: ccdb9d20e48a1f940e79841cd7d3c92ba2f63e8c [file] [log] [blame]
#!/bin/bash
#
# Copyright (C) 2016 The CyanogenMod Project
# Copyright (C) 2017-2020 The LineageOS Project
#
# SPDX-License-Identifier: Apache-2.0
#
PRODUCT_COPY_FILES_LIST=()
PRODUCT_COPY_FILES_HASHES=()
PRODUCT_COPY_FILES_FIXUP_HASHES=()
PRODUCT_PACKAGES_LIST=()
PRODUCT_PACKAGES_HASHES=()
PRODUCT_PACKAGES_FIXUP_HASHES=()
PACKAGE_LIST=()
VENDOR_STATE=-1
VENDOR_RADIO_STATE=-1
COMMON=-1
ARCHES=
FULLY_DEODEXED=-1
TMPDIR=$(mktemp -d)
HOST="$(uname | tr '[:upper:]' '[:lower:]')"
#
# cleanup
#
# kill our tmpfiles with fire on exit
#
function cleanup() {
rm -rf "${TMPDIR:?}"
}
trap cleanup 0
#
# setup_vendor
#
# $1: device name
# $2: vendor name
# $3: Android root directory
# $4: is common device - optional, default to false
# $5: cleanup - optional, default to true
# $6: custom vendor makefile name - optional, default to false
#
# Must be called before any other functions can be used. This
# sets up the internal state for a new vendor configuration.
#
function setup_vendor() {
local DEVICE="$1"
if [ -z "$DEVICE" ]; then
echo "\$DEVICE must be set before including this script!"
exit 1
fi
export VENDOR="$2"
if [ -z "$VENDOR" ]; then
echo "\$VENDOR must be set before including this script!"
exit 1
fi
export ANDROID_ROOT="$3"
if [ ! -d "$ANDROID_ROOT" ]; then
echo "\$ANDROID_ROOT must be set and valid before including this script!"
exit 1
fi
export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
if [ ! -d "$ANDROID_ROOT/$OUTDIR" ]; then
mkdir -p "$ANDROID_ROOT/$OUTDIR"
fi
VNDNAME="$6"
if [ -z "$VNDNAME" ]; then
VNDNAME="$DEVICE"
fi
export PRODUCTMK="$ANDROID_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
export ANDROIDBP="$ANDROID_ROOT"/"$OUTDIR"/Android.bp
export ANDROIDMK="$ANDROID_ROOT"/"$OUTDIR"/Android.mk
export BOARDMK="$ANDROID_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
if [ "$4" == "true" ] || [ "$4" == "1" ]; then
COMMON=1
else
COMMON=0
fi
if [ "$5" == "false" ] || [ "$5" == "0" ]; then
VENDOR_STATE=1
VENDOR_RADIO_STATE=1
else
VENDOR_STATE=0
VENDOR_RADIO_STATE=0
fi
export BINARIES_LOCATION="$ANDROID_ROOT"/prebuilts/extract-tools/${HOST}-x86/bin
for version in 0_8 0_9; do
export PATCHELF_${version}="$BINARIES_LOCATION"/patchelf-"${version}"
done
if [ -z "$PATCHELF_VERSION" ]; then
export PATCHELF_VERSION=0_9
fi
if [ -z "$PATCHELF" ]; then
local patchelf_variable="PATCHELF_${PATCHELF_VERSION}"
export PATCHELF=${!patchelf_variable}
fi
}
# Helper functions for parsing a spec.
# notes: an optional "|SHA1" that may appear in the format is stripped
# early from the spec in the parse_file_list function, and
# should not be present inside the input parameter passed
# to these functions.
#
# input: spec in the form of "src[:dst][;args]"
# output: "src"
#
function src_file() {
local SPEC="$1"
local SPLIT=(${SPEC//:/ })
local ARGS="$(target_args ${SPEC})"
# Regardless of there being a ":" delimiter or not in the spec,
# the source file is always either the first, or the only entry.
local SRC="${SPLIT[0]}"
# Remove target_args suffix, if present
echo "${SRC%;${ARGS}}"
}
#
# input: spec in the form of "src[:dst][;args]"
# output: "dst" if present, "src" otherwise.
#
function target_file() {
local SPEC="${1%%;*}"
local SPLIT=(${SPEC//:/ })
local ARGS="$(target_args ${SPEC})"
local DST=
case ${#SPLIT[@]} in
1)
# The spec doesn't have a : delimiter
DST="${SPLIT[0]}"
;;
*)
# The spec actually has a src:dst format
DST="${SPLIT[1]}"
;;
esac
# Remove target_args suffix, if present
echo "${DST%;${ARGS}}"
}
#
# input: spec in the form of "src[:dst][;args]"
# output: "args" if present, "" otherwise.
#
function target_args() {
local SPEC="$1"
local SPLIT=(${SPEC//;/ })
local ARGS=
case ${#SPLIT[@]} in
1)
# No ";" delimiter in the spec.
;;
*)
# The "args" are whatever comes after the ";" character.
# Basically the spec stripped of whatever is to the left of ";".
ARGS="${SPEC#${SPLIT[0]};}"
;;
esac
echo "${ARGS}"
}
#
# prefix_match:
#
# input:
# - $1: prefix
# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
# output:
# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
#
function prefix_match() {
local PREFIX="$1"
for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
local FILE=$(target_file "$LINE")
if [[ "$FILE" =~ ^"$PREFIX" ]]; then
local ARGS=$(target_args "$LINE")
if [ -z "${ARGS}" ]; then
echo "${FILE#$PREFIX}"
else
echo "${FILE#$PREFIX};${ARGS}"
fi
fi
done
}
#
# prefix_match_file:
#
# $1: the prefix to match on
# $2: the file to match the prefix for
#
# Internal function which returns true if a filename contains the
# specified prefix.
#
function prefix_match_file() {
local PREFIX="$1"
local FILE="$2"
if [[ "$FILE" =~ ^"$PREFIX" ]]; then
return 0
else
return 1
fi
}
#
# suffix_match_file:
#
# $1: the suffix to match on
# $2: the file to match the suffix for
#
# Internal function which returns true if a filename contains the
# specified suffix.
#
function suffix_match_file() {
local SUFFIX="$1"
local FILE="$2"
if [[ "$FILE" = *"$SUFFIX" ]]; then
return 0
else
return 1
fi
}
#
# truncate_file
#
# $1: the filename to truncate
# $2: the argument to output the truncated filename to
#
# Internal function which truncates a filename by removing the first dir
# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
#
function truncate_file() {
local FILE="$1"
RETURN_FILE="$2"
local FIND="${FILE%%/*}"
local LOCATION="${#FIND}+1"
echo ${FILE:$LOCATION}
}
#
# write_product_copy_files:
#
# $1: make treble compatible makefile - optional and deprecated, default to true
#
# Creates the PRODUCT_COPY_FILES section in the product makefile for all
# items in the list which do not start with a dash (-).
#
function write_product_copy_files() {
local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
local TARGET=
local FILE=
local LINEEND=
local TREBLE_COMPAT=$1
if [ "$COUNT" -eq "0" ]; then
return 0
fi
printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
for (( i=1; i<COUNT+1; i++ )); do
FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
LINEEND=" \\"
if [ "$i" -eq "$COUNT" ]; then
LINEEND=""
fi
TARGET=$(target_file "$FILE")
if prefix_match_file "product/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "system/product/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_PRODUCT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "system_ext/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "system/system_ext/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM_EXT)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "odm/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "vendor/odm/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "system/vendor/odm/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_ODM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "vendor/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "system/vendor/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
elif prefix_match_file "system/" $TARGET ; then
local OUTTARGET=$(truncate_file $TARGET)
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
else
printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_SYSTEM)/%s%s\n' \
"$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
fi
done
return 0
}
#
# write_blueprint_packages:
#
# $1: The LOCAL_MODULE_CLASS for the given module list
# $2: /system, /odm, /product, /system_ext, or /vendor partition
# $3: type-specific extra flags
# $4: Name of the array holding the target list
#
# Internal function which writes out the BUILD_PREBUILT stanzas
# for all modules in the list. This is called by write_product_packages
# after the modules are categorized.
#
function write_blueprint_packages() {
local CLASS="$1"
local PARTITION="$2"
local EXTRA="$3"
# Yes, this is a horrible hack - we create a new array using indirection
local ARR_NAME="$4[@]"
local FILELIST=("${!ARR_NAME}")
local FILE=
local ARGS=
local BASENAME=
local EXTENSION=
local PKGNAME=
local SRC=
local OVERRIDEPKG=
for P in "${FILELIST[@]}"; do
FILE=$(target_file "$P")
ARGS=$(target_args "$P")
BASENAME=$(basename "$FILE")
DIRNAME=$(dirname "$FILE")
EXTENSION=${BASENAME##*.}
PKGNAME=${BASENAME%.*}
# Add to final package list
PACKAGE_LIST+=("$PKGNAME")
SRC="proprietary"
if [ "$PARTITION" = "system" ]; then
SRC+="/system"
elif [ "$PARTITION" = "vendor" ]; then
SRC+="/vendor"
elif [ "$PARTITION" = "product" ]; then
SRC+="/product"
elif [ "$PARTITION" = "system_ext" ]; then
SRC+="/system_ext"
elif [ "$PARTITION" = "odm" ]; then
SRC+="/odm"
fi
if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
printf 'cc_prebuilt_library_shared {\n'
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "$VENDOR"
printf '\tstrip: {\n'
printf '\t\tnone: true,\n'
printf '\t},\n'
printf '\ttarget: {\n'
if [ "$EXTRA" = "both" ]; then
printf '\t\tandroid_arm: {\n'
printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
printf '\t\t},\n'
printf '\t\tandroid_arm64: {\n'
printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
printf '\t\t},\n'
elif [ "$EXTRA" = "64" ]; then
printf '\t\tandroid_arm64: {\n'
printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
printf '\t\t},\n'
else
printf '\t\tandroid_arm: {\n'
printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
printf '\t\t},\n'
fi
printf '\t},\n'
if [ "$EXTRA" != "none" ]; then
printf '\tcompile_multilib: "%s",\n' "$EXTRA"
fi
printf '\tcheck_elf_files: false,\n'
elif [ "$CLASS" = "APPS" ]; then
printf 'android_app_import {\n'
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "$VENDOR"
if [ "$EXTRA" = "priv-app" ]; then
SRC="$SRC/priv-app"
else
SRC="$SRC/app"
fi
printf '\tapk: "%s/%s",\n' "$SRC" "$FILE"
ARGS=(${ARGS//;/ })
USE_PLATFORM_CERTIFICATE="true"
for ARG in "${ARGS[@]}"; do
if [ "$ARG" = "PRESIGNED" ]; then
USE_PLATFORM_CERTIFICATE="false"
printf '\tpresigned: true,\n'
elif [[ "$ARG" =~ "OVERRIDES" ]]; then
OVERRIDEPKG=${ARG#*=}
OVERRIDEPKG=${OVERRIDEPKG//,/\", \"}
printf '\toverrides: ["%s"],\n' "$OVERRIDEPKG"
elif [ ! -z "$ARG" ]; then
USE_PLATFORM_CERTIFICATE="false"
printf '\tcertificate: "%s",\n' "$ARG"
fi
done
if [ "$USE_PLATFORM_CERTIFICATE" = "true" ]; then
printf '\tcertificate: "platform",\n'
fi
elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
printf 'dex_import {\n'
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "$VENDOR"
printf '\tjars: ["%s/framework/%s"],\n' "$SRC" "$FILE"
elif [ "$CLASS" = "ETC" ]; then
if [ "$EXTENSION" = "xml" ]; then
printf 'prebuilt_etc_xml {\n'
else
printf 'prebuilt_etc {\n'
fi
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "$VENDOR"
printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
printf '\tfilename_from_src: true,\n'
elif [ "$CLASS" = "EXECUTABLES" ]; then
if [ "$EXTENSION" = "sh" ]; then
printf 'sh_binary {\n'
else
printf 'cc_prebuilt_binary {\n'
fi
printf '\tname: "%s",\n' "$PKGNAME"
printf '\towner: "%s",\n' "$VENDOR"
if [ "$ARGS" = "rootfs" ]; then
SRC="$SRC/rootfs"
if [ "$EXTRA" = "sbin" ]; then
SRC="$SRC/sbin"
printf '\tdist {\n'
printf '\t\tdest: "%s",\n' "root/sbin"
printf '\t},'
fi
else
SRC="$SRC/bin"
fi
printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
if [ "$EXTENSION" != "sh" ]; then
printf '\tcheck_elf_files: false,\n'
fi
unset EXTENSION
else
printf '\tsrcs: ["%s/%s"],\n' "$SRC" "$FILE"
fi
if [ "$CLASS" = "APPS" ]; then
printf '\tdex_preopt: {\n'
printf '\t\tenabled: false,\n'
printf '\t},\n'
fi
if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
if [ "$DIRNAME" != "." ]; then
printf '\trelative_install_path: "%s",\n' "$DIRNAME"
fi
fi
if [ "$CLASS" = "ETC" ] ; then
if [ "$DIRNAME" != "." ]; then
printf '\tsub_dir: "%s",\n' "$DIRNAME"
fi
fi
if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ] ; then
printf '\tprefer: true,\n'
fi
if [ "$EXTRA" = "priv-app" ]; then
printf '\tprivileged: true,\n'
fi
if [ "$PARTITION" = "vendor" ]; then
printf '\tsoc_specific: true,\n'
elif [ "$PARTITION" = "product" ]; then
printf '\tproduct_specific: true,\n'
elif [ "$PARTITION" = "system_ext" ]; then
printf '\tsystem_ext_specific: true,\n'
elif [ "$PARTITION" = "odm" ]; then
printf '\tdevice_specific: true,\n'
fi
printf '}\n\n'
done
}
#
# write_makefile_packages:
#
# $1: The LOCAL_MODULE_CLASS for the given module list
# $2: /odm, /product, /system_ext, or /vendor partition
# $3: type-specific extra flags
# $4: Name of the array holding the target list
#
# Internal function which writes out the BUILD_PREBUILT stanzas
# for all modules in the list. This is called by write_product_packages
# after the modules are categorized.
#
function write_makefile_packages() {
local CLASS="$1"
local PARTITION="$2"
local EXTRA="$3"
# Yes, this is a horrible hack - we create a new array using indirection
local ARR_NAME="$4[@]"
local FILELIST=("${!ARR_NAME}")
local FILE=
local ARGS=
local BASENAME=
local EXTENSION=
local PKGNAME=
local SRC=
for P in "${FILELIST[@]}"; do
FILE=$(target_file "$P")
ARGS=$(target_args "$P")
BASENAME=$(basename "$FILE")
DIRNAME=$(dirname "$FILE")
EXTENSION=${BASENAME##*.}
EXTENSION="."$EXTENSION
if [ "$EXTENSION" = ".jar" ]; then
EXTENSION="\$(COMMON_JAVA_PACKAGE_SUFFIX)"
elif [ "$EXTENSION" = ".apk" ]; then
EXTENSION="\$(COMMON_ANDROID_PACKAGE_SUFFIX)"
fi
PKGNAME=${BASENAME%.*}
# Add to final package list
PACKAGE_LIST+=("$PKGNAME")
SRC="proprietary"
if [ "$PARTITION" = "system" ]; then
SRC+="/system"
elif [ "$PARTITION" = "vendor" ]; then
SRC+="/vendor"
elif [ "$PARTITION" = "product" ]; then
SRC+="/product"
elif [ "$PARTITION" = "system_ext" ]; then
SRC+="/system_ext"
elif [ "$PARTITION" = "odm" ]; then
SRC+="/odm"
fi
printf 'include $(CLEAR_VARS)\n'
printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
if [ "$EXTRA" = "both" ]; then
printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
#if [ "$VENDOR_PKG" = "true" ]; then
# echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
# echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
#else
# echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
# echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
#fi
elif [ "$EXTRA" = "64" ]; then
printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
else
printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
fi
if [ "$EXTRA" != "none" ]; then
printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
fi
elif [ "$CLASS" = "APPS" ]; then
if [ "$EXTRA" = "priv-app" ]; then
SRC="$SRC/priv-app"
else
SRC="$SRC/app"
fi
printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
local CERT=platform
if [ ! -z "$ARGS" ]; then
CERT="$ARGS"
fi
printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
local CERT=platform
if [ ! -z "$ARGS" ]; then
CERT="$ARGS"
fi
printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
elif [ "$CLASS" = "ETC" ]; then
printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
elif [ "$CLASS" = "EXECUTABLES" ]; then
if [ "$ARGS" = "rootfs" ]; then
SRC="$SRC/rootfs"
if [ "$EXTRA" = "sbin" ]; then
SRC="$SRC/sbin"
printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
fi
else
SRC="$SRC/bin"
fi
printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
unset EXTENSION
else
printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
fi
printf 'LOCAL_MODULE_TAGS := optional\n'
printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
if [ "$CLASS" = "APPS" ]; then
printf 'LOCAL_DEX_PREOPT := false\n'
fi
if [ ! -z "$EXTENSION" ]; then
printf 'LOCAL_MODULE_SUFFIX := %s\n' "$EXTENSION"
fi
if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
if [ "$DIRNAME" != "." ]; then
printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
fi
fi
if [ "$EXTRA" = "priv-app" ]; then
printf 'LOCAL_PRIVILEGED_MODULE := true\n'
fi
if [ "$PARTITION" = "vendor" ]; then
printf 'LOCAL_VENDOR_MODULE := true\n'
elif [ "$PARTITION" = "product" ]; then
printf 'LOCAL_PRODUCT_MODULE := true\n'
elif [ "$PARTITION" = "system_ext" ]; then
printf 'LOCAL_SYSTEM_EXT_MODULE := true\n'
elif [ "$PARTITION" = "odm" ]; then
printf 'LOCAL_ODM_MODULE := true\n'
fi
printf 'include $(BUILD_PREBUILT)\n\n'
done
}
#
# write_product_packages:
#
# This function will create prebuilt entries in the
# Android.bp and associated PRODUCT_PACKAGES list in the
# product makefile for all files in the blob list which
# start with a single dash (-) character.
#
function write_product_packages() {
PACKAGE_LIST=()
local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
if [ "$COUNT" = "0" ]; then
return 0
fi
# Figure out what's 32-bit, what's 64-bit, and what's multilib
# I really should not be doing this in bash due to shitty array passing :(
local T_LIB32=( $(prefix_match "lib/") )
local T_LIB64=( $(prefix_match "lib64/") )
local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
if [ "${#MULTILIBS[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "" "both" "MULTILIBS" >> "$ANDROIDBP"
fi
if [ "${#LIB32[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "" "32" "LIB32" >> "$ANDROIDBP"
fi
if [ "${#LIB64[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "" "64" "LIB64" >> "$ANDROIDBP"
fi
local T_S_LIB32=( $(prefix_match "system/lib/") )
local T_S_LIB64=( $(prefix_match "system/lib64/") )
local S_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${T_S_LIB64[@]}")) )
local S_LIB32=( $(comm -23 <(printf '%s\n' "${T_S_LIB32[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
local S_LIB64=( $(comm -23 <(printf '%s\n' "${T_S_LIB64[@]}") <(printf '%s\n' "${S_MULTILIBS[@]}")) )
if [ "${#S_MULTILIBS[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "system" "both" "S_MULTILIBS" >> "$ANDROIDBP"
fi
if [ "${#S_LIB32[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "system" "32" "S_LIB32" >> "$ANDROIDBP"
fi
if [ "${#S_LIB64[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "system" "64" "S_LIB64" >> "$ANDROIDBP"
fi
local T_V_LIB32=( $(prefix_match "vendor/lib/") )
local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "vendor" "both" "V_MULTILIBS" >> "$ANDROIDBP"
fi
if [ "${#V_LIB32[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "vendor" "32" "V_LIB32" >> "$ANDROIDBP"
fi
if [ "${#V_LIB64[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "vendor" "64" "V_LIB64" >> "$ANDROIDBP"
fi
local T_P_LIB32=( $(prefix_match "product/lib/") )
local T_P_LIB64=( $(prefix_match "product/lib64/") )
local P_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${T_P_LIB64[@]}")) )
local P_LIB32=( $(comm -23 <(printf '%s\n' "${T_P_LIB32[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
local P_LIB64=( $(comm -23 <(printf '%s\n' "${T_P_LIB64[@]}") <(printf '%s\n' "${P_MULTILIBS[@]}")) )
if [ "${#P_MULTILIBS[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "product" "both" "P_MULTILIBS" >> "$ANDROIDBP"
fi
if [ "${#P_LIB32[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "product" "32" "P_LIB32" >> "$ANDROIDBP"
fi
if [ "${#P_LIB64[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "product" "64" "P_LIB64" >> "$ANDROIDBP"
fi
local T_SE_LIB32=( $(prefix_match "system_ext/lib/") )
local T_SE_LIB64=( $(prefix_match "system_ext/lib64/") )
local SE_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${T_SE_LIB64[@]}")) )
local SE_LIB32=( $(comm -23 <(printf '%s\n' "${T_SE_LIB32[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
local SE_LIB64=( $(comm -23 <(printf '%s\n' "${T_SE_LIB64[@]}") <(printf '%s\n' "${SE_MULTILIBS[@]}")) )
if [ "${#SE_MULTILIBS[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "both" "SE_MULTILIBS" >> "$ANDROIDBP"
fi
if [ "${#SE_LIB32[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "32" "SE_LIB32" >> "$ANDROIDBP"
fi
if [ "${#SE_LIB64[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "system_ext" "64" "SE_LIB64" >> "$ANDROIDBP"
fi
local T_O_LIB32=( $(prefix_match "odm/lib/") )
local T_O_LIB64=( $(prefix_match "odm/lib64/") )
local O_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${T_O_LIB64[@]}")) )
local O_LIB32=( $(comm -23 <(printf '%s\n' "${T_O_LIB32[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
local O_LIB64=( $(comm -23 <(printf '%s\n' "${T_O_LIB64[@]}") <(printf '%s\n' "${O_MULTILIBS[@]}")) )
if [ "${#O_MULTILIBS[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "odm" "both" "O_MULTILIBS" >> "$ANDROIDBP"
fi
if [ "${#O_LIB32[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "odm" "32" "O_LIB32" >> "$ANDROIDBP"
fi
if [ "${#O_LIB64[@]}" -gt "0" ]; then
write_blueprint_packages "SHARED_LIBRARIES" "odm" "64" "O_LIB64" >> "$ANDROIDBP"
fi
# Apps
local APPS=( $(prefix_match "app/") )
if [ "${#APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "" "" "APPS" >> "$ANDROIDBP"
fi
local PRIV_APPS=( $(prefix_match "priv-app/") )
if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "" "priv-app" "PRIV_APPS" >> "$ANDROIDBP"
fi
local S_APPS=( $(prefix_match "system/app/") )
if [ "${#S_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "system" "" "S_APPS" >> "$ANDROIDBP"
fi
local S_PRIV_APPS=( $(prefix_match "system/priv-app/") )
if [ "${#S_PRIV_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "system" "priv-app" "S_PRIV_APPS" >> "$ANDROIDBP"
fi
local V_APPS=( $(prefix_match "vendor/app/") )
if [ "${#V_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "vendor" "" "V_APPS" >> "$ANDROIDBP"
fi
local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "vendor" "priv-app" "V_PRIV_APPS" >> "$ANDROIDBP"
fi
local P_APPS=( $(prefix_match "product/app/") )
if [ "${#P_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "product" "" "P_APPS" >> "$ANDROIDBP"
fi
local P_PRIV_APPS=( $(prefix_match "product/priv-app/") )
if [ "${#P_PRIV_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "product" "priv-app" "P_PRIV_APPS" >> "$ANDROIDBP"
fi
local SE_APPS=( $(prefix_match "system_ext/app/") )
if [ "${#SE_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "system_ext" "" "SE_APPS" >> "$ANDROIDBP"
fi
local SE_PRIV_APPS=( $(prefix_match "system_ext/priv-app/") )
if [ "${#SE_PRIV_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "system_ext" "priv-app" "SE_PRIV_APPS" >> "$ANDROIDBP"
fi
local O_APPS=( $(prefix_match "odm/app/") )
if [ "${#O_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "odm" "" "O_APPS" >> "$ANDROIDBP"
fi
local O_PRIV_APPS=( $(prefix_match "odm/priv-app/") )
if [ "${#O_PRIV_APPS[@]}" -gt "0" ]; then
write_blueprint_packages "APPS" "odm" "priv-app" "O_PRIV_APPS" >> "$ANDROIDBP"
fi
# Framework
local FRAMEWORK=( $(prefix_match "framework/") )
if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
write_blueprint_packages "JAVA_LIBRARIES" "" "" "FRAMEWORK" >> "$ANDROIDBP"
fi
local S_FRAMEWORK=( $(prefix_match "system/framework/") )
if [ "${#S_FRAMEWORK[@]}" -gt "0" ]; then
write_blueprint_packages "JAVA_LIBRARIES" "system" "" "S_FRAMEWORK" >> "$ANDROIDBP"
fi
local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
write_blueprint_packages "JAVA_LIBRARIES" "vendor" "" "V_FRAMEWORK" >> "$ANDROIDBP"
fi
local P_FRAMEWORK=( $(prefix_match "product/framework/") )
if [ "${#P_FRAMEWORK[@]}" -gt "0" ]; then
write_blueprint_packages "JAVA_LIBRARIES" "product" "" "P_FRAMEWORK" >> "$ANDROIDBP"
fi
local SE_FRAMEWORK=( $(prefix_match "system_ext/framework/") )
if [ "${#SE_FRAMEWORK[@]}" -gt "0" ]; then
write_blueprint_packages "JAVA_LIBRARIES" "system_ext" "" "SE_FRAMEWORK" >> "$ANDROIDBP"
fi
local O_FRAMEWORK=( $(prefix_match "odm/framework/") )
if [ "${#O_FRAMEWORK[@]}" -gt "0" ]; then
write_blueprint_packages "JAVA_LIBRARIES" "odm" "" "O_FRAMEWORK" >> "$ANDROIDBP"
fi
# Etc
local ETC=( $(prefix_match "etc/") )
if [ "${#ETC[@]}" -gt "0" ]; then
write_blueprint_packages "ETC" "" "" "ETC" >> "$ANDROIDBP"
fi
local S_ETC=( $(prefix_match "system/etc/") )
if [ "${#ETC[@]}" -gt "0" ]; then
write_blueprint_packages "ETC" "system" "" "S_ETC" >> "$ANDROIDBP"
fi
local V_ETC=( $(prefix_match "vendor/etc/") )
if [ "${#V_ETC[@]}" -gt "0" ]; then
write_blueprint_packages "ETC" "vendor" "" "V_ETC" >> "$ANDROIDBP"
fi
local P_ETC=( $(prefix_match "product/etc/") )
if [ "${#P_ETC[@]}" -gt "0" ]; then
write_blueprint_packages "ETC" "product" "" "P_ETC" >> "$ANDROIDBP"
fi
local SE_ETC=( $(prefix_match "system_ext/etc/") )
if [ "${#SE_ETC[@]}" -gt "0" ]; then
write_blueprint_packages "ETC" "system_ext" "" "SE_ETC" >> "$ANDROIDBP"
fi
local O_ETC=( $(prefix_match "odm/etc/") )
if [ "${#O_ETC[@]}" -gt "0" ]; then
write_blueprint_packages "ETC" "odm" "" "O_ETC" >> "$ANDROIDBP"
fi
# Executables
local BIN=( $(prefix_match "bin/") )
if [ "${#BIN[@]}" -gt "0" ]; then
write_blueprint_packages "EXECUTABLES" "" "" "BIN" >> "$ANDROIDBP"
fi
local S_BIN=( $(prefix_match "system/bin/") )
if [ "${#BIN[@]}" -gt "0" ]; then
write_blueprint_packages "EXECUTABLES" "system" "" "S_BIN" >> "$ANDROIDBP"
fi
local V_BIN=( $(prefix_match "vendor/bin/") )
if [ "${#V_BIN[@]}" -gt "0" ]; then
write_blueprint_packages "EXECUTABLES" "vendor" "" "V_BIN" >> "$ANDROIDBP"
fi
local P_BIN=( $(prefix_match "product/bin/") )
if [ "${#P_BIN[@]}" -gt "0" ]; then
write_blueprint_packages "EXECUTABLES" "product" "" "P_BIN" >> "$ANDROIDBP"
fi
local SE_BIN=( $(prefix_match "system_ext/bin/") )
if [ "${#SE_BIN[@]}" -gt "0" ]; then
write_blueprint_packages "EXECUTABLES" "system_ext" "" "SE_BIN" >> "$ANDROIDBP"
fi
local O_BIN=( $(prefix_match "odm/bin/") )
if [ "${#O_BIN[@]}" -gt "0" ]; then
write_blueprint_packages "EXECUTABLES" "odm" "" "O_BIN" >> "$ANDROIDBP"
fi
local SBIN=( $(prefix_match "sbin/") )
if [ "${#SBIN[@]}" -gt "0" ]; then
write_makefile_packages "EXECUTABLES" "" "sbin" "SBIN" >> "$ANDROIDMK"
fi
# Actually write out the final PRODUCT_PACKAGES list
local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
if [ "$PACKAGE_COUNT" -eq "0" ]; then
return 0
fi
printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
local LINEEND=" \\"
if [ "$i" -eq "$PACKAGE_COUNT" ]; then
LINEEND=""
fi
printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
done
}
#
# write_blueprint_header:
#
# $1: file which will be written to
#
# writes out the warning message regarding manual file modifications.
# note that this is not an append operation, and should
# be executed first!
#
function write_blueprint_header() {
if [ -f $1 ]; then
rm $1
fi
[ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
cat << EOF >> $1
// Automatically generated file. DO NOT MODIFY
//
// This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
EOF
}
#
# write_makefile_header:
#
# $1: file which will be written to
#
# writes out the warning message regarding manual file modifications.
# note that this is not an append operation, and should
# be executed first!
#
function write_makefile_header() {
if [ -f $1 ]; then
rm $1
fi
[ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
cat << EOF >> $1
# Automatically generated file. DO NOT MODIFY
#
# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
EOF
}
#
# write_headers:
#
# $1: devices falling under common to be added to guard - optional
# $2: custom guard - optional
#
# Calls write_makefile_header for each of the makefiles and
# write_blueprint_header for Android.bp and creates the initial
# path declaration and device guard for the Android.mk
#
function write_headers() {
write_makefile_header "$ANDROIDMK"
GUARD="$2"
if [ -z "$GUARD" ]; then
GUARD="TARGET_DEVICE"
fi
cat << EOF >> "$ANDROIDMK"
LOCAL_PATH := \$(call my-dir)
EOF
if [ "$COMMON" -ne 1 ]; then
cat << EOF >> "$ANDROIDMK"
ifeq (\$($GUARD),$DEVICE)
EOF
else
if [ -z "$1" ]; then
echo "Argument with devices to be added to guard must be set!"
exit 1
fi
cat << EOF >> "$ANDROIDMK"
ifneq (\$(filter $1,\$($GUARD)),)
EOF
fi
write_makefile_header "$BOARDMK"
write_makefile_header "$PRODUCTMK"
write_blueprint_header "$ANDROIDBP"
cat << EOF >> "$ANDROIDBP"
soong_namespace {
}
EOF
[ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
cat << EOF >> "$PRODUCTMK"
PRODUCT_SOONG_NAMESPACES += \\
vendor/$VENDOR/$DEVICE
EOF
}
#
# write_footers:
#
# Closes the inital guard and any other finalization tasks. Must
# be called as the final step.
#
function write_footers() {
cat << EOF >> "$ANDROIDMK"
endif
EOF
}
# Return success if adb is up and not in recovery
function _adb_connected {
{
if [[ "$(adb get-state)" == device ]]
then
return 0
fi
} 2>/dev/null
return 1
};
#
# parse_file_list:
#
# $1: input file
# $2: blob section in file - optional
#
# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
#
function parse_file_list() {
if [ -z "$1" ]; then
echo "An input file is expected!"
exit 1
elif [ ! -f "$1" ]; then
echo "Input file "$1" does not exist!"
exit 1
fi
if [ -n "$2" ]; then
echo "Using section \"$2\""
LIST=$TMPDIR/files.txt
# Match all lines starting with first line found to start* with '#'
# comment and contain** $2, and ending with first line to be empty*.
# *whitespaces (tabs, spaces) at the beginning of lines are discarded
# **the $2 match is case-insensitive
cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
else
LIST=$1
fi
PRODUCT_PACKAGES_LIST=()
PRODUCT_PACKAGES_HASHES=()
PRODUCT_PACKAGES_FIXUP_HASHES=()
PRODUCT_COPY_FILES_LIST=()
PRODUCT_COPY_FILES_HASHES=()
PRODUCT_COPY_FILES_FIXUP_HASHES=()
while read -r line; do
if [ -z "$line" ]; then continue; fi
# If the line has a pipe delimiter, a sha1 hash should follow.
# This indicates the file should be pinned and not overwritten
# when extracting files.
local SPLIT=(${line//\|/ })
local COUNT=${#SPLIT[@]}
local SPEC=${SPLIT[0]}
local HASH="x"
local FIXUP_HASH="x"
if [ "$COUNT" -gt "1" ]; then
HASH=${SPLIT[1]}
fi
if [ "$COUNT" -gt "2" ]; then
FIXUP_HASH=${SPLIT[2]}
fi
# if line starts with a dash, it needs to be packaged
if [[ "$SPEC" =~ ^- ]]; then
PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
PRODUCT_PACKAGES_HASHES+=("$HASH")
PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
# if line contains apk, jar or vintf fragment, it needs to be packaged
elif suffix_match_file ".apk" "$(src_file "$SPEC")" || \
suffix_match_file ".jar" "$(src_file "$SPEC")" || \
[[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
PRODUCT_PACKAGES_LIST+=("$SPEC")
PRODUCT_PACKAGES_HASHES+=("$HASH")
PRODUCT_PACKAGES_FIXUP_HASHES+=("$FIXUP_HASH")
else
PRODUCT_COPY_FILES_LIST+=("$SPEC")
PRODUCT_COPY_FILES_HASHES+=("$HASH")
PRODUCT_COPY_FILES_FIXUP_HASHES+=("$FIXUP_HASH")
fi
done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
}
#
# write_makefiles:
#
# $1: file containing the list of items to extract
# $2: make treble compatible makefile - optional
#
# Calls write_product_copy_files and write_product_packages on
# the given file and appends to the Android.bp as well as
# the product makefile.
#
function write_makefiles() {
parse_file_list "$1"
write_product_copy_files "$2"
write_product_packages
}
#
# append_firmware_calls_to_makefiles:
#
# Appends to Android.mk the calls to all images present in radio folder
# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
#
function append_firmware_calls_to_makefiles() {
cat << EOF >> "$ANDROIDMK"
ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
\$(call add-radio-file,radio/\$(f)))
\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
endif
EOF
}
#
# get_file:
#
# $1: input file
# $2: target file/folder
# $3: source of the file (can be "adb" or a local folder)
#
# Silently extracts the input file to defined target
# Returns success if file can be pulled from the device or found locally
#
function get_file() {
local SRC="$3"
if [ "$SRC" = "adb" ]; then
# try to pull
adb pull "$1" "$2" >/dev/null 2>&1 && return 0
return 1
else
# try to copy
cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
return 1
fi
};
#
# oat2dex:
#
# $1: extracted apk|jar (to check if deodex is required)
# $2: odexed apk|jar to deodex
# $3: source of the odexed apk|jar
#
# Convert apk|jar .odex in the corresposing classes.dex
#
function oat2dex() {
local CUSTOM_TARGET="$1"
local OEM_TARGET="$2"
local SRC="$3"
local TARGET=
local OAT=
if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
export BAKSMALIJAR="$ANDROID_ROOT"/prebuilts/extract-tools/common/smali/baksmali.jar
export SMALIJAR="$ANDROID_ROOT"/prebuilts/extract-tools/common/smali/smali.jar
fi
if [ -z "$VDEXEXTRACTOR" ]; then
export VDEXEXTRACTOR="$ANDROID_ROOT"/prebuilts/extract-tools/${HOST}-x86/bin/vdexExtractor
fi
if [ -z "$CDEXCONVERTER" ]; then
export CDEXCONVERTER="$ANDROID_ROOT"/prebuilts/extract-tools/${HOST}-x86/bin/compact_dex_converter
fi
# Extract existing boot.oats to the temp folder
if [ -z "$ARCHES" ]; then
echo "Checking if system is odexed and locating boot.oats..."
for ARCH in "arm64" "arm" "x86_64" "x86"; do
mkdir -p "$TMPDIR/system/framework/$ARCH"
if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
ARCHES+="$ARCH "
else
rmdir "$TMPDIR/system/framework/$ARCH"
fi
done
fi
if [ -z "$ARCHES" ]; then
FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
fi
if [ ! -f "$CUSTOM_TARGET" ]; then
return;
fi
if grep "classes.dex" "$CUSTOM_TARGET" >/dev/null; then
return 0 # target apk|jar is already odexed, return
fi
for ARCH in $ARCHES; do
BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
if get_file "$OAT" "$TMPDIR" "$SRC"; then
if get_file "$VDEX" "$TMPDIR" "$SRC"; then
"$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
CLASSES=$(ls "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes"*)
for CLASS in $CLASSES; do
NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
# Check if we have to deal with CompactDex
if [[ "$CLASS" == *.cdex ]]; then
"$CDEXCONVERTER" "$CLASS" &>/dev/null
mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
else
mv "$CLASS" "$TMPDIR/$NEWCLASS"
fi
done
else
java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
fi
elif [[ "$CUSTOM_TARGET" =~ .jar$ ]]; then
JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
if [ ! -f "$JAROAT" ]; then
JAROAT=$BOOTOAT
fi
# try to extract classes.dex from boot.vdex for frameworks jars
# fallback to boot.oat if vdex is not available
if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
"$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
CLASSES=$(ls "$TMPDIR/$(basename "${JARVDEX%.*}")_classes"*)
for CLASS in $CLASSES; do
NEWCLASS=$(echo "$CLASS" | sed 's/.*_//;s/cdex/dex/')
# Check if we have to deal with CompactDex
if [[ "$CLASS" == *.cdex ]]; then
"$CDEXCONVERTER" "$CLASS" &>/dev/null
mv "$CLASS.new" "$TMPDIR/$NEWCLASS"
else
mv "$CLASS" "$TMPDIR/$NEWCLASS"
fi
done
else
java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
fi
else
continue
fi
done
rm -rf "$TMPDIR/dexout"
}
#
# init_adb_connection:
#
# Starts adb server and waits for the device
#
function init_adb_connection() {
adb start-server # Prevent unexpected starting server message from adb get-state in the next line
if ! _adb_connected; then
echo "No device is online. Waiting for one..."
echo "Please connect USB and/or enable USB debugging"
until _adb_connected; do
sleep 1
done
echo "Device Found."
fi
# Retrieve IP and PORT info if we're using a TCP connection
TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
| head -1 | awk '{print $1}')
adb root &> /dev/null
sleep 0.3
if [ -n "$TCPIPPORT" ]; then
# adb root just killed our connection
# so reconnect...
adb connect "$TCPIPPORT"
fi
adb wait-for-device &> /dev/null
sleep 0.3
}
#
# fix_xml:
#
# $1: xml file to fix
#
function fix_xml() {
local XML="$1"
local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
grep -a '^<?xml version' "$XML" > "$TEMP_XML"
grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
mv "$TEMP_XML" "$XML"
}
function get_hash() {
local FILE="$1"
if [ "$(uname)" == "Darwin" ]; then
shasum "${FILE}" | awk '{print $1}'
else
sha1sum "${FILE}" | awk '{print $1}'
fi
}
function print_spec() {
local SPEC_PRODUCT_PACKAGE="$1"
local SPEC_SRC_FILE="$2"
local SPEC_DST_FILE="$3"
local SPEC_ARGS="$4"
local SPEC_HASH="$5"
local SPEC_FIXUP_HASH="$6"
local PRODUCT_PACKAGE=""
if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
PRODUCT_PACKAGE="-"
fi
local SRC=""
if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
SRC="${SPEC_SRC_FILE}:"
fi
local DST=""
if [ ! -z "${SPEC_DST_FILE}" ]; then
DST="${SPEC_DST_FILE}"
fi
local ARGS=""
if [ ! -z "${SPEC_ARGS}" ]; then
ARGS=";${SPEC_ARGS}"
fi
local HASH=""
if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
HASH="|${SPEC_HASH}"
fi
local FIXUP_HASH=""
if [ ! -z "${SPEC_FIXUP_HASH}" ] && [ "${SPEC_FIXUP_HASH}" != "x" ] && [ "${SPEC_FIXUP_HASH}" != "${SPEC_HASH}" ]; then
FIXUP_HASH="|${SPEC_FIXUP_HASH}"
fi
printf '%s%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}" "${FIXUP_HASH}"
}
# To be overridden by device-level extract-files.sh
# Parameters:
# $1: spec name of a blob. Can be used for filtering.
# If the spec is "src:dest", then $1 is "dest".
# If the spec is "src", then $1 is "src".
# $2: path to blob file. Can be used for fixups.
#
function blob_fixup() {
:
}
#
# extract:
#
# Positional parameters:
# $1: file containing the list of items to extract (aka proprietary-files.txt)
# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
#
# Non-positional parameters (coming after $2):
# --section: preferred way of selecting the portion to parse and extract from
# proprietary-files.txt
# --kang: if present, this option will activate the printing of hashes for the
# extracted blobs. Useful with --section for subsequent pinning of
# blobs taken from other origins.
#
function extract() {
# Consume positional parameters
local PROPRIETARY_FILES_TXT="$1"; shift
local SRC="$1"; shift
local SECTION=""
local KANG=false
# Consume optional, non-positional parameters
while [ "$#" -gt 0 ]; do
case "$1" in
-s|--section)
SECTION="$2"; shift
;;
-k|--kang)
KANG=true
DISABLE_PINNING=1
;;
*)
# Backwards-compatibility with the old behavior, where $3, if
# present, denoted an optional positional ${SECTION} argument.
# Users of ${SECTION} are encouraged to migrate from setting it as
# positional $3, to non-positional --section ${SECTION}, the
# reason being that it doesn't scale to have more than 1 optional
# positional argument.
SECTION="$1"
;;
esac
shift
done
if [ -z "$OUTDIR" ]; then
echo "Output dir not set!"
exit 1
fi
parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
# Allow failing, so we can try $DEST and/or $FILE
set +e
local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
local FIXUP_HASHLIST=( ${PRODUCT_COPY_FILES_FIXUP_HASHES[@]} ${PRODUCT_PACKAGES_FIXUP_HASHES[@]} )
local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
local COUNT=${#FILELIST[@]}
local OUTPUT_ROOT="$ANDROID_ROOT"/"$OUTDIR"/proprietary
local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
if [ "$SRC" = "adb" ]; then
init_adb_connection
fi
if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
DUMPDIR="$TMPDIR"/system_dump
# Check if we're working with the same zip that was passed last time.
# If so, let's just use what's already extracted.
MD5=`md5sum "$SRC"| awk '{print $1}'`
OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
if [ "$MD5" != "$OLDMD5" ]; then
rm -rf "$DUMPDIR"
mkdir "$DUMPDIR"
unzip "$SRC" -d "$DUMPDIR"
echo "$MD5" > "$DUMPDIR"/zipmd5.txt
# Stop if an A/B OTA zip is detected. We cannot extract these.
if [ -a "$DUMPDIR"/payload.bin ]; then
echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
exit 1
fi
for PARTITION in "system" "odm" "product" "system_ext" "vendor"
do
# If OTA is block based, extract it.
if [ -a "$DUMPDIR"/"$PARTITION".new.dat.br ]; then
echo "Converting "$PARTITION".new.dat.br to "$PARTITION".new.dat"
brotli -d "$DUMPDIR"/"$PARTITION".new.dat.br
rm "$DUMPDIR"/"$PARTITION".new.dat.br
fi
if [ -a "$DUMPDIR"/"$PARTITION".new.dat ]; then
echo "Converting "$PARTITION".new.dat to "$PARTITION".img"
python "$ANDROID_ROOT"/tools/extract-utils/sdat2img.py "$DUMPDIR"/"$PARTITION".transfer.list "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION".img 2>&1
rm -rf "$DUMPDIR"/"$PARTITION".new.dat "$DUMPDIR"/"$PARTITION"
mkdir "$DUMPDIR"/"$PARTITION" "$DUMPDIR"/tmp
echo "Requesting sudo access to mount the "$PARTITION".img"
sudo mount -o loop "$DUMPDIR"/"$PARTITION".img "$DUMPDIR"/tmp
cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/"$PARTITION"/
sudo umount "$DUMPDIR"/tmp
rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/"$PARTITION".img
fi
done
fi
SRC="$DUMPDIR"
fi
if [ "$VENDOR_STATE" -eq "0" ]; then
echo "Cleaning output directory ($OUTPUT_ROOT).."
rm -rf "${OUTPUT_TMP:?}"
mkdir -p "${OUTPUT_TMP:?}"
if [ -d "$OUTPUT_ROOT" ]; then
mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
fi
VENDOR_STATE=1
fi
echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
for (( i=1; i<COUNT+1; i++ )); do
local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
local OUTPUT_DIR=
local TMP_DIR=
local SRC_FILE=
local DST_FILE=
local IS_PRODUCT_PACKAGE=false
# Note: this relies on the fact that the ${FILELIST[@]} array
# contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
IS_PRODUCT_PACKAGE=true
fi
if [ "${SPEC_ARGS}" = "rootfs" ]; then
OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
TMP_DIR="${OUTPUT_TMP}/rootfs"
SRC_FILE="/${SPEC_SRC_FILE}"
DST_FILE="/${SPEC_DST_FILE}"
else
OUTPUT_DIR="${OUTPUT_ROOT}"
TMP_DIR="${OUTPUT_TMP}"
SRC_FILE="/system/${SPEC_SRC_FILE}"
DST_FILE="/system/${SPEC_DST_FILE}"
fi
# Strip the file path in the vendor repo of "system", if present
local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
local VENDOR_REPO_FILE="$OUTPUT_DIR/${BLOB_DISPLAY_NAME}"
mkdir -p $(dirname "${VENDOR_REPO_FILE}")
# Check pinned files
local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
local FIXUP_HASH="$(echo ${FIXUP_HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
local KEEP=""
if [ "$DISABLE_PINNING" != "1" ] && [ "$HASH" != "x" ]; then
if [ -f "${VENDOR_REPO_FILE}" ]; then
local PINNED="${VENDOR_REPO_FILE}"
else
local PINNED="${TMP_DIR}${DST_FILE#/system}"
fi
if [ -f "$PINNED" ]; then
local TMP_HASH=$(get_hash "${PINNED}")
if [ "${TMP_HASH}" = "${HASH}" ] || [ "${TMP_HASH}" = "${FIXUP_HASH}" ]; then
KEEP="1"
if [ ! -f "${VENDOR_REPO_FILE}" ]; then
cp -p "$PINNED" "${VENDOR_REPO_FILE}"
fi
fi
fi
fi
if [ "${KANG}" = false ]; then
printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
fi
if [ "$KEEP" = "1" ]; then
if [ "${FIXUP_HASH}" != "x" ]; then
printf ' + keeping pinned file with hash %s\n' "${FIXUP_HASH}"
else
printf ' + keeping pinned file with hash %s\n' "${HASH}"
fi
else
FOUND=false
# Try custom target first.
# Also try to search for files stripped of
# the "/system" prefix, if we're actually extracting
# from a system image.
for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
FOUND=true
break
}
done
if [ "${FOUND}" = false ]; then
colored_echo red " !! ${BLOB_DISPLAY_NAME}: file not found in source"
continue
fi
# Blob fixup pipeline has 2 parts: one that is fixed and
# one that is user-configurable
local PRE_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
# Deodex apk|jar if that's the case
if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
if [ -f "$TMPDIR/classes.dex" ]; then
touch -t 200901010000 "$TMPDIR/classes"*
zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes"*
rm "$TMPDIR/classes"*
printf ' (updated %s from odex files)\n' "${SRC_FILE}"
fi
elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
fix_xml "${VENDOR_REPO_FILE}"
fi
# Now run user-supplied fixup function
blob_fixup "${BLOB_DISPLAY_NAME}" "${VENDOR_REPO_FILE}"
local POST_FIXUP_HASH=$(get_hash ${VENDOR_REPO_FILE})
if [ -f "${VENDOR_REPO_FILE}" ]; then
local DIR=$(dirname "${VENDOR_REPO_FILE}")
local TYPE="${DIR##*/}"
if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
chmod 755 "${VENDOR_REPO_FILE}"
else
chmod 644 "${VENDOR_REPO_FILE}"
fi
fi
if [ "${KANG}" = true ]; then
print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${PRE_FIXUP_HASH}" "${POST_FIXUP_HASH}"
fi
# Check and print whether the fixup pipeline actually did anything.
# This isn't done right after the fixup pipeline because we want this print
# to come after print_spec above, when in kang mode.
if [ "${PRE_FIXUP_HASH}" != "${POST_FIXUP_HASH}" ]; then
printf " + Fixed up %s\n" "${BLOB_DISPLAY_NAME}"
# Now sanity-check the spec for this blob.
if [ "${KANG}" = false ] && [ "${FIXUP_HASH}" = "x" ] && [ "${HASH}" != "x" ]; then
colored_echo yellow "WARNING: The ${BLOB_DISPLAY_NAME} file was fixed up, but it is pinned."
colored_echo yellow "This is a mistake and you want to either remove the hash completely, or add an extra one."
fi
fi
fi
done
# Don't allow failing
set -e
}
#
# extract_firmware:
#
# $1: file containing the list of items to extract
# $2: path to extracted radio folder
#
function extract_firmware() {
if [ -z "$OUTDIR" ]; then
echo "Output dir not set!"
exit 1
fi
parse_file_list "$1"
# Don't allow failing
set -e
local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
local COUNT=${#FILELIST[@]}
local SRC="$2"
local OUTPUT_DIR="$ANDROID_ROOT"/"$OUTDIR"/radio
if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
echo "Cleaning firmware output directory ($OUTPUT_DIR).."
rm -rf "${OUTPUT_DIR:?}/"*
VENDOR_RADIO_STATE=1
fi
echo "Extracting $COUNT files in $1 from $SRC:"
for (( i=1; i<COUNT+1; i++ )); do
local FILE="${FILELIST[$i-1]}"
printf ' - %s \n' "/radio/$FILE"
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
fi
cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
chmod 644 "$OUTPUT_DIR/$FILE"
done
}
function extract_img_data() {
local image_file="$1"
local out_dir="$2"
local logFile="$TMPDIR/debugfs.log"
if [ ! -d "$out_dir" ]; then
mkdir -p "$out_dir"
fi
if [[ "$HOST_OS" == "Darwin" ]]; then
debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
echo "[-] Failed to extract data from '$image_file'"
abort 1
}
else
debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
do
debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
echo "[-] Failed to extract data from '$image_file'"
abort 1
}
done
fi
local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
if grep -Fq "$symlink_err" "$logFile"; then
echo "[-] Symlinks have not been properly processed from $image_file"
echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
abort 1
fi
}
function array_contains() {
local element
for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
return 1
}
function generate_prop_list_from_image() {
local image_file="$1"
local image_dir="$TMPDIR/image-temp"
local output_list="$2"
local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
local -n skipped_vendor_files="$3"
extract_img_data "$image_file" "$image_dir"
find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
do
if suffix_match_file ".odex" "$FILE" || suffix_match_file ".vdex" "$FILE" ; then
continue
fi
# Skip device defined skipped files since they will be re-generated at build time
if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
continue
fi
echo "vendor/$FILE" >> "$output_list_tmp"
done
# Sort merged file with all lists
LC_ALL=C sort -u "$output_list_tmp" > "$output_list"
# Clean-up
rm -f "$output_list_tmp"
}
function colored_echo() {
IFS=" "
local color=$1;
shift
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
if [ -t 1 ] ; then tput setaf $color; fi
printf '%s\n' "$*"
if [ -t 1 ] ; then tput sgr0; fi
}