blob: a4197a536e18e2e4c334e7fe32172daeb6b13136 [file] [log] [blame]
Steve Kondik4e2aaab2016-07-15 10:39:58 -07001#!/bin/bash
2#
3# Copyright (C) 2016 The CyanogenMod Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -070019PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070020PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -070021PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -070022PACKAGE_LIST=()
23VENDOR_STATE=-1
Louis Popia516c2f2016-07-25 15:51:13 +020024VENDOR_RADIO_STATE=-1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070025COMMON=-1
Luca Stefani7f9fff22016-07-18 13:47:55 +020026ARCHES=
27FULLY_DEODEXED=-1
28
29TMPDIR="/tmp/extractfiles.$$"
30mkdir "$TMPDIR"
Steve Kondik4e2aaab2016-07-15 10:39:58 -070031
32#
Steve Kondik48f8df82016-08-14 03:55:08 -070033# cleanup
34#
35# kill our tmpfiles with fire on exit
36#
37function cleanup() {
38 rm -rf "${TMPDIR:?}"
39}
40
41trap cleanup EXIT INT TERM ERR
42
43#
Steve Kondik4e2aaab2016-07-15 10:39:58 -070044# setup_vendor
45#
46# $1: device name
47# $2: vendor name
48# $3: CM root directory
49# $4: is common device - optional, default to false
50# $5: cleanup - optional, default to true
51#
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
68 export CM_ROOT="$3"
69 if [ ! -d "$CM_ROOT" ]; then
70 echo "\$CM_ROOT must be set and valid before including this script!"
71 exit 1
72 fi
73
74 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
75 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
76 mkdir -p "$CM_ROOT/$OUTDIR"
77 fi
78
79 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/"$DEVICE"-vendor.mk
80 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
81 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
82
83 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
84 COMMON=1
85 else
86 COMMON=0
87 fi
88
89 if [ "$5" == "true" ] || [ "$5" == "1" ]; then
90 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +020091 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070092 else
93 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +020094 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -070095 fi
96}
97
98#
99# target_file:
100#
101# $1: colon delimited list
102#
103# Returns destination filename without args
104#
105function target_file() {
106 local LINE="$1"
107 local SPLIT=(${LINE//:/ })
108 local COUNT=${#SPLIT[@]}
109 if [ "$COUNT" -gt "1" ]; then
110 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
111 printf '%s\n' "${SPLIT[1]}"
112 return 0
113 fi
114 fi
115 printf '%s\n' "${SPLIT[0]}"
116}
117
118#
119# target_args:
120#
121# $1: colon delimited list
122#
123# Returns optional arguments (last value) for given target
124#
125function target_args() {
126 local LINE="$1"
127 local SPLIT=(${LINE//:/ })
128 local COUNT=${#SPLIT[@]}
129 if [ "$COUNT" -gt "1" ]; then
130 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
131 printf '%s\n' "${SPLIT[$COUNT-1]}"
132 fi
133 fi
134}
135
136#
137# prefix_match:
138#
139# $1: the prefix to match on
140#
141# Internal function which loops thru the packages list and returns a new
142# list containing the matched files with the prefix stripped away.
143#
144function prefix_match() {
145 local PREFIX="$1"
146 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
147 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
148 printf '%s\n' "${FILE#$PREFIX}"
149 fi
150 done
151}
152
153#
154# write_product_copy_files:
155#
156# Creates the PRODUCT_COPY_FILES section in the product makefile for all
157# items in the list which do not start with a dash (-).
158#
159function write_product_copy_files() {
160 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
161 local TARGET=
162 local FILE=
163 local LINEEND=
164
165 if [ "$COUNT" -eq "0" ]; then
166 return 0
167 fi
168
169 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
170 for (( i=1; i<COUNT+1; i++ )); do
171 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
172 LINEEND=" \\"
173 if [ "$i" -eq "$COUNT" ]; then
174 LINEEND=""
175 fi
176
177 TARGET=$(target_file "$FILE")
178 printf ' %s/proprietary/%s:system/%s%s\n' \
179 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
180 done
181 return 0
182}
183
184#
185# write_packages:
186#
187# $1: The LOCAL_MODULE_CLASS for the given module list
188# $2: "true" if this package is part of the vendor/ path
Steve Kondika991cf12016-07-28 12:13:12 -0700189# $3: type-specific extra flags
190# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700191#
192# Internal function which writes out the BUILD_PREBUILT stanzas
193# for all modules in the list. This is called by write_product_packages
194# after the modules are categorized.
195#
196function write_packages() {
197
198 local CLASS="$1"
199 local VENDOR_PKG="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700200 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700201
202 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700203 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700204 local FILELIST=("${!ARR_NAME}")
205
206 local FILE=
207 local ARGS=
208 local BASENAME=
209 local EXTENSION=
210 local PKGNAME=
211 local SRC=
212
213 for P in "${FILELIST[@]}"; do
214 FILE=$(target_file "$P")
215 ARGS=$(target_args "$P")
216
217 BASENAME=$(basename "$FILE")
218 EXTENSION=${BASENAME##*.}
219 PKGNAME=${BASENAME%.*}
220
221 # Add to final package list
222 PACKAGE_LIST+=("$PKGNAME")
223
224 SRC="proprietary"
225 if [ "$VENDOR_PKG" = "true" ]; then
226 SRC+="/vendor"
227 fi
228
229 printf 'include $(CLEAR_VARS)\n'
230 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
231 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
232 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700233 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700234 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
235 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
236 #if [ "$VENDOR_PKG" = "true" ]; then
237 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
238 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
239 #else
240 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
241 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
242 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700243 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700244 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
245 else
246 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
247 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700248 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700249 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700250 fi
251 elif [ "$CLASS" = "APPS" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700252 if [ "$EXTRA" = "priv-app" ]; then
253 SRC="$SRC/priv-app"
254 else
255 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700256 fi
257 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
258 local CERT=platform
259 if [ ! -z "$ARGS" ]; then
260 CERT="$ARGS"
261 fi
262 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
263 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
264 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
265 elif [ "$CLASS" = "ETC" ]; then
266 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
267 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700268 if [ "$ARGS" = "rootfs" ]; then
269 SRC="$SRC/rootfs"
270 if [ "$EXTRA" = "sbin" ]; then
271 SRC="$SRC/sbin"
272 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
273 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
274 fi
275 else
276 SRC="$SRC/bin"
277 fi
278 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
279 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700280 else
Steve Kondika991cf12016-07-28 12:13:12 -0700281 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700282 fi
283 printf 'LOCAL_MODULE_TAGS := optional\n'
284 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400285 if [ "$CLASS" = "APPS" ]; then
286 printf 'LOCAL_DEX_PREOPT := false\n'
287 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700288 if [ ! -z "$EXTENSION" ]; then
289 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
290 fi
291 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700292 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
293 fi
294 if [ "$VENDOR_PKG" = "true" ]; then
295 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
296 fi
297 printf 'include $(BUILD_PREBUILT)\n\n'
298 done
299}
300
301#
302# write_product_packages:
303#
304# This function will create BUILD_PREBUILT entries in the
305# Android.mk and associated PRODUCT_PACKAGES list in the
306# product makefile for all files in the blob list which
307# start with a single dash (-) character.
308#
309function write_product_packages() {
310 PACKAGE_LIST=()
311
312 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
313
314 if [ "$COUNT" = "0" ]; then
315 return 0
316 fi
317
318 # Figure out what's 32-bit, what's 64-bit, and what's multilib
319 # I really should not be doing this in bash due to shitty array passing :(
320 local T_LIB32=( $(prefix_match "lib/") )
321 local T_LIB64=( $(prefix_match "lib64/") )
322 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
323 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700324 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700325
Steve Kondik03ce4002016-07-29 00:00:16 -0700326 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
327 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
328 fi
329 if [ "${#LIB32[@]}" -gt "0" ]; then
330 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
331 fi
332 if [ "${#LIB64[@]}" -gt "0" ]; then
333 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
334 fi
335
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700336 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
337 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
338 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
339 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700340 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700341
342 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700343 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700344 fi
345 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700346 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700347 fi
348 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700349 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700350 fi
351
352 # Apps
353 local APPS=( $(prefix_match "app/") )
354 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700355 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700356 fi
357 local PRIV_APPS=( $(prefix_match "priv-app/") )
358 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700359 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700360 fi
361 local V_APPS=( $(prefix_match "vendor/app/") )
362 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700363 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700364 fi
365 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
366 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700367 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700368 fi
369
370 # Framework
371 local FRAMEWORK=( $(prefix_match "framework/") )
372 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700373 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700374 fi
375
376 # Etc
377 local ETC=( $(prefix_match "etc/") )
378 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700379 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700380 fi
381 local V_ETC=( $(prefix_match "vendor/etc/") )
382 if [ "${#V_ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700383 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700384 fi
385
386 # Executables
387 local BIN=( $(prefix_match "bin/") )
388 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700389 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700390 fi
391 local V_BIN=( $(prefix_match "vendor/bin/") )
392 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700393 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700394 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700395 local SBIN=( $(prefix_match "sbin/") )
396 if [ "${#SBIN[@]}" -gt "0" ]; then
397 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
398 fi
399
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700400
401 # Actually write out the final PRODUCT_PACKAGES list
402 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
403
404 if [ "$PACKAGE_COUNT" -eq "0" ]; then
405 return 0
406 fi
407
408 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
409 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
410 local LINEEND=" \\"
411 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
412 LINEEND=""
413 fi
414 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
415 done
416}
417
418#
419# write_header:
420#
421# $1: file which will be written to
422#
423# writes out the copyright header with the current year.
424# note that this is not an append operation, and should
425# be executed first!
426#
427function write_header() {
428 YEAR=$(date +"%Y")
429
430 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
431
432 cat << EOF > $1
433# Copyright (C) $YEAR The CyanogenMod Project
434#
435# Licensed under the Apache License, Version 2.0 (the "License");
436# you may not use this file except in compliance with the License.
437# You may obtain a copy of the License at
438#
439# http://www.apache.org/licenses/LICENSE-2.0
440#
441# Unless required by applicable law or agreed to in writing, software
442# distributed under the License is distributed on an "AS IS" BASIS,
443# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
444# See the License for the specific language governing permissions and
445# limitations under the License.
446
447# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
448
449EOF
450}
451
452#
453# write_headers:
454#
455# $1: devices falling under common to be added to guard - optional
456#
457# Calls write_header for each of the makefiles and creates
458# the initial path declaration and device guard for the
459# Android.mk
460#
461function write_headers() {
462 write_header "$ANDROIDMK"
463 cat << EOF >> "$ANDROIDMK"
464LOCAL_PATH := \$(call my-dir)
465
466EOF
467 if [ "$COMMON" -ne 1 ]; then
468 cat << EOF >> "$ANDROIDMK"
469ifeq (\$(TARGET_DEVICE),$DEVICE)
470
471EOF
472 else
473 if [ -z "$1" ]; then
474 echo "Argument with devices to be added to guard must be set!"
475 exit 1
476 fi
477 cat << EOF >> "$ANDROIDMK"
478ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
479
480EOF
481 fi
482
483 write_header "$BOARDMK"
484 write_header "$PRODUCTMK"
485}
486
487#
488# write_footers:
489#
490# Closes the inital guard and any other finalization tasks. Must
491# be called as the final step.
492#
493function write_footers() {
494 cat << EOF >> "$ANDROIDMK"
495endif
496EOF
497}
498
499# Return success if adb is up and not in recovery
500function _adb_connected {
501 {
Steve Kondik7561d192016-09-01 21:40:27 -0700502 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700503 then
504 return 0
505 fi
506 } 2>/dev/null
507
508 return 1
509};
510
511#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100512# parse_file_list:
513#
514# $1: input file
515#
516# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700517#
518function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100519 if [ -z "$1" ]; then
520 echo "An input file is expected!"
521 exit 1
522 elif [ ! -f "$1" ]; then
523 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700524 exit 1
525 fi
526
527 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700528 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700529 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700530 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700531
532 while read -r line; do
533 if [ -z "$line" ]; then continue; fi
534
Steve Kondik48f8df82016-08-14 03:55:08 -0700535 # If the line has a pipe delimiter, a sha1 hash should follow.
536 # This indicates the file should be pinned and not overwritten
537 # when extracting files.
538 local SPLIT=(${line//\|/ })
539 local COUNT=${#SPLIT[@]}
540 local SPEC=${SPLIT[0]}
541 local HASH="x"
542 if [ "$COUNT" -gt "1" ]; then
543 HASH=${SPLIT[1]}
544 fi
545
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700546 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700547 if [[ "$SPEC" =~ ^- ]]; then
548 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
549 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700550 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700551 PRODUCT_COPY_FILES_LIST+=("$SPEC")
552 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700553 fi
554
555 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
556}
557
558#
559# write_makefiles:
560#
561# $1: file containing the list of items to extract
562#
563# Calls write_product_copy_files and write_product_packages on
564# the given file and appends to the Android.mk as well as
565# the product makefile.
566#
567function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700568 parse_file_list "$1"
569 write_product_copy_files
570 write_product_packages
571}
572
573#
Louis Popia516c2f2016-07-25 15:51:13 +0200574# append_firmware_calls_to_makefiles:
575#
576# Appends to Android.mk the calls to all images present in radio folder
577# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
578#
579function append_firmware_calls_to_makefiles() {
580 cat << EOF >> "$ANDROIDMK"
581ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
582
583RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
584\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
585 \$(call add-radio-file,radio/\$(f)))
586\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
587
588endif
589
590EOF
591}
592
593#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200594# get_file:
595#
596# $1: input file
597# $2: target file/folder
598# $3: source of the file (can be "adb" or a local folder)
599#
600# Silently extracts the input file to defined target
601# Returns success if file can be pulled from the device or found locally
602#
603function get_file() {
604 local SRC="$3"
605
606 if [ "$SRC" = "adb" ]; then
607 # try to pull
608 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
609
610 return 1
611 else
612 # try to copy
613 cp "$SRC/$1" "$2" 2>/dev/null && return 0
614
615 return 1
616 fi
617};
618
619#
620# oat2dex:
621#
622# $1: extracted apk|jar (to check if deodex is required)
623# $2: odexed apk|jar to deodex
624# $3: source of the odexed apk|jar
625#
626# Convert apk|jar .odex in the corresposing classes.dex
627#
628function oat2dex() {
629 local CM_TARGET="$1"
630 local OEM_TARGET="$2"
631 local SRC="$3"
632 local TARGET=
633 local OAT=
634
635 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
636 export BAKSMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/baksmali.jar
637 export SMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/smali.jar
638 fi
639
640 # Extract existing boot.oats to the temp folder
641 if [ -z "$ARCHES" ]; then
642 echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
643 for ARCH in "arm64" "arm" "x86_64" "x86"; do
644 if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
645 ARCHES+="$ARCH "
646 fi
647 done
648 fi
649
650 if [ -z "$ARCHES" ]; then
651 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
652 fi
653
Steve Kondik48f8df82016-08-14 03:55:08 -0700654 if [ ! -f "$CM_TARGET" ]; then
655 return;
656 fi
657
Luca Stefani7f9fff22016-07-18 13:47:55 +0200658 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
659 return 0 # target apk|jar is already odexed, return
660 fi
661
662 for ARCH in $ARCHES; do
663 BOOTOAT="$TMPDIR/boot_$ARCH.oat"
664
665 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
666
667 if get_file "$OAT" "$TMPDIR" "$SRC"; then
668 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
669 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
670 # try to extract classes.dex from boot.oat for framework jars
671 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
672 else
673 continue
674 fi
675
676 java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
677 done
678
679 rm -rf "$TMPDIR/dexout"
680}
681
682#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700683# init_adb_connection:
684#
685# Starts adb server and waits for the device
686#
687function init_adb_connection() {
688 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
689 if ! _adb_connected; then
690 echo "No device is online. Waiting for one..."
691 echo "Please connect USB and/or enable USB debugging"
692 until _adb_connected; do
693 sleep 1
694 done
695 echo "Device Found."
696 fi
697
698 # Retrieve IP and PORT info if we're using a TCP connection
699 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
700 | head -1 | awk '{print $1}')
701 adb root &> /dev/null
702 sleep 0.3
703 if [ -n "$TCPIPPORT" ]; then
704 # adb root just killed our connection
705 # so reconnect...
706 adb connect "$TCPIPPORT"
707 fi
708 adb wait-for-device &> /dev/null
709 sleep 0.3
710}
711
712#
Luca Stefani3a030122016-07-30 12:08:25 +0200713# fix_xml:
714#
715# $1: xml file to fix
716#
717function fix_xml() {
718 local XML="$1"
719 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
720
721 grep '^<?xml version' "$XML" > "$TEMP_XML"
722 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
723
724 mv "$TEMP_XML" "$XML"
725}
726
727#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700728# extract:
729#
730# $1: file containing the list of items to extract
731# $2: path to extracted system folder, or "adb" to extract from device
732#
733function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700734 if [ -z "$OUTDIR" ]; then
735 echo "Output dir not set!"
736 exit 1
737 fi
738
739 parse_file_list "$1"
740
741 # Allow failing, so we can try $DEST and/or $FILE
742 set +e
743
744 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -0700745 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700746 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700747 local SRC="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700748 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -0700749 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
750
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700751 if [ "$SRC" = "adb" ]; then
752 init_adb_connection
753 fi
754
755 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700756 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -0700757 rm -rf "${OUTPUT_TMP:?}"
758 mkdir -p "${OUTPUT_TMP:?}"
759 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700760 VENDOR_STATE=1
761 fi
762
763 echo "Extracting $COUNT files in $1 from $SRC:"
764
765 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700766
767 local FROM=$(target_file "${FILELIST[$i-1]}")
768 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700769 local SPLIT=(${FILELIST[$i-1]//:/ })
770 local FILE="${SPLIT[0]#-}"
Steve Kondika991cf12016-07-28 12:13:12 -0700771 local OUTPUT_DIR="$OUTPUT_ROOT"
Steve Kondik48f8df82016-08-14 03:55:08 -0700772 local TMP_DIR="$OUTPUT_TMP"
Steve Kondika991cf12016-07-28 12:13:12 -0700773 local TARGET=
774
775 if [ "$ARGS" = "rootfs" ]; then
776 TARGET="$FROM"
777 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik48f8df82016-08-14 03:55:08 -0700778 TMP_DIR="$TMP_DIR/rootfs"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700779 else
Steve Kondika991cf12016-07-28 12:13:12 -0700780 TARGET="system/$FROM"
781 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700782 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700783
784 if [ "$SRC" = "adb" ]; then
785 printf ' - %s .. ' "/$TARGET"
786 else
787 printf ' - %s \n' "/$TARGET"
788 fi
789
790 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700791 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
792 mkdir -p "$OUTPUT_DIR/$DIR"
793 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700794 local DEST="$OUTPUT_DIR/$FROM"
795
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700796 if [ "$SRC" = "adb" ]; then
797 # Try CM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700798 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700799 # if file does not exist try OEM target
800 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700801 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700802 fi
803 else
804 # Try OEM target first
Steve Kondik48f8df82016-08-14 03:55:08 -0700805 if [ -f "$SRC/$FILE" ]; then
806 cp "$SRC/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700807 # if file does not exist try CM target
Steve Kondik48f8df82016-08-14 03:55:08 -0700808 elif [ -f "$SRC/$TARGET" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700809 cp "$SRC/$TARGET" "$DEST"
Steve Kondik48f8df82016-08-14 03:55:08 -0700810 else
811 printf ' !! file not found in source\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700812 fi
813 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700814
Luca Stefani7f9fff22016-07-18 13:47:55 +0200815 if [ "$?" == "0" ]; then
816 # Deodex apk|jar if that's the case
817 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
818 oat2dex "$DEST" "$FILE" "$SRC"
819 if [ -f "$TMPDIR/classes.dex" ]; then
820 zip -gjq "$DEST" "$TMPDIR/classes.dex"
821 rm "$TMPDIR/classes.dex"
822 printf ' (updated %s from odex files)\n' "/$FILE"
823 fi
Luca Stefani3a030122016-07-30 12:08:25 +0200824 elif [[ "$DEST" =~ .xml$ ]]; then
825 fix_xml "$DEST"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200826 fi
827 fi
828
Steve Kondik48f8df82016-08-14 03:55:08 -0700829 # Check pinned files
830 local HASH="${HASHLIST[$i-1]}"
Steve Kondik79fa59b2016-09-02 21:10:02 -0700831 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700832 local KEEP=""
833 local TMP="$TMP_DIR/$FROM"
834 if [ -f "$TMP" ]; then
835 if [ ! -f "$DEST" ]; then
836 KEEP="1"
837 else
838 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
839 if [ "$DEST_HASH" != "$HASH" ]; then
840 KEEP="1"
841 fi
842 fi
843 if [ "$KEEP" = "1" ]; then
844 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
845 if [ "$TMP_HASH" = "$HASH" ]; then
846 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
847 cp -p "$TMP" "$DEST"
848 fi
849 fi
850 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700851 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700852
853 if [ -f "$DEST" ]; then
854 local TYPE="${DIR##*/}"
855 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
856 chmod 755 "$DEST"
857 else
858 chmod 644 "$DEST"
859 fi
860 fi
861
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700862 done
863
864 # Don't allow failing
865 set -e
866}
Louis Popia516c2f2016-07-25 15:51:13 +0200867
868#
869# extract_firmware:
870#
871# $1: file containing the list of items to extract
872# $2: path to extracted radio folder
873#
874function extract_firmware() {
875 if [ -z "$OUTDIR" ]; then
876 echo "Output dir not set!"
877 exit 1
878 fi
879
880 parse_file_list "$1"
881
882 # Don't allow failing
883 set -e
884
885 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
886 local COUNT=${#FILELIST[@]}
887 local SRC="$2"
888 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
889
890 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
891 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
892 rm -rf "${OUTPUT_DIR:?}/"*
893 VENDOR_RADIO_STATE=1
894 fi
895
896 echo "Extracting $COUNT files in $1 from $SRC:"
897
898 for (( i=1; i<COUNT+1; i++ )); do
899 local FILE="${FILELIST[$i-1]}"
900 printf ' - %s \n' "/radio/$FILE"
901
902 if [ ! -d "$OUTPUT_DIR" ]; then
903 mkdir -p "$OUTPUT_DIR"
904 fi
905 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
906 chmod 644 "$OUTPUT_DIR/$FILE"
907 done
908}