blob: fb7602b92acf7dbe1cd6a73e2fa30a5127d09d81 [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"
Steve Kondika991cf12016-07-28 12:13:12 -0700285 if [ ! -z "$EXTENSION" ]; then
286 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
287 fi
288 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700289 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
290 fi
291 if [ "$VENDOR_PKG" = "true" ]; then
292 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
293 fi
294 printf 'include $(BUILD_PREBUILT)\n\n'
295 done
296}
297
298#
299# write_product_packages:
300#
301# This function will create BUILD_PREBUILT entries in the
302# Android.mk and associated PRODUCT_PACKAGES list in the
303# product makefile for all files in the blob list which
304# start with a single dash (-) character.
305#
306function write_product_packages() {
307 PACKAGE_LIST=()
308
309 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
310
311 if [ "$COUNT" = "0" ]; then
312 return 0
313 fi
314
315 # Figure out what's 32-bit, what's 64-bit, and what's multilib
316 # I really should not be doing this in bash due to shitty array passing :(
317 local T_LIB32=( $(prefix_match "lib/") )
318 local T_LIB64=( $(prefix_match "lib64/") )
319 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
320 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700321 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700322
Steve Kondik03ce4002016-07-29 00:00:16 -0700323 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
324 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
325 fi
326 if [ "${#LIB32[@]}" -gt "0" ]; then
327 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
328 fi
329 if [ "${#LIB64[@]}" -gt "0" ]; then
330 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
331 fi
332
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700333 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
334 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
335 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
336 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700337 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700338
339 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700340 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700341 fi
342 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700343 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700344 fi
345 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700346 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700347 fi
348
349 # Apps
350 local APPS=( $(prefix_match "app/") )
351 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700352 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700353 fi
354 local PRIV_APPS=( $(prefix_match "priv-app/") )
355 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700356 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700357 fi
358 local V_APPS=( $(prefix_match "vendor/app/") )
359 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700360 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700361 fi
362 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
363 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700364 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700365 fi
366
367 # Framework
368 local FRAMEWORK=( $(prefix_match "framework/") )
369 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700370 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700371 fi
372
373 # Etc
374 local ETC=( $(prefix_match "etc/") )
375 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700376 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700377 fi
378 local V_ETC=( $(prefix_match "vendor/etc/") )
379 if [ "${#V_ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700380 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700381 fi
382
383 # Executables
384 local BIN=( $(prefix_match "bin/") )
385 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700386 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700387 fi
388 local V_BIN=( $(prefix_match "vendor/bin/") )
389 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700390 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700391 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700392 local SBIN=( $(prefix_match "sbin/") )
393 if [ "${#SBIN[@]}" -gt "0" ]; then
394 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
395 fi
396
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700397
398 # Actually write out the final PRODUCT_PACKAGES list
399 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
400
401 if [ "$PACKAGE_COUNT" -eq "0" ]; then
402 return 0
403 fi
404
405 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
406 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
407 local LINEEND=" \\"
408 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
409 LINEEND=""
410 fi
411 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
412 done
413}
414
415#
416# write_header:
417#
418# $1: file which will be written to
419#
420# writes out the copyright header with the current year.
421# note that this is not an append operation, and should
422# be executed first!
423#
424function write_header() {
425 YEAR=$(date +"%Y")
426
427 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
428
429 cat << EOF > $1
430# Copyright (C) $YEAR The CyanogenMod Project
431#
432# Licensed under the Apache License, Version 2.0 (the "License");
433# you may not use this file except in compliance with the License.
434# You may obtain a copy of the License at
435#
436# http://www.apache.org/licenses/LICENSE-2.0
437#
438# Unless required by applicable law or agreed to in writing, software
439# distributed under the License is distributed on an "AS IS" BASIS,
440# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
441# See the License for the specific language governing permissions and
442# limitations under the License.
443
444# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
445
446EOF
447}
448
449#
450# write_headers:
451#
452# $1: devices falling under common to be added to guard - optional
453#
454# Calls write_header for each of the makefiles and creates
455# the initial path declaration and device guard for the
456# Android.mk
457#
458function write_headers() {
459 write_header "$ANDROIDMK"
460 cat << EOF >> "$ANDROIDMK"
461LOCAL_PATH := \$(call my-dir)
462
463EOF
464 if [ "$COMMON" -ne 1 ]; then
465 cat << EOF >> "$ANDROIDMK"
466ifeq (\$(TARGET_DEVICE),$DEVICE)
467
468EOF
469 else
470 if [ -z "$1" ]; then
471 echo "Argument with devices to be added to guard must be set!"
472 exit 1
473 fi
474 cat << EOF >> "$ANDROIDMK"
475ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
476
477EOF
478 fi
479
480 write_header "$BOARDMK"
481 write_header "$PRODUCTMK"
482}
483
484#
485# write_footers:
486#
487# Closes the inital guard and any other finalization tasks. Must
488# be called as the final step.
489#
490function write_footers() {
491 cat << EOF >> "$ANDROIDMK"
492endif
493EOF
494}
495
496# Return success if adb is up and not in recovery
497function _adb_connected {
498 {
499 if [[ "$(adb get-state)" == device &&
500 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
501 then
502 return 0
503 fi
504 } 2>/dev/null
505
506 return 1
507};
508
509#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100510# parse_file_list:
511#
512# $1: input file
513#
514# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700515#
516function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100517 if [ -z "$1" ]; then
518 echo "An input file is expected!"
519 exit 1
520 elif [ ! -f "$1" ]; then
521 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700522 exit 1
523 fi
524
525 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700526 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700527 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700528 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700529
530 while read -r line; do
531 if [ -z "$line" ]; then continue; fi
532
Steve Kondik48f8df82016-08-14 03:55:08 -0700533 # If the line has a pipe delimiter, a sha1 hash should follow.
534 # This indicates the file should be pinned and not overwritten
535 # when extracting files.
536 local SPLIT=(${line//\|/ })
537 local COUNT=${#SPLIT[@]}
538 local SPEC=${SPLIT[0]}
539 local HASH="x"
540 if [ "$COUNT" -gt "1" ]; then
541 HASH=${SPLIT[1]}
542 fi
543
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700544 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700545 if [[ "$SPEC" =~ ^- ]]; then
546 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
547 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700548 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700549 PRODUCT_COPY_FILES_LIST+=("$SPEC")
550 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700551 fi
552
553 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
554}
555
556#
557# write_makefiles:
558#
559# $1: file containing the list of items to extract
560#
561# Calls write_product_copy_files and write_product_packages on
562# the given file and appends to the Android.mk as well as
563# the product makefile.
564#
565function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700566 parse_file_list "$1"
567 write_product_copy_files
568 write_product_packages
569}
570
571#
Louis Popia516c2f2016-07-25 15:51:13 +0200572# append_firmware_calls_to_makefiles:
573#
574# Appends to Android.mk the calls to all images present in radio folder
575# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
576#
577function append_firmware_calls_to_makefiles() {
578 cat << EOF >> "$ANDROIDMK"
579ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
580
581RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
582\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
583 \$(call add-radio-file,radio/\$(f)))
584\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
585
586endif
587
588EOF
589}
590
591#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200592# get_file:
593#
594# $1: input file
595# $2: target file/folder
596# $3: source of the file (can be "adb" or a local folder)
597#
598# Silently extracts the input file to defined target
599# Returns success if file can be pulled from the device or found locally
600#
601function get_file() {
602 local SRC="$3"
603
604 if [ "$SRC" = "adb" ]; then
605 # try to pull
606 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
607
608 return 1
609 else
610 # try to copy
611 cp "$SRC/$1" "$2" 2>/dev/null && return 0
612
613 return 1
614 fi
615};
616
617#
618# oat2dex:
619#
620# $1: extracted apk|jar (to check if deodex is required)
621# $2: odexed apk|jar to deodex
622# $3: source of the odexed apk|jar
623#
624# Convert apk|jar .odex in the corresposing classes.dex
625#
626function oat2dex() {
627 local CM_TARGET="$1"
628 local OEM_TARGET="$2"
629 local SRC="$3"
630 local TARGET=
631 local OAT=
632
633 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
634 export BAKSMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/baksmali.jar
635 export SMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/smali.jar
636 fi
637
638 # Extract existing boot.oats to the temp folder
639 if [ -z "$ARCHES" ]; then
640 echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
641 for ARCH in "arm64" "arm" "x86_64" "x86"; do
642 if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
643 ARCHES+="$ARCH "
644 fi
645 done
646 fi
647
648 if [ -z "$ARCHES" ]; then
649 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
650 fi
651
Steve Kondik48f8df82016-08-14 03:55:08 -0700652 if [ ! -f "$CM_TARGET" ]; then
653 return;
654 fi
655
Luca Stefani7f9fff22016-07-18 13:47:55 +0200656 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
657 return 0 # target apk|jar is already odexed, return
658 fi
659
660 for ARCH in $ARCHES; do
661 BOOTOAT="$TMPDIR/boot_$ARCH.oat"
662
663 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
664
665 if get_file "$OAT" "$TMPDIR" "$SRC"; then
666 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
667 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
668 # try to extract classes.dex from boot.oat for framework jars
669 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
670 else
671 continue
672 fi
673
674 java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
675 done
676
677 rm -rf "$TMPDIR/dexout"
678}
679
680#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700681# init_adb_connection:
682#
683# Starts adb server and waits for the device
684#
685function init_adb_connection() {
686 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
687 if ! _adb_connected; then
688 echo "No device is online. Waiting for one..."
689 echo "Please connect USB and/or enable USB debugging"
690 until _adb_connected; do
691 sleep 1
692 done
693 echo "Device Found."
694 fi
695
696 # Retrieve IP and PORT info if we're using a TCP connection
697 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
698 | head -1 | awk '{print $1}')
699 adb root &> /dev/null
700 sleep 0.3
701 if [ -n "$TCPIPPORT" ]; then
702 # adb root just killed our connection
703 # so reconnect...
704 adb connect "$TCPIPPORT"
705 fi
706 adb wait-for-device &> /dev/null
707 sleep 0.3
708}
709
710#
Luca Stefani3a030122016-07-30 12:08:25 +0200711# fix_xml:
712#
713# $1: xml file to fix
714#
715function fix_xml() {
716 local XML="$1"
717 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
718
719 grep '^<?xml version' "$XML" > "$TEMP_XML"
720 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
721
722 mv "$TEMP_XML" "$XML"
723}
724
725#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700726# extract:
727#
728# $1: file containing the list of items to extract
729# $2: path to extracted system folder, or "adb" to extract from device
730#
731function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700732 if [ -z "$OUTDIR" ]; then
733 echo "Output dir not set!"
734 exit 1
735 fi
736
737 parse_file_list "$1"
738
739 # Allow failing, so we can try $DEST and/or $FILE
740 set +e
741
742 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -0700743 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700744 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700745 local SRC="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700746 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -0700747 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
748
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700749 if [ "$SRC" = "adb" ]; then
750 init_adb_connection
751 fi
752
753 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700754 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -0700755 rm -rf "${OUTPUT_TMP:?}"
756 mkdir -p "${OUTPUT_TMP:?}"
757 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700758 VENDOR_STATE=1
759 fi
760
761 echo "Extracting $COUNT files in $1 from $SRC:"
762
763 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700764
765 local FROM=$(target_file "${FILELIST[$i-1]}")
766 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700767 local SPLIT=(${FILELIST[$i-1]//:/ })
768 local FILE="${SPLIT[0]#-}"
Steve Kondika991cf12016-07-28 12:13:12 -0700769 local OUTPUT_DIR="$OUTPUT_ROOT"
Steve Kondik48f8df82016-08-14 03:55:08 -0700770 local TMP_DIR="$OUTPUT_TMP"
Steve Kondika991cf12016-07-28 12:13:12 -0700771 local TARGET=
772
773 if [ "$ARGS" = "rootfs" ]; then
774 TARGET="$FROM"
775 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik48f8df82016-08-14 03:55:08 -0700776 TMP_DIR="$TMP_DIR/rootfs"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700777 else
Steve Kondika991cf12016-07-28 12:13:12 -0700778 TARGET="system/$FROM"
779 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700780 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700781
782 if [ "$SRC" = "adb" ]; then
783 printf ' - %s .. ' "/$TARGET"
784 else
785 printf ' - %s \n' "/$TARGET"
786 fi
787
788 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700789 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
790 mkdir -p "$OUTPUT_DIR/$DIR"
791 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700792 local DEST="$OUTPUT_DIR/$FROM"
793
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700794 if [ "$SRC" = "adb" ]; then
795 # Try CM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700796 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700797 # if file does not exist try OEM target
798 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700799 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700800 fi
801 else
802 # Try OEM target first
Steve Kondik48f8df82016-08-14 03:55:08 -0700803 if [ -f "$SRC/$FILE" ]; then
804 cp "$SRC/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700805 # if file does not exist try CM target
Steve Kondik48f8df82016-08-14 03:55:08 -0700806 elif [ -f "$SRC/$TARGET" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700807 cp "$SRC/$TARGET" "$DEST"
Steve Kondik48f8df82016-08-14 03:55:08 -0700808 else
809 printf ' !! file not found in source\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700810 fi
811 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700812
Luca Stefani7f9fff22016-07-18 13:47:55 +0200813 if [ "$?" == "0" ]; then
814 # Deodex apk|jar if that's the case
815 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
816 oat2dex "$DEST" "$FILE" "$SRC"
817 if [ -f "$TMPDIR/classes.dex" ]; then
818 zip -gjq "$DEST" "$TMPDIR/classes.dex"
819 rm "$TMPDIR/classes.dex"
820 printf ' (updated %s from odex files)\n' "/$FILE"
821 fi
Luca Stefani3a030122016-07-30 12:08:25 +0200822 elif [[ "$DEST" =~ .xml$ ]]; then
823 fix_xml "$DEST"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200824 fi
825 fi
826
Steve Kondik48f8df82016-08-14 03:55:08 -0700827 # Check pinned files
828 local HASH="${HASHLIST[$i-1]}"
829 if [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
830 local KEEP=""
831 local TMP="$TMP_DIR/$FROM"
832 if [ -f "$TMP" ]; then
833 if [ ! -f "$DEST" ]; then
834 KEEP="1"
835 else
836 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
837 if [ "$DEST_HASH" != "$HASH" ]; then
838 KEEP="1"
839 fi
840 fi
841 if [ "$KEEP" = "1" ]; then
842 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
843 if [ "$TMP_HASH" = "$HASH" ]; then
844 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
845 cp -p "$TMP" "$DEST"
846 fi
847 fi
848 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700849 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700850
851 if [ -f "$DEST" ]; then
852 local TYPE="${DIR##*/}"
853 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
854 chmod 755 "$DEST"
855 else
856 chmod 644 "$DEST"
857 fi
858 fi
859
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700860 done
861
862 # Don't allow failing
863 set -e
864}
Louis Popia516c2f2016-07-25 15:51:13 +0200865
866#
867# extract_firmware:
868#
869# $1: file containing the list of items to extract
870# $2: path to extracted radio folder
871#
872function extract_firmware() {
873 if [ -z "$OUTDIR" ]; then
874 echo "Output dir not set!"
875 exit 1
876 fi
877
878 parse_file_list "$1"
879
880 # Don't allow failing
881 set -e
882
883 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
884 local COUNT=${#FILELIST[@]}
885 local SRC="$2"
886 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
887
888 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
889 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
890 rm -rf "${OUTPUT_DIR:?}/"*
891 VENDOR_RADIO_STATE=1
892 fi
893
894 echo "Extracting $COUNT files in $1 from $SRC:"
895
896 for (( i=1; i<COUNT+1; i++ )); do
897 local FILE="${FILELIST[$i-1]}"
898 printf ' - %s \n' "/radio/$FILE"
899
900 if [ ! -d "$OUTPUT_DIR" ]; then
901 mkdir -p "$OUTPUT_DIR"
902 fi
903 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
904 chmod 644 "$OUTPUT_DIR/$FILE"
905 done
906}