blob: 3e29f2aefd21171e65a854c36b0c6633ffdb2295 [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
684 cat $1 | sed -n '/# '"$2"'/I,/^\s*$/p' > $LIST
685 else
686 LIST=$1
687 fi
688
689
Steve Kondik5bd66602016-07-15 10:39:58 -0700690 PRODUCT_PACKAGES_LIST=()
691 PRODUCT_PACKAGES_HASHES=()
692 PRODUCT_COPY_FILES_LIST=()
693 PRODUCT_COPY_FILES_HASHES=()
694
695 while read -r line; do
696 if [ -z "$line" ]; then continue; fi
697
698 # If the line has a pipe delimiter, a sha1 hash should follow.
699 # This indicates the file should be pinned and not overwritten
700 # when extracting files.
701 local SPLIT=(${line//\|/ })
702 local COUNT=${#SPLIT[@]}
703 local SPEC=${SPLIT[0]}
704 local HASH="x"
705 if [ "$COUNT" -gt "1" ]; then
706 HASH=${SPLIT[1]}
707 fi
708
709 # if line starts with a dash, it needs to be packaged
710 if [[ "$SPEC" =~ ^- ]]; then
711 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
712 PRODUCT_PACKAGES_HASHES+=("$HASH")
713 else
714 PRODUCT_COPY_FILES_LIST+=("$SPEC")
715 PRODUCT_COPY_FILES_HASHES+=("$HASH")
716 fi
717
Rashed Abdel-Tawabb0d08e82017-04-04 02:48:18 -0400718 done < <(egrep -v '(^#|^[[:space:]]*$)' "$LIST" | LC_ALL=C sort | uniq)
Steve Kondik5bd66602016-07-15 10:39:58 -0700719}
720
721#
722# write_makefiles:
723#
724# $1: file containing the list of items to extract
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400725# $2: make treble compatible makefile - optional
Steve Kondik5bd66602016-07-15 10:39:58 -0700726#
727# Calls write_product_copy_files and write_product_packages on
728# the given file and appends to the Android.mk as well as
729# the product makefile.
730#
731function write_makefiles() {
732 parse_file_list "$1"
Rashed Abdel-Tawab7fd3ccb2017-10-07 14:18:39 -0400733 write_product_copy_files "$2"
Steve Kondik5bd66602016-07-15 10:39:58 -0700734 write_product_packages
735}
736
737#
738# append_firmware_calls_to_makefiles:
739#
740# Appends to Android.mk the calls to all images present in radio folder
741# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
742#
743function append_firmware_calls_to_makefiles() {
744 cat << EOF >> "$ANDROIDMK"
745ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
746
747RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
748\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
749 \$(call add-radio-file,radio/\$(f)))
750\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
751
752endif
753
754EOF
755}
756
757#
758# get_file:
759#
760# $1: input file
761# $2: target file/folder
762# $3: source of the file (can be "adb" or a local folder)
763#
764# Silently extracts the input file to defined target
765# Returns success if file can be pulled from the device or found locally
766#
767function get_file() {
768 local SRC="$3"
769
770 if [ "$SRC" = "adb" ]; then
771 # try to pull
772 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
773
774 return 1
775 else
776 # try to copy
Vladimir Olteanfe49eae2018-06-25 00:05:56 +0300777 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
778 cp -r "$SRC/${1#/system}" "$2" 2>/dev/null && return 0
Vladimir Oltean6780da32019-01-06 19:38:31 +0200779 cp -r "$SRC/system/$1" "$2" 2>/dev/null && return 0
Steve Kondik5bd66602016-07-15 10:39:58 -0700780
781 return 1
782 fi
783};
784
785#
786# oat2dex:
787#
788# $1: extracted apk|jar (to check if deodex is required)
789# $2: odexed apk|jar to deodex
790# $3: source of the odexed apk|jar
791#
792# Convert apk|jar .odex in the corresposing classes.dex
793#
794function oat2dex() {
theimpulson9a911af2019-08-14 03:25:12 +0000795 local OMNI_TARGET="$1"
Steve Kondik5bd66602016-07-15 10:39:58 -0700796 local OEM_TARGET="$2"
797 local SRC="$3"
798 local TARGET=
Joe Maplesfb3941c2018-01-05 14:51:33 -0500799 local OAT=
800 local HOST="$(uname)"
Steve Kondik5bd66602016-07-15 10:39:58 -0700801
Joe Maplesfb3941c2018-01-05 14:51:33 -0500802 if [ -z "$ANDROID_HOST_OUT" ]; then
803 echo "ERROR: ANDROID_HOST_OUT not found!"
804 echo "ERROR: Please lunch a device before running this script."
805 exit 1
Steve Kondik5bd66602016-07-15 10:39:58 -0700806 fi
807
Joe Maplesfb3941c2018-01-05 14:51:33 -0500808 if [ -z "$OATDUMP" ] || [ -z "$VDEXEXTRACTOR" ]; then
809 if [ ! -f "$ANDROID_HOST_OUT/bin/oatdump" ]; then
810 echo "ERROR: oatdump utility not found!"
811 echo "ERROR: Please run 'make oatdump'"
812 echo "ERROR: from the top of the android tree before running this script."
813 exit 1
814 else
815 export OATDUMP="$ANDROID_HOST_OUT/bin/oatdump"
816 fi
theimpulson9a911af2019-08-14 03:25:12 +0000817 export VDEXEXTRACTOR="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/vdexExtractor
Joe Maplesfb3941c2018-01-05 14:51:33 -0500818 fi
819
codeworkx85eda752018-09-23 12:36:57 +0200820 if [ -z "$CDEXCONVERTER" ]; then
theimpulson9a911af2019-08-14 03:25:12 +0000821 export CDEXCONVERTER="$OMNI_ROOT"/vendor/omni/build/tools/"$HOST"/compact_dex_converter
codeworkx85eda752018-09-23 12:36:57 +0200822 fi
823
Steve Kondik5bd66602016-07-15 10:39:58 -0700824 # Extract existing boot.oats to the temp folder
825 if [ -z "$ARCHES" ]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500826 echo "Checking if system is odexed and locating boot.oats..."
Steve Kondik5bd66602016-07-15 10:39:58 -0700827 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Jake Whatley9843b322017-01-25 21:49:16 -0500828 mkdir -p "$TMPDIR/system/framework/$ARCH"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +0300829 if get_file "/system/framework/$ARCH" "$TMPDIR/system/framework/" "$SRC"; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700830 ARCHES+="$ARCH "
Jake Whatley9843b322017-01-25 21:49:16 -0500831 else
832 rmdir "$TMPDIR/system/framework/$ARCH"
Steve Kondik5bd66602016-07-15 10:39:58 -0700833 fi
834 done
835 fi
836
837 if [ -z "$ARCHES" ]; then
838 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
839 fi
840
theimpulson9a911af2019-08-14 03:25:12 +0000841 if [ ! -f "$OMNI_TARGET" ]; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700842 return;
843 fi
844
theimpulson9a911af2019-08-14 03:25:12 +0000845 if grep "classes.dex" "$OMNI_TARGET" >/dev/null; then
Steve Kondik5bd66602016-07-15 10:39:58 -0700846 return 0 # target apk|jar is already odexed, return
847 fi
848
849 for ARCH in $ARCHES; do
Jake Whatley9843b322017-01-25 21:49:16 -0500850 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Steve Kondik5bd66602016-07-15 10:39:58 -0700851
Joe Maplesfb3941c2018-01-05 14:51:33 -0500852 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
853 local VDEX="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").vdex"
Steve Kondik5bd66602016-07-15 10:39:58 -0700854
Joe Maplesfb3941c2018-01-05 14:51:33 -0500855
856 if get_file "$OAT" "$TMPDIR" "$SRC"; then
857 if get_file "$VDEX" "$TMPDIR" "$SRC"; then
858 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$VDEX")" > /dev/null
codeworkx85eda752018-09-23 12:36:57 +0200859 # Check if we have to deal with CompactDex
860 if [ -f "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" ]; then
861 "$CDEXCONVERTER" "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex" &> /dev/null
862 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
863 else
TheStrix6bd412c2018-10-03 19:06:49 +0530864 mv "$TMPDIR/$(basename "${OEM_TARGET%.*}")_classes.dex" "$TMPDIR/classes.dex"
codeworkx85eda752018-09-23 12:36:57 +0200865 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500866 else
867 "$OATDUMP" --oat-file="$TMPDIR/$(basename "$OAT")" --export-dex-to="$TMPDIR" > /dev/null
868 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
869 fi
theimpulson9a911af2019-08-14 03:25:12 +0000870 elif [[ "$OMNI_TARGET" =~ .jar$ ]]; then
Jake Whatley9843b322017-01-25 21:49:16 -0500871 JAROAT="$TMPDIR/system/framework/$ARCH/boot-$(basename ${OEM_TARGET%.*}).oat"
Luca Stefani082f1e82018-10-07 12:44:53 +0200872 JARVDEX="/system/framework/boot-$(basename ${OEM_TARGET%.*}).vdex"
Jake Whatley9843b322017-01-25 21:49:16 -0500873 if [ ! -f "$JAROAT" ]; then
Luca Stefani082f1e82018-10-07 12:44:53 +0200874 JAROAT=$BOOTOAT
Jake Whatley9843b322017-01-25 21:49:16 -0500875 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500876 # try to extract classes.dex from boot.vdex for frameworks jars
877 # fallback to boot.oat if vdex is not available
Luca Stefani082f1e82018-10-07 12:44:53 +0200878 if get_file "$JARVDEX" "$TMPDIR" "$SRC"; then
879 "$VDEXEXTRACTOR" -o "$TMPDIR/" -i "$TMPDIR/$(basename "$JARVDEX")"
880 # Check if we have to deal with CompactDex
881 if [ -f "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" ]; then
882 "$CDEXCONVERTER" "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex" &> /dev/null
883 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.cdex.new" "$TMPDIR/classes.dex"
884 else
885 mv "$TMPDIR/$(basename "${JARVDEX%.*}")_classes.dex" "$TMPDIR/classes.dex"
886 fi
Joe Maplesfb3941c2018-01-05 14:51:33 -0500887 else
888 "$OATDUMP" --oat-file="$JAROAT" --export-dex-to="$TMPDIR" > /dev/null
889 mv "$(find "$TMPDIR" -maxdepth 1 -type f -name "*_export.dex" | wc -l | tr -d ' ')" "$TMPDIR/classes.dex"
890 fi
Steve Kondik5bd66602016-07-15 10:39:58 -0700891 else
892 continue
893 fi
894
Steve Kondik5bd66602016-07-15 10:39:58 -0700895 done
Steve Kondik5bd66602016-07-15 10:39:58 -0700896}
897
898#
899# init_adb_connection:
900#
901# Starts adb server and waits for the device
902#
903function init_adb_connection() {
904 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
905 if ! _adb_connected; then
906 echo "No device is online. Waiting for one..."
907 echo "Please connect USB and/or enable USB debugging"
908 until _adb_connected; do
909 sleep 1
910 done
911 echo "Device Found."
912 fi
913
914 # Retrieve IP and PORT info if we're using a TCP connection
915 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
916 | head -1 | awk '{print $1}')
917 adb root &> /dev/null
918 sleep 0.3
919 if [ -n "$TCPIPPORT" ]; then
920 # adb root just killed our connection
921 # so reconnect...
922 adb connect "$TCPIPPORT"
923 fi
924 adb wait-for-device &> /dev/null
925 sleep 0.3
926}
927
928#
929# fix_xml:
930#
931# $1: xml file to fix
932#
933function fix_xml() {
934 local XML="$1"
935 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
936
Dobroslaw Kijowski3af2a8d2017-05-18 12:35:02 +0200937 grep -a '^<?xml version' "$XML" > "$TEMP_XML"
938 grep -av '^<?xml version' "$XML" >> "$TEMP_XML"
Steve Kondik5bd66602016-07-15 10:39:58 -0700939
940 mv "$TEMP_XML" "$XML"
941}
942
943#
944# extract:
945#
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200946# Positional parameters:
947# $1: file containing the list of items to extract (aka proprietary-files.txt)
Dan Pasanen0cc05012017-03-21 09:06:11 -0500948# $2: path to extracted system folder, an ota zip file, or "adb" to extract from device
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200949# $3: section in list file to extract - optional. Setting section via $3 is deprecated.
950#
951# Non-positional parameters (coming after $2):
952# --section: preferred way of selecting the portion to parse and extract from
953# proprietary-files.txt
Steve Kondik5bd66602016-07-15 10:39:58 -0700954#
955function extract() {
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200956 # Consume positional parameters
957 local PROPRIETARY_FILES_TXT="$1"; shift
958 local SRC="$1"; shift
959 local SECTION=""
960
961 # Consume optional, non-positional parameters
962 while [ "$#" -gt 0 ]; do
963 case "$1" in
964 -s|--section)
965 SECTION="$2"; shift
966 ;;
967 *)
968 # Backwards-compatibility with the old behavior, where $3, if
969 # present, denoted an optional positional ${SECTION} argument.
970 # Users of ${SECTION} are encouraged to migrate from setting it as
971 # positional $3, to non-positional --section ${SECTION}, the
972 # reason being that it doesn't scale to have more than 1 optional
973 # positional argument.
974 SECTION="$1"
975 ;;
976 esac
977 shift
978 done
979
Steve Kondik5bd66602016-07-15 10:39:58 -0700980 if [ -z "$OUTDIR" ]; then
981 echo "Output dir not set!"
982 exit 1
983 fi
984
Vladimir Oltean724a7bc2019-01-17 03:04:16 +0200985 parse_file_list "${PROPRIETARY_FILES_TXT}" "${SECTION}"
Steve Kondik5bd66602016-07-15 10:39:58 -0700986
987 # Allow failing, so we can try $DEST and/or $FILE
988 set +e
989
990 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
991 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
992 local COUNT=${#FILELIST[@]}
theimpulson9a911af2019-08-14 03:25:12 +0000993 local OUTPUT_ROOT="$OMNI_ROOT"/"$OUTDIR"/proprietary
Steve Kondik5bd66602016-07-15 10:39:58 -0700994 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
995
996 if [ "$SRC" = "adb" ]; then
997 init_adb_connection
998 fi
999
Dan Pasanen0cc05012017-03-21 09:06:11 -05001000 if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
theimpulson9a911af2019-08-14 03:25:12 +00001001 DUMPDIR="$OMNI_ROOT"/system_dump
Dan Pasanen0cc05012017-03-21 09:06:11 -05001002
1003 # Check if we're working with the same zip that was passed last time.
1004 # If so, let's just use what's already extracted.
1005 MD5=`md5sum "$SRC"| awk '{print $1}'`
1006 OLDMD5=`cat "$DUMPDIR"/zipmd5.txt`
1007
1008 if [ "$MD5" != "$OLDMD5" ]; then
1009 rm -rf "$DUMPDIR"
1010 mkdir "$DUMPDIR"
1011 unzip "$SRC" -d "$DUMPDIR"
1012 echo "$MD5" > "$DUMPDIR"/zipmd5.txt
1013
1014 # Stop if an A/B OTA zip is detected. We cannot extract these.
1015 if [ -a "$DUMPDIR"/payload.bin ]; then
1016 echo "A/B style OTA zip detected. This is not supported at this time. Stopping..."
1017 exit 1
1018 # If OTA is block based, extract it.
1019 elif [ -a "$DUMPDIR"/system.new.dat ]; then
1020 echo "Converting system.new.dat to system.img"
theimpulson9a911af2019-08-14 03:25:12 +00001021 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 -05001022 rm -rf "$DUMPDIR"/system.new.dat "$DUMPDIR"/system
1023 mkdir "$DUMPDIR"/system "$DUMPDIR"/tmp
1024 echo "Requesting sudo access to mount the system.img"
1025 sudo mount -o loop "$DUMPDIR"/system.img "$DUMPDIR"/tmp
1026 cp -r "$DUMPDIR"/tmp/* "$DUMPDIR"/system/
1027 sudo umount "$DUMPDIR"/tmp
1028 rm -rf "$DUMPDIR"/tmp "$DUMPDIR"/system.img
1029 fi
1030 fi
1031
1032 SRC="$DUMPDIR"
1033 fi
1034
Steve Kondik5bd66602016-07-15 10:39:58 -07001035 if [ "$VENDOR_STATE" -eq "0" ]; then
1036 echo "Cleaning output directory ($OUTPUT_ROOT).."
1037 rm -rf "${OUTPUT_TMP:?}"
1038 mkdir -p "${OUTPUT_TMP:?}"
Jake Whatley9843b322017-01-25 21:49:16 -05001039 if [ -d "$OUTPUT_ROOT" ]; then
1040 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
1041 fi
Steve Kondik5bd66602016-07-15 10:39:58 -07001042 VENDOR_STATE=1
1043 fi
1044
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001045 echo "Extracting ${COUNT} files in ${PROPRIETARY_FILES_TXT} from ${SRC}:"
Steve Kondik5bd66602016-07-15 10:39:58 -07001046
1047 for (( i=1; i<COUNT+1; i++ )); do
1048
Vladimir Oltean8e2de652018-06-24 20:41:30 +03001049 local SPEC_SRC_FILE=$(src_file "${FILELIST[$i-1]}")
Vladimir Olteanb06f3aa2018-06-24 20:38:04 +03001050 local SPEC_DST_FILE=$(target_file "${FILELIST[$i-1]}")
Vladimir Olteand6391332018-06-24 20:42:01 +03001051 local SPEC_ARGS=$(target_args "${FILELIST[$i-1]}")
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001052 local OUTPUT_DIR=
1053 local TMP_DIR=
1054 local SRC_FILE=
1055 local DST_FILE=
Steve Kondik5bd66602016-07-15 10:39:58 -07001056
Vladimir Olteand6391332018-06-24 20:42:01 +03001057 if [ "${SPEC_ARGS}" = "rootfs" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001058 OUTPUT_DIR="${OUTPUT_ROOT}/rootfs"
1059 TMP_DIR="${OUTPUT_TMP}/rootfs"
1060 SRC_FILE="/${SPEC_SRC_FILE}"
1061 DST_FILE="/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001062 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001063 OUTPUT_DIR="${OUTPUT_ROOT}"
1064 TMP_DIR="${OUTPUT_TMP}"
1065 SRC_FILE="/system/${SPEC_SRC_FILE}"
1066 DST_FILE="/system/${SPEC_DST_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001067 fi
1068
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001069 # Strip the file path in the vendor repo of "system", if present
1070 local VENDOR_REPO_FILE="$OUTPUT_DIR/${DST_FILE#/system}"
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001071 local BLOB_DISPLAY_NAME="${DST_FILE#/system/}"
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001072 mkdir -p $(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001073
Gabriele M58270a32017-11-13 23:15:29 +01001074 # Check pinned files
1075 local HASH="${HASHLIST[$i-1]}"
1076 local KEEP=""
1077 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001078 if [ -f "${VENDOR_REPO_FILE}" ]; then
1079 local PINNED="${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001080 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001081 local PINNED="${TMP_DIR}${DST_FILE#/system}"
Gabriele M58270a32017-11-13 23:15:29 +01001082 fi
1083 if [ -f "$PINNED" ]; then
1084 if [ "$(uname)" == "Darwin" ]; then
1085 local TMP_HASH=$(shasum "$PINNED" | awk '{print $1}' )
1086 else
1087 local TMP_HASH=$(sha1sum "$PINNED" | awk '{print $1}' )
1088 fi
1089 if [ "$TMP_HASH" = "$HASH" ]; then
1090 KEEP="1"
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001091 if [ ! -f "${VENDOR_REPO_FILE}" ]; then
1092 cp -p "$PINNED" "${VENDOR_REPO_FILE}"
Gabriele M58270a32017-11-13 23:15:29 +01001093 fi
1094 fi
1095 fi
1096 fi
1097
1098 if [ "$KEEP" = "1" ]; then
1099 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
Steve Kondik5bd66602016-07-15 10:39:58 -07001100 else
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001101 FOUND=false
1102 # Try Lineage target first.
1103 # Also try to search for files stripped of
1104 # the "/system" prefix, if we're actually extracting
1105 # from a system image.
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001106 for CANDIDATE in "${DST_FILE}" "${SRC_FILE}"; do
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001107 get_file ${CANDIDATE} ${VENDOR_REPO_FILE} ${SRC} && {
1108 FOUND=true
1109 break
1110 }
1111 done
1112
1113 if [ "${FOUND}" = false ]; then
Vladimir Oltean724a7bc2019-01-17 03:04:16 +02001114 printf ' !! %s: file not found in source\n' "${BLOB_DISPLAY_NAME}"
Vladimir Oltean11329372018-10-18 00:44:02 +03001115 continue
Steve Kondik5bd66602016-07-15 10:39:58 -07001116 fi
1117 fi
1118
1119 if [ "$?" == "0" ]; then
1120 # Deodex apk|jar if that's the case
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001121 if [[ "$FULLY_DEODEXED" -ne "1" && "${VENDOR_REPO_FILE}" =~ .(apk|jar)$ ]]; then
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001122 oat2dex "${VENDOR_REPO_FILE}" "${SRC_FILE}" "$SRC"
Steve Kondik5bd66602016-07-15 10:39:58 -07001123 if [ -f "$TMPDIR/classes.dex" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001124 zip -gjq "${VENDOR_REPO_FILE}" "$TMPDIR/classes.dex"
Steve Kondik5bd66602016-07-15 10:39:58 -07001125 rm "$TMPDIR/classes.dex"
Vladimir Olteanfe49eae2018-06-25 00:05:56 +03001126 printf ' (updated %s from odex files)\n' "${SRC_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001127 fi
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001128 elif [[ "${VENDOR_REPO_FILE}" =~ .xml$ ]]; then
1129 fix_xml "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001130 fi
1131 fi
1132
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001133 if [ -f "${VENDOR_REPO_FILE}" ]; then
Vladimir Olteanb5500d72018-06-24 21:06:12 +03001134 local DIR=$(dirname "${VENDOR_REPO_FILE}")
Steve Kondik5bd66602016-07-15 10:39:58 -07001135 local TYPE="${DIR##*/}"
1136 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001137 chmod 755 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001138 else
Vladimir Oltean4daf5592018-06-24 20:46:42 +03001139 chmod 644 "${VENDOR_REPO_FILE}"
Steve Kondik5bd66602016-07-15 10:39:58 -07001140 fi
1141 fi
1142
1143 done
1144
1145 # Don't allow failing
1146 set -e
1147}
1148
1149#
1150# extract_firmware:
1151#
1152# $1: file containing the list of items to extract
1153# $2: path to extracted radio folder
1154#
1155function extract_firmware() {
1156 if [ -z "$OUTDIR" ]; then
1157 echo "Output dir not set!"
1158 exit 1
1159 fi
1160
1161 parse_file_list "$1"
1162
1163 # Don't allow failing
1164 set -e
1165
1166 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
1167 local COUNT=${#FILELIST[@]}
1168 local SRC="$2"
theimpulson9a911af2019-08-14 03:25:12 +00001169 local OUTPUT_DIR="$OMNI_ROOT"/"$OUTDIR"/radio
Steve Kondik5bd66602016-07-15 10:39:58 -07001170
1171 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
1172 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
1173 rm -rf "${OUTPUT_DIR:?}/"*
1174 VENDOR_RADIO_STATE=1
1175 fi
1176
1177 echo "Extracting $COUNT files in $1 from $SRC:"
1178
1179 for (( i=1; i<COUNT+1; i++ )); do
1180 local FILE="${FILELIST[$i-1]}"
1181 printf ' - %s \n' "/radio/$FILE"
1182
1183 if [ ! -d "$OUTPUT_DIR" ]; then
1184 mkdir -p "$OUTPUT_DIR"
1185 fi
1186 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
1187 chmod 644 "$OUTPUT_DIR/$FILE"
1188 done
1189}
Rashed Abdel-Tawab841c6e82019-03-29 20:07:25 -07001190
1191function extract_img_data() {
1192 local image_file="$1"
1193 local out_dir="$2"
1194 local logFile="$TMPDIR/debugfs.log"
1195
1196 if [ ! -d "$out_dir" ]; then
1197 mkdir -p "$out_dir"
1198 fi
1199
1200 if [[ "$HOST_OS" == "Darwin" ]]; then
1201 debugfs -R "rdump / \"$out_dir\"" "$image_file" &> "$logFile" || {
1202 echo "[-] Failed to extract data from '$image_file'"
1203 abort 1
1204 }
1205 else
1206 debugfs -R 'ls -p' "$image_file" 2>/dev/null | cut -d '/' -f6 | while read -r entry
1207 do
1208 debugfs -R "rdump \"$entry\" \"$out_dir\"" "$image_file" >> "$logFile" 2>&1 || {
1209 echo "[-] Failed to extract data from '$image_file'"
1210 abort 1
1211 }
1212 done
1213 fi
1214
1215 local symlink_err="rdump: Attempt to read block from filesystem resulted in short read while reading symlink"
1216 if grep -Fq "$symlink_err" "$logFile"; then
1217 echo "[-] Symlinks have not been properly processed from $image_file"
1218 echo "[!] If you don't have a compatible debugfs version, modify 'execute-all.sh' to disable 'USE_DEBUGFS' flag"
1219 abort 1
1220 fi
1221}
1222
1223declare -ra VENDOR_SKIP_FILES=(
1224 "bin/toybox_vendor"
1225 "bin/toolbox"
1226 "bin/grep"
1227 "build.prop"
1228 "compatibility_matrix.xml"
1229 "default.prop"
1230 "etc/NOTICE.xml.gz"
1231 "etc/vintf/compatibility_matrix.xml"
1232 "etc/vintf/manifest.xml"
1233 "etc/wifi/wpa_supplicant.conf"
1234 "manifest.xml"
1235 "overlay/DisplayCutoutEmulationCorner/DisplayCutoutEmulationCornerOverlay.apk"
1236 "overlay/DisplayCutoutEmulationDouble/DisplayCutoutEmulationDoubleOverlay.apk"
1237 "overlay/DisplayCutoutEmulationTall/DisplayCutoutEmulationTallOverlay.apk"
1238 "overlay/DisplayCutoutNoCutout/NoCutoutOverlay.apk"
1239 "overlay/framework-res__auto_generated_rro.apk"
1240 "overlay/SysuiDarkTheme/SysuiDarkThemeOverlay.apk"
1241)
1242
1243function array_contains() {
1244 local element
1245 for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done
1246 return 1
1247}
1248
1249function generate_prop_list_from_image() {
1250 local image_file="$1"
1251 local image_dir="$TMPDIR/image-temp"
1252 local output_list="$2"
1253 local output_list_tmp="$TMPDIR/_proprietary-blobs.txt"
1254 local -n skipped_vendor_files="$3"
1255
1256 extract_img_data "$image_file" "$image_dir"
1257
1258 find "$image_dir" -not -type d | sed "s#^$image_dir/##" | while read -r FILE
1259 do
1260 # Skip VENDOR_SKIP_FILES since it will be re-generated at build time
1261 if array_contains "$FILE" "${VENDOR_SKIP_FILES[@]}"; then
1262 continue
1263 fi
1264 # Skip device defined skipped files since they will be re-generated at build time
1265 if array_contains "$FILE" "${skipped_vendor_files[@]}"; then
1266 continue
1267 fi
1268 if suffix_match_file ".apk" "$FILE" ; then
1269 echo "-vendor/$FILE" >> "$output_list_tmp"
1270 else
1271 echo "vendor/$FILE" >> "$output_list_tmp"
1272 fi
1273 done
1274
1275 # Sort merged file with all lists
1276 sort -u "$output_list_tmp" > "$output_list"
1277
1278 # Clean-up
1279 rm -f "$output_list_tmp"
1280}