blob: b883219f9fef242de9e94644a113555ec77cd263 [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
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400462# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700463#
464# Calls write_header for each of the makefiles and creates
465# the initial path declaration and device guard for the
466# Android.mk
467#
468function write_headers() {
469 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400470
471 GUARD="$2"
472 if [ -z "$GUARD" ]; then
473 GUARD="TARGET_DEVICE"
474 fi
475
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700476 cat << EOF >> "$ANDROIDMK"
477LOCAL_PATH := \$(call my-dir)
478
479EOF
480 if [ "$COMMON" -ne 1 ]; then
481 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400482ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700483
484EOF
485 else
486 if [ -z "$1" ]; then
487 echo "Argument with devices to be added to guard must be set!"
488 exit 1
489 fi
490 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400491ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700492
493EOF
494 fi
495
496 write_header "$BOARDMK"
497 write_header "$PRODUCTMK"
498}
499
500#
501# write_footers:
502#
503# Closes the inital guard and any other finalization tasks. Must
504# be called as the final step.
505#
506function write_footers() {
507 cat << EOF >> "$ANDROIDMK"
508endif
509EOF
510}
511
512# Return success if adb is up and not in recovery
513function _adb_connected {
514 {
Steve Kondik7561d192016-09-01 21:40:27 -0700515 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700516 then
517 return 0
518 fi
519 } 2>/dev/null
520
521 return 1
522};
523
524#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100525# parse_file_list:
526#
527# $1: input file
528#
529# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700530#
531function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100532 if [ -z "$1" ]; then
533 echo "An input file is expected!"
534 exit 1
535 elif [ ! -f "$1" ]; then
536 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700537 exit 1
538 fi
539
540 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700541 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700542 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700543 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700544
545 while read -r line; do
546 if [ -z "$line" ]; then continue; fi
547
Steve Kondik48f8df82016-08-14 03:55:08 -0700548 # If the line has a pipe delimiter, a sha1 hash should follow.
549 # This indicates the file should be pinned and not overwritten
550 # when extracting files.
551 local SPLIT=(${line//\|/ })
552 local COUNT=${#SPLIT[@]}
553 local SPEC=${SPLIT[0]}
554 local HASH="x"
555 if [ "$COUNT" -gt "1" ]; then
556 HASH=${SPLIT[1]}
557 fi
558
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700559 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700560 if [[ "$SPEC" =~ ^- ]]; then
561 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
562 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700563 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700564 PRODUCT_COPY_FILES_LIST+=("$SPEC")
565 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700566 fi
567
568 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
569}
570
571#
572# write_makefiles:
573#
574# $1: file containing the list of items to extract
575#
576# Calls write_product_copy_files and write_product_packages on
577# the given file and appends to the Android.mk as well as
578# the product makefile.
579#
580function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700581 parse_file_list "$1"
582 write_product_copy_files
583 write_product_packages
584}
585
586#
Louis Popia516c2f2016-07-25 15:51:13 +0200587# append_firmware_calls_to_makefiles:
588#
589# Appends to Android.mk the calls to all images present in radio folder
590# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
591#
592function append_firmware_calls_to_makefiles() {
593 cat << EOF >> "$ANDROIDMK"
594ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
595
596RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
597\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
598 \$(call add-radio-file,radio/\$(f)))
599\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
600
601endif
602
603EOF
604}
605
606#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200607# get_file:
608#
609# $1: input file
610# $2: target file/folder
611# $3: source of the file (can be "adb" or a local folder)
612#
613# Silently extracts the input file to defined target
614# Returns success if file can be pulled from the device or found locally
615#
616function get_file() {
617 local SRC="$3"
618
619 if [ "$SRC" = "adb" ]; then
620 # try to pull
621 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
622
623 return 1
624 else
625 # try to copy
626 cp "$SRC/$1" "$2" 2>/dev/null && return 0
627
628 return 1
629 fi
630};
631
632#
633# oat2dex:
634#
635# $1: extracted apk|jar (to check if deodex is required)
636# $2: odexed apk|jar to deodex
637# $3: source of the odexed apk|jar
638#
639# Convert apk|jar .odex in the corresposing classes.dex
640#
641function oat2dex() {
642 local CM_TARGET="$1"
643 local OEM_TARGET="$2"
644 local SRC="$3"
645 local TARGET=
646 local OAT=
647
648 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
649 export BAKSMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/baksmali.jar
650 export SMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/smali.jar
651 fi
652
653 # Extract existing boot.oats to the temp folder
654 if [ -z "$ARCHES" ]; then
655 echo "Checking if system is odexed and extracting boot.oats, if applicable. This may take a while..."
656 for ARCH in "arm64" "arm" "x86_64" "x86"; do
657 if get_file "system/framework/$ARCH/boot.oat" "$TMPDIR/boot_$ARCH.oat" "$SRC"; then
658 ARCHES+="$ARCH "
659 fi
660 done
661 fi
662
663 if [ -z "$ARCHES" ]; then
664 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
665 fi
666
Steve Kondik48f8df82016-08-14 03:55:08 -0700667 if [ ! -f "$CM_TARGET" ]; then
668 return;
669 fi
670
Luca Stefani7f9fff22016-07-18 13:47:55 +0200671 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
672 return 0 # target apk|jar is already odexed, return
673 fi
674
675 for ARCH in $ARCHES; do
676 BOOTOAT="$TMPDIR/boot_$ARCH.oat"
677
678 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
679
680 if get_file "$OAT" "$TMPDIR" "$SRC"; then
681 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
682 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
683 # try to extract classes.dex from boot.oat for framework jars
684 java -jar "$BAKSMALIJAR" -x -o "$TMPDIR/dexout" -c "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
685 else
686 continue
687 fi
688
689 java -jar "$SMALIJAR" "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
690 done
691
692 rm -rf "$TMPDIR/dexout"
693}
694
695#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700696# init_adb_connection:
697#
698# Starts adb server and waits for the device
699#
700function init_adb_connection() {
701 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
702 if ! _adb_connected; then
703 echo "No device is online. Waiting for one..."
704 echo "Please connect USB and/or enable USB debugging"
705 until _adb_connected; do
706 sleep 1
707 done
708 echo "Device Found."
709 fi
710
711 # Retrieve IP and PORT info if we're using a TCP connection
712 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
713 | head -1 | awk '{print $1}')
714 adb root &> /dev/null
715 sleep 0.3
716 if [ -n "$TCPIPPORT" ]; then
717 # adb root just killed our connection
718 # so reconnect...
719 adb connect "$TCPIPPORT"
720 fi
721 adb wait-for-device &> /dev/null
722 sleep 0.3
723}
724
725#
Luca Stefani3a030122016-07-30 12:08:25 +0200726# fix_xml:
727#
728# $1: xml file to fix
729#
730function fix_xml() {
731 local XML="$1"
732 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
733
734 grep '^<?xml version' "$XML" > "$TEMP_XML"
735 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
736
737 mv "$TEMP_XML" "$XML"
738}
739
740#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700741# extract:
742#
743# $1: file containing the list of items to extract
744# $2: path to extracted system folder, or "adb" to extract from device
745#
746function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700747 if [ -z "$OUTDIR" ]; then
748 echo "Output dir not set!"
749 exit 1
750 fi
751
752 parse_file_list "$1"
753
754 # Allow failing, so we can try $DEST and/or $FILE
755 set +e
756
757 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -0700758 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700759 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700760 local SRC="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700761 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -0700762 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
763
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700764 if [ "$SRC" = "adb" ]; then
765 init_adb_connection
766 fi
767
768 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700769 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -0700770 rm -rf "${OUTPUT_TMP:?}"
771 mkdir -p "${OUTPUT_TMP:?}"
772 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700773 VENDOR_STATE=1
774 fi
775
776 echo "Extracting $COUNT files in $1 from $SRC:"
777
778 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700779
780 local FROM=$(target_file "${FILELIST[$i-1]}")
781 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700782 local SPLIT=(${FILELIST[$i-1]//:/ })
783 local FILE="${SPLIT[0]#-}"
Steve Kondika991cf12016-07-28 12:13:12 -0700784 local OUTPUT_DIR="$OUTPUT_ROOT"
Steve Kondik48f8df82016-08-14 03:55:08 -0700785 local TMP_DIR="$OUTPUT_TMP"
Steve Kondika991cf12016-07-28 12:13:12 -0700786 local TARGET=
787
788 if [ "$ARGS" = "rootfs" ]; then
789 TARGET="$FROM"
790 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik48f8df82016-08-14 03:55:08 -0700791 TMP_DIR="$TMP_DIR/rootfs"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700792 else
Steve Kondika991cf12016-07-28 12:13:12 -0700793 TARGET="system/$FROM"
794 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700795 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700796
797 if [ "$SRC" = "adb" ]; then
798 printf ' - %s .. ' "/$TARGET"
799 else
800 printf ' - %s \n' "/$TARGET"
801 fi
802
803 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700804 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
805 mkdir -p "$OUTPUT_DIR/$DIR"
806 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700807 local DEST="$OUTPUT_DIR/$FROM"
808
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700809 if [ "$SRC" = "adb" ]; then
810 # Try CM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700811 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700812 # if file does not exist try OEM target
813 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700814 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700815 fi
816 else
817 # Try OEM target first
Steve Kondik48f8df82016-08-14 03:55:08 -0700818 if [ -f "$SRC/$FILE" ]; then
819 cp "$SRC/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700820 # if file does not exist try CM target
Steve Kondik48f8df82016-08-14 03:55:08 -0700821 elif [ -f "$SRC/$TARGET" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700822 cp "$SRC/$TARGET" "$DEST"
Steve Kondik48f8df82016-08-14 03:55:08 -0700823 else
824 printf ' !! file not found in source\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700825 fi
826 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700827
Luca Stefani7f9fff22016-07-18 13:47:55 +0200828 if [ "$?" == "0" ]; then
829 # Deodex apk|jar if that's the case
830 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
831 oat2dex "$DEST" "$FILE" "$SRC"
832 if [ -f "$TMPDIR/classes.dex" ]; then
833 zip -gjq "$DEST" "$TMPDIR/classes.dex"
834 rm "$TMPDIR/classes.dex"
835 printf ' (updated %s from odex files)\n' "/$FILE"
836 fi
Luca Stefani3a030122016-07-30 12:08:25 +0200837 elif [[ "$DEST" =~ .xml$ ]]; then
838 fix_xml "$DEST"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200839 fi
840 fi
841
Steve Kondik48f8df82016-08-14 03:55:08 -0700842 # Check pinned files
843 local HASH="${HASHLIST[$i-1]}"
Steve Kondik79fa59b2016-09-02 21:10:02 -0700844 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700845 local KEEP=""
846 local TMP="$TMP_DIR/$FROM"
847 if [ -f "$TMP" ]; then
848 if [ ! -f "$DEST" ]; then
849 KEEP="1"
850 else
851 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
852 if [ "$DEST_HASH" != "$HASH" ]; then
853 KEEP="1"
854 fi
855 fi
856 if [ "$KEEP" = "1" ]; then
857 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
858 if [ "$TMP_HASH" = "$HASH" ]; then
859 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
860 cp -p "$TMP" "$DEST"
861 fi
862 fi
863 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700864 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700865
866 if [ -f "$DEST" ]; then
867 local TYPE="${DIR##*/}"
868 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
869 chmod 755 "$DEST"
870 else
871 chmod 644 "$DEST"
872 fi
873 fi
874
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700875 done
876
877 # Don't allow failing
878 set -e
879}
Louis Popia516c2f2016-07-25 15:51:13 +0200880
881#
882# extract_firmware:
883#
884# $1: file containing the list of items to extract
885# $2: path to extracted radio folder
886#
887function extract_firmware() {
888 if [ -z "$OUTDIR" ]; then
889 echo "Output dir not set!"
890 exit 1
891 fi
892
893 parse_file_list "$1"
894
895 # Don't allow failing
896 set -e
897
898 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
899 local COUNT=${#FILELIST[@]}
900 local SRC="$2"
901 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
902
903 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
904 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
905 rm -rf "${OUTPUT_DIR:?}/"*
906 VENDOR_RADIO_STATE=1
907 fi
908
909 echo "Extracting $COUNT files in $1 from $SRC:"
910
911 for (( i=1; i<COUNT+1; i++ )); do
912 local FILE="${FILELIST[$i-1]}"
913 printf ' - %s \n' "/radio/$FILE"
914
915 if [ ! -d "$OUTPUT_DIR" ]; then
916 mkdir -p "$OUTPUT_DIR"
917 fi
918 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
919 chmod 644 "$OUTPUT_DIR/$FILE"
920 done
921}