blob: 2e93ca7f1b8f4b7174ae5d63ec7c0e8a2922a31c [file] [log] [blame]
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
19PRODUCT_PACKAGES_LIST=()
20PACKAGE_LIST=()
21VENDOR_STATE=-1
Louis Popia516c2f2016-07-25 15:51:13 +020022VENDOR_RADIO_STATE=-1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070023COMMON=-1
Luca Stefani7f9fff22016-07-18 13:47:55 +020024ARCHES=
25FULLY_DEODEXED=-1
26
27TMPDIR="/tmp/extractfiles.$$"
28mkdir "$TMPDIR"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070029
30#
31# setup_vendor
32#
33# $1: device name
34# $2: vendor name
35# $3: CM root directory
36# $4: is common device - optional, default to false
37# $5: cleanup - optional, default to true
38#
39# Must be called before any other functions can be used. This
40# sets up the internal state for a new vendor configuration.
41#
42function setup_vendor() {
43 local DEVICE="$1"
44 if [ -z "$DEVICE" ]; then
45 echo "\$DEVICE must be set before including this script!"
46 exit 1
47 fi
48
49 export VENDOR="$2"
50 if [ -z "$VENDOR" ]; then
51 echo "\$VENDOR must be set before including this script!"
52 exit 1
53 fi
54
55 export CM_ROOT="$3"
56 if [ ! -d "$CM_ROOT" ]; then
57 echo "\$CM_ROOT must be set and valid before including this script!"
58 exit 1
59 fi
60
61 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
62 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
63 mkdir -p "$CM_ROOT/$OUTDIR"
64 fi
65
66 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/"$DEVICE"-vendor.mk
67 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
68 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
69
70 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
71 COMMON=1
72 else
73 COMMON=0
74 fi
75
76 if [ "$5" == "true" ] || [ "$5" == "1" ]; then
77 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +020078 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070079 else
80 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +020081 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -070082 fi
83}
84
85#
86# target_file:
87#
88# $1: colon delimited list
89#
90# Returns destination filename without args
91#
92function target_file() {
93 local LINE="$1"
94 local SPLIT=(${LINE//:/ })
95 local COUNT=${#SPLIT[@]}
96 if [ "$COUNT" -gt "1" ]; then
97 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
98 printf '%s\n' "${SPLIT[1]}"
99 return 0
100 fi
101 fi
102 printf '%s\n' "${SPLIT[0]}"
103}
104
105#
106# target_args:
107#
108# $1: colon delimited list
109#
110# Returns optional arguments (last value) for given target
111#
112function target_args() {
113 local LINE="$1"
114 local SPLIT=(${LINE//:/ })
115 local COUNT=${#SPLIT[@]}
116 if [ "$COUNT" -gt "1" ]; then
117 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
118 printf '%s\n' "${SPLIT[$COUNT-1]}"
119 fi
120 fi
121}
122
123#
124# prefix_match:
125#
126# $1: the prefix to match on
127#
128# Internal function which loops thru the packages list and returns a new
129# list containing the matched files with the prefix stripped away.
130#
131function prefix_match() {
132 local PREFIX="$1"
133 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
134 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
135 printf '%s\n' "${FILE#$PREFIX}"
136 fi
137 done
138}
139
140#
141# write_product_copy_files:
142#
143# Creates the PRODUCT_COPY_FILES section in the product makefile for all
144# items in the list which do not start with a dash (-).
145#
146function write_product_copy_files() {
147 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
148 local TARGET=
149 local FILE=
150 local LINEEND=
151
152 if [ "$COUNT" -eq "0" ]; then
153 return 0
154 fi
155
156 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
157 for (( i=1; i<COUNT+1; i++ )); do
158 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
159 LINEEND=" \\"
160 if [ "$i" -eq "$COUNT" ]; then
161 LINEEND=""
162 fi
163
164 TARGET=$(target_file "$FILE")
165 printf ' %s/proprietary/%s:system/%s%s\n' \
166 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
167 done
168 return 0
169}
170
171#
172# write_packages:
173#
174# $1: The LOCAL_MODULE_CLASS for the given module list
175# $2: "true" if this package is part of the vendor/ path
Steve Kondika991cf12016-07-28 12:13:12 -0700176# $3: type-specific extra flags
177# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700178#
179# Internal function which writes out the BUILD_PREBUILT stanzas
180# for all modules in the list. This is called by write_product_packages
181# after the modules are categorized.
182#
183function write_packages() {
184
185 local CLASS="$1"
186 local VENDOR_PKG="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700187 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700188
189 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700190 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700191 local FILELIST=("${!ARR_NAME}")
192
193 local FILE=
194 local ARGS=
195 local BASENAME=
196 local EXTENSION=
197 local PKGNAME=
198 local SRC=
199
200 for P in "${FILELIST[@]}"; do
201 FILE=$(target_file "$P")
202 ARGS=$(target_args "$P")
203
204 BASENAME=$(basename "$FILE")
205 EXTENSION=${BASENAME##*.}
206 PKGNAME=${BASENAME%.*}
207
208 # Add to final package list
209 PACKAGE_LIST+=("$PKGNAME")
210
211 SRC="proprietary"
212 if [ "$VENDOR_PKG" = "true" ]; then
213 SRC+="/vendor"
214 fi
215
216 printf 'include $(CLEAR_VARS)\n'
217 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
218 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
219 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700220 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700221 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
222 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
223 #if [ "$VENDOR_PKG" = "true" ]; then
224 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
225 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
226 #else
227 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
228 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
229 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700230 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700231 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
232 else
233 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
234 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700235 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700236 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700237 fi
238 elif [ "$CLASS" = "APPS" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700239 if [ "$EXTRA" = "priv-app" ]; then
240 SRC="$SRC/priv-app"
241 else
242 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700243 fi
244 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
245 local CERT=platform
246 if [ ! -z "$ARGS" ]; then
247 CERT="$ARGS"
248 fi
249 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
250 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
251 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
252 elif [ "$CLASS" = "ETC" ]; then
253 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
254 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700255 if [ "$ARGS" = "rootfs" ]; then
256 SRC="$SRC/rootfs"
257 if [ "$EXTRA" = "sbin" ]; then
258 SRC="$SRC/sbin"
259 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
260 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
261 fi
262 else
263 SRC="$SRC/bin"
264 fi
265 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
266 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700267 else
Steve Kondika991cf12016-07-28 12:13:12 -0700268 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700269 fi
270 printf 'LOCAL_MODULE_TAGS := optional\n'
271 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Steve Kondika991cf12016-07-28 12:13:12 -0700272 if [ ! -z "$EXTENSION" ]; then
273 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
274 fi
275 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700276 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
277 fi
278 if [ "$VENDOR_PKG" = "true" ]; then
279 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
280 fi
281 printf 'include $(BUILD_PREBUILT)\n\n'
282 done
283}
284
285#
286# write_product_packages:
287#
288# This function will create BUILD_PREBUILT entries in the
289# Android.mk and associated PRODUCT_PACKAGES list in the
290# product makefile for all files in the blob list which
291# start with a single dash (-) character.
292#
293function write_product_packages() {
294 PACKAGE_LIST=()
295
296 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
297
298 if [ "$COUNT" = "0" ]; then
299 return 0
300 fi
301
302 # Figure out what's 32-bit, what's 64-bit, and what's multilib
303 # I really should not be doing this in bash due to shitty array passing :(
304 local T_LIB32=( $(prefix_match "lib/") )
305 local T_LIB64=( $(prefix_match "lib64/") )
306 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
307 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700308 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700309
Steve Kondik03ce4002016-07-29 00:00:16 -0700310 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
311 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
312 fi
313 if [ "${#LIB32[@]}" -gt "0" ]; then
314 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
315 fi
316 if [ "${#LIB64[@]}" -gt "0" ]; then
317 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
318 fi
319
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700320 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
321 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
322 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
323 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700324 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700325
326 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700327 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700328 fi
329 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700330 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700331 fi
332 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700333 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700334 fi
335
336 # Apps
337 local APPS=( $(prefix_match "app/") )
338 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700339 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700340 fi
341 local PRIV_APPS=( $(prefix_match "priv-app/") )
342 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700343 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700344 fi
345 local V_APPS=( $(prefix_match "vendor/app/") )
346 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700347 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700348 fi
349 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
350 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700351 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700352 fi
353
354 # Framework
355 local FRAMEWORK=( $(prefix_match "framework/") )
356 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700357 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700358 fi
359
360 # Etc
361 local ETC=( $(prefix_match "etc/") )
362 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700363 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700364 fi
365 local V_ETC=( $(prefix_match "vendor/etc/") )
366 if [ "${#V_ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700367 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700368 fi
369
370 # Executables
371 local BIN=( $(prefix_match "bin/") )
372 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700373 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700374 fi
375 local V_BIN=( $(prefix_match "vendor/bin/") )
376 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700377 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700378 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700379 local SBIN=( $(prefix_match "sbin/") )
380 if [ "${#SBIN[@]}" -gt "0" ]; then
381 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
382 fi
383
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700384
385 # Actually write out the final PRODUCT_PACKAGES list
386 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
387
388 if [ "$PACKAGE_COUNT" -eq "0" ]; then
389 return 0
390 fi
391
392 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
393 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
394 local LINEEND=" \\"
395 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
396 LINEEND=""
397 fi
398 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
399 done
400}
401
402#
403# write_header:
404#
405# $1: file which will be written to
406#
407# writes out the copyright header with the current year.
408# note that this is not an append operation, and should
409# be executed first!
410#
411function write_header() {
412 YEAR=$(date +"%Y")
413
414 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
415
416 cat << EOF > $1
417# Copyright (C) $YEAR The CyanogenMod Project
418#
419# Licensed under the Apache License, Version 2.0 (the "License");
420# you may not use this file except in compliance with the License.
421# You may obtain a copy of the License at
422#
423# http://www.apache.org/licenses/LICENSE-2.0
424#
425# Unless required by applicable law or agreed to in writing, software
426# distributed under the License is distributed on an "AS IS" BASIS,
427# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
428# See the License for the specific language governing permissions and
429# limitations under the License.
430
431# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
432
433EOF
434}
435
436#
437# write_headers:
438#
439# $1: devices falling under common to be added to guard - optional
440#
441# Calls write_header for each of the makefiles and creates
442# the initial path declaration and device guard for the
443# Android.mk
444#
445function write_headers() {
446 write_header "$ANDROIDMK"
447 cat << EOF >> "$ANDROIDMK"
448LOCAL_PATH := \$(call my-dir)
449
450EOF
451 if [ "$COMMON" -ne 1 ]; then
452 cat << EOF >> "$ANDROIDMK"
453ifeq (\$(TARGET_DEVICE),$DEVICE)
454
455EOF
456 else
457 if [ -z "$1" ]; then
458 echo "Argument with devices to be added to guard must be set!"
459 exit 1
460 fi
461 cat << EOF >> "$ANDROIDMK"
462ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
463
464EOF
465 fi
466
467 write_header "$BOARDMK"
468 write_header "$PRODUCTMK"
469}
470
471#
472# write_footers:
473#
474# Closes the inital guard and any other finalization tasks. Must
475# be called as the final step.
476#
477function write_footers() {
478 cat << EOF >> "$ANDROIDMK"
479endif
480EOF
481}
482
483# Return success if adb is up and not in recovery
484function _adb_connected {
485 {
486 if [[ "$(adb get-state)" == device &&
487 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
488 then
489 return 0
490 fi
491 } 2>/dev/null
492
493 return 1
494};
495
496#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100497# parse_file_list:
498#
499# $1: input file
500#
501# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700502#
503function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100504 if [ -z "$1" ]; then
505 echo "An input file is expected!"
506 exit 1
507 elif [ ! -f "$1" ]; then
508 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700509 exit 1
510 fi
511
512 PRODUCT_PACKAGES_LIST=()
513 PRODUCT_COPY_FILES_LIST=()
514
515 while read -r line; do
516 if [ -z "$line" ]; then continue; fi
517
518 # if line starts with a dash, it needs to be packaged
519 if [[ "$line" =~ ^- ]]; then
520 PRODUCT_PACKAGES_LIST+=("${line#-}")
521 else
522 PRODUCT_COPY_FILES_LIST+=("$line")
523 fi
524
525 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
526}
527
528#
529# write_makefiles:
530#
531# $1: file containing the list of items to extract
532#
533# Calls write_product_copy_files and write_product_packages on
534# the given file and appends to the Android.mk as well as
535# the product makefile.
536#
537function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700538 parse_file_list "$1"
539 write_product_copy_files
540 write_product_packages
541}
542
543#
Louis Popia516c2f2016-07-25 15:51:13 +0200544# append_firmware_calls_to_makefiles:
545#
546# Appends to Android.mk the calls to all images present in radio folder
547# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
548#
549function append_firmware_calls_to_makefiles() {
550 cat << EOF >> "$ANDROIDMK"
551ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
552
553RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
554\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
555 \$(call add-radio-file,radio/\$(f)))
556\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
557
558endif
559
560EOF
561}
562
563#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200564# get_file:
565#
566# $1: input file
567# $2: target file/folder
568# $3: source of the file (can be "adb" or a local folder)
569#
570# Silently extracts the input file to defined target
571# Returns success if file can be pulled from the device or found locally
572#
573function get_file() {
574 local SRC="$3"
575
576 if [ "$SRC" = "adb" ]; then
577 # try to pull
578 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
579
580 return 1
581 else
582 # try to copy
583 cp "$SRC/$1" "$2" 2>/dev/null && return 0
584
585 return 1
586 fi
587};
588
589#
590# oat2dex:
591#
592# $1: extracted apk|jar (to check if deodex is required)
593# $2: odexed apk|jar to deodex
594# $3: source of the odexed apk|jar
595#
596# Convert apk|jar .odex in the corresposing classes.dex
597#
598function oat2dex() {
599 local CM_TARGET="$1"
600 local OEM_TARGET="$2"
601 local SRC="$3"
602 local TARGET=
603 local OAT=
604
605 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
606 export BAKSMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/baksmali.jar
607 export SMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/smali.jar
608 fi
609
610 # Extract existing boot.oats to the temp folder
611 if [ -z "$ARCHES" ]; then
612 echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
613 for ARCH in "arm64" "arm" "x86_64" "x86"; do
614 if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
615 ARCHES+="$ARCH "
616 fi
617 done
618 fi
619
620 if [ -z "$ARCHES" ]; then
621 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
622 fi
623
624 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
625 return 0 # target apk|jar is already odexed, return
626 fi
627
628 for ARCH in $ARCHES; do
629 BOOTOAT="$TMPDIR/boot_$ARCH.oat"
630
631 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
632
633 if get_file "$OAT" "$TMPDIR" "$SRC"; then
634 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
635 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
636 # try to extract classes.dex from boot.oat for framework jars
637 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
638 else
639 continue
640 fi
641
642 java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
643 done
644
645 rm -rf "$TMPDIR/dexout"
646}
647
648#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700649# init_adb_connection:
650#
651# Starts adb server and waits for the device
652#
653function init_adb_connection() {
654 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
655 if ! _adb_connected; then
656 echo "No device is online. Waiting for one..."
657 echo "Please connect USB and/or enable USB debugging"
658 until _adb_connected; do
659 sleep 1
660 done
661 echo "Device Found."
662 fi
663
664 # Retrieve IP and PORT info if we're using a TCP connection
665 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
666 | head -1 | awk '{print $1}')
667 adb root &> /dev/null
668 sleep 0.3
669 if [ -n "$TCPIPPORT" ]; then
670 # adb root just killed our connection
671 # so reconnect...
672 adb connect "$TCPIPPORT"
673 fi
674 adb wait-for-device &> /dev/null
675 sleep 0.3
676}
677
678#
Luca Stefani3a030122016-07-30 12:08:25 +0200679# fix_xml:
680#
681# $1: xml file to fix
682#
683function fix_xml() {
684 local XML="$1"
685 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
686
687 grep '^<?xml version' "$XML" > "$TEMP_XML"
688 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
689
690 mv "$TEMP_XML" "$XML"
691}
692
693#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700694# extract:
695#
696# $1: file containing the list of items to extract
697# $2: path to extracted system folder, or "adb" to extract from device
698#
699function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700700 if [ -z "$OUTDIR" ]; then
701 echo "Output dir not set!"
702 exit 1
703 fi
704
705 parse_file_list "$1"
706
707 # Allow failing, so we can try $DEST and/or $FILE
708 set +e
709
710 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
711 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700712 local SRC="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700713 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700714 if [ "$SRC" = "adb" ]; then
715 init_adb_connection
716 fi
717
718 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700719 echo "Cleaning output directory ($OUTPUT_ROOT).."
720 rm -rf "${OUTPUT_ROOT:?}/"*
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700721 VENDOR_STATE=1
722 fi
723
724 echo "Extracting $COUNT files in $1 from $SRC:"
725
726 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700727
728 local FROM=$(target_file "${FILELIST[$i-1]}")
729 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700730 local SPLIT=(${FILELIST[$i-1]//:/ })
731 local FILE="${SPLIT[0]#-}"
Steve Kondika991cf12016-07-28 12:13:12 -0700732 local OUTPUT_DIR="$OUTPUT_ROOT"
733 local TARGET=
734
735 if [ "$ARGS" = "rootfs" ]; then
736 TARGET="$FROM"
737 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700738 else
Steve Kondika991cf12016-07-28 12:13:12 -0700739 TARGET="system/$FROM"
740 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700741 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700742
743 if [ "$SRC" = "adb" ]; then
744 printf ' - %s .. ' "/$TARGET"
745 else
746 printf ' - %s \n' "/$TARGET"
747 fi
748
749 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700750 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
751 mkdir -p "$OUTPUT_DIR/$DIR"
752 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700753 local DEST="$OUTPUT_DIR/$FROM"
754
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700755 if [ "$SRC" = "adb" ]; then
756 # Try CM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700757 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700758 # if file does not exist try OEM target
759 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700760 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700761 fi
762 else
763 # Try OEM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700764 cp "$SRC/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700765 # if file does not exist try CM target
766 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700767 cp "$SRC/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700768 fi
769 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700770
Luca Stefani7f9fff22016-07-18 13:47:55 +0200771 if [ "$?" == "0" ]; then
772 # Deodex apk|jar if that's the case
773 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
774 oat2dex "$DEST" "$FILE" "$SRC"
775 if [ -f "$TMPDIR/classes.dex" ]; then
776 zip -gjq "$DEST" "$TMPDIR/classes.dex"
777 rm "$TMPDIR/classes.dex"
778 printf ' (updated %s from odex files)\n' "/$FILE"
779 fi
Luca Stefani3a030122016-07-30 12:08:25 +0200780 elif [[ "$DEST" =~ .xml$ ]]; then
781 fix_xml "$DEST"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200782 fi
783 fi
784
Steve Kondika991cf12016-07-28 12:13:12 -0700785 local TYPE="${DIR##*/}"
786 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
787 chmod 755 "$DEST"
788 else
789 chmod 644 "$DEST"
790 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700791 done
792
793 # Don't allow failing
794 set -e
795}
Louis Popia516c2f2016-07-25 15:51:13 +0200796
797#
798# extract_firmware:
799#
800# $1: file containing the list of items to extract
801# $2: path to extracted radio folder
802#
803function extract_firmware() {
804 if [ -z "$OUTDIR" ]; then
805 echo "Output dir not set!"
806 exit 1
807 fi
808
809 parse_file_list "$1"
810
811 # Don't allow failing
812 set -e
813
814 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
815 local COUNT=${#FILELIST[@]}
816 local SRC="$2"
817 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
818
819 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
820 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
821 rm -rf "${OUTPUT_DIR:?}/"*
822 VENDOR_RADIO_STATE=1
823 fi
824
825 echo "Extracting $COUNT files in $1 from $SRC:"
826
827 for (( i=1; i<COUNT+1; i++ )); do
828 local FILE="${FILELIST[$i-1]}"
829 printf ' - %s \n' "/radio/$FILE"
830
831 if [ ! -d "$OUTPUT_DIR" ]; then
832 mkdir -p "$OUTPUT_DIR"
833 fi
834 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
835 chmod 644 "$OUTPUT_DIR/$FILE"
836 done
837}