blob: e1957500238d5e90f1fcc1411b64f54ad20fe6e7 [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=()
Steve Kondik48f8df82016-08-14 03:55:08 -070019PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070020PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -070021PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070022PACKAGE_LIST=()
23VENDOR_STATE=-1
Louis Popia516c2f2016-07-25 15:51:13 +020024VENDOR_RADIO_STATE=-1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070025COMMON=-1
Luca Stefani7f9fff22016-07-18 13:47:55 +020026ARCHES=
27FULLY_DEODEXED=-1
28
Rashed Abdel-Tawab11186d62017-08-05 23:11:35 -040029TMPDIR=$(mktemp -d)
Steve Kondik4e2aaab2016-07-15 10:39:58 -070030
31#
Steve Kondik48f8df82016-08-14 03:55:08 -070032# cleanup
33#
34# kill our tmpfiles with fire on exit
35#
36function cleanup() {
37 rm -rf "${TMPDIR:?}"
38}
39
Gabriele M6c3c2c02017-10-11 12:55:51 +020040trap cleanup 0
Steve Kondik48f8df82016-08-14 03:55:08 -070041
42#
Steve Kondik4e2aaab2016-07-15 10:39:58 -070043# setup_vendor
44#
45# $1: device name
46# $2: vendor name
Luca Stefani5c60e4f2017-08-17 19:28:48 +020047# $3: Lineage root directory
Steve Kondik4e2aaab2016-07-15 10:39:58 -070048# $4: is common device - optional, default to false
49# $5: cleanup - optional, default to true
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040050# $6: custom vendor makefile name - optional, default to false
Steve Kondik4e2aaab2016-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
Luca Stefani5c60e4f2017-08-17 19:28:48 +020068 export LINEAGE_ROOT="$3"
69 if [ ! -d "$LINEAGE_ROOT" ]; then
70 echo "\$LINEAGE_ROOT must be set and valid before including this script!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070071 exit 1
72 fi
73
74 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
Luca Stefani5c60e4f2017-08-17 19:28:48 +020075 if [ ! -d "$LINEAGE_ROOT/$OUTDIR" ]; then
76 mkdir -p "$LINEAGE_ROOT/$OUTDIR"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070077 fi
78
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040079 VNDNAME="$6"
80 if [ -z "$VNDNAME" ]; then
81 VNDNAME="$DEVICE"
82 fi
83
Luca Stefani5c60e4f2017-08-17 19:28:48 +020084 export PRODUCTMK="$LINEAGE_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
85 export ANDROIDMK="$LINEAGE_ROOT"/"$OUTDIR"/Android.mk
86 export BOARDMK="$LINEAGE_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -070087
88 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
89 COMMON=1
90 else
91 COMMON=0
92 fi
93
Gabriele Mb6effb32017-05-01 18:22:04 +020094 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -070095 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +020096 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070097 else
98 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +020099 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700100 fi
101}
102
Vladimir Oltean95643282018-06-24 20:22:41 +0300103# Helper functions for parsing a spec.
104# notes: an optional "|SHA1" that may appear in the format is stripped
105# early from the spec in the parse_file_list function, and
106# should not be present inside the input parameter passed
107# to these functions.
108
109#
110# input: spec in the form of "src[:dst][;args]"
111# output: "src"
112#
113function src_file() {
114 local SPEC="$1"
115 local SPLIT=(${SPEC//:/ })
116 local ARGS="$(target_args ${SPEC})"
117 # Regardless of there being a ":" delimiter or not in the spec,
118 # the source file is always either the first, or the only entry.
119 local SRC="${SPLIT[0]}"
120 # Remove target_args suffix, if present
121 echo "${SRC%;${ARGS}}"
122}
123
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700124#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300125# input: spec in the form of "src[:dst][;args]"
126# output: "dst" if present, "src" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700127#
128function target_file() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300129 local SPEC="$1"
130 local SPLIT=(${SPEC//:/ })
131 local ARGS="$(target_args ${SPEC})"
132 local DST=
133 case ${#SPLIT[@]} in
134 1)
135 # The spec doesn't have a : delimiter
136 DST="${SPLIT[0]}"
137 ;;
138 *)
139 # The spec actually has a src:dst format
140 DST="${SPLIT[1]}"
141 ;;
142 esac
143 # Remove target_args suffix, if present
144 echo "${DST%;${ARGS}}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700145}
146
147#
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300148# input: spec in the form of "src[:dst][;args]"
149# output: "args" if present, "" otherwise.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700150#
151function target_args() {
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300152 local SPEC="$1"
153 local SPLIT=(${SPEC//;/ })
154 local ARGS=
155 case ${#SPLIT[@]} in
156 1)
157 # No ";" delimiter in the spec.
158 ;;
159 *)
160 # The "args" are whatever comes after the ";" character.
161 # Basically the spec stripped of whatever is to the left of ";".
162 ARGS="${SPEC#${SPLIT[0]};}"
163 ;;
164 esac
165 echo "${ARGS}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700166}
167
168#
169# prefix_match:
170#
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300171# input:
172# - $1: prefix
173# - (global variable) PRODUCT_PACKAGES_LIST: array of [src:]dst[;args] specs.
174# output:
175# - new array consisting of dst[;args] entries where $1 is a prefix of ${dst}.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700176#
177function prefix_match() {
178 local PREFIX="$1"
Vladimir Olteana48b9fe2018-04-02 22:37:09 +0300179 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
180 local FILE=$(target_file "$LINE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700181 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean2654eaa2018-06-12 01:17:35 +0300182 local ARGS=$(target_args "$LINE")
183 if [ -z "${ARGS}" ]; then
184 echo "${FILE#$PREFIX}"
185 else
186 echo "${FILE#$PREFIX};${ARGS}"
187 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700188 fi
189 done
190}
191
192#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400193# prefix_match_file:
194#
195# $1: the prefix to match on
196# $2: the file to match the prefix for
197#
198# Internal function which returns true if a filename contains the
199# specified prefix.
200#
201function prefix_match_file() {
202 local PREFIX="$1"
203 local FILE="$2"
204 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
205 return 0
206 else
207 return 1
208 fi
209}
210
211#
212# truncate_file
213#
214# $1: the filename to truncate
215# $2: the argument to output the truncated filename to
216#
217# Internal function which truncates a filename by removing the first dir
218# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
219#
220function truncate_file() {
221 local FILE="$1"
222 RETURN_FILE="$2"
223 local FIND="${FILE%%/*}"
224 local LOCATION="${#FIND}+1"
225 echo ${FILE:$LOCATION}
226}
227
228#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700229# write_product_copy_files:
230#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400231# $1: make treble compatible makefile - optional, default to false
232#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700233# Creates the PRODUCT_COPY_FILES section in the product makefile for all
234# items in the list which do not start with a dash (-).
235#
236function write_product_copy_files() {
237 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
238 local TARGET=
239 local FILE=
240 local LINEEND=
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400241 local TREBLE_COMPAT=$1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700242
243 if [ "$COUNT" -eq "0" ]; then
244 return 0
245 fi
246
247 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
248 for (( i=1; i<COUNT+1; i++ )); do
249 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
250 LINEEND=" \\"
251 if [ "$i" -eq "$COUNT" ]; then
252 LINEEND=""
253 fi
254
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300255 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400256 if [ "$TREBLE_COMPAT" == "true" ] || [ "$TREBLE_COMPAT" == "1" ]; then
257 if prefix_match_file "vendor/" $TARGET ; then
258 local OUTTARGET=$(truncate_file $TARGET)
259 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
260 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
261 else
262 printf ' %s/proprietary/%s:system/%s%s\n' \
263 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
264 fi
265 else
266 printf ' %s/proprietary/%s:system/%s%s\n' \
267 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
268 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700269 done
270 return 0
271}
272
273#
274# write_packages:
275#
276# $1: The LOCAL_MODULE_CLASS for the given module list
277# $2: "true" if this package is part of the vendor/ path
Steve Kondika991cf12016-07-28 12:13:12 -0700278# $3: type-specific extra flags
279# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700280#
281# Internal function which writes out the BUILD_PREBUILT stanzas
282# for all modules in the list. This is called by write_product_packages
283# after the modules are categorized.
284#
285function write_packages() {
286
287 local CLASS="$1"
288 local VENDOR_PKG="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700289 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700290
291 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700292 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700293 local FILELIST=("${!ARR_NAME}")
294
295 local FILE=
296 local ARGS=
297 local BASENAME=
298 local EXTENSION=
299 local PKGNAME=
300 local SRC=
301
302 for P in "${FILELIST[@]}"; do
Vladimir Oltean6a7946b2018-06-24 20:09:55 +0300303 FILE=$(target_file "$P")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700304 ARGS=$(target_args "$P")
305
306 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100307 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700308 EXTENSION=${BASENAME##*.}
309 PKGNAME=${BASENAME%.*}
310
311 # Add to final package list
312 PACKAGE_LIST+=("$PKGNAME")
313
314 SRC="proprietary"
315 if [ "$VENDOR_PKG" = "true" ]; then
316 SRC+="/vendor"
317 fi
318
319 printf 'include $(CLEAR_VARS)\n'
320 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
321 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
322 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700323 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700324 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
325 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
326 #if [ "$VENDOR_PKG" = "true" ]; then
327 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
328 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
329 #else
330 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
331 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
332 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700333 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700334 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
335 else
336 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
337 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700338 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700339 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700340 fi
341 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200342 if [ "$EXTRA" = "priv-app" ]; then
343 SRC="$SRC/priv-app"
344 else
345 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700346 fi
347 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
348 local CERT=platform
349 if [ ! -z "$ARGS" ]; then
350 CERT="$ARGS"
351 fi
352 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
353 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
354 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200355 local CERT=platform
356 if [ ! -z "$ARGS" ]; then
357 CERT="$ARGS"
358 fi
359 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700360 elif [ "$CLASS" = "ETC" ]; then
361 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
362 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700363 if [ "$ARGS" = "rootfs" ]; then
364 SRC="$SRC/rootfs"
365 if [ "$EXTRA" = "sbin" ]; then
366 SRC="$SRC/sbin"
367 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
368 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
369 fi
370 else
371 SRC="$SRC/bin"
372 fi
373 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
374 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700375 else
Steve Kondika991cf12016-07-28 12:13:12 -0700376 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700377 fi
378 printf 'LOCAL_MODULE_TAGS := optional\n'
379 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400380 if [ "$CLASS" = "APPS" ]; then
381 printf 'LOCAL_DEX_PREOPT := false\n'
382 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700383 if [ ! -z "$EXTENSION" ]; then
384 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
385 fi
M1cha15f226c2017-01-04 09:00:11 +0100386 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
387 if [ "$DIRNAME" != "." ]; then
388 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
389 fi
390 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700391 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700392 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
393 fi
394 if [ "$VENDOR_PKG" = "true" ]; then
Ethan Chen5bc3c842018-02-17 20:03:54 -0800395 printf 'LOCAL_VENDOR_MODULE := true\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700396 fi
397 printf 'include $(BUILD_PREBUILT)\n\n'
398 done
399}
400
401#
402# write_product_packages:
403#
404# This function will create BUILD_PREBUILT entries in the
405# Android.mk and associated PRODUCT_PACKAGES list in the
406# product makefile for all files in the blob list which
407# start with a single dash (-) character.
408#
409function write_product_packages() {
410 PACKAGE_LIST=()
411
412 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
413
414 if [ "$COUNT" = "0" ]; then
415 return 0
416 fi
417
418 # Figure out what's 32-bit, what's 64-bit, and what's multilib
419 # I really should not be doing this in bash due to shitty array passing :(
420 local T_LIB32=( $(prefix_match "lib/") )
421 local T_LIB64=( $(prefix_match "lib64/") )
422 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
423 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700424 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700425
Steve Kondik03ce4002016-07-29 00:00:16 -0700426 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
427 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
428 fi
429 if [ "${#LIB32[@]}" -gt "0" ]; then
430 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
431 fi
432 if [ "${#LIB64[@]}" -gt "0" ]; then
433 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
434 fi
435
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700436 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
437 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
438 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
439 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700440 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700441
442 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700443 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700444 fi
445 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700446 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700447 fi
448 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700449 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700450 fi
451
452 # Apps
453 local APPS=( $(prefix_match "app/") )
454 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700455 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700456 fi
457 local PRIV_APPS=( $(prefix_match "priv-app/") )
458 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700459 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700460 fi
461 local V_APPS=( $(prefix_match "vendor/app/") )
462 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700463 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700464 fi
465 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
466 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700467 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700468 fi
469
470 # Framework
471 local FRAMEWORK=( $(prefix_match "framework/") )
472 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700473 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700474 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200475 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestasa3f97c72018-02-27 22:31:55 +0200476 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Christian Oderc16f3272017-10-08 23:15:52 +0200477 write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
478 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700479
480 # Etc
481 local ETC=( $(prefix_match "etc/") )
482 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700483 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700484 fi
485 local V_ETC=( $(prefix_match "vendor/etc/") )
486 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab00c07662017-10-08 17:33:42 -0400487 write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700488 fi
489
490 # Executables
491 local BIN=( $(prefix_match "bin/") )
492 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700493 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700494 fi
495 local V_BIN=( $(prefix_match "vendor/bin/") )
496 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700497 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700498 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700499 local SBIN=( $(prefix_match "sbin/") )
500 if [ "${#SBIN[@]}" -gt "0" ]; then
501 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
502 fi
503
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700504
505 # Actually write out the final PRODUCT_PACKAGES list
506 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
507
508 if [ "$PACKAGE_COUNT" -eq "0" ]; then
509 return 0
510 fi
511
512 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
513 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
514 local LINEEND=" \\"
515 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
516 LINEEND=""
517 fi
518 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
519 done
520}
521
522#
523# write_header:
524#
525# $1: file which will be written to
526#
527# writes out the copyright header with the current year.
528# note that this is not an append operation, and should
529# be executed first!
530#
531function write_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600532 if [ -f $1 ]; then
533 rm $1
534 fi
535
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700536 YEAR=$(date +"%Y")
537
538 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
539
Matt Mower8945f5e2017-01-07 14:08:17 -0600540 NUM_REGEX='^[0-9]+$'
541 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
542 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
543 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
544 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
545 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
546 fi
547 if [ $YEAR -eq 2017 ]; then
548 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
549 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
550 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
551 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
552 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
553 else
554 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
555 fi
556 else
557 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
558 fi
559
560 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700561#
562# Licensed under the Apache License, Version 2.0 (the "License");
563# you may not use this file except in compliance with the License.
564# You may obtain a copy of the License at
565#
566# http://www.apache.org/licenses/LICENSE-2.0
567#
568# Unless required by applicable law or agreed to in writing, software
569# distributed under the License is distributed on an "AS IS" BASIS,
570# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
571# See the License for the specific language governing permissions and
572# limitations under the License.
573
574# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
575
576EOF
577}
578
579#
580# write_headers:
581#
582# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400583# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700584#
585# Calls write_header for each of the makefiles and creates
586# the initial path declaration and device guard for the
587# Android.mk
588#
589function write_headers() {
590 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400591
592 GUARD="$2"
593 if [ -z "$GUARD" ]; then
594 GUARD="TARGET_DEVICE"
595 fi
596
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700597 cat << EOF >> "$ANDROIDMK"
598LOCAL_PATH := \$(call my-dir)
599
600EOF
601 if [ "$COMMON" -ne 1 ]; then
602 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400603ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700604
605EOF
606 else
607 if [ -z "$1" ]; then
608 echo "Argument with devices to be added to guard must be set!"
609 exit 1
610 fi
611 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400612ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700613
614EOF
615 fi
616
617 write_header "$BOARDMK"
618 write_header "$PRODUCTMK"
619}
620
621#
622# write_footers:
623#
624# Closes the inital guard and any other finalization tasks. Must
625# be called as the final step.
626#
627function write_footers() {
628 cat << EOF >> "$ANDROIDMK"
629endif
630EOF
631}
632
633# Return success if adb is up and not in recovery
634function _adb_connected {
635 {
Steve Kondik7561d192016-09-01 21:40:27 -0700636 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700637 then
638 return 0
639 fi
640 } 2>/dev/null
641
642 return 1
643};
644
645#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100646# parse_file_list:
647#
648# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400649# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +0100650#
651# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700652#
653function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100654 if [ -z "$1" ]; then
655 echo "An input file is expected!"
656 exit 1
657 elif [ ! -f "$1" ]; then
658 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700659 exit 1
660 fi
661
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400662 if [ $# -eq 2 ]; then
663 LIST=$TMPDIR/files.txt
664 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
665 else
666 LIST=$1
667 fi
668
669
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700670 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700671 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700672 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700673 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700674
675 while read -r line; do
676 if [ -z "$line" ]; then continue; fi
677
Steve Kondik48f8df82016-08-14 03:55:08 -0700678 # If the line has a pipe delimiter, a sha1 hash should follow.
679 # This indicates the file should be pinned and not overwritten
680 # when extracting files.
681 local SPLIT=(${line//\|/ })
682 local COUNT=${#SPLIT[@]}
683 local SPEC=${SPLIT[0]}
684 local HASH="x"
685 if [ "$COUNT" -gt "1" ]; then
686 HASH=${SPLIT[1]}
687 fi
688
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700689 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700690 if [[ "$SPEC" =~ ^- ]]; then
691 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
692 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700693 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700694 PRODUCT_COPY_FILES_LIST+=("$SPEC")
695 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700696 fi
697
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400698 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700699}
700
701#
702# write_makefiles:
703#
704# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400705# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700706#
707# Calls write_product_copy_files and write_product_packages on
708# the given file and appends to the Android.mk as well as
709# the product makefile.
710#
711function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700712 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400713 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700714 write_product_packages
715}
716
717#
Louis Popia516c2f2016-07-25 15:51:13 +0200718# append_firmware_calls_to_makefiles:
719#
720# Appends to Android.mk the calls to all images present in radio folder
721# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
722#
723function append_firmware_calls_to_makefiles() {
724 cat << EOF >> "$ANDROIDMK"
725ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
726
727RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
728\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
729 \$(call add-radio-file,radio/\$(f)))
730\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
731
732endif
733
734EOF
735}
736
737#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200738# get_file:
739#
740# $1: input file
741# $2: target file/folder
742# $3: source of the file (can be "adb" or a local folder)
743#
744# Silently extracts the input file to defined target
745# Returns success if file can be pulled from the device or found locally
746#
747function get_file() {
748 local SRC="$3"
749
750 if [ "$SRC" = "adb" ]; then
751 # try to pull
752 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
753
754 return 1
755 else
756 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +0300757 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
758 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200759
760 return 1
761 fi
762};
763
764#
765# oat2dex:
766#
767# $1: extracted apk|jar (to check if deodex is required)
768# $2: odexed apk|jar to deodex
769# $3: source of the odexed apk|jar
770#
771# Convert apk|jar .odex in the corresposing classes.dex
772#
773function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200774 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200775 local OEM_TARGET="$2"
776 local SRC="$3"
777 local TARGET=
778 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -0500779 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200780
781 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200782 export BAKSMALIJAR="$LINEAGE_ROOT"/vendor/lineage/build/tools/smali/baksmali.jar
783 export SMALIJAR="$LINEAGE_ROOT"/vendor/lineage/build/tools/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +0200784 fi
785
Joe Maples9be579f2018-01-05 14:51:33 -0500786 if [ -z "$VDEXEXTRACTOR" ]; then
787 export VDEXEXTRACTOR="$LINEAGE_ROOT"/vendor/lineage/build/tools/"$HOST"/vdexExtractor
788 fi
789
Luca Stefani7f9fff22016-07-18 13:47:55 +0200790 # Extract existing boot.oats to the temp folder
791 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700792 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200793 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700794 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +0300795 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200796 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700797 else
798 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200799 fi
800 done
801 fi
802
803 if [ -z "$ARCHES" ]; then
804 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
805 fi
806
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200807 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700808 return;
809 fi
810
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200811 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200812 return 0 # target apk|jar is already odexed, return
813 fi
814
815 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700816 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200817
818 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400819 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200820
821 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400822 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -0500823 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
824 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}").apk_classes.dex" "$TMPDIR/classes.dex"
825 else
826 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
827 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400828 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200829 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +0100830 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Joe Maples9be579f2018-01-05 14:51:33 -0500831 JARVDEX="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +0100832 if [ ! -f "$JAROAT" ]; then
833 JAROAT=$BOOTOAT;
834 fi
Joe Maples9be579f2018-01-05 14:51:33 -0500835
836 # try to extract classes.dex from boot.vdex for frameworks jars
837 # fallback to boot.oat if vdex is not available
838 if [ -f "$JARVDEX" ]; then
839 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$JARVDEX" > /dev/null
840 mv "$TMPDIR/boot-$(basename "${OEM_TARGET%.*}").apk_classes.dex" "$TMPDIR/classes.dex"
841 else
842 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
843 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
844 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +0200845 else
846 continue
847 fi
848
Luca Stefani7f9fff22016-07-18 13:47:55 +0200849 done
850
851 rm -rf "$TMPDIR/dexout"
852}
853
854#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700855# init_adb_connection:
856#
857# Starts adb server and waits for the device
858#
859function init_adb_connection() {
860 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
861 if ! _adb_connected; then
862 echo "No device is online. Waiting for one..."
863 echo "Please connect USB and/or enable USB debugging"
864 until _adb_connected; do
865 sleep 1
866 done
867 echo "Device Found."
868 fi
869
870 # Retrieve IP and PORT info if we're using a TCP connection
871 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
872 | head -1 | awk '{print $1}')
873 adb root &> /dev/null
874 sleep 0.3
875 if [ -n "$TCPIPPORT" ]; then
876 # adb root just killed our connection
877 # so reconnect...
878 adb connect "$TCPIPPORT"
879 fi
880 adb wait-for-device &> /dev/null
881 sleep 0.3
882}
883
884#
Luca Stefani3a030122016-07-30 12:08:25 +0200885# fix_xml:
886#
887# $1: xml file to fix
888#
889function fix_xml() {
890 local XML="$1"
891 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
892
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +0200893 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
894 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +0200895
896 mv "$TEMP_XML" "$XML"
897}
898
899#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700900# extract:
901#
902# $1: file containing the list of items to extract
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500903# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400904# $3: section in list file to extract - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700905#
906function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700907 if [ -z "$OUTDIR" ]; then
908 echo "Output dir not set!"
909 exit 1
910 fi
911
Harry Youd9c5d1292017-08-05 09:18:56 +0100912 if [ -z "$3" ]; then
913 parse_file_list "$1"
914 else
915 parse_file_list "$1" "$3"
916 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700917
918 # Allow failing, so we can try $DEST and/or $FILE
919 set +e
920
921 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -0700922 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700923 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700924 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200925 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -0700926 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
927
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700928 if [ "$SRC" = "adb" ]; then
929 init_adb_connection
930 fi
931
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500932 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +0100933 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500934
935 # Check if we're working with the same zip that was passed last time.
936 # If so, let's just use what's already extracted.
937 MD5=`md5sum "$SRC"| awk '{print $1}'`
938 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
939
940 if [ "$MD5" != "$OLDMD5" ]; then
941 rm -rf "$DUMPDIR"
942 mkdir "$DUMPDIR"
943 unzip "$SRC" -d "$DUMPDIR"
944 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
945
946 # Stop if an A/B OTA zip is detected. We cannot extract these.
947 if [ -a "$DUMPDIR"/payload.bin ]; then
948 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
949 exit 1
950 # If OTA is block based, extract it.
951 elif [ -a "$DUMPDIR"/system.new.dat ]; then
952 echo "Converting system.new.dat to system.img"
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200953 python "$LINEAGE_ROOT"/vendor/lineage/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500954 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
955 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
956 echo "Requesting sudo access to mount the system.img"
957 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
958 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
959 sudo umount "$DUMPDIR"/tmp
960 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
961 fi
962 fi
963
964 SRC="$DUMPDIR"
965 fi
966
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700967 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700968 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -0700969 rm -rf "${OUTPUT_TMP:?}"
970 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +0100971 if [ -d "$OUTPUT_ROOT" ]; then
972 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
973 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700974 VENDOR_STATE=1
975 fi
976
977 echo "Extracting $COUNT files in $1 from $SRC:"
978
979 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700980
Vladimir Olteanda3b6442018-06-24 20:41:30 +0300981 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +0300982 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +0300983 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +0300984 local OUTPUT_DIR=
985 local TMP_DIR=
986 local SRC_FILE=
987 local DST_FILE=
Steve Kondika991cf12016-07-28 12:13:12 -0700988
Vladimir Olteand652a062018-06-24 20:42:01 +0300989 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +0300990 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
991 TMP_DIR="${OUTPUT_TMP}/rootfs"
992 SRC_FILE="/${SPEC_SRC_FILE}"
993 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700994 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +0300995 OUTPUT_DIR="${OUTPUT_ROOT}"
996 TMP_DIR="${OUTPUT_TMP}"
997 SRC_FILE="/system/${SPEC_SRC_FILE}"
998 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700999 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001000
1001 if [ "$SRC" = "adb" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001002 printf ' - %s .. ' "${DST_FILE}"
Steve Kondika991cf12016-07-28 12:13:12 -07001003 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001004 printf ' - %s \n' "${DST_FILE}"
Steve Kondika991cf12016-07-28 12:13:12 -07001005 fi
1006
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001007 # Strip the file path in the vendor repo of "system", if present
1008 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
1009 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001010
Gabriele Me6df25b2017-10-11 00:58:59 +02001011 # Check pinned files
1012 local HASH="${HASHLIST[$i-1]}"
1013 local KEEP=""
1014 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001015 if [ -f "${VENDOR_REPO_FILE}" ]; then
1016 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001017 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001018 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001019 fi
1020 if [ -f "$PINNED" ]; then
1021 if [ "$(uname)" == "Darwin" ]; then
1022 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
1023 else
1024 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
1025 fi
1026 if [ "$TMP_HASH" = "$HASH" ]; then
1027 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001028 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1029 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001030 fi
1031 fi
1032 fi
1033 fi
1034
1035 if [ "$KEEP" = "1" ]; then
1036 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001037 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001038 FOUND=false
1039 # Try Lineage target first.
1040 # Also try to search for files stripped of
1041 # the "/system" prefix, if we're actually extracting
1042 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001043 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001044 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1045 FOUND=true
1046 break
1047 }
1048 done
1049
1050 if [ "${FOUND}" = false ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -07001051 printf ' !! file not found in source\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001052 fi
1053 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001054
Luca Stefani7f9fff22016-07-18 13:47:55 +02001055 if [ "$?" == "0" ]; then
1056 # Deodex apk|jar if that's the case
Vladimir Olteand6747712018-06-24 20:46:42 +03001057 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
Vladimir Olteand5773252018-06-25 00:05:56 +03001058 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001059 if [ -f "$TMPDIR/classes.dex" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001060 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes.dex"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001061 rm "$TMPDIR/classes.dex"
Vladimir Olteand5773252018-06-25 00:05:56 +03001062 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001063 fi
Vladimir Olteand6747712018-06-24 20:46:42 +03001064 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1065 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001066 fi
1067 fi
1068
Vladimir Olteand6747712018-06-24 20:46:42 +03001069 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001070 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001071 local TYPE="${DIR##*/}"
1072 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001073 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001074 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001075 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001076 fi
1077 fi
1078
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001079 done
1080
1081 # Don't allow failing
1082 set -e
1083}
Louis Popia516c2f2016-07-25 15:51:13 +02001084
1085#
1086# extract_firmware:
1087#
1088# $1: file containing the list of items to extract
1089# $2: path to extracted radio folder
1090#
1091function extract_firmware() {
1092 if [ -z "$OUTDIR" ]; then
1093 echo "Output dir not set!"
1094 exit 1
1095 fi
1096
1097 parse_file_list "$1"
1098
1099 # Don't allow failing
1100 set -e
1101
1102 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1103 local COUNT=${#FILELIST[@]}
1104 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001105 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001106
1107 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1108 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1109 rm -rf "${OUTPUT_DIR:?}/"*
1110 VENDOR_RADIO_STATE=1
1111 fi
1112
1113 echo "Extracting $COUNT files in $1 from $SRC:"
1114
1115 for (( i=1; i<COUNT+1; i++ )); do
1116 local FILE="${FILELIST[$i-1]}"
1117 printf ' - %s \n' "/radio/$FILE"
1118
1119 if [ ! -d "$OUTPUT_DIR" ]; then
1120 mkdir -p "$OUTPUT_DIR"
1121 fi
1122 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1123 chmod 644 "$OUTPUT_DIR/$FILE"
1124 done
1125}