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