blob: aed50666a235b91ee6c7820faf156d48d02a90b9 [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
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#
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200126# $1: semicolon delimited list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700127#
128# Returns optional arguments (last value) for given target
129#
130function target_args() {
131 local LINE="$1"
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200132 local SPLIT=(${LINE//;/ })
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700133 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"
151 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
152 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
153 printf '%s\n' "${FILE#$PREFIX}"
154 fi
155 done
156}
157
158#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400159# prefix_match_file:
160#
161# $1: the prefix to match on
162# $2: the file to match the prefix for
163#
164# Internal function which returns true if a filename contains the
165# specified prefix.
166#
167function prefix_match_file() {
168 local PREFIX="$1"
169 local FILE="$2"
170 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
171 return 0
172 else
173 return 1
174 fi
175}
176
177#
178# truncate_file
179#
180# $1: the filename to truncate
181# $2: the argument to output the truncated filename to
182#
183# Internal function which truncates a filename by removing the first dir
184# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
185#
186function truncate_file() {
187 local FILE="$1"
188 RETURN_FILE="$2"
189 local FIND="${FILE%%/*}"
190 local LOCATION="${#FIND}+1"
191 echo ${FILE:$LOCATION}
192}
193
194#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700195# write_product_copy_files:
196#
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400197# $1: make treble compatible makefile - optional, default to false
198#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700199# Creates the PRODUCT_COPY_FILES section in the product makefile for all
200# items in the list which do not start with a dash (-).
201#
202function write_product_copy_files() {
203 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
204 local TARGET=
205 local FILE=
206 local LINEEND=
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400207 local TREBLE_COMPAT=$1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700208
209 if [ "$COUNT" -eq "0" ]; then
210 return 0
211 fi
212
213 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
214 for (( i=1; i<COUNT+1; i++ )); do
215 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
216 LINEEND=" \\"
217 if [ "$i" -eq "$COUNT" ]; then
218 LINEEND=""
219 fi
220
221 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400222 if [ "$TREBLE_COMPAT" == "true" ] || [ "$TREBLE_COMPAT" == "1" ]; then
223 if prefix_match_file "vendor/" $TARGET ; then
224 local OUTTARGET=$(truncate_file $TARGET)
225 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
226 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
227 else
228 printf ' %s/proprietary/%s:system/%s%s\n' \
229 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
230 fi
231 else
232 printf ' %s/proprietary/%s:system/%s%s\n' \
233 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
234 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700235 done
236 return 0
237}
238
239#
240# write_packages:
241#
242# $1: The LOCAL_MODULE_CLASS for the given module list
243# $2: "true" if this package is part of the vendor/ path
Steve Kondika991cf12016-07-28 12:13:12 -0700244# $3: type-specific extra flags
245# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700246#
247# Internal function which writes out the BUILD_PREBUILT stanzas
248# for all modules in the list. This is called by write_product_packages
249# after the modules are categorized.
250#
251function write_packages() {
252
253 local CLASS="$1"
254 local VENDOR_PKG="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700255 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700256
257 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700258 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700259 local FILELIST=("${!ARR_NAME}")
260
261 local FILE=
262 local ARGS=
263 local BASENAME=
264 local EXTENSION=
265 local PKGNAME=
266 local SRC=
267
268 for P in "${FILELIST[@]}"; do
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200269 FILE=$(echo $(target_file "$P") | sed 's/\;.*//')
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700270 ARGS=$(target_args "$P")
271
272 BASENAME=$(basename "$FILE")
M1cha15f226c2017-01-04 09:00:11 +0100273 DIRNAME=$(dirname "$FILE")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700274 EXTENSION=${BASENAME##*.}
275 PKGNAME=${BASENAME%.*}
276
277 # Add to final package list
278 PACKAGE_LIST+=("$PKGNAME")
279
280 SRC="proprietary"
281 if [ "$VENDOR_PKG" = "true" ]; then
282 SRC+="/vendor"
283 fi
284
285 printf 'include $(CLEAR_VARS)\n'
286 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
287 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
288 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700289 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700290 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
291 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
292 #if [ "$VENDOR_PKG" = "true" ]; then
293 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
294 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
295 #else
296 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
297 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
298 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700299 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700300 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
301 else
302 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
303 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700304 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700305 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700306 fi
307 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200308 if [ "$EXTRA" = "priv-app" ]; then
309 SRC="$SRC/priv-app"
310 else
311 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700312 fi
313 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
314 local CERT=platform
315 if [ ! -z "$ARGS" ]; then
316 CERT="$ARGS"
317 fi
318 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
319 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
320 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmock082e0ec2016-10-04 21:11:43 +0200321 local CERT=platform
322 if [ ! -z "$ARGS" ]; then
323 CERT="$ARGS"
324 fi
325 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700326 elif [ "$CLASS" = "ETC" ]; then
327 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
328 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700329 if [ "$ARGS" = "rootfs" ]; then
330 SRC="$SRC/rootfs"
331 if [ "$EXTRA" = "sbin" ]; then
332 SRC="$SRC/sbin"
333 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
334 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
335 fi
336 else
337 SRC="$SRC/bin"
338 fi
339 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
340 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700341 else
Steve Kondika991cf12016-07-28 12:13:12 -0700342 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700343 fi
344 printf 'LOCAL_MODULE_TAGS := optional\n'
345 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400346 if [ "$CLASS" = "APPS" ]; then
347 printf 'LOCAL_DEX_PREOPT := false\n'
348 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700349 if [ ! -z "$EXTENSION" ]; then
350 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
351 fi
M1cha15f226c2017-01-04 09:00:11 +0100352 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
353 if [ "$DIRNAME" != "." ]; then
354 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
355 fi
356 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700357 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700358 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
359 fi
360 if [ "$VENDOR_PKG" = "true" ]; then
361 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
362 fi
363 printf 'include $(BUILD_PREBUILT)\n\n'
364 done
365}
366
367#
368# write_product_packages:
369#
370# This function will create BUILD_PREBUILT entries in the
371# Android.mk and associated PRODUCT_PACKAGES list in the
372# product makefile for all files in the blob list which
373# start with a single dash (-) character.
374#
375function write_product_packages() {
376 PACKAGE_LIST=()
377
378 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
379
380 if [ "$COUNT" = "0" ]; then
381 return 0
382 fi
383
384 # Figure out what's 32-bit, what's 64-bit, and what's multilib
385 # I really should not be doing this in bash due to shitty array passing :(
386 local T_LIB32=( $(prefix_match "lib/") )
387 local T_LIB64=( $(prefix_match "lib64/") )
388 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
389 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700390 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700391
Steve Kondik03ce4002016-07-29 00:00:16 -0700392 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
393 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
394 fi
395 if [ "${#LIB32[@]}" -gt "0" ]; then
396 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
397 fi
398 if [ "${#LIB64[@]}" -gt "0" ]; then
399 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
400 fi
401
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700402 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
403 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
404 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
405 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700406 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700407
408 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700409 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700410 fi
411 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700412 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700413 fi
414 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700415 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700416 fi
417
418 # Apps
419 local APPS=( $(prefix_match "app/") )
420 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700421 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700422 fi
423 local PRIV_APPS=( $(prefix_match "priv-app/") )
424 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700425 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700426 fi
427 local V_APPS=( $(prefix_match "vendor/app/") )
428 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700429 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700430 fi
431 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
432 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700433 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700434 fi
435
436 # Framework
437 local FRAMEWORK=( $(prefix_match "framework/") )
438 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700439 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700440 fi
Christian Oderc16f3272017-10-08 23:15:52 +0200441 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
442 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
443 write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
444 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700445
446 # Etc
447 local ETC=( $(prefix_match "etc/") )
448 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700449 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700450 fi
451 local V_ETC=( $(prefix_match "vendor/etc/") )
452 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawab00c07662017-10-08 17:33:42 -0400453 write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700454 fi
455
456 # Executables
457 local BIN=( $(prefix_match "bin/") )
458 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700459 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700460 fi
461 local V_BIN=( $(prefix_match "vendor/bin/") )
462 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700463 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700464 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700465 local SBIN=( $(prefix_match "sbin/") )
466 if [ "${#SBIN[@]}" -gt "0" ]; then
467 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
468 fi
469
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700470
471 # Actually write out the final PRODUCT_PACKAGES list
472 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
473
474 if [ "$PACKAGE_COUNT" -eq "0" ]; then
475 return 0
476 fi
477
478 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
479 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
480 local LINEEND=" \\"
481 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
482 LINEEND=""
483 fi
484 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
485 done
486}
487
488#
489# write_header:
490#
491# $1: file which will be written to
492#
493# writes out the copyright header with the current year.
494# note that this is not an append operation, and should
495# be executed first!
496#
497function write_header() {
Matt Mower8945f5e2017-01-07 14:08:17 -0600498 if [ -f $1 ]; then
499 rm $1
500 fi
501
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700502 YEAR=$(date +"%Y")
503
504 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
505
Matt Mower8945f5e2017-01-07 14:08:17 -0600506 NUM_REGEX='^[0-9]+$'
507 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
508 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
509 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
510 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
511 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
512 fi
513 if [ $YEAR -eq 2017 ]; then
514 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
515 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
516 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
517 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
518 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
519 else
520 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
521 fi
522 else
523 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
524 fi
525
526 cat << EOF >> $1
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700527#
528# Licensed under the Apache License, Version 2.0 (the "License");
529# you may not use this file except in compliance with the License.
530# You may obtain a copy of the License at
531#
532# http://www.apache.org/licenses/LICENSE-2.0
533#
534# Unless required by applicable law or agreed to in writing, software
535# distributed under the License is distributed on an "AS IS" BASIS,
536# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
537# See the License for the specific language governing permissions and
538# limitations under the License.
539
540# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
541
542EOF
543}
544
545#
546# write_headers:
547#
548# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400549# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700550#
551# Calls write_header for each of the makefiles and creates
552# the initial path declaration and device guard for the
553# Android.mk
554#
555function write_headers() {
556 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400557
558 GUARD="$2"
559 if [ -z "$GUARD" ]; then
560 GUARD="TARGET_DEVICE"
561 fi
562
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700563 cat << EOF >> "$ANDROIDMK"
564LOCAL_PATH := \$(call my-dir)
565
566EOF
567 if [ "$COMMON" -ne 1 ]; then
568 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400569ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700570
571EOF
572 else
573 if [ -z "$1" ]; then
574 echo "Argument with devices to be added to guard must be set!"
575 exit 1
576 fi
577 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400578ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700579
580EOF
581 fi
582
583 write_header "$BOARDMK"
584 write_header "$PRODUCTMK"
585}
586
587#
588# write_footers:
589#
590# Closes the inital guard and any other finalization tasks. Must
591# be called as the final step.
592#
593function write_footers() {
594 cat << EOF >> "$ANDROIDMK"
595endif
596EOF
597}
598
599# Return success if adb is up and not in recovery
600function _adb_connected {
601 {
Steve Kondik7561d192016-09-01 21:40:27 -0700602 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700603 then
604 return 0
605 fi
606 } 2>/dev/null
607
608 return 1
609};
610
611#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100612# parse_file_list:
613#
614# $1: input file
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400615# $2: blob section in file - optional
Bruno Martins3b96ba52016-07-27 15:00:05 +0100616#
617# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700618#
619function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100620 if [ -z "$1" ]; then
621 echo "An input file is expected!"
622 exit 1
623 elif [ ! -f "$1" ]; then
624 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700625 exit 1
626 fi
627
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400628 if [ $# -eq 2 ]; then
629 LIST=$TMPDIR/files.txt
630 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
631 else
632 LIST=$1
633 fi
634
635
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700636 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700637 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700638 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700639 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700640
641 while read -r line; do
642 if [ -z "$line" ]; then continue; fi
643
Steve Kondik48f8df82016-08-14 03:55:08 -0700644 # If the line has a pipe delimiter, a sha1 hash should follow.
645 # This indicates the file should be pinned and not overwritten
646 # when extracting files.
647 local SPLIT=(${line//\|/ })
648 local COUNT=${#SPLIT[@]}
649 local SPEC=${SPLIT[0]}
650 local HASH="x"
651 if [ "$COUNT" -gt "1" ]; then
652 HASH=${SPLIT[1]}
653 fi
654
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700655 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700656 if [[ "$SPEC" =~ ^- ]]; then
Michael Bestasc9828362017-12-21 03:20:08 +0200657 SPEC=$(echo "${SPEC}" | sed 's/[^"]*://')
Steve Kondik48f8df82016-08-14 03:55:08 -0700658 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
659 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700660 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700661 PRODUCT_COPY_FILES_LIST+=("$SPEC")
662 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700663 fi
664
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400665 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700666}
667
668#
669# write_makefiles:
670#
671# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400672# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700673#
674# Calls write_product_copy_files and write_product_packages on
675# the given file and appends to the Android.mk as well as
676# the product makefile.
677#
678function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700679 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400680 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700681 write_product_packages
682}
683
684#
Louis Popia516c2f2016-07-25 15:51:13 +0200685# append_firmware_calls_to_makefiles:
686#
687# Appends to Android.mk the calls to all images present in radio folder
688# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
689#
690function append_firmware_calls_to_makefiles() {
691 cat << EOF >> "$ANDROIDMK"
692ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
693
694RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
695\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
696 \$(call add-radio-file,radio/\$(f)))
697\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
698
699endif
700
701EOF
702}
703
704#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200705# get_file:
706#
707# $1: input file
708# $2: target file/folder
709# $3: source of the file (can be "adb" or a local folder)
710#
711# Silently extracts the input file to defined target
712# Returns success if file can be pulled from the device or found locally
713#
714function get_file() {
715 local SRC="$3"
716
717 if [ "$SRC" = "adb" ]; then
718 # try to pull
719 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
720
721 return 1
722 else
723 # try to copy
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700724 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200725
726 return 1
727 fi
728};
729
730#
731# oat2dex:
732#
733# $1: extracted apk|jar (to check if deodex is required)
734# $2: odexed apk|jar to deodex
735# $3: source of the odexed apk|jar
736#
737# Convert apk|jar .odex in the corresposing classes.dex
738#
739function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200740 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200741 local OEM_TARGET="$2"
742 local SRC="$3"
743 local TARGET=
744 local OAT=
745
746 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200747 export BAKSMALIJAR="$LINEAGE_ROOT"/vendor/lineage/build/tools/smali/baksmali.jar
748 export SMALIJAR="$LINEAGE_ROOT"/vendor/lineage/build/tools/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +0200749 fi
750
751 # Extract existing boot.oats to the temp folder
752 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700753 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200754 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700755 mkdir -p "$TMPDIR/system/framework/$ARCH"
Rashed Abdel-Tawab11186d62017-08-05 23:11:35 -0400756 if [ -d "$SRC/framework" ] && [ "$SRC" != "adb" ]; then
757 ARCHDIR="framework/$ARCH/"
758 else
759 ARCHDIR="system/framework/$ARCH/"
760 fi
761 if get_file "$ARCHDIR" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200762 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700763 else
764 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200765 fi
766 done
767 fi
768
769 if [ -z "$ARCHES" ]; then
770 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
771 fi
772
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200773 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700774 return;
775 fi
776
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200777 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200778 return 0 # target apk|jar is already odexed, return
779 fi
780
781 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700782 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200783
784 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400785 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200786
787 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400788 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
789 echo "WARNING: Deodexing with VDEX. Still experimental"
790 fi
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700791 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200792 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +0100793 # try to extract classes.dex from boot.oats for framework jars
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400794 # TODO: check if extraction from boot.vdex is needed
Gabriele M4cf635a2017-01-05 22:10:00 +0100795 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
796 if [ ! -f "$JAROAT" ]; then
797 JAROAT=$BOOTOAT;
798 fi
799 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200800 else
801 continue
802 fi
803
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700804 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
Luca Stefani7f9fff22016-07-18 13:47:55 +0200805 done
806
807 rm -rf "$TMPDIR/dexout"
808}
809
810#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700811# init_adb_connection:
812#
813# Starts adb server and waits for the device
814#
815function init_adb_connection() {
816 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
817 if ! _adb_connected; then
818 echo "No device is online. Waiting for one..."
819 echo "Please connect USB and/or enable USB debugging"
820 until _adb_connected; do
821 sleep 1
822 done
823 echo "Device Found."
824 fi
825
826 # Retrieve IP and PORT info if we're using a TCP connection
827 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
828 | head -1 | awk '{print $1}')
829 adb root &> /dev/null
830 sleep 0.3
831 if [ -n "$TCPIPPORT" ]; then
832 # adb root just killed our connection
833 # so reconnect...
834 adb connect "$TCPIPPORT"
835 fi
836 adb wait-for-device &> /dev/null
837 sleep 0.3
838}
839
840#
Luca Stefani3a030122016-07-30 12:08:25 +0200841# fix_xml:
842#
843# $1: xml file to fix
844#
845function fix_xml() {
846 local XML="$1"
847 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
848
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +0200849 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
850 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +0200851
852 mv "$TEMP_XML" "$XML"
853}
854
855#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700856# extract:
857#
858# $1: file containing the list of items to extract
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500859# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400860# $3: section in list file to extract - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700861#
862function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700863 if [ -z "$OUTDIR" ]; then
864 echo "Output dir not set!"
865 exit 1
866 fi
867
Harry Youd9c5d1292017-08-05 09:18:56 +0100868 if [ -z "$3" ]; then
869 parse_file_list "$1"
870 else
871 parse_file_list "$1" "$3"
872 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700873
874 # Allow failing, so we can try $DEST and/or $FILE
875 set +e
876
877 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -0700878 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700879 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700880 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200881 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -0700882 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
883
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700884 if [ "$SRC" = "adb" ]; then
885 init_adb_connection
886 fi
887
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500888 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +0100889 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500890
891 # Check if we're working with the same zip that was passed last time.
892 # If so, let's just use what's already extracted.
893 MD5=`md5sum "$SRC"| awk '{print $1}'`
894 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
895
896 if [ "$MD5" != "$OLDMD5" ]; then
897 rm -rf "$DUMPDIR"
898 mkdir "$DUMPDIR"
899 unzip "$SRC" -d "$DUMPDIR"
900 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
901
902 # Stop if an A/B OTA zip is detected. We cannot extract these.
903 if [ -a "$DUMPDIR"/payload.bin ]; then
904 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
905 exit 1
906 # If OTA is block based, extract it.
907 elif [ -a "$DUMPDIR"/system.new.dat ]; then
908 echo "Converting system.new.dat to system.img"
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200909 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 -0500910 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
911 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
912 echo "Requesting sudo access to mount the system.img"
913 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
914 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
915 sudo umount "$DUMPDIR"/tmp
916 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
917 fi
918 fi
919
920 SRC="$DUMPDIR"
921 fi
922
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700923 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700924 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -0700925 rm -rf "${OUTPUT_TMP:?}"
926 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +0100927 if [ -d "$OUTPUT_ROOT" ]; then
928 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
929 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700930 VENDOR_STATE=1
931 fi
932
933 echo "Extracting $COUNT files in $1 from $SRC:"
934
935 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700936
937 local FROM=$(target_file "${FILELIST[$i-1]}")
938 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700939 local SPLIT=(${FILELIST[$i-1]//:/ })
Michael Bestas3f9b94c2018-01-25 21:05:36 +0200940 local FILE=$(echo "${SPLIT[0]#-}" | sed 's/\;.*//')
Steve Kondika991cf12016-07-28 12:13:12 -0700941 local OUTPUT_DIR="$OUTPUT_ROOT"
Steve Kondik48f8df82016-08-14 03:55:08 -0700942 local TMP_DIR="$OUTPUT_TMP"
Steve Kondika991cf12016-07-28 12:13:12 -0700943 local TARGET=
944
945 if [ "$ARGS" = "rootfs" ]; then
946 TARGET="$FROM"
947 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik48f8df82016-08-14 03:55:08 -0700948 TMP_DIR="$TMP_DIR/rootfs"
Rashed Abdel-Tawab8b87b092017-04-04 18:03:35 -0400949 elif [ -f "$SRC/$FILE" ] && [ "$SRC" != "adb" ]; then
950 TARGET="$FROM"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700951 else
Steve Kondika991cf12016-07-28 12:13:12 -0700952 TARGET="system/$FROM"
953 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700954 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700955
956 if [ "$SRC" = "adb" ]; then
957 printf ' - %s .. ' "/$TARGET"
958 else
959 printf ' - %s \n' "/$TARGET"
960 fi
961
962 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700963 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
964 mkdir -p "$OUTPUT_DIR/$DIR"
965 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700966 local DEST="$OUTPUT_DIR/$FROM"
967
Gabriele Me6df25b2017-10-11 00:58:59 +0200968 # Check pinned files
969 local HASH="${HASHLIST[$i-1]}"
970 local KEEP=""
971 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
972 if [ -f "$DEST" ]; then
973 local PINNED="$DEST"
974 else
975 local PINNED="$TMP_DIR/$FROM"
976 fi
977 if [ -f "$PINNED" ]; then
978 if [ "$(uname)" == "Darwin" ]; then
979 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
980 else
981 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
982 fi
983 if [ "$TMP_HASH" = "$HASH" ]; then
984 KEEP="1"
985 if [ ! -f "$DEST" ]; then
986 cp -p "$PINNED" "$DEST"
987 fi
988 fi
989 fi
990 fi
991
992 if [ "$KEEP" = "1" ]; then
993 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
994 elif [ "$SRC" = "adb" ]; then
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200995 # Try Lineage target first
Steve Kondika991cf12016-07-28 12:13:12 -0700996 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700997 # if file does not exist try OEM target
998 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700999 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001000 fi
1001 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001002 # Try Lineage target first
Christopher R. Palmer052d9d92017-03-04 05:12:29 -05001003 if [ -f "$SRC/$TARGET" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001004 cp "$SRC/$TARGET" "$DEST"
Christopher R. Palmer052d9d92017-03-04 05:12:29 -05001005 # if file does not exist try OEM target
1006 elif [ -f "$SRC/$FILE" ]; then
1007 cp "$SRC/$FILE" "$DEST"
Steve Kondik48f8df82016-08-14 03:55:08 -07001008 else
1009 printf ' !! file not found in source\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001010 fi
1011 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001012
Luca Stefani7f9fff22016-07-18 13:47:55 +02001013 if [ "$?" == "0" ]; then
1014 # Deodex apk|jar if that's the case
1015 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
1016 oat2dex "$DEST" "$FILE" "$SRC"
1017 if [ -f "$TMPDIR/classes.dex" ]; then
1018 zip -gjq "$DEST" "$TMPDIR/classes.dex"
1019 rm "$TMPDIR/classes.dex"
1020 printf ' (updated %s from odex files)\n' "/$FILE"
1021 fi
Luca Stefani3a030122016-07-30 12:08:25 +02001022 elif [[ "$DEST" =~ .xml$ ]]; then
1023 fix_xml "$DEST"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001024 fi
1025 fi
1026
Steve Kondik48f8df82016-08-14 03:55:08 -07001027 if [ -f "$DEST" ]; then
1028 local TYPE="${DIR##*/}"
1029 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
1030 chmod 755 "$DEST"
1031 else
1032 chmod 644 "$DEST"
1033 fi
1034 fi
1035
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001036 done
1037
1038 # Don't allow failing
1039 set -e
1040}
Louis Popia516c2f2016-07-25 15:51:13 +02001041
1042#
1043# extract_firmware:
1044#
1045# $1: file containing the list of items to extract
1046# $2: path to extracted radio folder
1047#
1048function extract_firmware() {
1049 if [ -z "$OUTDIR" ]; then
1050 echo "Output dir not set!"
1051 exit 1
1052 fi
1053
1054 parse_file_list "$1"
1055
1056 # Don't allow failing
1057 set -e
1058
1059 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1060 local COUNT=${#FILELIST[@]}
1061 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001062 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001063
1064 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1065 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1066 rm -rf "${OUTPUT_DIR:?}/"*
1067 VENDOR_RADIO_STATE=1
1068 fi
1069
1070 echo "Extracting $COUNT files in $1 from $SRC:"
1071
1072 for (( i=1; i<COUNT+1; i++ )); do
1073 local FILE="${FILELIST[$i-1]}"
1074 printf ' - %s \n' "/radio/$FILE"
1075
1076 if [ ! -d "$OUTPUT_DIR" ]; then
1077 mkdir -p "$OUTPUT_DIR"
1078 fi
1079 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1080 chmod 644 "$OUTPUT_DIR/$FILE"
1081 done
1082}