blob: aaae3755ab7401ccf88d253750005a8a7c24d687 [file] [log] [blame]
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001#!/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
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_PACKAGES_LIST=()
20PACKAGE_LIST=()
21VENDOR_STATE=-1
22COMMON=-1
23
24#
25# setup_vendor
26#
27# $1: device name
28# $2: vendor name
29# $3: CM root directory
30# $4: is common device - optional, default to false
31# $5: cleanup - optional, default to true
32#
33# Must be called before any other functions can be used. This
34# sets up the internal state for a new vendor configuration.
35#
36function setup_vendor() {
37 local DEVICE="$1"
38 if [ -z "$DEVICE" ]; then
39 echo "\$DEVICE must be set before including this script!"
40 exit 1
41 fi
42
43 export VENDOR="$2"
44 if [ -z "$VENDOR" ]; then
45 echo "\$VENDOR must be set before including this script!"
46 exit 1
47 fi
48
49 export CM_ROOT="$3"
50 if [ ! -d "$CM_ROOT" ]; then
51 echo "\$CM_ROOT must be set and valid before including this script!"
52 exit 1
53 fi
54
55 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
56 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
57 mkdir -p "$CM_ROOT/$OUTDIR"
58 fi
59
60 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/"$DEVICE"-vendor.mk
61 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
62 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
63
64 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
65 COMMON=1
66 else
67 COMMON=0
68 fi
69
70 if [ "$5" == "true" ] || [ "$5" == "1" ]; then
71 VENDOR_STATE=1
72 else
73 VENDOR_STATE=0
74 fi
75}
76
77#
78# target_file:
79#
80# $1: colon delimited list
81#
82# Returns destination filename without args
83#
84function target_file() {
85 local LINE="$1"
86 local SPLIT=(${LINE//:/ })
87 local COUNT=${#SPLIT[@]}
88 if [ "$COUNT" -gt "1" ]; then
89 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
90 printf '%s\n' "${SPLIT[1]}"
91 return 0
92 fi
93 fi
94 printf '%s\n' "${SPLIT[0]}"
95}
96
97#
98# target_args:
99#
100# $1: colon delimited list
101#
102# Returns optional arguments (last value) for given target
103#
104function target_args() {
105 local LINE="$1"
106 local SPLIT=(${LINE//:/ })
107 local COUNT=${#SPLIT[@]}
108 if [ "$COUNT" -gt "1" ]; then
109 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
110 printf '%s\n' "${SPLIT[$COUNT-1]}"
111 fi
112 fi
113}
114
115#
116# prefix_match:
117#
118# $1: the prefix to match on
119#
120# Internal function which loops thru the packages list and returns a new
121# list containing the matched files with the prefix stripped away.
122#
123function prefix_match() {
124 local PREFIX="$1"
125 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
126 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
127 printf '%s\n' "${FILE#$PREFIX}"
128 fi
129 done
130}
131
132#
133# write_product_copy_files:
134#
135# Creates the PRODUCT_COPY_FILES section in the product makefile for all
136# items in the list which do not start with a dash (-).
137#
138function write_product_copy_files() {
139 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
140 local TARGET=
141 local FILE=
142 local LINEEND=
143
144 if [ "$COUNT" -eq "0" ]; then
145 return 0
146 fi
147
148 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
149 for (( i=1; i<COUNT+1; i++ )); do
150 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
151 LINEEND=" \\"
152 if [ "$i" -eq "$COUNT" ]; then
153 LINEEND=""
154 fi
155
156 TARGET=$(target_file "$FILE")
157 printf ' %s/proprietary/%s:system/%s%s\n' \
158 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
159 done
160 return 0
161}
162
163#
164# write_packages:
165#
166# $1: The LOCAL_MODULE_CLASS for the given module list
167# $2: "true" if this package is part of the vendor/ path
Steve Kondika991cf12016-07-28 12:13:12 -0700168# $3: type-specific extra flags
169# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700170#
171# Internal function which writes out the BUILD_PREBUILT stanzas
172# for all modules in the list. This is called by write_product_packages
173# after the modules are categorized.
174#
175function write_packages() {
176
177 local CLASS="$1"
178 local VENDOR_PKG="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700179 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700180
181 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700182 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700183 local FILELIST=("${!ARR_NAME}")
184
185 local FILE=
186 local ARGS=
187 local BASENAME=
188 local EXTENSION=
189 local PKGNAME=
190 local SRC=
191
192 for P in "${FILELIST[@]}"; do
193 FILE=$(target_file "$P")
194 ARGS=$(target_args "$P")
195
196 BASENAME=$(basename "$FILE")
197 EXTENSION=${BASENAME##*.}
198 PKGNAME=${BASENAME%.*}
199
200 # Add to final package list
201 PACKAGE_LIST+=("$PKGNAME")
202
203 SRC="proprietary"
204 if [ "$VENDOR_PKG" = "true" ]; then
205 SRC+="/vendor"
206 fi
207
208 printf 'include $(CLEAR_VARS)\n'
209 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
210 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
211 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700212 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700213 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
214 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
215 #if [ "$VENDOR_PKG" = "true" ]; then
216 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
217 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
218 #else
219 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
220 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
221 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700222 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700223 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
224 else
225 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
226 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700227 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700228 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700229 fi
230 elif [ "$CLASS" = "APPS" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700231 if [ "$EXTRA" = "priv-app" ]; then
232 SRC="$SRC/priv-app"
233 else
234 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700235 fi
236 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
237 local CERT=platform
238 if [ ! -z "$ARGS" ]; then
239 CERT="$ARGS"
240 fi
241 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
242 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
243 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
244 elif [ "$CLASS" = "ETC" ]; then
245 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
246 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700247 if [ "$ARGS" = "rootfs" ]; then
248 SRC="$SRC/rootfs"
249 if [ "$EXTRA" = "sbin" ]; then
250 SRC="$SRC/sbin"
251 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
252 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
253 fi
254 else
255 SRC="$SRC/bin"
256 fi
257 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
258 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700259 else
Steve Kondika991cf12016-07-28 12:13:12 -0700260 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700261 fi
262 printf 'LOCAL_MODULE_TAGS := optional\n'
263 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Steve Kondika991cf12016-07-28 12:13:12 -0700264 if [ ! -z "$EXTENSION" ]; then
265 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
266 fi
267 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700268 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
269 fi
270 if [ "$VENDOR_PKG" = "true" ]; then
271 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
272 fi
273 printf 'include $(BUILD_PREBUILT)\n\n'
274 done
275}
276
277#
278# write_product_packages:
279#
280# This function will create BUILD_PREBUILT entries in the
281# Android.mk and associated PRODUCT_PACKAGES list in the
282# product makefile for all files in the blob list which
283# start with a single dash (-) character.
284#
285function write_product_packages() {
286 PACKAGE_LIST=()
287
288 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
289
290 if [ "$COUNT" = "0" ]; then
291 return 0
292 fi
293
294 # Figure out what's 32-bit, what's 64-bit, and what's multilib
295 # I really should not be doing this in bash due to shitty array passing :(
296 local T_LIB32=( $(prefix_match "lib/") )
297 local T_LIB64=( $(prefix_match "lib64/") )
298 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
299 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700300 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700301
Steve Kondik03ce4002016-07-29 00:00:16 -0700302 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
303 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
304 fi
305 if [ "${#LIB32[@]}" -gt "0" ]; then
306 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
307 fi
308 if [ "${#LIB64[@]}" -gt "0" ]; then
309 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
310 fi
311
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700312 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
313 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
314 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
315 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700316 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700317
318 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700319 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700320 fi
321 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700322 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700323 fi
324 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700325 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700326 fi
327
328 # Apps
329 local APPS=( $(prefix_match "app/") )
330 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700331 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700332 fi
333 local PRIV_APPS=( $(prefix_match "priv-app/") )
334 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700335 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700336 fi
337 local V_APPS=( $(prefix_match "vendor/app/") )
338 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700339 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700340 fi
341 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
342 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700343 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700344 fi
345
346 # Framework
347 local FRAMEWORK=( $(prefix_match "framework/") )
348 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700349 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700350 fi
351
352 # Etc
353 local ETC=( $(prefix_match "etc/") )
354 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700355 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700356 fi
357 local V_ETC=( $(prefix_match "vendor/etc/") )
358 if [ "${#V_ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700359 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700360 fi
361
362 # Executables
363 local BIN=( $(prefix_match "bin/") )
364 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700365 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700366 fi
367 local V_BIN=( $(prefix_match "vendor/bin/") )
368 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700369 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700370 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700371 local SBIN=( $(prefix_match "sbin/") )
372 if [ "${#SBIN[@]}" -gt "0" ]; then
373 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
374 fi
375
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700376
377 # Actually write out the final PRODUCT_PACKAGES list
378 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
379
380 if [ "$PACKAGE_COUNT" -eq "0" ]; then
381 return 0
382 fi
383
384 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
385 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
386 local LINEEND=" \\"
387 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
388 LINEEND=""
389 fi
390 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
391 done
392}
393
394#
395# write_header:
396#
397# $1: file which will be written to
398#
399# writes out the copyright header with the current year.
400# note that this is not an append operation, and should
401# be executed first!
402#
403function write_header() {
404 YEAR=$(date +"%Y")
405
406 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
407
408 cat << EOF > $1
409# Copyright (C) $YEAR The CyanogenMod Project
410#
411# Licensed under the Apache License, Version 2.0 (the "License");
412# you may not use this file except in compliance with the License.
413# You may obtain a copy of the License at
414#
415# http://www.apache.org/licenses/LICENSE-2.0
416#
417# Unless required by applicable law or agreed to in writing, software
418# distributed under the License is distributed on an "AS IS" BASIS,
419# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
420# See the License for the specific language governing permissions and
421# limitations under the License.
422
423# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
424
425EOF
426}
427
428#
429# write_headers:
430#
431# $1: devices falling under common to be added to guard - optional
432#
433# Calls write_header for each of the makefiles and creates
434# the initial path declaration and device guard for the
435# Android.mk
436#
437function write_headers() {
438 write_header "$ANDROIDMK"
439 cat << EOF >> "$ANDROIDMK"
440LOCAL_PATH := \$(call my-dir)
441
442EOF
443 if [ "$COMMON" -ne 1 ]; then
444 cat << EOF >> "$ANDROIDMK"
445ifeq (\$(TARGET_DEVICE),$DEVICE)
446
447EOF
448 else
449 if [ -z "$1" ]; then
450 echo "Argument with devices to be added to guard must be set!"
451 exit 1
452 fi
453 cat << EOF >> "$ANDROIDMK"
454ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
455
456EOF
457 fi
458
459 write_header "$BOARDMK"
460 write_header "$PRODUCTMK"
461}
462
463#
464# write_footers:
465#
466# Closes the inital guard and any other finalization tasks. Must
467# be called as the final step.
468#
469function write_footers() {
470 cat << EOF >> "$ANDROIDMK"
471endif
472EOF
473}
474
475# Return success if adb is up and not in recovery
476function _adb_connected {
477 {
478 if [[ "$(adb get-state)" == device &&
479 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
480 then
481 return 0
482 fi
483 } 2>/dev/null
484
485 return 1
486};
487
488#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100489# parse_file_list:
490#
491# $1: input file
492#
493# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700494#
495function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100496 if [ -z "$1" ]; then
497 echo "An input file is expected!"
498 exit 1
499 elif [ ! -f "$1" ]; then
500 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700501 exit 1
502 fi
503
504 PRODUCT_PACKAGES_LIST=()
505 PRODUCT_COPY_FILES_LIST=()
506
507 while read -r line; do
508 if [ -z "$line" ]; then continue; fi
509
510 # if line starts with a dash, it needs to be packaged
511 if [[ "$line" =~ ^- ]]; then
512 PRODUCT_PACKAGES_LIST+=("${line#-}")
513 else
514 PRODUCT_COPY_FILES_LIST+=("$line")
515 fi
516
517 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
518}
519
520#
521# write_makefiles:
522#
523# $1: file containing the list of items to extract
524#
525# Calls write_product_copy_files and write_product_packages on
526# the given file and appends to the Android.mk as well as
527# the product makefile.
528#
529function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700530 parse_file_list "$1"
531 write_product_copy_files
532 write_product_packages
533}
534
535#
536# init_adb_connection:
537#
538# Starts adb server and waits for the device
539#
540function init_adb_connection() {
541 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
542 if ! _adb_connected; then
543 echo "No device is online. Waiting for one..."
544 echo "Please connect USB and/or enable USB debugging"
545 until _adb_connected; do
546 sleep 1
547 done
548 echo "Device Found."
549 fi
550
551 # Retrieve IP and PORT info if we're using a TCP connection
552 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
553 | head -1 | awk '{print $1}')
554 adb root &> /dev/null
555 sleep 0.3
556 if [ -n "$TCPIPPORT" ]; then
557 # adb root just killed our connection
558 # so reconnect...
559 adb connect "$TCPIPPORT"
560 fi
561 adb wait-for-device &> /dev/null
562 sleep 0.3
563}
564
565#
566# extract:
567#
568# $1: file containing the list of items to extract
569# $2: path to extracted system folder, or "adb" to extract from device
570#
571function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700572 if [ -z "$OUTDIR" ]; then
573 echo "Output dir not set!"
574 exit 1
575 fi
576
577 parse_file_list "$1"
578
579 # Allow failing, so we can try $DEST and/or $FILE
580 set +e
581
582 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
583 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700584 local SRC="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700585 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700586 if [ "$SRC" = "adb" ]; then
587 init_adb_connection
588 fi
589
590 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700591 echo "Cleaning output directory ($OUTPUT_ROOT).."
592 rm -rf "${OUTPUT_ROOT:?}/"*
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700593 VENDOR_STATE=1
594 fi
595
596 echo "Extracting $COUNT files in $1 from $SRC:"
597
598 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700599
600 local FROM=$(target_file "${FILELIST[$i-1]}")
601 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700602 local SPLIT=(${FILELIST[$i-1]//:/ })
603 local FILE="${SPLIT[0]#-}"
Steve Kondika991cf12016-07-28 12:13:12 -0700604 local OUTPUT_DIR="$OUTPUT_ROOT"
605 local TARGET=
606
607 if [ "$ARGS" = "rootfs" ]; then
608 TARGET="$FROM"
609 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700610 else
Steve Kondika991cf12016-07-28 12:13:12 -0700611 TARGET="system/$FROM"
612 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700613 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700614
615 if [ "$SRC" = "adb" ]; then
616 printf ' - %s .. ' "/$TARGET"
617 else
618 printf ' - %s \n' "/$TARGET"
619 fi
620
621 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700622 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
623 mkdir -p "$OUTPUT_DIR/$DIR"
624 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700625 local DEST="$OUTPUT_DIR/$FROM"
626
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700627 if [ "$SRC" = "adb" ]; then
628 # Try CM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700629 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700630 # if file does not exist try OEM target
631 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700632 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700633 fi
634 else
635 # Try OEM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700636 cp "$SRC/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700637 # if file does not exist try CM target
638 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700639 cp "$SRC/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700640 fi
641 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700642
643 local TYPE="${DIR##*/}"
644 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
645 chmod 755 "$DEST"
646 else
647 chmod 644 "$DEST"
648 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700649 done
650
651 # Don't allow failing
652 set -e
653}