blob: 116aec6510c697026f17c7d3830d1a2c5408e04c [file] [log] [blame]
Steve Kondik5bd66602016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_COPY_FILES_HASHES=()
20PRODUCT_PACKAGES_LIST=()
21PRODUCT_PACKAGES_HASHES=()
22PACKAGE_LIST=()
23VENDOR_STATE=-1
24VENDOR_RADIO_STATE=-1
25COMMON=-1
26ARCHES=
27FULLY_DEODEXED=-1
28
Rashed Abdel-Tawabe7d9b5c2017-08-05 23:11:35 -040029TMPDIR=$(mktemp -d)
Steve Kondik5bd66602016-07-15 10:39:58 -070030
31#
32# cleanup
33#
34# kill our tmpfiles with fire on exit
35#
36function cleanup() {
37 rm -rf "${TMPDIR:?}"
38}
39
Gabriele Mb8e54572017-10-11 12:55:51 +020040trap cleanup 0
Steve Kondik5bd66602016-07-15 10:39:58 -070041
42#
43# setup_vendor
44#
45# $1: device name
46# $2: vendor name
theimpulson9a911af2019-08-14 03:25:12 +000047# $3: OMNI root directory
Steve Kondik5bd66602016-07-15 10:39:58 -070048# $4: is common device - optional, default to false
49# $5: cleanup - optional, default to true
Jake Whatley9843b322017-01-25 21:49:16 -050050# $6: custom vendor makefile name - optional, default to false
Steve Kondik5bd66602016-07-15 10:39:58 -070051#
52# Must be called before any other functions can be used. This
53# sets up the internal state for a new vendor configuration.
54#
55function setup_vendor() {
56 local DEVICE="$1"
57 if [ -z "$DEVICE" ]; then
58 echo "\$DEVICE must be set before including this script!"
59 exit 1
60 fi
61
62 export VENDOR="$2"
63 if [ -z "$VENDOR" ]; then
64 echo "\$VENDOR must be set before including this script!"
65 exit 1
66 fi
67
theimpulson9a911af2019-08-14 03:25:12 +000068 export OMNI_ROOT="$3"
69 if [ ! -d "$OMNI_ROOT" ]; then
70 echo "\$OMNI_ROOT must be set and valid before including this script!"
Steve Kondik5bd66602016-07-15 10:39:58 -070071 exit 1
72 fi
73
74 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
theimpulson9a911af2019-08-14 03:25:12 +000075 if [ ! -d "$OMNI_ROOT/$OUTDIR" ]; then
76 mkdir -p "$OMNI_ROOT/$OUTDIR"
Steve Kondik5bd66602016-07-15 10:39:58 -070077 fi
78
Jake Whatley9843b322017-01-25 21:49:16 -050079 VNDNAME="$6"
80 if [ -z "$VNDNAME" ]; then
81 VNDNAME="$DEVICE"
82 fi
83
theimpulson9a911af2019-08-14 03:25:12 +000084 export PRODUCTMK="$OMNI_ROOT"/"$OUTDIR"/device-vendor.mk
85 export ANDROIDMK="$OMNI_ROOT"/"$OUTDIR"/Android.mk
86 export BOARDMK="$OMNI_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
Steve Kondik5bd66602016-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 Mc44696d2017-05-01 18:22:04 +020094 if [ "$5" == "false" ] || [ "$5" == "0" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -070095 VENDOR_STATE=1
96 VENDOR_RADIO_STATE=1
97 else
98 VENDOR_STATE=0
99 VENDOR_RADIO_STATE=0
100 fi
101}
102
Vladimir Oltean75d8e052018-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 Kondik5bd66602016-07-15 10:39:58 -0700124#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300125# input: spec in the form of "src[:dst][;args]"
126# output: "dst" if present, "src" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700127#
128function target_file() {
Vladimir Olteanc70bc122018-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 Kondik5bd66602016-07-15 10:39:58 -0700145}
146
147#
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300148# input: spec in the form of "src[:dst][;args]"
149# output: "args" if present, "" otherwise.
Steve Kondik5bd66602016-07-15 10:39:58 -0700150#
151function target_args() {
Vladimir Olteanc70bc122018-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 Kondik5bd66602016-07-15 10:39:58 -0700166}
167
168#
169# prefix_match:
170#
Vladimir Oltean011b6b62018-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 Kondik5bd66602016-07-15 10:39:58 -0700176#
177function prefix_match() {
178 local PREFIX="$1"
Vladimir Oltean7220f362018-04-02 22:37:09 +0300179 for LINE in "${PRODUCT_PACKAGES_LIST[@]}"; do
180 local FILE=$(target_file "$LINE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700181 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
Vladimir Oltean011b6b62018-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 Kondik5bd66602016-07-15 10:39:58 -0700188 fi
189 done
190}
191
192#
Rashed Abdel-Tawab7fd3ccb2017-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#
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -0700212# suffix_match_file:
213#
214# $1: the suffix to match on
215# $2: the file to match the suffix for
216#
217# Internal function which returns true if a filename contains the
218# specified suffix.
219#
220function suffix_match_file() {
221 local SUFFIX="$1"
222 local FILE="$2"
223 if [[ "$FILE" = *"$SUFFIX" ]]; then
224 return 0
225 else
226 return 1
227 fi
228}
229
230#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400231# truncate_file
232#
233# $1: the filename to truncate
234# $2: the argument to output the truncated filename to
235#
236# Internal function which truncates a filename by removing the first dir
237# in the path. ex. vendor/lib/libsdmextension.so -> lib/libsdmextension.so
238#
239function truncate_file() {
240 local FILE="$1"
241 RETURN_FILE="$2"
242 local FIND="${FILE%%/*}"
243 local LOCATION="${#FIND}+1"
244 echo ${FILE:$LOCATION}
245}
246
247#
Steve Kondik5bd66602016-07-15 10:39:58 -0700248# write_product_copy_files:
249#
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400250# $1: make treble compatible makefile - optional, default to false
251#
Steve Kondik5bd66602016-07-15 10:39:58 -0700252# Creates the PRODUCT_COPY_FILES section in the product makefile for all
253# items in the list which do not start with a dash (-).
254#
255function write_product_copy_files() {
256 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
257 local TARGET=
258 local FILE=
259 local LINEEND=
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400260 local TREBLE_COMPAT=$1
Steve Kondik5bd66602016-07-15 10:39:58 -0700261
262 if [ "$COUNT" -eq "0" ]; then
263 return 0
264 fi
265
266 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
267 for (( i=1; i<COUNT+1; i++ )); do
268 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
269 LINEEND=" \\"
270 if [ "$i" -eq "$COUNT" ]; then
271 LINEEND=""
272 fi
273
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300274 TARGET=$(target_file "$FILE")
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400275 if [ "$TREBLE_COMPAT" == "true" ] || [ "$TREBLE_COMPAT" == "1" ]; then
276 if prefix_match_file "vendor/" $TARGET ; then
277 local OUTTARGET=$(truncate_file $TARGET)
278 printf ' %s/proprietary/%s:$(TARGET_COPY_OUT_VENDOR)/%s%s\n' \
279 "$OUTDIR" "$TARGET" "$OUTTARGET" "$LINEEND" >> "$PRODUCTMK"
280 else
281 printf ' %s/proprietary/%s:system/%s%s\n' \
282 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
283 fi
284 else
285 printf ' %s/proprietary/%s:system/%s%s\n' \
286 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
287 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700288 done
289 return 0
290}
291
292#
293# write_packages:
294#
295# $1: The LOCAL_MODULE_CLASS for the given module list
296# $2: "true" if this package is part of the vendor/ path
297# $3: type-specific extra flags
298# $4: Name of the array holding the target list
299#
300# Internal function which writes out the BUILD_PREBUILT stanzas
301# for all modules in the list. This is called by write_product_packages
302# after the modules are categorized.
303#
304function write_packages() {
305
306 local CLASS="$1"
307 local VENDOR_PKG="$2"
308 local EXTRA="$3"
309
310 # Yes, this is a horrible hack - we create a new array using indirection
311 local ARR_NAME="$4[@]"
312 local FILELIST=("${!ARR_NAME}")
313
314 local FILE=
315 local ARGS=
316 local BASENAME=
317 local EXTENSION=
318 local PKGNAME=
319 local SRC=
320
321 for P in "${FILELIST[@]}"; do
Vladimir Olteanc70bc122018-06-24 20:09:55 +0300322 FILE=$(target_file "$P")
Steve Kondik5bd66602016-07-15 10:39:58 -0700323 ARGS=$(target_args "$P")
324
325 BASENAME=$(basename "$FILE")
M1cha3e8c5bf2017-01-04 09:00:11 +0100326 DIRNAME=$(dirname "$FILE")
Steve Kondik5bd66602016-07-15 10:39:58 -0700327 EXTENSION=${BASENAME##*.}
328 PKGNAME=${BASENAME%.*}
329
330 # Add to final package list
331 PACKAGE_LIST+=("$PKGNAME")
332
333 SRC="proprietary"
334 if [ "$VENDOR_PKG" = "true" ]; then
335 SRC+="/vendor"
336 fi
337
338 printf 'include $(CLEAR_VARS)\n'
339 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
340 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
341 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
342 if [ "$EXTRA" = "both" ]; then
343 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
344 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
345 #if [ "$VENDOR_PKG" = "true" ]; then
346 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
347 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
348 #else
349 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
350 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
351 #fi
352 elif [ "$EXTRA" = "64" ]; then
353 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
354 else
355 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
356 fi
357 if [ "$EXTRA" != "none" ]; then
358 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
359 fi
360 elif [ "$CLASS" = "APPS" ]; then
Michael Bestas9c6f2eb2018-01-25 21:05:36 +0200361 if [ "$EXTRA" = "priv-app" ]; then
362 SRC="$SRC/priv-app"
363 else
364 SRC="$SRC/app"
Steve Kondik5bd66602016-07-15 10:39:58 -0700365 fi
366 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
367 local CERT=platform
368 if [ ! -z "$ARGS" ]; then
369 CERT="$ARGS"
370 fi
371 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
372 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
373 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
Elektroschmockdd792302016-10-04 21:11:43 +0200374 local CERT=platform
375 if [ ! -z "$ARGS" ]; then
376 CERT="$ARGS"
377 fi
378 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
Steve Kondik5bd66602016-07-15 10:39:58 -0700379 elif [ "$CLASS" = "ETC" ]; then
380 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
381 elif [ "$CLASS" = "EXECUTABLES" ]; then
382 if [ "$ARGS" = "rootfs" ]; then
383 SRC="$SRC/rootfs"
384 if [ "$EXTRA" = "sbin" ]; then
385 SRC="$SRC/sbin"
386 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
387 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
388 fi
389 else
390 SRC="$SRC/bin"
391 fi
392 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
393 unset EXTENSION
394 else
395 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
396 fi
397 printf 'LOCAL_MODULE_TAGS := optional\n'
398 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang173575f3bb2016-08-28 20:38:45 -0400399 if [ "$CLASS" = "APPS" ]; then
400 printf 'LOCAL_DEX_PREOPT := false\n'
401 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700402 if [ ! -z "$EXTENSION" ]; then
403 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
404 fi
M1cha3e8c5bf2017-01-04 09:00:11 +0100405 if [ "$CLASS" = "SHARED_LIBRARIES" ] || [ "$CLASS" = "EXECUTABLES" ]; then
406 if [ "$DIRNAME" != "." ]; then
407 printf 'LOCAL_MODULE_RELATIVE_PATH := %s\n' "$DIRNAME"
408 fi
409 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700410 if [ "$EXTRA" = "priv-app" ]; then
411 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
412 fi
413 if [ "$VENDOR_PKG" = "true" ]; then
Ethan Chen4f738f52018-02-17 20:03:54 -0800414 printf 'LOCAL_VENDOR_MODULE := true\n'
Steve Kondik5bd66602016-07-15 10:39:58 -0700415 fi
416 printf 'include $(BUILD_PREBUILT)\n\n'
417 done
418}
419
420#
421# write_product_packages:
422#
423# This function will create BUILD_PREBUILT entries in the
424# Android.mk and associated PRODUCT_PACKAGES list in the
425# product makefile for all files in the blob list which
426# start with a single dash (-) character.
427#
428function write_product_packages() {
429 PACKAGE_LIST=()
430
431 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
432
433 if [ "$COUNT" = "0" ]; then
434 return 0
435 fi
436
437 # Figure out what's 32-bit, what's 64-bit, and what's multilib
438 # I really should not be doing this in bash due to shitty array passing :(
439 local T_LIB32=( $(prefix_match "lib/") )
440 local T_LIB64=( $(prefix_match "lib64/") )
441 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
442 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
443 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
444
445 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
446 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
447 fi
448 if [ "${#LIB32[@]}" -gt "0" ]; then
449 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
450 fi
451 if [ "${#LIB64[@]}" -gt "0" ]; then
452 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
453 fi
454
455 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
456 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
457 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
458 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
459 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
460
461 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
462 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
463 fi
464 if [ "${#V_LIB32[@]}" -gt "0" ]; then
465 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
466 fi
467 if [ "${#V_LIB64[@]}" -gt "0" ]; then
468 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
469 fi
470
471 # Apps
472 local APPS=( $(prefix_match "app/") )
473 if [ "${#APPS[@]}" -gt "0" ]; then
474 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
475 fi
476 local PRIV_APPS=( $(prefix_match "priv-app/") )
477 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
478 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
479 fi
480 local V_APPS=( $(prefix_match "vendor/app/") )
481 if [ "${#V_APPS[@]}" -gt "0" ]; then
482 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
483 fi
484 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
485 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
486 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
487 fi
488
489 # Framework
490 local FRAMEWORK=( $(prefix_match "framework/") )
491 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
492 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
493 fi
Christian Oder974b5902017-10-08 23:15:52 +0200494 local V_FRAMEWORK=( $(prefix_match "vendor/framework/") )
Michael Bestas26eb01e2018-02-27 22:31:55 +0200495 if [ "${#V_FRAMEWORK[@]}" -gt "0" ]; then
Christian Oder974b5902017-10-08 23:15:52 +0200496 write_packages "JAVA_LIBRARIES" "true" "" "V_FRAMEWORK" >> "$ANDROIDMK"
497 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700498
499 # Etc
500 local ETC=( $(prefix_match "etc/") )
501 if [ "${#ETC[@]}" -gt "0" ]; then
502 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
503 fi
504 local V_ETC=( $(prefix_match "vendor/etc/") )
505 if [ "${#V_ETC[@]}" -gt "0" ]; then
Rashed Abdel-Tawabcc98bc32017-10-08 17:33:42 -0400506 write_packages "ETC" "true" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik5bd66602016-07-15 10:39:58 -0700507 fi
508
509 # Executables
510 local BIN=( $(prefix_match "bin/") )
511 if [ "${#BIN[@]}" -gt "0" ]; then
512 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
513 fi
514 local V_BIN=( $(prefix_match "vendor/bin/") )
515 if [ "${#V_BIN[@]}" -gt "0" ]; then
516 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
517 fi
518 local SBIN=( $(prefix_match "sbin/") )
519 if [ "${#SBIN[@]}" -gt "0" ]; then
520 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
521 fi
522
523
524 # Actually write out the final PRODUCT_PACKAGES list
525 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
526
527 if [ "$PACKAGE_COUNT" -eq "0" ]; then
528 return 0
529 fi
530
531 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
532 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
533 local LINEEND=" \\"
534 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
535 LINEEND=""
536 fi
537 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
538 done
539}
540
541#
542# write_header:
543#
544# $1: file which will be written to
545#
546# writes out the copyright header with the current year.
547# note that this is not an append operation, and should
548# be executed first!
549#
550function write_header() {
Jake Whatley9843b322017-01-25 21:49:16 -0500551 if [ -f $1 ]; then
552 rm $1
553 fi
554
Steve Kondik5bd66602016-07-15 10:39:58 -0700555 YEAR=$(date +"%Y")
556
557 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
558
Jake Whatley9843b322017-01-25 21:49:16 -0500559 NUM_REGEX='^[0-9]+$'
560 if [[ $INITIAL_COPYRIGHT_YEAR =~ $NUM_REGEX ]] && [ $INITIAL_COPYRIGHT_YEAR -le $YEAR ]; then
561 if [ $INITIAL_COPYRIGHT_YEAR -lt 2016 ]; then
562 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-2016 The CyanogenMod Project\n" > $1
563 elif [ $INITIAL_COPYRIGHT_YEAR -eq 2016 ]; then
564 printf "# Copyright (C) 2016 The CyanogenMod Project\n" > $1
565 fi
566 if [ $YEAR -eq 2017 ]; then
567 printf "# Copyright (C) 2017 The LineageOS Project\n" >> $1
568 elif [ $INITIAL_COPYRIGHT_YEAR -eq $YEAR ]; then
569 printf "# Copyright (C) $YEAR The LineageOS Project\n" >> $1
570 elif [ $INITIAL_COPYRIGHT_YEAR -le 2017 ]; then
571 printf "# Copyright (C) 2017-$YEAR The LineageOS Project\n" >> $1
572 else
573 printf "# Copyright (C) $INITIAL_COPYRIGHT_YEAR-$YEAR The LineageOS Project\n" >> $1
574 fi
575 else
576 printf "# Copyright (C) $YEAR The LineageOS Project\n" > $1
577 fi
578
579 cat << EOF >> $1
Steve Kondik5bd66602016-07-15 10:39:58 -0700580#
581# Licensed under the Apache License, Version 2.0 (the "License");
582# you may not use this file except in compliance with the License.
583# You may obtain a copy of the License at
584#
585# http://www.apache.org/licenses/LICENSE-2.0
586#
587# Unless required by applicable law or agreed to in writing, software
588# distributed under the License is distributed on an "AS IS" BASIS,
589# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
590# See the License for the specific language governing permissions and
591# limitations under the License.
592
593# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
594
595EOF
596}
597
598#
599# write_headers:
600#
601# $1: devices falling under common to be added to guard - optional
Jake Whatley9843b322017-01-25 21:49:16 -0500602# $2: custom guard - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700603#
604# Calls write_header for each of the makefiles and creates
605# the initial path declaration and device guard for the
606# Android.mk
607#
608function write_headers() {
609 write_header "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500610
611 GUARD="$2"
612 if [ -z "$GUARD" ]; then
613 GUARD="TARGET_DEVICE"
614 fi
615
Steve Kondik5bd66602016-07-15 10:39:58 -0700616 cat << EOF >> "$ANDROIDMK"
617LOCAL_PATH := \$(call my-dir)
618
619EOF
620 if [ "$COMMON" -ne 1 ]; then
621 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500622ifeq (\$($GUARD),$DEVICE)
Steve Kondik5bd66602016-07-15 10:39:58 -0700623
624EOF
625 else
626 if [ -z "$1" ]; then
627 echo "Argument with devices to be added to guard must be set!"
628 exit 1
629 fi
630 cat << EOF >> "$ANDROIDMK"
Jake Whatley9843b322017-01-25 21:49:16 -0500631ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik5bd66602016-07-15 10:39:58 -0700632
633EOF
634 fi
635
636 write_header "$BOARDMK"
637 write_header "$PRODUCTMK"
638}
639
640#
641# write_footers:
642#
643# Closes the inital guard and any other finalization tasks. Must
644# be called as the final step.
645#
646function write_footers() {
647 cat << EOF >> "$ANDROIDMK"
648endif
649EOF
650}
651
652# Return success if adb is up and not in recovery
653function _adb_connected {
654 {
Jake Whatley9843b322017-01-25 21:49:16 -0500655 if [[ "$(adb get-state)" == device ]]
Steve Kondik5bd66602016-07-15 10:39:58 -0700656 then
657 return 0
658 fi
659 } 2>/dev/null
660
661 return 1
662};
663
664#
665# parse_file_list:
666#
667# $1: input file
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400668# $2: blob section in file - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700669#
670# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
671#
672function parse_file_list() {
673 if [ -z "$1" ]; then
674 echo "An input file is expected!"
675 exit 1
676 elif [ ! -f "$1" ]; then
677 echo "Input file "$1" does not exist!"
678 exit 1
679 fi
680
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200681 if [ -n "$2" ]; then
682 echo "Using section \"$2\""
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400683 LIST=$TMPDIR/files.txt
Vladimir Olteanfa79f212019-01-19 00:44:07 +0200684 # Match all lines starting with first line found to start* with '#'
685 # comment and contain** $2, and ending with first line to be empty*.
686 # *whitespaces (tabs, spaces) at the beginning of lines are discarded
687 # **the $2 match is case-insensitive
688 cat $1 | sed -n '/^[[:space:]]*#.*'"$2"'/I,/^[[:space:]]*$/ p' > $LIST
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400689 else
690 LIST=$1
691 fi
692
693
Steve Kondik5bd66602016-07-15 10:39:58 -0700694 PRODUCT_PACKAGES_LIST=()
695 PRODUCT_PACKAGES_HASHES=()
696 PRODUCT_COPY_FILES_LIST=()
697 PRODUCT_COPY_FILES_HASHES=()
698
699 while read -r line; do
700 if [ -z "$line" ]; then continue; fi
701
702 # If the line has a pipe delimiter, a sha1 hash should follow.
703 # This indicates the file should be pinned and not overwritten
704 # when extracting files.
705 local SPLIT=(${line//\|/ })
706 local COUNT=${#SPLIT[@]}
707 local SPEC=${SPLIT[0]}
708 local HASH="x"
709 if [ "$COUNT" -gt "1" ]; then
710 HASH=${SPLIT[1]}
711 fi
712
713 # if line starts with a dash, it needs to be packaged
714 if [[ "$SPEC" =~ ^- ]]; then
715 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
716 PRODUCT_PACKAGES_HASHES+=("$HASH")
717 else
718 PRODUCT_COPY_FILES_LIST+=("$SPEC")
719 PRODUCT_COPY_FILES_HASHES+=("$HASH")
720 fi
721
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400722 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700723}
724
725#
726# write_makefiles:
727#
728# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400729# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700730#
731# Calls write_product_copy_files and write_product_packages on
732# the given file and appends to the Android.mk as well as
733# the product makefile.
734#
735function write_makefiles() {
736 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400737 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700738 write_product_packages
739}
740
741#
742# append_firmware_calls_to_makefiles:
743#
744# Appends to Android.mk the calls to all images present in radio folder
745# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
746#
747function append_firmware_calls_to_makefiles() {
748 cat << EOF >> "$ANDROIDMK"
749ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
750
751RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
752\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
753 \$(call add-radio-file,radio/\$(f)))
754\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
755
756endif
757
758EOF
759}
760
761#
762# get_file:
763#
764# $1: input file
765# $2: target file/folder
766# $3: source of the file (can be "adb" or a local folder)
767#
768# Silently extracts the input file to defined target
769# Returns success if file can be pulled from the device or found locally
770#
771function get_file() {
772 local SRC="$3"
773
774 if [ "$SRC" = "adb" ]; then
775 # try to pull
776 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
777
778 return 1
779 else
780 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +0300781 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
782 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +0200783 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700784
785 return 1
786 fi
787};
788
789#
790# oat2dex:
791#
792# $1: extracted apk|jar (to check if deodex is required)
793# $2: odexed apk|jar to deodex
794# $3: source of the odexed apk|jar
795#
796# Convert apk|jar .odex in the corresposing classes.dex
797#
798function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +0000799 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -0700800 local OEM_TARGET="$2"
801 local SRC="$3"
802 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -0500803 local OAT=
804 local HOST="$(uname)"
Steve Kondik5bd66602016-07-15 10:39:58 -0700805
Joe Maplesfb3941c2018-01-05 14:51:33 -0500806 if [ -z "$ANDROID_HOST_OUT" ]; then
807 echo "ERROR: ANDROID_HOST_OUT not found!"
808 echo "ERROR: Please lunch a device before running this script."
809 exit 1
Steve Kondik5bd66602016-07-15 10:39:58 -0700810 fi
811
Joe Maplesfb3941c2018-01-05 14:51:33 -0500812 if [ -z "$OATDUMP" ] || [ -z "$VDEXEXTRACTOR" ]; then
813 if [ ! -f "$ANDROID_HOST_OUT/bin/oatdump" ]; then
814 echo "ERROR: oatdump utility not found!"
815 echo "ERROR: Please run 'make oatdump'"
816 echo "ERROR: from the top of the android tree before running this script."
817 exit 1
818 else
819 export OATDUMP="$ANDROID_HOST_OUT/bin/oatdump"
820 fi
theimpulson9a911af2019-08-14 03:25:12 +0000821 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/vdexExtractor
Joe Maplesfb3941c2018-01-05 14:51:33 -0500822 fi
823
codeworkx85eda752018-09-23 12:36:57 +0200824 if [ -z "$CDEXCONVERTER" ]; then
theimpulson9a911af2019-08-14 03:25:12 +0000825 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +0200826 fi
827
Steve Kondik5bd66602016-07-15 10:39:58 -0700828 # Extract existing boot.oats to the temp folder
829 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500830 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700831 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500832 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +0300833 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700834 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500835 else
836 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700837 fi
838 done
839 fi
840
841 if [ -z "$ARCHES" ]; then
842 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
843 fi
844
theimpulson9a911af2019-08-14 03:25:12 +0000845 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700846 return;
847 fi
848
theimpulson9a911af2019-08-14 03:25:12 +0000849 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700850 return 0 # target apk|jar is already odexed, return
851 fi
852
853 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -0500854 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700855
Joe Maplesfb3941c2018-01-05 14:51:33 -0500856 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
857 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -0700858
Joe Maplesfb3941c2018-01-05 14:51:33 -0500859
860 if get_file "$OAT" "$TMPDIR" "$SRC"; then
861 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
862 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
codeworkx85eda752018-09-23 12:36:57 +0200863 # Check if we have to deal with CompactDex
864 if [ -f "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" ]; then
865 "$CDEXCONVERTER" "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" &> /dev/null
866 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
867 else
TheStrix6bd412c2018-10-03 19:06:49 +0530868 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.dex" "$TMPDIR/classes.dex"
codeworkx85eda752018-09-23 12:36:57 +0200869 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500870 else
871 "$OATDUMP" --oat-file="$TMPDIR/$(basename "$OAT")" --export-dex-to="$TMPDIR" > /dev/null
872 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
873 fi
theimpulson9a911af2019-08-14 03:25:12 +0000874 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500875 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +0200876 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -0500877 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +0200878 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -0500879 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500880 # try to extract classes.dex from boot.vdex for frameworks jars
881 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +0200882 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
883 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")"
884 # Check if we have to deal with CompactDex
885 if [ -f "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" ]; then
886 "$CDEXCONVERTER" "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" &> /dev/null
887 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
888 else
889 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.dex" "$TMPDIR/classes.dex"
890 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500891 else
892 "$OATDUMP" --oat-file="$JAROAT" --export-dex-to="$TMPDIR" > /dev/null
893 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
894 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700895 else
896 continue
897 fi
898
Steve Kondik5bd66602016-07-15 10:39:58 -0700899 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700900}
901
902#
903# init_adb_connection:
904#
905# Starts adb server and waits for the device
906#
907function init_adb_connection() {
908 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
909 if ! _adb_connected; then
910 echo "No device is online. Waiting for one..."
911 echo "Please connect USB and/or enable USB debugging"
912 until _adb_connected; do
913 sleep 1
914 done
915 echo "Device Found."
916 fi
917
918 # Retrieve IP and PORT info if we're using a TCP connection
919 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
920 | head -1 | awk '{print $1}')
921 adb root &> /dev/null
922 sleep 0.3
923 if [ -n "$TCPIPPORT" ]; then
924 # adb root just killed our connection
925 # so reconnect...
926 adb connect "$TCPIPPORT"
927 fi
928 adb wait-for-device &> /dev/null
929 sleep 0.3
930}
931
932#
933# fix_xml:
934#
935# $1: xml file to fix
936#
937function fix_xml() {
938 local XML="$1"
939 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
940
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200941 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
942 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700943
944 mv "$TEMP_XML" "$XML"
945}
946
947#
948# extract:
949#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200950# Positional parameters:
951# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -0500952# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200953# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
954#
955# Non-positional parameters (coming after $2):
956# --section: preferred way of selecting the portion to parse and extract from
957# proprietary-files.txt
Steve Kondik5bd66602016-07-15 10:39:58 -0700958#
959function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200960 # Consume positional parameters
961 local PROPRIETARY_FILES_TXT="$1"; shift
962 local SRC="$1"; shift
963 local SECTION=""
964
965 # Consume optional, non-positional parameters
966 while [ "$#" -gt 0 ]; do
967 case "$1" in
968 -s|--section)
969 SECTION="$2"; shift
970 ;;
971 *)
972 # Backwards-compatibility with the old behavior, where $3, if
973 # present, denoted an optional positional ${SECTION} argument.
974 # Users of ${SECTION} are encouraged to migrate from setting it as
975 # positional $3, to non-positional --section ${SECTION}, the
976 # reason being that it doesn't scale to have more than 1 optional
977 # positional argument.
978 SECTION="$1"
979 ;;
980 esac
981 shift
982 done
983
Steve Kondik5bd66602016-07-15 10:39:58 -0700984 if [ -z "$OUTDIR" ]; then
985 echo "Output dir not set!"
986 exit 1
987 fi
988
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200989 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700990
991 # Allow failing, so we can try $DEST and/or $FILE
992 set +e
993
994 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
995 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
996 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +0000997 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -0700998 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
999
1000 if [ "$SRC" = "adb" ]; then
1001 init_adb_connection
1002 fi
1003
Dan Pasanen0cc05012017-03-21 09:06:11 -05001004 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
theimpulson9a911af2019-08-14 03:25:12 +00001005 DUMPDIR="$OMNI_ROOT"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001006
1007 # Check if we're working with the same zip that was passed last time.
1008 # If so, let's just use what's already extracted.
1009 MD5=`md5sum "$SRC"| awk '{print $1}'`
1010 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1011
1012 if [ "$MD5" != "$OLDMD5" ]; then
1013 rm -rf "$DUMPDIR"
1014 mkdir "$DUMPDIR"
1015 unzip "$SRC" -d "$DUMPDIR"
1016 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1017
1018 # Stop if an A/B OTA zip is detected. We cannot extract these.
1019 if [ -a "$DUMPDIR"/payload.bin ]; then
1020 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1021 exit 1
1022 # If OTA is block based, extract it.
1023 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1024 echo "Converting system.new.dat to system.img"
theimpulson9a911af2019-08-14 03:25:12 +00001025 python "$OMNI_ROOT"/vendor/omni/build/tools/sdat2img.py "$DUMPDIR"/system.transfer.list "$DUMPDIR"/system.new.dat "$DUMPDIR"/system.img 2>&1
Dan Pasanen0cc05012017-03-21 09:06:11 -05001026 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1027 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1028 echo "Requesting sudo access to mount the system.img"
1029 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1030 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1031 sudo umount "$DUMPDIR"/tmp
1032 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1033 fi
1034 fi
1035
1036 SRC="$DUMPDIR"
1037 fi
1038
Steve Kondik5bd66602016-07-15 10:39:58 -07001039 if [ "$VENDOR_STATE" -eq "0" ]; then
1040 echo "Cleaning output directory ($OUTPUT_ROOT).."
1041 rm -rf "${OUTPUT_TMP:?}"
1042 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001043 if [ -d "$OUTPUT_ROOT" ]; then
1044 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1045 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001046 VENDOR_STATE=1
1047 fi
1048
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001049 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001050
1051 for (( i=1; i<COUNT+1; i++ )); do
1052
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001053 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001054 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001055 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001056 local OUTPUT_DIR=
1057 local TMP_DIR=
1058 local SRC_FILE=
1059 local DST_FILE=
Steve Kondik5bd66602016-07-15 10:39:58 -07001060
Vladimir Olteand6391332018-06-24 20:42:01 +03001061 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001062 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1063 TMP_DIR="${OUTPUT_TMP}/rootfs"
1064 SRC_FILE="/${SPEC_SRC_FILE}"
1065 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001066 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001067 OUTPUT_DIR="${OUTPUT_ROOT}"
1068 TMP_DIR="${OUTPUT_TMP}"
1069 SRC_FILE="/system/${SPEC_SRC_FILE}"
1070 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001071 fi
1072
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001073 # Strip the file path in the vendor repo of "system", if present
1074 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001075 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001076 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001077
Gabriele M58270a32017-11-13 23:15:29 +01001078 # Check pinned files
1079 local HASH="${HASHLIST[$i-1]}"
1080 local KEEP=""
1081 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001082 if [ -f "${VENDOR_REPO_FILE}" ]; then
1083 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001084 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001085 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001086 fi
1087 if [ -f "$PINNED" ]; then
1088 if [ "$(uname)" == "Darwin" ]; then
1089 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
1090 else
1091 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
1092 fi
1093 if [ "$TMP_HASH" = "$HASH" ]; then
1094 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001095 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1096 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001097 fi
1098 fi
1099 fi
1100 fi
1101
1102 if [ "$KEEP" = "1" ]; then
1103 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001104 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001105 FOUND=false
1106 # Try Lineage target first.
1107 # Also try to search for files stripped of
1108 # the "/system" prefix, if we're actually extracting
1109 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001110 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001111 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1112 FOUND=true
1113 break
1114 }
1115 done
1116
1117 if [ "${FOUND}" = false ]; then
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001118 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Oltean11329372018-10-18 00:44:02 +03001119 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001120 fi
1121 fi
1122
1123 if [ "$?" == "0" ]; then
1124 # Deodex apk|jar if that's the case
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001125 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001126 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Steve Kondik5bd66602016-07-15 10:39:58 -07001127 if [ -f "$TMPDIR/classes.dex" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001128 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes.dex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001129 rm "$TMPDIR/classes.dex"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001130 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001131 fi
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001132 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1133 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001134 fi
1135 fi
1136
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001137 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001138 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001139 local TYPE="${DIR##*/}"
1140 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001141 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001142 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001143 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001144 fi
1145 fi
1146
1147 done
1148
1149 # Don't allow failing
1150 set -e
1151}
1152
1153#
1154# extract_firmware:
1155#
1156# $1: file containing the list of items to extract
1157# $2: path to extracted radio folder
1158#
1159function extract_firmware() {
1160 if [ -z "$OUTDIR" ]; then
1161 echo "Output dir not set!"
1162 exit 1
1163 fi
1164
1165 parse_file_list "$1"
1166
1167 # Don't allow failing
1168 set -e
1169
1170 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1171 local COUNT=${#FILELIST[@]}
1172 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001173 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001174
1175 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1176 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1177 rm -rf "${OUTPUT_DIR:?}/"*
1178 VENDOR_RADIO_STATE=1
1179 fi
1180
1181 echo "Extracting $COUNT files in $1 from $SRC:"
1182
1183 for (( i=1; i<COUNT+1; i++ )); do
1184 local FILE="${FILELIST[$i-1]}"
1185 printf ' - %s \n' "/radio/$FILE"
1186
1187 if [ ! -d "$OUTPUT_DIR" ]; then
1188 mkdir -p "$OUTPUT_DIR"
1189 fi
1190 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1191 chmod 644 "$OUTPUT_DIR/$FILE"
1192 done
1193}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001194
1195function extract_img_data() {
1196 local image_file="$1"
1197 local out_dir="$2"
1198 local logFile="$TMPDIR/debugfs.log"
1199
1200 if [ ! -d "$out_dir" ]; then
1201 mkdir -p "$out_dir"
1202 fi
1203
1204 if [[ "$HOST_OS" == "Darwin" ]]; then
1205 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1206 echo "[-] Failed to extract data from '$image_file'"
1207 abort 1
1208 }
1209 else
1210 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1211 do
1212 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1213 echo "[-] Failed to extract data from '$image_file'"
1214 abort 1
1215 }
1216 done
1217 fi
1218
1219 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1220 if grep -Fq "$symlink_err" "$logFile"; then
1221 echo "[-] Symlinks have not been properly processed from $image_file"
1222 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1223 abort 1
1224 fi
1225}
1226
1227declare -ra VENDOR_SKIP_FILES=(
1228 "bin/toybox_vendor"
1229 "bin/toolbox"
1230 "bin/grep"
1231 "build.prop"
1232 "compatibility_matrix.xml"
1233 "default.prop"
1234 "etc/NOTICE.xml.gz"
1235 "etc/vintf/compatibility_matrix.xml"
1236 "etc/vintf/manifest.xml"
1237 "etc/wifi/wpa_supplicant.conf"
1238 "manifest.xml"
1239 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1240 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1241 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1242 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1243 "overlay/framework-res__auto_generated_rro.apk"
1244 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1245)
1246
1247function array_contains() {
1248 local element
1249 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1250 return 1
1251}
1252
1253function generate_prop_list_from_image() {
1254 local image_file="$1"
1255 local image_dir="$TMPDIR/image-temp"
1256 local output_list="$2"
1257 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1258 local -n skipped_vendor_files="$3"
1259
1260 extract_img_data "$image_file" "$image_dir"
1261
1262 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1263 do
1264 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1265 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1266 continue
1267 fi
1268 # Skip device defined skipped files since they will be re-generated at build time
1269 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1270 continue
1271 fi
1272 if suffix_match_file ".apk" "$FILE" ; then
1273 echo "-vendor/$FILE" >> "$output_list_tmp"
1274 else
1275 echo "vendor/$FILE" >> "$output_list_tmp"
1276 fi
1277 done
1278
1279 # Sort merged file with all lists
1280 sort -u "$output_list_tmp" > "$output_list"
1281
1282 # Clean-up
1283 rm -f "$output_list_tmp"
1284}