Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2016 The CyanogenMod 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 | |
| 18 | PRODUCT_COPY_FILES_LIST=() |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 19 | PRODUCT_COPY_FILES_HASHES=() |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 20 | PRODUCT_PACKAGES_LIST=() |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 21 | PRODUCT_PACKAGES_HASHES=() |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 22 | PACKAGE_LIST=() |
| 23 | VENDOR_STATE=-1 |
Louis Popi | a516c2f | 2016-07-25 15:51:13 +0200 | [diff] [blame] | 24 | VENDOR_RADIO_STATE=-1 |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 25 | COMMON=-1 |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 26 | ARCHES= |
| 27 | FULLY_DEODEXED=-1 |
| 28 | |
| 29 | TMPDIR="/tmp/extractfiles.$$" |
| 30 | mkdir "$TMPDIR" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 31 | |
| 32 | # |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 33 | # cleanup |
| 34 | # |
| 35 | # kill our tmpfiles with fire on exit |
| 36 | # |
| 37 | function cleanup() { |
| 38 | rm -rf "${TMPDIR:?}" |
| 39 | } |
| 40 | |
| 41 | trap cleanup EXIT INT TERM ERR |
| 42 | |
| 43 | # |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 44 | # setup_vendor |
| 45 | # |
| 46 | # $1: device name |
| 47 | # $2: vendor name |
| 48 | # $3: CM root directory |
| 49 | # $4: is common device - optional, default to false |
| 50 | # $5: cleanup - optional, default to true |
Rashed Abdel-Tawab | 5f17315 | 2016-10-01 20:33:00 -0400 | [diff] [blame] | 51 | # $6: custom vendor makefile name - optional, default to false |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 52 | # |
| 53 | # Must be called before any other functions can be used. This |
| 54 | # sets up the internal state for a new vendor configuration. |
| 55 | # |
| 56 | function setup_vendor() { |
| 57 | local DEVICE="$1" |
| 58 | if [ -z "$DEVICE" ]; then |
| 59 | echo "\$DEVICE must be set before including this script!" |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | export VENDOR="$2" |
| 64 | if [ -z "$VENDOR" ]; then |
| 65 | echo "\$VENDOR must be set before including this script!" |
| 66 | exit 1 |
| 67 | fi |
| 68 | |
| 69 | export CM_ROOT="$3" |
| 70 | if [ ! -d "$CM_ROOT" ]; then |
| 71 | echo "\$CM_ROOT must be set and valid before including this script!" |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
| 75 | export OUTDIR=vendor/"$VENDOR"/"$DEVICE" |
| 76 | if [ ! -d "$CM_ROOT/$OUTDIR" ]; then |
| 77 | mkdir -p "$CM_ROOT/$OUTDIR" |
| 78 | fi |
| 79 | |
Rashed Abdel-Tawab | 5f17315 | 2016-10-01 20:33:00 -0400 | [diff] [blame] | 80 | VNDNAME="$6" |
| 81 | if [ -z "$VNDNAME" ]; then |
| 82 | VNDNAME="$DEVICE" |
| 83 | fi |
| 84 | |
| 85 | export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 86 | export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk |
| 87 | export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk |
| 88 | |
| 89 | if [ "$4" == "true" ] || [ "$4" == "1" ]; then |
| 90 | COMMON=1 |
| 91 | else |
| 92 | COMMON=0 |
| 93 | fi |
| 94 | |
| 95 | if [ "$5" == "true" ] || [ "$5" == "1" ]; then |
| 96 | VENDOR_STATE=1 |
Louis Popi | a516c2f | 2016-07-25 15:51:13 +0200 | [diff] [blame] | 97 | VENDOR_RADIO_STATE=1 |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 98 | else |
| 99 | VENDOR_STATE=0 |
Louis Popi | a516c2f | 2016-07-25 15:51:13 +0200 | [diff] [blame] | 100 | VENDOR_RADIO_STATE=0 |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 101 | fi |
| 102 | } |
| 103 | |
| 104 | # |
| 105 | # target_file: |
| 106 | # |
| 107 | # $1: colon delimited list |
| 108 | # |
| 109 | # Returns destination filename without args |
| 110 | # |
| 111 | function target_file() { |
| 112 | local LINE="$1" |
| 113 | local SPLIT=(${LINE//:/ }) |
| 114 | local COUNT=${#SPLIT[@]} |
| 115 | if [ "$COUNT" -gt "1" ]; then |
| 116 | if [[ "${SPLIT[1]}" =~ .*/.* ]]; then |
| 117 | printf '%s\n' "${SPLIT[1]}" |
| 118 | return 0 |
| 119 | fi |
| 120 | fi |
| 121 | printf '%s\n' "${SPLIT[0]}" |
| 122 | } |
| 123 | |
| 124 | # |
| 125 | # target_args: |
| 126 | # |
| 127 | # $1: colon delimited list |
| 128 | # |
| 129 | # Returns optional arguments (last value) for given target |
| 130 | # |
| 131 | function target_args() { |
| 132 | local LINE="$1" |
| 133 | local SPLIT=(${LINE//:/ }) |
| 134 | local COUNT=${#SPLIT[@]} |
| 135 | if [ "$COUNT" -gt "1" ]; then |
| 136 | if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then |
| 137 | printf '%s\n' "${SPLIT[$COUNT-1]}" |
| 138 | fi |
| 139 | fi |
| 140 | } |
| 141 | |
| 142 | # |
| 143 | # prefix_match: |
| 144 | # |
| 145 | # $1: the prefix to match on |
| 146 | # |
| 147 | # Internal function which loops thru the packages list and returns a new |
| 148 | # list containing the matched files with the prefix stripped away. |
| 149 | # |
| 150 | function prefix_match() { |
| 151 | local PREFIX="$1" |
| 152 | for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do |
| 153 | if [[ "$FILE" =~ ^"$PREFIX" ]]; then |
| 154 | printf '%s\n' "${FILE#$PREFIX}" |
| 155 | fi |
| 156 | done |
| 157 | } |
| 158 | |
| 159 | # |
| 160 | # write_product_copy_files: |
| 161 | # |
| 162 | # Creates the PRODUCT_COPY_FILES section in the product makefile for all |
| 163 | # items in the list which do not start with a dash (-). |
| 164 | # |
| 165 | function write_product_copy_files() { |
| 166 | local COUNT=${#PRODUCT_COPY_FILES_LIST[@]} |
| 167 | local TARGET= |
| 168 | local FILE= |
| 169 | local LINEEND= |
| 170 | |
| 171 | if [ "$COUNT" -eq "0" ]; then |
| 172 | return 0 |
| 173 | fi |
| 174 | |
| 175 | printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK" |
| 176 | for (( i=1; i<COUNT+1; i++ )); do |
| 177 | FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}" |
| 178 | LINEEND=" \\" |
| 179 | if [ "$i" -eq "$COUNT" ]; then |
| 180 | LINEEND="" |
| 181 | fi |
| 182 | |
| 183 | TARGET=$(target_file "$FILE") |
| 184 | printf ' %s/proprietary/%s:system/%s%s\n' \ |
| 185 | "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK" |
| 186 | done |
| 187 | return 0 |
| 188 | } |
| 189 | |
| 190 | # |
| 191 | # write_packages: |
| 192 | # |
| 193 | # $1: The LOCAL_MODULE_CLASS for the given module list |
| 194 | # $2: "true" if this package is part of the vendor/ path |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 195 | # $3: type-specific extra flags |
| 196 | # $4: Name of the array holding the target list |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 197 | # |
| 198 | # Internal function which writes out the BUILD_PREBUILT stanzas |
| 199 | # for all modules in the list. This is called by write_product_packages |
| 200 | # after the modules are categorized. |
| 201 | # |
| 202 | function write_packages() { |
| 203 | |
| 204 | local CLASS="$1" |
| 205 | local VENDOR_PKG="$2" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 206 | local EXTRA="$3" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 207 | |
| 208 | # Yes, this is a horrible hack - we create a new array using indirection |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 209 | local ARR_NAME="$4[@]" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 210 | local FILELIST=("${!ARR_NAME}") |
| 211 | |
| 212 | local FILE= |
| 213 | local ARGS= |
| 214 | local BASENAME= |
| 215 | local EXTENSION= |
| 216 | local PKGNAME= |
| 217 | local SRC= |
| 218 | |
| 219 | for P in "${FILELIST[@]}"; do |
| 220 | FILE=$(target_file "$P") |
| 221 | ARGS=$(target_args "$P") |
| 222 | |
| 223 | BASENAME=$(basename "$FILE") |
| 224 | EXTENSION=${BASENAME##*.} |
| 225 | PKGNAME=${BASENAME%.*} |
| 226 | |
| 227 | # Add to final package list |
| 228 | PACKAGE_LIST+=("$PKGNAME") |
| 229 | |
| 230 | SRC="proprietary" |
| 231 | if [ "$VENDOR_PKG" = "true" ]; then |
| 232 | SRC+="/vendor" |
| 233 | fi |
| 234 | |
| 235 | printf 'include $(CLEAR_VARS)\n' |
| 236 | printf 'LOCAL_MODULE := %s\n' "$PKGNAME" |
| 237 | printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR" |
| 238 | if [ "$CLASS" = "SHARED_LIBRARIES" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 239 | if [ "$EXTRA" = "both" ]; then |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 240 | printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE" |
| 241 | printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE" |
| 242 | #if [ "$VENDOR_PKG" = "true" ]; then |
| 243 | # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)" |
| 244 | # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)" |
| 245 | #else |
| 246 | # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)" |
| 247 | # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)" |
| 248 | #fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 249 | elif [ "$EXTRA" = "64" ]; then |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 250 | printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE" |
| 251 | else |
| 252 | printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE" |
| 253 | fi |
Steve Kondik | 03ce400 | 2016-07-29 00:00:16 -0700 | [diff] [blame] | 254 | if [ "$EXTRA" != "none" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 255 | printf 'LOCAL_MULTILIB := %s\n' "$EXTRA" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 256 | fi |
| 257 | elif [ "$CLASS" = "APPS" ]; then |
HashBang | d303031 | 2016-08-01 14:36:46 -0400 | [diff] [blame] | 258 | if [ -z "$ARGS" ]; then |
| 259 | if [ "$EXTRA" = "priv-app" ]; then |
| 260 | SRC="$SRC/priv-app" |
| 261 | else |
| 262 | SRC="$SRC/app" |
| 263 | fi |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 264 | fi |
| 265 | printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE" |
| 266 | local CERT=platform |
| 267 | if [ ! -z "$ARGS" ]; then |
| 268 | CERT="$ARGS" |
| 269 | fi |
| 270 | printf 'LOCAL_CERTIFICATE := %s\n' "$CERT" |
| 271 | elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then |
| 272 | printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE" |
Elektroschmock | 082e0ec | 2016-10-04 21:11:43 +0200 | [diff] [blame] | 273 | local CERT=platform |
| 274 | if [ ! -z "$ARGS" ]; then |
| 275 | CERT="$ARGS" |
| 276 | fi |
| 277 | printf 'LOCAL_CERTIFICATE := %s\n' "$CERT" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 278 | elif [ "$CLASS" = "ETC" ]; then |
| 279 | printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE" |
| 280 | elif [ "$CLASS" = "EXECUTABLES" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 281 | if [ "$ARGS" = "rootfs" ]; then |
| 282 | SRC="$SRC/rootfs" |
| 283 | if [ "$EXTRA" = "sbin" ]; then |
| 284 | SRC="$SRC/sbin" |
| 285 | printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)" |
| 286 | printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)" |
| 287 | fi |
| 288 | else |
| 289 | SRC="$SRC/bin" |
| 290 | fi |
| 291 | printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE" |
| 292 | unset EXTENSION |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 293 | else |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 294 | printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 295 | fi |
| 296 | printf 'LOCAL_MODULE_TAGS := optional\n' |
| 297 | printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS" |
Hashbang173 | 3b3a0e1 | 2016-08-28 20:38:45 -0400 | [diff] [blame] | 298 | if [ "$CLASS" = "APPS" ]; then |
| 299 | printf 'LOCAL_DEX_PREOPT := false\n' |
| 300 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 301 | if [ ! -z "$EXTENSION" ]; then |
| 302 | printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION" |
| 303 | fi |
| 304 | if [ "$EXTRA" = "priv-app" ]; then |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 305 | printf 'LOCAL_PRIVILEGED_MODULE := true\n' |
| 306 | fi |
| 307 | if [ "$VENDOR_PKG" = "true" ]; then |
| 308 | printf 'LOCAL_PROPRIETARY_MODULE := true\n' |
| 309 | fi |
| 310 | printf 'include $(BUILD_PREBUILT)\n\n' |
| 311 | done |
| 312 | } |
| 313 | |
| 314 | # |
| 315 | # write_product_packages: |
| 316 | # |
| 317 | # This function will create BUILD_PREBUILT entries in the |
| 318 | # Android.mk and associated PRODUCT_PACKAGES list in the |
| 319 | # product makefile for all files in the blob list which |
| 320 | # start with a single dash (-) character. |
| 321 | # |
| 322 | function write_product_packages() { |
| 323 | PACKAGE_LIST=() |
| 324 | |
| 325 | local COUNT=${#PRODUCT_PACKAGES_LIST[@]} |
| 326 | |
| 327 | if [ "$COUNT" = "0" ]; then |
| 328 | return 0 |
| 329 | fi |
| 330 | |
| 331 | # Figure out what's 32-bit, what's 64-bit, and what's multilib |
| 332 | # I really should not be doing this in bash due to shitty array passing :( |
| 333 | local T_LIB32=( $(prefix_match "lib/") ) |
| 334 | local T_LIB64=( $(prefix_match "lib64/") ) |
| 335 | local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) ) |
| 336 | local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) ) |
Steve Kondik | 60ef86d | 2016-07-20 20:03:40 -0700 | [diff] [blame] | 337 | local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) ) |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 338 | |
Steve Kondik | 03ce400 | 2016-07-29 00:00:16 -0700 | [diff] [blame] | 339 | if [ "${#MULTILIBS[@]}" -gt "0" ]; then |
| 340 | write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK" |
| 341 | fi |
| 342 | if [ "${#LIB32[@]}" -gt "0" ]; then |
| 343 | write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK" |
| 344 | fi |
| 345 | if [ "${#LIB64[@]}" -gt "0" ]; then |
| 346 | write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK" |
| 347 | fi |
| 348 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 349 | local T_V_LIB32=( $(prefix_match "vendor/lib/") ) |
| 350 | local T_V_LIB64=( $(prefix_match "vendor/lib64/") ) |
| 351 | local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) ) |
| 352 | local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) ) |
Steve Kondik | 60ef86d | 2016-07-20 20:03:40 -0700 | [diff] [blame] | 353 | local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) ) |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 354 | |
| 355 | if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 356 | write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 357 | fi |
| 358 | if [ "${#V_LIB32[@]}" -gt "0" ]; then |
Steve Kondik | 03ce400 | 2016-07-29 00:00:16 -0700 | [diff] [blame] | 359 | write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 360 | fi |
| 361 | if [ "${#V_LIB64[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 362 | write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 363 | fi |
| 364 | |
| 365 | # Apps |
| 366 | local APPS=( $(prefix_match "app/") ) |
| 367 | if [ "${#APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 368 | write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 369 | fi |
| 370 | local PRIV_APPS=( $(prefix_match "priv-app/") ) |
| 371 | if [ "${#PRIV_APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 372 | write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 373 | fi |
| 374 | local V_APPS=( $(prefix_match "vendor/app/") ) |
| 375 | if [ "${#V_APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 376 | write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 377 | fi |
| 378 | local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") ) |
| 379 | if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 380 | write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 381 | fi |
| 382 | |
| 383 | # Framework |
| 384 | local FRAMEWORK=( $(prefix_match "framework/") ) |
| 385 | if [ "${#FRAMEWORK[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 386 | write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 387 | fi |
| 388 | |
| 389 | # Etc |
| 390 | local ETC=( $(prefix_match "etc/") ) |
| 391 | if [ "${#ETC[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 392 | write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 393 | fi |
| 394 | local V_ETC=( $(prefix_match "vendor/etc/") ) |
| 395 | if [ "${#V_ETC[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 396 | write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 397 | fi |
| 398 | |
| 399 | # Executables |
| 400 | local BIN=( $(prefix_match "bin/") ) |
| 401 | if [ "${#BIN[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 402 | write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 403 | fi |
| 404 | local V_BIN=( $(prefix_match "vendor/bin/") ) |
| 405 | if [ "${#V_BIN[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 406 | write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 407 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 408 | local SBIN=( $(prefix_match "sbin/") ) |
| 409 | if [ "${#SBIN[@]}" -gt "0" ]; then |
| 410 | write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK" |
| 411 | fi |
| 412 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 413 | |
| 414 | # Actually write out the final PRODUCT_PACKAGES list |
| 415 | local PACKAGE_COUNT=${#PACKAGE_LIST[@]} |
| 416 | |
| 417 | if [ "$PACKAGE_COUNT" -eq "0" ]; then |
| 418 | return 0 |
| 419 | fi |
| 420 | |
| 421 | printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK" |
| 422 | for (( i=1; i<PACKAGE_COUNT+1; i++ )); do |
| 423 | local LINEEND=" \\" |
| 424 | if [ "$i" -eq "$PACKAGE_COUNT" ]; then |
| 425 | LINEEND="" |
| 426 | fi |
| 427 | printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK" |
| 428 | done |
| 429 | } |
| 430 | |
| 431 | # |
| 432 | # write_header: |
| 433 | # |
| 434 | # $1: file which will be written to |
| 435 | # |
| 436 | # writes out the copyright header with the current year. |
| 437 | # note that this is not an append operation, and should |
| 438 | # be executed first! |
| 439 | # |
| 440 | function write_header() { |
| 441 | YEAR=$(date +"%Y") |
| 442 | |
| 443 | [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON" |
| 444 | |
| 445 | cat << EOF > $1 |
| 446 | # Copyright (C) $YEAR The CyanogenMod Project |
| 447 | # |
| 448 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 449 | # you may not use this file except in compliance with the License. |
| 450 | # You may obtain a copy of the License at |
| 451 | # |
| 452 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 453 | # |
| 454 | # Unless required by applicable law or agreed to in writing, software |
| 455 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 456 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 457 | # See the License for the specific language governing permissions and |
| 458 | # limitations under the License. |
| 459 | |
| 460 | # This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh |
| 461 | |
| 462 | EOF |
| 463 | } |
| 464 | |
| 465 | # |
| 466 | # write_headers: |
| 467 | # |
| 468 | # $1: devices falling under common to be added to guard - optional |
Rashed Abdel-Tawab | d53bff1 | 2016-10-02 01:00:54 -0400 | [diff] [blame] | 469 | # $2: custom guard - optional |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 470 | # |
| 471 | # Calls write_header for each of the makefiles and creates |
| 472 | # the initial path declaration and device guard for the |
| 473 | # Android.mk |
| 474 | # |
| 475 | function write_headers() { |
| 476 | write_header "$ANDROIDMK" |
Rashed Abdel-Tawab | d53bff1 | 2016-10-02 01:00:54 -0400 | [diff] [blame] | 477 | |
| 478 | GUARD="$2" |
| 479 | if [ -z "$GUARD" ]; then |
| 480 | GUARD="TARGET_DEVICE" |
| 481 | fi |
| 482 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 483 | cat << EOF >> "$ANDROIDMK" |
| 484 | LOCAL_PATH := \$(call my-dir) |
| 485 | |
| 486 | EOF |
| 487 | if [ "$COMMON" -ne 1 ]; then |
| 488 | cat << EOF >> "$ANDROIDMK" |
Rashed Abdel-Tawab | d53bff1 | 2016-10-02 01:00:54 -0400 | [diff] [blame] | 489 | ifeq (\$($GUARD),$DEVICE) |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 490 | |
| 491 | EOF |
| 492 | else |
| 493 | if [ -z "$1" ]; then |
| 494 | echo "Argument with devices to be added to guard must be set!" |
| 495 | exit 1 |
| 496 | fi |
| 497 | cat << EOF >> "$ANDROIDMK" |
Rashed Abdel-Tawab | d53bff1 | 2016-10-02 01:00:54 -0400 | [diff] [blame] | 498 | ifneq (\$(filter $1,\$($GUARD)),) |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 499 | |
| 500 | EOF |
| 501 | fi |
| 502 | |
| 503 | write_header "$BOARDMK" |
| 504 | write_header "$PRODUCTMK" |
| 505 | } |
| 506 | |
| 507 | # |
| 508 | # write_footers: |
| 509 | # |
| 510 | # Closes the inital guard and any other finalization tasks. Must |
| 511 | # be called as the final step. |
| 512 | # |
| 513 | function write_footers() { |
| 514 | cat << EOF >> "$ANDROIDMK" |
| 515 | endif |
| 516 | EOF |
| 517 | } |
| 518 | |
| 519 | # Return success if adb is up and not in recovery |
| 520 | function _adb_connected { |
| 521 | { |
Steve Kondik | 7561d19 | 2016-09-01 21:40:27 -0700 | [diff] [blame] | 522 | if [[ "$(adb get-state)" == device ]] |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 523 | then |
| 524 | return 0 |
| 525 | fi |
| 526 | } 2>/dev/null |
| 527 | |
| 528 | return 1 |
| 529 | }; |
| 530 | |
| 531 | # |
Bruno Martins | 3b96ba5 | 2016-07-27 15:00:05 +0100 | [diff] [blame] | 532 | # parse_file_list: |
| 533 | # |
| 534 | # $1: input file |
| 535 | # |
| 536 | # Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 537 | # |
| 538 | function parse_file_list() { |
Bruno Martins | 3b96ba5 | 2016-07-27 15:00:05 +0100 | [diff] [blame] | 539 | if [ -z "$1" ]; then |
| 540 | echo "An input file is expected!" |
| 541 | exit 1 |
| 542 | elif [ ! -f "$1" ]; then |
| 543 | echo "Input file "$1" does not exist!" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 544 | exit 1 |
| 545 | fi |
| 546 | |
| 547 | PRODUCT_PACKAGES_LIST=() |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 548 | PRODUCT_PACKAGES_HASHES=() |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 549 | PRODUCT_COPY_FILES_LIST=() |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 550 | PRODUCT_COPY_FILES_HASHES=() |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 551 | |
| 552 | while read -r line; do |
| 553 | if [ -z "$line" ]; then continue; fi |
| 554 | |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 555 | # If the line has a pipe delimiter, a sha1 hash should follow. |
| 556 | # This indicates the file should be pinned and not overwritten |
| 557 | # when extracting files. |
| 558 | local SPLIT=(${line//\|/ }) |
| 559 | local COUNT=${#SPLIT[@]} |
| 560 | local SPEC=${SPLIT[0]} |
| 561 | local HASH="x" |
| 562 | if [ "$COUNT" -gt "1" ]; then |
| 563 | HASH=${SPLIT[1]} |
| 564 | fi |
| 565 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 566 | # if line starts with a dash, it needs to be packaged |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 567 | if [[ "$SPEC" =~ ^- ]]; then |
| 568 | PRODUCT_PACKAGES_LIST+=("${SPEC#-}") |
| 569 | PRODUCT_PACKAGES_HASHES+=("$HASH") |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 570 | else |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 571 | PRODUCT_COPY_FILES_LIST+=("$SPEC") |
| 572 | PRODUCT_COPY_FILES_HASHES+=("$HASH") |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 573 | fi |
| 574 | |
| 575 | done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq) |
| 576 | } |
| 577 | |
| 578 | # |
| 579 | # write_makefiles: |
| 580 | # |
| 581 | # $1: file containing the list of items to extract |
| 582 | # |
| 583 | # Calls write_product_copy_files and write_product_packages on |
| 584 | # the given file and appends to the Android.mk as well as |
| 585 | # the product makefile. |
| 586 | # |
| 587 | function write_makefiles() { |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 588 | parse_file_list "$1" |
| 589 | write_product_copy_files |
| 590 | write_product_packages |
| 591 | } |
| 592 | |
| 593 | # |
Louis Popi | a516c2f | 2016-07-25 15:51:13 +0200 | [diff] [blame] | 594 | # append_firmware_calls_to_makefiles: |
| 595 | # |
| 596 | # Appends to Android.mk the calls to all images present in radio folder |
| 597 | # (filesmap file used by releasetools to map firmware images should be kept in the device tree) |
| 598 | # |
| 599 | function append_firmware_calls_to_makefiles() { |
| 600 | cat << EOF >> "$ANDROIDMK" |
| 601 | ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio)) |
| 602 | |
| 603 | RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*) |
| 604 | \$(foreach f, \$(notdir \$(RADIO_FILES)), \\ |
| 605 | \$(call add-radio-file,radio/\$(f))) |
| 606 | \$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap) |
| 607 | |
| 608 | endif |
| 609 | |
| 610 | EOF |
| 611 | } |
| 612 | |
| 613 | # |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 614 | # get_file: |
| 615 | # |
| 616 | # $1: input file |
| 617 | # $2: target file/folder |
| 618 | # $3: source of the file (can be "adb" or a local folder) |
| 619 | # |
| 620 | # Silently extracts the input file to defined target |
| 621 | # Returns success if file can be pulled from the device or found locally |
| 622 | # |
| 623 | function get_file() { |
| 624 | local SRC="$3" |
| 625 | |
| 626 | if [ "$SRC" = "adb" ]; then |
| 627 | # try to pull |
| 628 | adb pull "$1" "$2" >/dev/null 2>&1 && return 0 |
| 629 | |
| 630 | return 1 |
| 631 | else |
| 632 | # try to copy |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 633 | cp -r "$SRC/$1" "$2" 2>/dev/null && return 0 |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 634 | |
| 635 | return 1 |
| 636 | fi |
| 637 | }; |
| 638 | |
| 639 | # |
| 640 | # oat2dex: |
| 641 | # |
| 642 | # $1: extracted apk|jar (to check if deodex is required) |
| 643 | # $2: odexed apk|jar to deodex |
| 644 | # $3: source of the odexed apk|jar |
| 645 | # |
| 646 | # Convert apk|jar .odex in the corresposing classes.dex |
| 647 | # |
| 648 | function oat2dex() { |
| 649 | local CM_TARGET="$1" |
| 650 | local OEM_TARGET="$2" |
| 651 | local SRC="$3" |
| 652 | local TARGET= |
| 653 | local OAT= |
| 654 | |
| 655 | if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then |
| 656 | export BAKSMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/baksmali.jar |
| 657 | export SMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/smali.jar |
| 658 | fi |
| 659 | |
| 660 | # Extract existing boot.oats to the temp folder |
| 661 | if [ -z "$ARCHES" ]; then |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 662 | echo "Checking if system is odexed and locating boot.oats..." |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 663 | for ARCH in "arm64" "arm" "x86_64" "x86"; do |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 664 | mkdir -p "$TMPDIR/system/framework/$ARCH" |
| 665 | if get_file "system/framework/$ARCH/" "$TMPDIR/system/framework/" "$SRC"; then |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 666 | ARCHES+="$ARCH " |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 667 | else |
| 668 | rmdir "$TMPDIR/system/framework/$ARCH" |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 669 | fi |
| 670 | done |
| 671 | fi |
| 672 | |
| 673 | if [ -z "$ARCHES" ]; then |
| 674 | FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return |
| 675 | fi |
| 676 | |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 677 | if [ ! -f "$CM_TARGET" ]; then |
| 678 | return; |
| 679 | fi |
| 680 | |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 681 | if grep "classes.dex" "$CM_TARGET" >/dev/null; then |
| 682 | return 0 # target apk|jar is already odexed, return |
| 683 | fi |
| 684 | |
| 685 | for ARCH in $ARCHES; do |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 686 | BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat" |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 687 | |
| 688 | local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex" |
| 689 | |
| 690 | if get_file "$OAT" "$TMPDIR" "$SRC"; then |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 691 | java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")" |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 692 | elif [[ "$CM_TARGET" =~ .jar$ ]]; then |
| 693 | # try to extract classes.dex from boot.oat for framework jars |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 694 | java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT" |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 695 | else |
| 696 | continue |
| 697 | fi |
| 698 | |
Sam Mortimer | 2e994ce | 2016-10-05 09:50:49 -0700 | [diff] [blame] | 699 | java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 700 | done |
| 701 | |
| 702 | rm -rf "$TMPDIR/dexout" |
| 703 | } |
| 704 | |
| 705 | # |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 706 | # init_adb_connection: |
| 707 | # |
| 708 | # Starts adb server and waits for the device |
| 709 | # |
| 710 | function init_adb_connection() { |
| 711 | adb start-server # Prevent unexpected starting server message from adb get-state in the next line |
| 712 | if ! _adb_connected; then |
| 713 | echo "No device is online. Waiting for one..." |
| 714 | echo "Please connect USB and/or enable USB debugging" |
| 715 | until _adb_connected; do |
| 716 | sleep 1 |
| 717 | done |
| 718 | echo "Device Found." |
| 719 | fi |
| 720 | |
| 721 | # Retrieve IP and PORT info if we're using a TCP connection |
| 722 | TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \ |
| 723 | | head -1 | awk '{print $1}') |
| 724 | adb root &> /dev/null |
| 725 | sleep 0.3 |
| 726 | if [ -n "$TCPIPPORT" ]; then |
| 727 | # adb root just killed our connection |
| 728 | # so reconnect... |
| 729 | adb connect "$TCPIPPORT" |
| 730 | fi |
| 731 | adb wait-for-device &> /dev/null |
| 732 | sleep 0.3 |
| 733 | } |
| 734 | |
| 735 | # |
Luca Stefani | 3a03012 | 2016-07-30 12:08:25 +0200 | [diff] [blame] | 736 | # fix_xml: |
| 737 | # |
| 738 | # $1: xml file to fix |
| 739 | # |
| 740 | function fix_xml() { |
| 741 | local XML="$1" |
| 742 | local TEMP_XML="$TMPDIR/`basename "$XML"`.temp" |
| 743 | |
| 744 | grep '^<?xml version' "$XML" > "$TEMP_XML" |
| 745 | grep -v '^<?xml version' "$XML" >> "$TEMP_XML" |
| 746 | |
| 747 | mv "$TEMP_XML" "$XML" |
| 748 | } |
| 749 | |
| 750 | # |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 751 | # extract: |
| 752 | # |
| 753 | # $1: file containing the list of items to extract |
| 754 | # $2: path to extracted system folder, or "adb" to extract from device |
| 755 | # |
| 756 | function extract() { |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 757 | if [ -z "$OUTDIR" ]; then |
| 758 | echo "Output dir not set!" |
| 759 | exit 1 |
| 760 | fi |
| 761 | |
| 762 | parse_file_list "$1" |
| 763 | |
| 764 | # Allow failing, so we can try $DEST and/or $FILE |
| 765 | set +e |
| 766 | |
| 767 | local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} ) |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 768 | local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} ) |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 769 | local COUNT=${#FILELIST[@]} |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 770 | local SRC="$2" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 771 | local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 772 | local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary |
| 773 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 774 | if [ "$SRC" = "adb" ]; then |
| 775 | init_adb_connection |
| 776 | fi |
| 777 | |
| 778 | if [ "$VENDOR_STATE" -eq "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 779 | echo "Cleaning output directory ($OUTPUT_ROOT).." |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 780 | rm -rf "${OUTPUT_TMP:?}" |
| 781 | mkdir -p "${OUTPUT_TMP:?}" |
| 782 | mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 783 | VENDOR_STATE=1 |
| 784 | fi |
| 785 | |
| 786 | echo "Extracting $COUNT files in $1 from $SRC:" |
| 787 | |
| 788 | for (( i=1; i<COUNT+1; i++ )); do |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 789 | |
| 790 | local FROM=$(target_file "${FILELIST[$i-1]}") |
| 791 | local ARGS=$(target_args "${FILELIST[$i-1]}") |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 792 | local SPLIT=(${FILELIST[$i-1]//:/ }) |
| 793 | local FILE="${SPLIT[0]#-}" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 794 | local OUTPUT_DIR="$OUTPUT_ROOT" |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 795 | local TMP_DIR="$OUTPUT_TMP" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 796 | local TARGET= |
| 797 | |
| 798 | if [ "$ARGS" = "rootfs" ]; then |
| 799 | TARGET="$FROM" |
| 800 | OUTPUT_DIR="$OUTPUT_DIR/rootfs" |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 801 | TMP_DIR="$TMP_DIR/rootfs" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 802 | else |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 803 | TARGET="system/$FROM" |
| 804 | FILE="system/$FILE" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 805 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 806 | |
| 807 | if [ "$SRC" = "adb" ]; then |
| 808 | printf ' - %s .. ' "/$TARGET" |
| 809 | else |
| 810 | printf ' - %s \n' "/$TARGET" |
| 811 | fi |
| 812 | |
| 813 | local DIR=$(dirname "$FROM") |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 814 | if [ ! -d "$OUTPUT_DIR/$DIR" ]; then |
| 815 | mkdir -p "$OUTPUT_DIR/$DIR" |
| 816 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 817 | local DEST="$OUTPUT_DIR/$FROM" |
| 818 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 819 | if [ "$SRC" = "adb" ]; then |
| 820 | # Try CM target first |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 821 | adb pull "/$TARGET" "$DEST" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 822 | # if file does not exist try OEM target |
| 823 | if [ "$?" != "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 824 | adb pull "/$FILE" "$DEST" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 825 | fi |
| 826 | else |
| 827 | # Try OEM target first |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 828 | if [ -f "$SRC/$FILE" ]; then |
| 829 | cp "$SRC/$FILE" "$DEST" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 830 | # if file does not exist try CM target |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 831 | elif [ -f "$SRC/$TARGET" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 832 | cp "$SRC/$TARGET" "$DEST" |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 833 | else |
| 834 | printf ' !! file not found in source\n' |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 835 | fi |
| 836 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 837 | |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 838 | if [ "$?" == "0" ]; then |
| 839 | # Deodex apk|jar if that's the case |
| 840 | if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then |
| 841 | oat2dex "$DEST" "$FILE" "$SRC" |
| 842 | if [ -f "$TMPDIR/classes.dex" ]; then |
| 843 | zip -gjq "$DEST" "$TMPDIR/classes.dex" |
| 844 | rm "$TMPDIR/classes.dex" |
| 845 | printf ' (updated %s from odex files)\n' "/$FILE" |
| 846 | fi |
Luca Stefani | 3a03012 | 2016-07-30 12:08:25 +0200 | [diff] [blame] | 847 | elif [[ "$DEST" =~ .xml$ ]]; then |
| 848 | fix_xml "$DEST" |
Luca Stefani | 7f9fff2 | 2016-07-18 13:47:55 +0200 | [diff] [blame] | 849 | fi |
| 850 | fi |
| 851 | |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 852 | # Check pinned files |
| 853 | local HASH="${HASHLIST[$i-1]}" |
Steve Kondik | 79fa59b | 2016-09-02 21:10:02 -0700 | [diff] [blame] | 854 | if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 855 | local KEEP="" |
| 856 | local TMP="$TMP_DIR/$FROM" |
| 857 | if [ -f "$TMP" ]; then |
| 858 | if [ ! -f "$DEST" ]; then |
| 859 | KEEP="1" |
| 860 | else |
Rashed Abdel-Tawab | 5b38c4f | 2016-11-23 23:00:37 -0500 | [diff] [blame] | 861 | if [ "$(uname)" == "Darwin" ]; then |
| 862 | local DEST_HASH=$(shasum "$DEST" | awk '{print $1}' ) |
| 863 | else |
| 864 | local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' ) |
| 865 | fi |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 866 | if [ "$DEST_HASH" != "$HASH" ]; then |
| 867 | KEEP="1" |
| 868 | fi |
| 869 | fi |
| 870 | if [ "$KEEP" = "1" ]; then |
Rashed Abdel-Tawab | 5b38c4f | 2016-11-23 23:00:37 -0500 | [diff] [blame] | 871 | if [ "$(uname)" == "Darwin" ]; then |
| 872 | local TMP_HASH=$(shasum "$TMP" | awk '{print $1}' ) |
| 873 | else |
| 874 | local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' ) |
| 875 | fi |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 876 | if [ "$TMP_HASH" = "$HASH" ]; then |
| 877 | printf ' + (keeping pinned file with hash %s)\n' "$HASH" |
| 878 | cp -p "$TMP" "$DEST" |
| 879 | fi |
| 880 | fi |
| 881 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 882 | fi |
Steve Kondik | 48f8df8 | 2016-08-14 03:55:08 -0700 | [diff] [blame] | 883 | |
| 884 | if [ -f "$DEST" ]; then |
| 885 | local TYPE="${DIR##*/}" |
| 886 | if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then |
| 887 | chmod 755 "$DEST" |
| 888 | else |
| 889 | chmod 644 "$DEST" |
| 890 | fi |
| 891 | fi |
| 892 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 893 | done |
| 894 | |
| 895 | # Don't allow failing |
| 896 | set -e |
| 897 | } |
Louis Popi | a516c2f | 2016-07-25 15:51:13 +0200 | [diff] [blame] | 898 | |
| 899 | # |
| 900 | # extract_firmware: |
| 901 | # |
| 902 | # $1: file containing the list of items to extract |
| 903 | # $2: path to extracted radio folder |
| 904 | # |
| 905 | function extract_firmware() { |
| 906 | if [ -z "$OUTDIR" ]; then |
| 907 | echo "Output dir not set!" |
| 908 | exit 1 |
| 909 | fi |
| 910 | |
| 911 | parse_file_list "$1" |
| 912 | |
| 913 | # Don't allow failing |
| 914 | set -e |
| 915 | |
| 916 | local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ) |
| 917 | local COUNT=${#FILELIST[@]} |
| 918 | local SRC="$2" |
| 919 | local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio |
| 920 | |
| 921 | if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then |
| 922 | echo "Cleaning firmware output directory ($OUTPUT_DIR).." |
| 923 | rm -rf "${OUTPUT_DIR:?}/"* |
| 924 | VENDOR_RADIO_STATE=1 |
| 925 | fi |
| 926 | |
| 927 | echo "Extracting $COUNT files in $1 from $SRC:" |
| 928 | |
| 929 | for (( i=1; i<COUNT+1; i++ )); do |
| 930 | local FILE="${FILELIST[$i-1]}" |
| 931 | printf ' - %s \n' "/radio/$FILE" |
| 932 | |
| 933 | if [ ! -d "$OUTPUT_DIR" ]; then |
| 934 | mkdir -p "$OUTPUT_DIR" |
| 935 | fi |
| 936 | cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE" |
| 937 | chmod 644 "$OUTPUT_DIR/$FILE" |
| 938 | done |
| 939 | } |