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