blob: 2b0fafbf77defc72dbe3aa1e79079d85772c79dd [file] [log] [blame]
Steve Kondik5bd66602016-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_COPY_FILES_HASHES=()
20PRODUCT_PACKAGES_LIST=()
21PRODUCT_PACKAGES_HASHES=()
22PACKAGE_LIST=()
23VENDOR_STATE=-1
24VENDOR_RADIO_STATE=-1
25COMMON=-1
26ARCHES=
27FULLY_DEODEXED=-1
28
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -040029TMPDIR=$(mktemp -d)
Steve Kondik5bd66602016-07-15 10:39:58 -070030
31#
32# cleanup
33#
34# kill our tmpfiles with fire on exit
35#
36function cleanup() {
37 rm -rf "${TMPDIR:?}"
38}
39
40trap 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 Whatley9843b322017-01-25 21:49:16 -050050# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070051#
52# Must be called before any other functions can be used. This
53# sets up the internal state for a new vendor configuration.
54#
55function 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 Whatley9843b322017-01-25 21:49:16 -050079 VNDNAME="$6"
80 if [ -z "$VNDNAME" ]; then
81 VNDNAME="$DEVICE"
82 fi
83
Jake Whatleycb7cd072016-09-18 20:55:12 +020084 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/device-vendor.mk
Steve Kondik5bd66602016-07-15 10:39:58 -070085 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 Mc44696d2017-05-01 18:22:04 +020094 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070095 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#
110function 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#
130function 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#
149function prefix_match() {
150 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300151 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
152 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700153 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
154 printf '%s\n' "${FILE#$PREFIX}"
155 fi
156 done
157}
158
159#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400160# 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#
168function 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#
187function 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 Kondik5bd66602016-07-15 10:39:58 -0700196# write_product_copy_files:
197#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400198# $1: make treble compatible makefile - optional, default to false
199#
Steve Kondik5bd66602016-07-15 10:39:58 -0700200# 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#
203function write_product_copy_files() {
204 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
205 local TARGET=
206 local FILE=
207 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400208 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700209
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-Tawab7fd3ccb2017-10-07 14:18:39 -0400223 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 Kondik5bd66602016-07-15 10:39:58 -0700236 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#
252function 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")
M1cha3e8c5bf2017-01-04 09:00:11 +0100274 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700275 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 Whatley9843b322017-01-25 21:49:16 -0500309 if [ -z "$ARGS" ]; then
310 if [ "$EXTRA" = "priv-app" ]; then
311 SRC="$SRC/priv-app"
312 else
313 SRC="$SRC/app"
314 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700315 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"
Elektroschmockdd792302016-10-04 21:11:43 +0200324 local CERT=platform
325 if [ ! -z "$ARGS" ]; then
326 CERT="$ARGS"
327 fi
328 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700329 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"
Hashbang173575f3bb2016-08-28 20:38:45 -0400349 if [ "$CLASS" = "APPS" ]; then
350 printf 'LOCAL_DEX_PREOPT := false\n'
351 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700352 if [ ! -z "$EXTENSION" ]; then
353 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
354 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100355 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 Kondik5bd66602016-07-15 10:39:58 -0700360 if [ "$EXTRA" = "priv-app" ]; then
361 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
362 fi
363 if [ "$VENDOR_PKG" = "true" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800364 printf 'LOCAL_VENDOR_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700365 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#
378function 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 Oder974b5902017-10-08 23:15:52 +0200444 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200445 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Christian Oder974b5902017-10-08 23:15:52 +0200446 write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
447 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700448
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-Tawabcc98bc32017-10-08 17:33:42 -0400456 write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700457 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#
500function write_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500501 if [ -f $1 ]; then
502 rm $1
503 fi
504
Steve Kondik5bd66602016-07-15 10:39:58 -0700505 YEAR=$(date +"%Y")
506
507 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
508
Jake Whatley9843b322017-01-25 21:49:16 -0500509 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 Kondik5bd66602016-07-15 10:39:58 -0700530#
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
545EOF
546}
547
548#
549# write_headers:
550#
551# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500552# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700553#
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#
558function write_headers() {
559 write_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500560
561 GUARD="$2"
562 if [ -z "$GUARD" ]; then
563 GUARD="TARGET_DEVICE"
564 fi
565
Steve Kondik5bd66602016-07-15 10:39:58 -0700566 cat << EOF >> "$ANDROIDMK"
567LOCAL_PATH := \$(call my-dir)
568
569EOF
570 if [ "$COMMON" -ne 1 ]; then
571 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500572ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700573
574EOF
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 Whatley9843b322017-01-25 21:49:16 -0500581ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700582
583EOF
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#
596function write_footers() {
597 cat << EOF >> "$ANDROIDMK"
598endif
599EOF
600}
601
602# Return success if adb is up and not in recovery
603function _adb_connected {
604 {
Jake Whatley9843b322017-01-25 21:49:16 -0500605 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700606 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-Tawabb0d08e82017-04-04 02:48:18 -0400618# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700619#
620# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
621#
622function 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-Tawabb0d08e82017-04-04 02:48:18 -0400631 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 Kondik5bd66602016-07-15 10:39:58 -0700639 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-Tawabb0d08e82017-04-04 02:48:18 -0400667 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700668}
669
670#
671# write_makefiles:
672#
673# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400674# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700675#
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#
680function write_makefiles() {
681 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400682 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700683 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#
692function append_firmware_calls_to_makefiles() {
693 cat << EOF >> "$ANDROIDMK"
694ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
695
696RADIO_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
701endif
702
703EOF
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#
716function 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 Whatley9843b322017-01-25 21:49:16 -0500726 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700727
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#
741function oat2dex() {
742 local CM_TARGET="$1"
743 local OEM_TARGET="$2"
744 local SRC="$3"
745 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -0500746 local OAT=
747 local HOST="$(uname)"
Steve Kondik5bd66602016-07-15 10:39:58 -0700748
Joe Maplesfb3941c2018-01-05 14:51:33 -0500749 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 Kondik5bd66602016-07-15 10:39:58 -0700753 fi
754
Joe Maplesfb3941c2018-01-05 14:51:33 -0500755 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 Kondik5bd66602016-07-15 10:39:58 -0700767 # Extract existing boot.oats to the temp folder
768 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500769 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700770 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500771 mkdir -p "$TMPDIR/system/framework/$ARCH"
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -0400772 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 Kondik5bd66602016-07-15 10:39:58 -0700778 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500779 else
780 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700781 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 Whatley9843b322017-01-25 21:49:16 -0500798 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700799
Joe Maplesfb3941c2018-01-05 14:51:33 -0500800 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 Kondik5bd66602016-07-15 10:39:58 -0700802
Joe Maplesfb3941c2018-01-05 14:51:33 -0500803
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 Kondik5bd66602016-07-15 10:39:58 -0700812 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500813 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Joe Maplesfb3941c2018-01-05 14:51:33 -0500814 JARVDEX="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -0500815 if [ ! -f "$JAROAT" ]; then
816 JAROAT=$BOOTOAT;
817 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500818
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 Kondik5bd66602016-07-15 10:39:58 -0700828 else
829 continue
830 fi
831
Steve Kondik5bd66602016-07-15 10:39:58 -0700832 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700833}
834
835#
836# init_adb_connection:
837#
838# Starts adb server and waits for the device
839#
840function 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#
870function fix_xml() {
871 local XML="$1"
872 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
873
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200874 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
875 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700876
877 mv "$TEMP_XML" "$XML"
878}
879
880#
881# extract:
882#
883# $1: file containing the list of items to extract
Dan Pasanen0cc05012017-03-21 09:06:11 -0500884# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400885# $3: section in list file to extract - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700886#
887function extract() {
888 if [ -z "$OUTDIR" ]; then
889 echo "Output dir not set!"
890 exit 1
891 fi
892
Harry Youd972c4112017-08-05 09:18:56 +0100893 if [ -z "$3" ]; then
894 parse_file_list "$1"
895 else
896 parse_file_list "$1" "$3"
897 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700898
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 Pasanen0cc05012017-03-21 09:06:11 -0500913 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 Kondik5bd66602016-07-15 10:39:58 -0700948 if [ "$VENDOR_STATE" -eq "0" ]; then
949 echo "Cleaning output directory ($OUTPUT_ROOT).."
950 rm -rf "${OUTPUT_TMP:?}"
951 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -0500952 if [ -d "$OUTPUT_ROOT" ]; then
953 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
954 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700955 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-Tawab7da4a1b2017-04-04 18:03:35 -0400974 elif [ -f "$SRC/$FILE" ] && [ "$SRC" != "adb" ]; then
975 TARGET="$FROM"
Steve Kondik5bd66602016-07-15 10:39:58 -0700976 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 M58270a32017-11-13 23:15:29 +0100993 # 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 Kondik5bd66602016-07-15 10:39:58 -07001020 # 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. Palmer1fbf6872017-03-04 05:12:29 -05001027 # Try CM target first
1028 if [ -f "$SRC/$TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001029 cp "$SRC/$TARGET" "$DEST"
Christopher R. Palmer1fbf6872017-03-04 05:12:29 -05001030 # if file does not exist try OEM target
1031 elif [ -f "$SRC/$FILE" ]; then
1032 cp "$SRC/$FILE" "$DEST"
Steve Kondik5bd66602016-07-15 10:39:58 -07001033 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 Whatley9843b322017-01-25 21:49:16 -05001054 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -07001055 local KEEP=""
1056 local TMP="$TMP_DIR/$FROM"
1057 if [ -f "$TMP" ]; then
1058 if [ ! -f "$DEST" ]; then
1059 KEEP="1"
1060 else
Jake Whatley9843b322017-01-25 21:49:16 -05001061 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 Kondik5bd66602016-07-15 10:39:58 -07001066 if [ "$DEST_HASH" != "$HASH" ]; then
1067 KEEP="1"
1068 fi
1069 fi
1070 if [ "$KEEP" = "1" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -05001071 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 Kondik5bd66602016-07-15 10:39:58 -07001076 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#
1105function 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}