blob: 143ff2ee87afb4daafc319415cecfc83b1531d2f [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
Vladimir Olteanc5034462019-01-17 03:04:16 +0200662 if [ -n "$2" ]; then
663 echo "Using section \"$2\""
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400664 LIST=$TMPDIR/files.txt
Vladimir Oltean5238ba82019-01-19 00:44:07 +0200665 # Match all lines starting with first line found to start* with '#'
666 # comment and contain** $2, and ending with first line to be empty*.
667 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
668 # **the $2 match is case-insensitive
669 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400670 else
671 LIST=$1
672 fi
673
674
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700675 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700676 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700677 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700678 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700679
680 while read -r line; do
681 if [ -z "$line" ]; then continue; fi
682
Steve Kondik48f8df82016-08-14 03:55:08 -0700683 # If the line has a pipe delimiter, a sha1 hash should follow.
684 # This indicates the file should be pinned and not overwritten
685 # when extracting files.
686 local SPLIT=(${line//\|/ })
687 local COUNT=${#SPLIT[@]}
688 local SPEC=${SPLIT[0]}
689 local HASH="x"
690 if [ "$COUNT" -gt "1" ]; then
691 HASH=${SPLIT[1]}
692 fi
693
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700694 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700695 if [[ "$SPEC" =~ ^- ]]; then
696 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
697 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700698 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700699 PRODUCT_COPY_FILES_LIST+=("$SPEC")
700 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700701 fi
702
Rashed Abdel-Tawab855fbdd2017-04-04 02:48:18 -0400703 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700704}
705
706#
707# write_makefiles:
708#
709# $1: file containing the list of items to extract
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400710# $2: make treble compatible makefile - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700711#
712# Calls write_product_copy_files and write_product_packages on
713# the given file and appends to the Android.mk as well as
714# the product makefile.
715#
716function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700717 parse_file_list "$1"
Rashed Abdel-Tawab0ca76432017-10-07 14:18:39 -0400718 write_product_copy_files "$2"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700719 write_product_packages
720}
721
722#
Louis Popia516c2f2016-07-25 15:51:13 +0200723# append_firmware_calls_to_makefiles:
724#
725# Appends to Android.mk the calls to all images present in radio folder
726# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
727#
728function append_firmware_calls_to_makefiles() {
729 cat << EOF >> "$ANDROIDMK"
730ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
731
732RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
733\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
734 \$(call add-radio-file,radio/\$(f)))
735\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
736
737endif
738
739EOF
740}
741
742#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200743# get_file:
744#
745# $1: input file
746# $2: target file/folder
747# $3: source of the file (can be "adb" or a local folder)
748#
749# Silently extracts the input file to defined target
750# Returns success if file can be pulled from the device or found locally
751#
752function get_file() {
753 local SRC="$3"
754
755 if [ "$SRC" = "adb" ]; then
756 # try to pull
757 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
758
759 return 1
760 else
761 # try to copy
Vladimir Olteand5773252018-06-25 00:05:56 +0300762 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
763 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean78d690d2019-01-06 19:38:31 +0200764 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200765
766 return 1
767 fi
768};
769
770#
771# oat2dex:
772#
773# $1: extracted apk|jar (to check if deodex is required)
774# $2: odexed apk|jar to deodex
775# $3: source of the odexed apk|jar
776#
777# Convert apk|jar .odex in the corresposing classes.dex
778#
779function oat2dex() {
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200780 local LINEAGE_TARGET="$1"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200781 local OEM_TARGET="$2"
782 local SRC="$3"
783 local TARGET=
784 local OAT=
Joe Maples9be579f2018-01-05 14:51:33 -0500785 local HOST="$(uname)"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200786
787 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200788 export BAKSMALIJAR="$LINEAGE_ROOT"/vendor/lineage/build/tools/smali/baksmali.jar
789 export SMALIJAR="$LINEAGE_ROOT"/vendor/lineage/build/tools/smali/smali.jar
Luca Stefani7f9fff22016-07-18 13:47:55 +0200790 fi
791
Joe Maples9be579f2018-01-05 14:51:33 -0500792 if [ -z "$VDEXEXTRACTOR" ]; then
793 export VDEXEXTRACTOR="$LINEAGE_ROOT"/vendor/lineage/build/tools/"$HOST"/vdexExtractor
794 fi
795
codeworkx1c29bf62018-09-23 12:36:57 +0200796 if [ -z "$CDEXCONVERTER" ]; then
797 export CDEXCONVERTER="$LINEAGE_ROOT"/vendor/lineage/build/tools/"$HOST"/compact_dex_converter
798 fi
799
Luca Stefani7f9fff22016-07-18 13:47:55 +0200800 # Extract existing boot.oats to the temp folder
801 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700802 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200803 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700804 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteand5773252018-06-25 00:05:56 +0300805 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200806 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700807 else
808 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200809 fi
810 done
811 fi
812
813 if [ -z "$ARCHES" ]; then
814 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
815 fi
816
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200817 if [ ! -f "$LINEAGE_TARGET" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700818 return;
819 fi
820
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200821 if grep "classes.dex" "$LINEAGE_TARGET" >/dev/null; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200822 return 0 # target apk|jar is already odexed, return
823 fi
824
825 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700826 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200827
828 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400829 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200830
831 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400832 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
Joe Maples9be579f2018-01-05 14:51:33 -0500833 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
codeworkx1c29bf62018-09-23 12:36:57 +0200834 # Check if we have to deal with CompactDex
835 if [ -f "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" ]; then
836 "$CDEXCONVERTER" "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" &> /dev/null
837 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
838 else
TheStrix5aad0742018-10-03 19:06:49 +0530839 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.dex" "$TMPDIR/classes.dex"
codeworkx1c29bf62018-09-23 12:36:57 +0200840 fi
Joe Maples9be579f2018-01-05 14:51:33 -0500841 else
842 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
843 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
Rashed Abdel-Tawab54b5d5e2017-08-23 15:13:17 -0400844 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200845 elif [[ "$LINEAGE_TARGET" =~ .jar$ ]]; then
Gabriele M4cf635a2017-01-05 22:10:00 +0100846 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefanif6096e92018-10-07 12:44:53 +0200847 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Gabriele M4cf635a2017-01-05 22:10:00 +0100848 if [ ! -f "$JAROAT" ]; then
Luca Stefanif6096e92018-10-07 12:44:53 +0200849 JAROAT=$BOOTOAT
Gabriele M4cf635a2017-01-05 22:10:00 +0100850 fi
Joe Maples9be579f2018-01-05 14:51:33 -0500851 # try to extract classes.dex from boot.vdex for frameworks jars
852 # fallback to boot.oat if vdex is not available
Luca Stefanif6096e92018-10-07 12:44:53 +0200853 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
Luca Stefani99a66bf2018-10-31 19:16:05 +0100854 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")" > /dev/null
Luca Stefanif6096e92018-10-07 12:44:53 +0200855 # Check if we have to deal with CompactDex
856 if [ -f "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" ]; then
857 "$CDEXCONVERTER" "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" &> /dev/null
858 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
859 else
860 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.dex" "$TMPDIR/classes.dex"
861 fi
Joe Maples9be579f2018-01-05 14:51:33 -0500862 else
863 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$JAROAT/$OEM_TARGET"
864 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex"
865 fi
Luca Stefani7f9fff22016-07-18 13:47:55 +0200866 else
867 continue
868 fi
869
Luca Stefani7f9fff22016-07-18 13:47:55 +0200870 done
871
872 rm -rf "$TMPDIR/dexout"
873}
874
875#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700876# init_adb_connection:
877#
878# Starts adb server and waits for the device
879#
880function init_adb_connection() {
881 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
882 if ! _adb_connected; then
883 echo "No device is online. Waiting for one..."
884 echo "Please connect USB and/or enable USB debugging"
885 until _adb_connected; do
886 sleep 1
887 done
888 echo "Device Found."
889 fi
890
891 # Retrieve IP and PORT info if we're using a TCP connection
892 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
893 | head -1 | awk '{print $1}')
894 adb root &> /dev/null
895 sleep 0.3
896 if [ -n "$TCPIPPORT" ]; then
897 # adb root just killed our connection
898 # so reconnect...
899 adb connect "$TCPIPPORT"
900 fi
901 adb wait-for-device &> /dev/null
902 sleep 0.3
903}
904
905#
Luca Stefani3a030122016-07-30 12:08:25 +0200906# fix_xml:
907#
908# $1: xml file to fix
909#
910function fix_xml() {
911 local XML="$1"
912 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
913
Dobroslaw Kijowski65f03f12017-05-18 12:35:02 +0200914 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
915 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Luca Stefani3a030122016-07-30 12:08:25 +0200916
917 mv "$TEMP_XML" "$XML"
918}
919
Vladimir Olteanbec92eb2019-01-17 03:05:52 +0200920function print_spec() {
921 local SPEC_PRODUCT_PACKAGE="$1"
922 local SPEC_SRC_FILE="$2"
923 local SPEC_DST_FILE="$3"
924 local SPEC_ARGS="$4"
925 local SPEC_HASH="$5"
926
927 local PRODUCT_PACKAGE=""
928 if [ ${SPEC_PRODUCT_PACKAGE} = true ]; then
929 PRODUCT_PACKAGE="-"
930 fi
931 local SRC=""
932 if [ ! -z "${SPEC_SRC_FILE}" ] && [ "${SPEC_SRC_FILE}" != "${SPEC_DST_FILE}" ]; then
933 SRC="${SPEC_SRC_FILE}:"
934 fi
935 local DST=""
936 if [ ! -z "${SPEC_DST_FILE}" ]; then
937 DST="${SPEC_DST_FILE}"
938 fi
939 local ARGS=""
940 if [ ! -z "${SPEC_ARGS}" ]; then
941 ARGS=";${SPEC_ARGS}"
942 fi
943 local HASH=""
944 if [ ! -z "${SPEC_HASH}" ] && [ "${SPEC_HASH}" != "x" ]; then
945 HASH="|${SPEC_HASH}"
946 fi
947 printf '%s%s%s%s%s\n' "${PRODUCT_PACKAGE}" "${SRC}" "${DST}" "${ARGS}" "${HASH}"
948}
949
Luca Stefani3a030122016-07-30 12:08:25 +0200950#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700951# extract:
952#
Vladimir Olteanc5034462019-01-17 03:04:16 +0200953# Positional parameters:
954# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen7dc287f2017-03-21 09:06:11 -0500955# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Olteanc5034462019-01-17 03:04:16 +0200956# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
957#
958# Non-positional parameters (coming after $2):
959# --section: preferred way of selecting the portion to parse and extract from
960# proprietary-files.txt
Vladimir Olteanbec92eb2019-01-17 03:05:52 +0200961# --kang: if present, this option will activate the printing of hashes for the
962# extracted blobs. Useful with --section for subsequent pinning of
963# blobs taken from other origins.
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700964#
965function extract() {
Vladimir Olteanc5034462019-01-17 03:04:16 +0200966 # Consume positional parameters
967 local PROPRIETARY_FILES_TXT="$1"; shift
968 local SRC="$1"; shift
969 local SECTION=""
Vladimir Olteanbec92eb2019-01-17 03:05:52 +0200970 local KANG=false
Vladimir Olteanc5034462019-01-17 03:04:16 +0200971
972 # Consume optional, non-positional parameters
973 while [ "$#" -gt 0 ]; do
974 case "$1" in
975 -s|--section)
976 SECTION="$2"; shift
977 ;;
Vladimir Olteanbec92eb2019-01-17 03:05:52 +0200978 -k|--kang)
979 KANG=true
980 DISABLE_PINNING=1
981 ;;
Vladimir Olteanc5034462019-01-17 03:04:16 +0200982 *)
983 # Backwards-compatibility with the old behavior, where $3, if
984 # present, denoted an optional positional ${SECTION} argument.
985 # Users of ${SECTION} are encouraged to migrate from setting it as
986 # positional $3, to non-positional --section ${SECTION}, the
987 # reason being that it doesn't scale to have more than 1 optional
988 # positional argument.
989 SECTION="$1"
990 ;;
991 esac
992 shift
993 done
994
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700995 if [ -z "$OUTDIR" ]; then
996 echo "Output dir not set!"
997 exit 1
998 fi
999
Vladimir Olteanc5034462019-01-17 03:04:16 +02001000 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001001
1002 # Allow failing, so we can try $DEST and/or $FILE
1003 set +e
1004
1005 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -07001006 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001007 local PRODUCT_COPY_FILES_COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001008 local COUNT=${#FILELIST[@]}
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001009 local OUTPUT_ROOT="$LINEAGE_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -07001010 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
1011
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001012 if [ "$SRC" = "adb" ]; then
1013 init_adb_connection
1014 fi
1015
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001016 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
conbold575c6352017-11-10 16:33:38 +01001017 DUMPDIR="$TMPDIR"/system_dump
Dan Pasanen7dc287f2017-03-21 09:06:11 -05001018
1019 # Check if we're working with the same zip that was passed last time.
1020 # If so, let's just use what's already extracted.
1021 MD5=`md5sum "$SRC"| awk '{print $1}'`
1022 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1023
1024 if [ "$MD5" != "$OLDMD5" ]; then
1025 rm -rf "$DUMPDIR"
1026 mkdir "$DUMPDIR"
1027 unzip "$SRC" -d "$DUMPDIR"
1028 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1029
1030 # Stop if an A/B OTA zip is detected. We cannot extract these.
1031 if [ -a "$DUMPDIR"/payload.bin ]; then
1032 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1033 exit 1
1034 # If OTA is block based, extract it.
1035 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1036 echo "Converting system.new.dat to system.img"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001037 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 -05001038 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1039 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1040 echo "Requesting sudo access to mount the system.img"
1041 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1042 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1043 sudo umount "$DUMPDIR"/tmp
1044 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1045 fi
1046 fi
1047
1048 SRC="$DUMPDIR"
1049 fi
1050
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001051 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -07001052 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -07001053 rm -rf "${OUTPUT_TMP:?}"
1054 mkdir -p "${OUTPUT_TMP:?}"
Adrian DC3c6bdac2017-01-15 14:03:26 +01001055 if [ -d "$OUTPUT_ROOT" ]; then
1056 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1057 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001058 VENDOR_STATE=1
1059 fi
1060
Vladimir Olteanc5034462019-01-17 03:04:16 +02001061 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001062
1063 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -07001064
Vladimir Olteanda3b6442018-06-24 20:41:30 +03001065 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Oltean411e0692018-06-24 20:38:04 +03001066 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand652a062018-06-24 20:42:01 +03001067 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001068 local OUTPUT_DIR=
1069 local TMP_DIR=
1070 local SRC_FILE=
1071 local DST_FILE=
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001072 local IS_PRODUCT_PACKAGE=false
1073
1074 # Note: this relies on the fact that the ${FILELIST[@]} array
1075 # contains first ${PRODUCT_COPY_FILES_LIST[@]}, then ${PRODUCT_PACKAGES_LIST[@]}.
1076 if [ "${i}" -gt "${PRODUCT_COPY_FILES_COUNT}" ]; then
1077 IS_PRODUCT_PACKAGE=true
1078 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001079
Vladimir Olteand652a062018-06-24 20:42:01 +03001080 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001081 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1082 TMP_DIR="${OUTPUT_TMP}/rootfs"
1083 SRC_FILE="/${SPEC_SRC_FILE}"
1084 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001085 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001086 OUTPUT_DIR="${OUTPUT_ROOT}"
1087 TMP_DIR="${OUTPUT_TMP}"
1088 SRC_FILE="/system/${SPEC_SRC_FILE}"
1089 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001090 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001091
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001092 # Strip the file path in the vendor repo of "system", if present
1093 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Olteanc5034462019-01-17 03:04:16 +02001094 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001095 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondika991cf12016-07-28 12:13:12 -07001096
Gabriele Me6df25b2017-10-11 00:58:59 +02001097 # Check pinned files
Vladimir Olteanb2c38212019-01-17 02:47:02 +02001098 local HASH="$(echo ${HASHLIST[$i-1]} | awk '{ print tolower($0); }')"
Gabriele Me6df25b2017-10-11 00:58:59 +02001099 local KEEP=""
1100 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001101 if [ -f "${VENDOR_REPO_FILE}" ]; then
1102 local PINNED="${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001103 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001104 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001105 fi
1106 if [ -f "$PINNED" ]; then
1107 if [ "$(uname)" == "Darwin" ]; then
1108 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
1109 else
1110 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
1111 fi
1112 if [ "$TMP_HASH" = "$HASH" ]; then
1113 KEEP="1"
Vladimir Olteand6747712018-06-24 20:46:42 +03001114 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1115 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele Me6df25b2017-10-11 00:58:59 +02001116 fi
1117 fi
1118 fi
1119 fi
1120
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001121 if [ "${KANG}" = false ]; then
1122 printf ' - %s\n' "${BLOB_DISPLAY_NAME}"
1123 fi
1124
Gabriele Me6df25b2017-10-11 00:58:59 +02001125 if [ "$KEEP" = "1" ]; then
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001126 printf ' + keeping pinned file with hash %s\n' "${HASH}"
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001127 else
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001128 FOUND=false
1129 # Try Lineage target first.
1130 # Also try to search for files stripped of
1131 # the "/system" prefix, if we're actually extracting
1132 # from a system image.
Vladimir Olteand5773252018-06-25 00:05:56 +03001133 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001134 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1135 FOUND=true
1136 break
1137 }
1138 done
1139
1140 if [ "${FOUND}" = false ]; then
Vladimir Olteanc5034462019-01-17 03:04:16 +02001141 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Olteanb8084ec2018-10-18 00:44:02 +03001142 continue
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001143 fi
1144 fi
Steve Kondika991cf12016-07-28 12:13:12 -07001145
Luca Stefani7f9fff22016-07-18 13:47:55 +02001146 if [ "$?" == "0" ]; then
1147 # Deodex apk|jar if that's the case
Vladimir Olteand6747712018-06-24 20:46:42 +03001148 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
Vladimir Olteand5773252018-06-25 00:05:56 +03001149 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001150 if [ -f "$TMPDIR/classes.dex" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001151 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes.dex"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001152 rm "$TMPDIR/classes.dex"
Vladimir Olteand5773252018-06-25 00:05:56 +03001153 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001154 fi
Vladimir Olteand6747712018-06-24 20:46:42 +03001155 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1156 fix_xml "${VENDOR_REPO_FILE}"
Luca Stefani7f9fff22016-07-18 13:47:55 +02001157 fi
1158 fi
1159
Vladimir Olteand6747712018-06-24 20:46:42 +03001160 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Oltean5f15e3e2018-06-24 21:06:12 +03001161 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik48f8df82016-08-14 03:55:08 -07001162 local TYPE="${DIR##*/}"
1163 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Olteand6747712018-06-24 20:46:42 +03001164 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001165 else
Vladimir Olteand6747712018-06-24 20:46:42 +03001166 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik48f8df82016-08-14 03:55:08 -07001167 fi
1168 fi
1169
Vladimir Olteanbec92eb2019-01-17 03:05:52 +02001170 if [ "${KANG}" = true ]; then
1171 print_spec "${IS_PRODUCT_PACKAGE}" "${SPEC_SRC_FILE}" "${SPEC_DST_FILE}" "${SPEC_ARGS}" "${HASH}"
1172 fi
1173
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001174 done
1175
1176 # Don't allow failing
1177 set -e
1178}
Louis Popia516c2f2016-07-25 15:51:13 +02001179
1180#
1181# extract_firmware:
1182#
1183# $1: file containing the list of items to extract
1184# $2: path to extracted radio folder
1185#
1186function extract_firmware() {
1187 if [ -z "$OUTDIR" ]; then
1188 echo "Output dir not set!"
1189 exit 1
1190 fi
1191
1192 parse_file_list "$1"
1193
1194 # Don't allow failing
1195 set -e
1196
1197 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1198 local COUNT=${#FILELIST[@]}
1199 local SRC="$2"
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001200 local OUTPUT_DIR="$LINEAGE_ROOT"/"$OUTDIR"/radio
Louis Popia516c2f2016-07-25 15:51:13 +02001201
1202 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1203 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1204 rm -rf "${OUTPUT_DIR:?}/"*
1205 VENDOR_RADIO_STATE=1
1206 fi
1207
1208 echo "Extracting $COUNT files in $1 from $SRC:"
1209
1210 for (( i=1; i<COUNT+1; i++ )); do
1211 local FILE="${FILELIST[$i-1]}"
1212 printf ' - %s \n' "/radio/$FILE"
1213
1214 if [ ! -d "$OUTPUT_DIR" ]; then
1215 mkdir -p "$OUTPUT_DIR"
1216 fi
1217 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1218 chmod 644 "$OUTPUT_DIR/$FILE"
1219 done
1220}