blob: 771e6bfbf9d0427e9e3c9b18ba473a75b4f93796 [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
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040051# $6: custom vendor makefile name - optional, default to false
Steve Kondik4e2aaab2016-07-15 10:39:58 -070052#
53# Must be called before any other functions can be used. This
54# sets up the internal state for a new vendor configuration.
55#
56function setup_vendor() {
57 local DEVICE="$1"
58 if [ -z "$DEVICE" ]; then
59 echo "\$DEVICE must be set before including this script!"
60 exit 1
61 fi
62
63 export VENDOR="$2"
64 if [ -z "$VENDOR" ]; then
65 echo "\$VENDOR must be set before including this script!"
66 exit 1
67 fi
68
69 export CM_ROOT="$3"
70 if [ ! -d "$CM_ROOT" ]; then
71 echo "\$CM_ROOT must be set and valid before including this script!"
72 exit 1
73 fi
74
75 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
76 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
77 mkdir -p "$CM_ROOT/$OUTDIR"
78 fi
79
Rashed Abdel-Tawab5f173152016-10-01 20:33:00 -040080 VNDNAME="$6"
81 if [ -z "$VNDNAME" ]; then
82 VNDNAME="$DEVICE"
83 fi
84
85 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/"$VNDNAME"-vendor.mk
Steve Kondik4e2aaab2016-07-15 10:39:58 -070086 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
87 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
88
89 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
90 COMMON=1
91 else
92 COMMON=0
93 fi
94
95 if [ "$5" == "true" ] || [ "$5" == "1" ]; then
96 VENDOR_STATE=1
Louis Popia516c2f2016-07-25 15:51:13 +020097 VENDOR_RADIO_STATE=1
Steve Kondik4e2aaab2016-07-15 10:39:58 -070098 else
99 VENDOR_STATE=0
Louis Popia516c2f2016-07-25 15:51:13 +0200100 VENDOR_RADIO_STATE=0
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700101 fi
102}
103
104#
105# target_file:
106#
107# $1: colon delimited list
108#
109# Returns destination filename without args
110#
111function target_file() {
112 local LINE="$1"
113 local SPLIT=(${LINE//:/ })
114 local COUNT=${#SPLIT[@]}
115 if [ "$COUNT" -gt "1" ]; then
116 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
117 printf '%s\n' "${SPLIT[1]}"
118 return 0
119 fi
120 fi
121 printf '%s\n' "${SPLIT[0]}"
122}
123
124#
125# target_args:
126#
127# $1: colon delimited list
128#
129# Returns optional arguments (last value) for given target
130#
131function target_args() {
132 local LINE="$1"
133 local SPLIT=(${LINE//:/ })
134 local COUNT=${#SPLIT[@]}
135 if [ "$COUNT" -gt "1" ]; then
136 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
137 printf '%s\n' "${SPLIT[$COUNT-1]}"
138 fi
139 fi
140}
141
142#
143# prefix_match:
144#
145# $1: the prefix to match on
146#
147# Internal function which loops thru the packages list and returns a new
148# list containing the matched files with the prefix stripped away.
149#
150function prefix_match() {
151 local PREFIX="$1"
152 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
153 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
154 printf '%s\n' "${FILE#$PREFIX}"
155 fi
156 done
157}
158
159#
160# write_product_copy_files:
161#
162# Creates the PRODUCT_COPY_FILES section in the product makefile for all
163# items in the list which do not start with a dash (-).
164#
165function write_product_copy_files() {
166 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
167 local TARGET=
168 local FILE=
169 local LINEEND=
170
171 if [ "$COUNT" -eq "0" ]; then
172 return 0
173 fi
174
175 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
176 for (( i=1; i<COUNT+1; i++ )); do
177 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
178 LINEEND=" \\"
179 if [ "$i" -eq "$COUNT" ]; then
180 LINEEND=""
181 fi
182
183 TARGET=$(target_file "$FILE")
184 printf ' %s/proprietary/%s:system/%s%s\n' \
185 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
186 done
187 return 0
188}
189
190#
191# write_packages:
192#
193# $1: The LOCAL_MODULE_CLASS for the given module list
194# $2: "true" if this package is part of the vendor/ path
Steve Kondika991cf12016-07-28 12:13:12 -0700195# $3: type-specific extra flags
196# $4: Name of the array holding the target list
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700197#
198# Internal function which writes out the BUILD_PREBUILT stanzas
199# for all modules in the list. This is called by write_product_packages
200# after the modules are categorized.
201#
202function write_packages() {
203
204 local CLASS="$1"
205 local VENDOR_PKG="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700206 local EXTRA="$3"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700207
208 # Yes, this is a horrible hack - we create a new array using indirection
Steve Kondika991cf12016-07-28 12:13:12 -0700209 local ARR_NAME="$4[@]"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700210 local FILELIST=("${!ARR_NAME}")
211
212 local FILE=
213 local ARGS=
214 local BASENAME=
215 local EXTENSION=
216 local PKGNAME=
217 local SRC=
218
219 for P in "${FILELIST[@]}"; do
220 FILE=$(target_file "$P")
221 ARGS=$(target_args "$P")
222
223 BASENAME=$(basename "$FILE")
224 EXTENSION=${BASENAME##*.}
225 PKGNAME=${BASENAME%.*}
226
227 # Add to final package list
228 PACKAGE_LIST+=("$PKGNAME")
229
230 SRC="proprietary"
231 if [ "$VENDOR_PKG" = "true" ]; then
232 SRC+="/vendor"
233 fi
234
235 printf 'include $(CLEAR_VARS)\n'
236 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
237 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
238 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700239 if [ "$EXTRA" = "both" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700240 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
241 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
242 #if [ "$VENDOR_PKG" = "true" ]; then
243 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
244 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
245 #else
246 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
247 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
248 #fi
Steve Kondika991cf12016-07-28 12:13:12 -0700249 elif [ "$EXTRA" = "64" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700250 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
251 else
252 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
253 fi
Steve Kondik03ce4002016-07-29 00:00:16 -0700254 if [ "$EXTRA" != "none" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700255 printf 'LOCAL_MULTILIB := %s\n' "$EXTRA"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700256 fi
257 elif [ "$CLASS" = "APPS" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700258 if [ "$EXTRA" = "priv-app" ]; then
259 SRC="$SRC/priv-app"
260 else
261 SRC="$SRC/app"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700262 fi
263 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
264 local CERT=platform
265 if [ ! -z "$ARGS" ]; then
266 CERT="$ARGS"
267 fi
268 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
269 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
270 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
271 elif [ "$CLASS" = "ETC" ]; then
272 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
273 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700274 if [ "$ARGS" = "rootfs" ]; then
275 SRC="$SRC/rootfs"
276 if [ "$EXTRA" = "sbin" ]; then
277 SRC="$SRC/sbin"
278 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
279 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
280 fi
281 else
282 SRC="$SRC/bin"
283 fi
284 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
285 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700286 else
Steve Kondika991cf12016-07-28 12:13:12 -0700287 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700288 fi
289 printf 'LOCAL_MODULE_TAGS := optional\n'
290 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400291 if [ "$CLASS" = "APPS" ]; then
292 printf 'LOCAL_DEX_PREOPT := false\n'
293 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700294 if [ ! -z "$EXTENSION" ]; then
295 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
296 fi
297 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700298 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
299 fi
300 if [ "$VENDOR_PKG" = "true" ]; then
301 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
302 fi
303 printf 'include $(BUILD_PREBUILT)\n\n'
304 done
305}
306
307#
308# write_product_packages:
309#
310# This function will create BUILD_PREBUILT entries in the
311# Android.mk and associated PRODUCT_PACKAGES list in the
312# product makefile for all files in the blob list which
313# start with a single dash (-) character.
314#
315function write_product_packages() {
316 PACKAGE_LIST=()
317
318 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
319
320 if [ "$COUNT" = "0" ]; then
321 return 0
322 fi
323
324 # Figure out what's 32-bit, what's 64-bit, and what's multilib
325 # I really should not be doing this in bash due to shitty array passing :(
326 local T_LIB32=( $(prefix_match "lib/") )
327 local T_LIB64=( $(prefix_match "lib64/") )
328 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
329 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700330 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700331
Steve Kondik03ce4002016-07-29 00:00:16 -0700332 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
333 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
334 fi
335 if [ "${#LIB32[@]}" -gt "0" ]; then
336 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
337 fi
338 if [ "${#LIB64[@]}" -gt "0" ]; then
339 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
340 fi
341
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700342 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
343 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
344 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
345 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700346 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700347
348 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700349 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700350 fi
351 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700352 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700353 fi
354 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700355 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700356 fi
357
358 # Apps
359 local APPS=( $(prefix_match "app/") )
360 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700361 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700362 fi
363 local PRIV_APPS=( $(prefix_match "priv-app/") )
364 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700365 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700366 fi
367 local V_APPS=( $(prefix_match "vendor/app/") )
368 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700369 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700370 fi
371 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
372 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700373 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700374 fi
375
376 # Framework
377 local FRAMEWORK=( $(prefix_match "framework/") )
378 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700379 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700380 fi
381
382 # Etc
383 local ETC=( $(prefix_match "etc/") )
384 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700385 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700386 fi
387 local V_ETC=( $(prefix_match "vendor/etc/") )
388 if [ "${#V_ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700389 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700390 fi
391
392 # Executables
393 local BIN=( $(prefix_match "bin/") )
394 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700395 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700396 fi
397 local V_BIN=( $(prefix_match "vendor/bin/") )
398 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700399 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700400 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700401 local SBIN=( $(prefix_match "sbin/") )
402 if [ "${#SBIN[@]}" -gt "0" ]; then
403 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
404 fi
405
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700406
407 # Actually write out the final PRODUCT_PACKAGES list
408 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
409
410 if [ "$PACKAGE_COUNT" -eq "0" ]; then
411 return 0
412 fi
413
414 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
415 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
416 local LINEEND=" \\"
417 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
418 LINEEND=""
419 fi
420 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
421 done
422}
423
424#
425# write_header:
426#
427# $1: file which will be written to
428#
429# writes out the copyright header with the current year.
430# note that this is not an append operation, and should
431# be executed first!
432#
433function write_header() {
434 YEAR=$(date +"%Y")
435
436 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
437
438 cat << EOF > $1
439# Copyright (C) $YEAR The CyanogenMod Project
440#
441# Licensed under the Apache License, Version 2.0 (the "License");
442# you may not use this file except in compliance with the License.
443# You may obtain a copy of the License at
444#
445# http://www.apache.org/licenses/LICENSE-2.0
446#
447# Unless required by applicable law or agreed to in writing, software
448# distributed under the License is distributed on an "AS IS" BASIS,
449# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
450# See the License for the specific language governing permissions and
451# limitations under the License.
452
453# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
454
455EOF
456}
457
458#
459# write_headers:
460#
461# $1: devices falling under common to be added to guard - optional
462#
463# Calls write_header for each of the makefiles and creates
464# the initial path declaration and device guard for the
465# Android.mk
466#
467function write_headers() {
468 write_header "$ANDROIDMK"
469 cat << EOF >> "$ANDROIDMK"
470LOCAL_PATH := \$(call my-dir)
471
472EOF
473 if [ "$COMMON" -ne 1 ]; then
474 cat << EOF >> "$ANDROIDMK"
475ifeq (\$(TARGET_DEVICE),$DEVICE)
476
477EOF
478 else
479 if [ -z "$1" ]; then
480 echo "Argument with devices to be added to guard must be set!"
481 exit 1
482 fi
483 cat << EOF >> "$ANDROIDMK"
484ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
485
486EOF
487 fi
488
489 write_header "$BOARDMK"
490 write_header "$PRODUCTMK"
491}
492
493#
494# write_footers:
495#
496# Closes the inital guard and any other finalization tasks. Must
497# be called as the final step.
498#
499function write_footers() {
500 cat << EOF >> "$ANDROIDMK"
501endif
502EOF
503}
504
505# Return success if adb is up and not in recovery
506function _adb_connected {
507 {
Steve Kondik7561d192016-09-01 21:40:27 -0700508 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700509 then
510 return 0
511 fi
512 } 2>/dev/null
513
514 return 1
515};
516
517#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100518# parse_file_list:
519#
520# $1: input file
521#
522# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700523#
524function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100525 if [ -z "$1" ]; then
526 echo "An input file is expected!"
527 exit 1
528 elif [ ! -f "$1" ]; then
529 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700530 exit 1
531 fi
532
533 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700534 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700535 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700536 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700537
538 while read -r line; do
539 if [ -z "$line" ]; then continue; fi
540
Steve Kondik48f8df82016-08-14 03:55:08 -0700541 # If the line has a pipe delimiter, a sha1 hash should follow.
542 # This indicates the file should be pinned and not overwritten
543 # when extracting files.
544 local SPLIT=(${line//\|/ })
545 local COUNT=${#SPLIT[@]}
546 local SPEC=${SPLIT[0]}
547 local HASH="x"
548 if [ "$COUNT" -gt "1" ]; then
549 HASH=${SPLIT[1]}
550 fi
551
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700552 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700553 if [[ "$SPEC" =~ ^- ]]; then
554 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
555 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700556 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700557 PRODUCT_COPY_FILES_LIST+=("$SPEC")
558 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700559 fi
560
561 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
562}
563
564#
565# write_makefiles:
566#
567# $1: file containing the list of items to extract
568#
569# Calls write_product_copy_files and write_product_packages on
570# the given file and appends to the Android.mk as well as
571# the product makefile.
572#
573function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700574 parse_file_list "$1"
575 write_product_copy_files
576 write_product_packages
577}
578
579#
Louis Popia516c2f2016-07-25 15:51:13 +0200580# append_firmware_calls_to_makefiles:
581#
582# Appends to Android.mk the calls to all images present in radio folder
583# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
584#
585function append_firmware_calls_to_makefiles() {
586 cat << EOF >> "$ANDROIDMK"
587ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
588
589RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
590\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
591 \$(call add-radio-file,radio/\$(f)))
592\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
593
594endif
595
596EOF
597}
598
599#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200600# get_file:
601#
602# $1: input file
603# $2: target file/folder
604# $3: source of the file (can be "adb" or a local folder)
605#
606# Silently extracts the input file to defined target
607# Returns success if file can be pulled from the device or found locally
608#
609function get_file() {
610 local SRC="$3"
611
612 if [ "$SRC" = "adb" ]; then
613 # try to pull
614 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
615
616 return 1
617 else
618 # try to copy
619 cp "$SRC/$1" "$2" 2>/dev/null && return 0
620
621 return 1
622 fi
623};
624
625#
626# oat2dex:
627#
628# $1: extracted apk|jar (to check if deodex is required)
629# $2: odexed apk|jar to deodex
630# $3: source of the odexed apk|jar
631#
632# Convert apk|jar .odex in the corresposing classes.dex
633#
634function oat2dex() {
635 local CM_TARGET="$1"
636 local OEM_TARGET="$2"
637 local SRC="$3"
638 local TARGET=
639 local OAT=
640
641 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
642 export BAKSMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/baksmali.jar
643 export SMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/smali.jar
644 fi
645
646 # Extract existing boot.oats to the temp folder
647 if [ -z "$ARCHES" ]; then
648 echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
649 for ARCH in "arm64" "arm" "x86_64" "x86"; do
650 if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
651 ARCHES+="$ARCH "
652 fi
653 done
654 fi
655
656 if [ -z "$ARCHES" ]; then
657 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
658 fi
659
Steve Kondik48f8df82016-08-14 03:55:08 -0700660 if [ ! -f "$CM_TARGET" ]; then
661 return;
662 fi
663
Luca Stefani7f9fff22016-07-18 13:47:55 +0200664 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
665 return 0 # target apk|jar is already odexed, return
666 fi
667
668 for ARCH in $ARCHES; do
669 BOOTOAT="$TMPDIR/boot_$ARCH.oat"
670
671 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
672
673 if get_file "$OAT" "$TMPDIR" "$SRC"; then
674 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
675 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
676 # try to extract classes.dex from boot.oat for framework jars
677 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
678 else
679 continue
680 fi
681
682 java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
683 done
684
685 rm -rf "$TMPDIR/dexout"
686}
687
688#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700689# init_adb_connection:
690#
691# Starts adb server and waits for the device
692#
693function init_adb_connection() {
694 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
695 if ! _adb_connected; then
696 echo "No device is online. Waiting for one..."
697 echo "Please connect USB and/or enable USB debugging"
698 until _adb_connected; do
699 sleep 1
700 done
701 echo "Device Found."
702 fi
703
704 # Retrieve IP and PORT info if we're using a TCP connection
705 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
706 | head -1 | awk '{print $1}')
707 adb root &> /dev/null
708 sleep 0.3
709 if [ -n "$TCPIPPORT" ]; then
710 # adb root just killed our connection
711 # so reconnect...
712 adb connect "$TCPIPPORT"
713 fi
714 adb wait-for-device &> /dev/null
715 sleep 0.3
716}
717
718#
Luca Stefani3a030122016-07-30 12:08:25 +0200719# fix_xml:
720#
721# $1: xml file to fix
722#
723function fix_xml() {
724 local XML="$1"
725 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
726
727 grep '^<?xml version' "$XML" > "$TEMP_XML"
728 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
729
730 mv "$TEMP_XML" "$XML"
731}
732
733#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700734# extract:
735#
736# $1: file containing the list of items to extract
737# $2: path to extracted system folder, or "adb" to extract from device
738#
739function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700740 if [ -z "$OUTDIR" ]; then
741 echo "Output dir not set!"
742 exit 1
743 fi
744
745 parse_file_list "$1"
746
747 # Allow failing, so we can try $DEST and/or $FILE
748 set +e
749
750 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -0700751 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700752 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700753 local SRC="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700754 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -0700755 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
756
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700757 if [ "$SRC" = "adb" ]; then
758 init_adb_connection
759 fi
760
761 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700762 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -0700763 rm -rf "${OUTPUT_TMP:?}"
764 mkdir -p "${OUTPUT_TMP:?}"
765 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700766 VENDOR_STATE=1
767 fi
768
769 echo "Extracting $COUNT files in $1 from $SRC:"
770
771 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700772
773 local FROM=$(target_file "${FILELIST[$i-1]}")
774 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700775 local SPLIT=(${FILELIST[$i-1]//:/ })
776 local FILE="${SPLIT[0]#-}"
Steve Kondika991cf12016-07-28 12:13:12 -0700777 local OUTPUT_DIR="$OUTPUT_ROOT"
Steve Kondik48f8df82016-08-14 03:55:08 -0700778 local TMP_DIR="$OUTPUT_TMP"
Steve Kondika991cf12016-07-28 12:13:12 -0700779 local TARGET=
780
781 if [ "$ARGS" = "rootfs" ]; then
782 TARGET="$FROM"
783 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik48f8df82016-08-14 03:55:08 -0700784 TMP_DIR="$TMP_DIR/rootfs"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700785 else
Steve Kondika991cf12016-07-28 12:13:12 -0700786 TARGET="system/$FROM"
787 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700788 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700789
790 if [ "$SRC" = "adb" ]; then
791 printf ' - %s .. ' "/$TARGET"
792 else
793 printf ' - %s \n' "/$TARGET"
794 fi
795
796 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700797 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
798 mkdir -p "$OUTPUT_DIR/$DIR"
799 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700800 local DEST="$OUTPUT_DIR/$FROM"
801
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700802 if [ "$SRC" = "adb" ]; then
803 # Try CM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700804 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700805 # if file does not exist try OEM target
806 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700807 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700808 fi
809 else
810 # Try OEM target first
Steve Kondik48f8df82016-08-14 03:55:08 -0700811 if [ -f "$SRC/$FILE" ]; then
812 cp "$SRC/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700813 # if file does not exist try CM target
Steve Kondik48f8df82016-08-14 03:55:08 -0700814 elif [ -f "$SRC/$TARGET" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700815 cp "$SRC/$TARGET" "$DEST"
Steve Kondik48f8df82016-08-14 03:55:08 -0700816 else
817 printf ' !! file not found in source\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700818 fi
819 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700820
Luca Stefani7f9fff22016-07-18 13:47:55 +0200821 if [ "$?" == "0" ]; then
822 # Deodex apk|jar if that's the case
823 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
824 oat2dex "$DEST" "$FILE" "$SRC"
825 if [ -f "$TMPDIR/classes.dex" ]; then
826 zip -gjq "$DEST" "$TMPDIR/classes.dex"
827 rm "$TMPDIR/classes.dex"
828 printf ' (updated %s from odex files)\n' "/$FILE"
829 fi
Luca Stefani3a030122016-07-30 12:08:25 +0200830 elif [[ "$DEST" =~ .xml$ ]]; then
831 fix_xml "$DEST"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200832 fi
833 fi
834
Steve Kondik48f8df82016-08-14 03:55:08 -0700835 # Check pinned files
836 local HASH="${HASHLIST[$i-1]}"
Steve Kondik79fa59b2016-09-02 21:10:02 -0700837 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700838 local KEEP=""
839 local TMP="$TMP_DIR/$FROM"
840 if [ -f "$TMP" ]; then
841 if [ ! -f "$DEST" ]; then
842 KEEP="1"
843 else
844 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
845 if [ "$DEST_HASH" != "$HASH" ]; then
846 KEEP="1"
847 fi
848 fi
849 if [ "$KEEP" = "1" ]; then
850 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
851 if [ "$TMP_HASH" = "$HASH" ]; then
852 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
853 cp -p "$TMP" "$DEST"
854 fi
855 fi
856 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700857 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700858
859 if [ -f "$DEST" ]; then
860 local TYPE="${DIR##*/}"
861 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
862 chmod 755 "$DEST"
863 else
864 chmod 644 "$DEST"
865 fi
866 fi
867
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700868 done
869
870 # Don't allow failing
871 set -e
872}
Louis Popia516c2f2016-07-25 15:51:13 +0200873
874#
875# extract_firmware:
876#
877# $1: file containing the list of items to extract
878# $2: path to extracted radio folder
879#
880function extract_firmware() {
881 if [ -z "$OUTDIR" ]; then
882 echo "Output dir not set!"
883 exit 1
884 fi
885
886 parse_file_list "$1"
887
888 # Don't allow failing
889 set -e
890
891 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
892 local COUNT=${#FILELIST[@]}
893 local SRC="$2"
894 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
895
896 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
897 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
898 rm -rf "${OUTPUT_DIR:?}/"*
899 VENDOR_RADIO_STATE=1
900 fi
901
902 echo "Extracting $COUNT files in $1 from $SRC:"
903
904 for (( i=1; i<COUNT+1; i++ )); do
905 local FILE="${FILELIST[$i-1]}"
906 printf ' - %s \n' "/radio/$FILE"
907
908 if [ ! -d "$OUTPUT_DIR" ]; then
909 mkdir -p "$OUTPUT_DIR"
910 fi
911 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
912 chmod 644 "$OUTPUT_DIR/$FILE"
913 done
914}