blob: 95a1c9f5e645cc30fa455bf550fff899211532bb [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
HashBangd3030312016-08-01 14:36:46 -0400258 if [ -z "$ARGS" ]; then
259 if [ "$EXTRA" = "priv-app" ]; then
260 SRC="$SRC/priv-app"
261 else
262 SRC="$SRC/app"
263 fi
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700264 fi
265 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
266 local CERT=platform
267 if [ ! -z "$ARGS" ]; then
268 CERT="$ARGS"
269 fi
270 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
271 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
272 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
273 elif [ "$CLASS" = "ETC" ]; then
274 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
275 elif [ "$CLASS" = "EXECUTABLES" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700276 if [ "$ARGS" = "rootfs" ]; then
277 SRC="$SRC/rootfs"
278 if [ "$EXTRA" = "sbin" ]; then
279 SRC="$SRC/sbin"
280 printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)"
281 printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)"
282 fi
283 else
284 SRC="$SRC/bin"
285 fi
286 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
287 unset EXTENSION
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700288 else
Steve Kondika991cf12016-07-28 12:13:12 -0700289 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700290 fi
291 printf 'LOCAL_MODULE_TAGS := optional\n'
292 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
Hashbang1733b3a0e12016-08-28 20:38:45 -0400293 if [ "$CLASS" = "APPS" ]; then
294 printf 'LOCAL_DEX_PREOPT := false\n'
295 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700296 if [ ! -z "$EXTENSION" ]; then
297 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
298 fi
299 if [ "$EXTRA" = "priv-app" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700300 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
301 fi
302 if [ "$VENDOR_PKG" = "true" ]; then
303 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
304 fi
305 printf 'include $(BUILD_PREBUILT)\n\n'
306 done
307}
308
309#
310# write_product_packages:
311#
312# This function will create BUILD_PREBUILT entries in the
313# Android.mk and associated PRODUCT_PACKAGES list in the
314# product makefile for all files in the blob list which
315# start with a single dash (-) character.
316#
317function write_product_packages() {
318 PACKAGE_LIST=()
319
320 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
321
322 if [ "$COUNT" = "0" ]; then
323 return 0
324 fi
325
326 # Figure out what's 32-bit, what's 64-bit, and what's multilib
327 # I really should not be doing this in bash due to shitty array passing :(
328 local T_LIB32=( $(prefix_match "lib/") )
329 local T_LIB64=( $(prefix_match "lib64/") )
330 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
331 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700332 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700333
Steve Kondik03ce4002016-07-29 00:00:16 -0700334 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
335 write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
336 fi
337 if [ "${#LIB32[@]}" -gt "0" ]; then
338 write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK"
339 fi
340 if [ "${#LIB64[@]}" -gt "0" ]; then
341 write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK"
342 fi
343
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700344 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
345 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
346 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
347 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700348 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700349
350 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700351 write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700352 fi
353 if [ "${#V_LIB32[@]}" -gt "0" ]; then
Steve Kondik03ce4002016-07-29 00:00:16 -0700354 write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700355 fi
356 if [ "${#V_LIB64[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700357 write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700358 fi
359
360 # Apps
361 local APPS=( $(prefix_match "app/") )
362 if [ "${#APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700363 write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700364 fi
365 local PRIV_APPS=( $(prefix_match "priv-app/") )
366 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700367 write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700368 fi
369 local V_APPS=( $(prefix_match "vendor/app/") )
370 if [ "${#V_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700371 write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700372 fi
373 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
374 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700375 write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700376 fi
377
378 # Framework
379 local FRAMEWORK=( $(prefix_match "framework/") )
380 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700381 write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700382 fi
383
384 # Etc
385 local ETC=( $(prefix_match "etc/") )
386 if [ "${#ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700387 write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700388 fi
389 local V_ETC=( $(prefix_match "vendor/etc/") )
390 if [ "${#V_ETC[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700391 write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700392 fi
393
394 # Executables
395 local BIN=( $(prefix_match "bin/") )
396 if [ "${#BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700397 write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700398 fi
399 local V_BIN=( $(prefix_match "vendor/bin/") )
400 if [ "${#V_BIN[@]}" -gt "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700401 write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700402 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700403 local SBIN=( $(prefix_match "sbin/") )
404 if [ "${#SBIN[@]}" -gt "0" ]; then
405 write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK"
406 fi
407
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700408
409 # Actually write out the final PRODUCT_PACKAGES list
410 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
411
412 if [ "$PACKAGE_COUNT" -eq "0" ]; then
413 return 0
414 fi
415
416 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
417 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
418 local LINEEND=" \\"
419 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
420 LINEEND=""
421 fi
422 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
423 done
424}
425
426#
427# write_header:
428#
429# $1: file which will be written to
430#
431# writes out the copyright header with the current year.
432# note that this is not an append operation, and should
433# be executed first!
434#
435function write_header() {
436 YEAR=$(date +"%Y")
437
438 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
439
440 cat << EOF > $1
441# Copyright (C) $YEAR The CyanogenMod Project
442#
443# Licensed under the Apache License, Version 2.0 (the "License");
444# you may not use this file except in compliance with the License.
445# You may obtain a copy of the License at
446#
447# http://www.apache.org/licenses/LICENSE-2.0
448#
449# Unless required by applicable law or agreed to in writing, software
450# distributed under the License is distributed on an "AS IS" BASIS,
451# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
452# See the License for the specific language governing permissions and
453# limitations under the License.
454
455# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
456
457EOF
458}
459
460#
461# write_headers:
462#
463# $1: devices falling under common to be added to guard - optional
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400464# $2: custom guard - optional
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700465#
466# Calls write_header for each of the makefiles and creates
467# the initial path declaration and device guard for the
468# Android.mk
469#
470function write_headers() {
471 write_header "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400472
473 GUARD="$2"
474 if [ -z "$GUARD" ]; then
475 GUARD="TARGET_DEVICE"
476 fi
477
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700478 cat << EOF >> "$ANDROIDMK"
479LOCAL_PATH := \$(call my-dir)
480
481EOF
482 if [ "$COMMON" -ne 1 ]; then
483 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400484ifeq (\$($GUARD),$DEVICE)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700485
486EOF
487 else
488 if [ -z "$1" ]; then
489 echo "Argument with devices to be added to guard must be set!"
490 exit 1
491 fi
492 cat << EOF >> "$ANDROIDMK"
Rashed Abdel-Tawabd53bff12016-10-02 01:00:54 -0400493ifneq (\$(filter $1,\$($GUARD)),)
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700494
495EOF
496 fi
497
498 write_header "$BOARDMK"
499 write_header "$PRODUCTMK"
500}
501
502#
503# write_footers:
504#
505# Closes the inital guard and any other finalization tasks. Must
506# be called as the final step.
507#
508function write_footers() {
509 cat << EOF >> "$ANDROIDMK"
510endif
511EOF
512}
513
514# Return success if adb is up and not in recovery
515function _adb_connected {
516 {
Steve Kondik7561d192016-09-01 21:40:27 -0700517 if [[ "$(adb get-state)" == device ]]
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700518 then
519 return 0
520 fi
521 } 2>/dev/null
522
523 return 1
524};
525
526#
Bruno Martins3b96ba52016-07-27 15:00:05 +0100527# parse_file_list:
528#
529# $1: input file
530#
531# Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700532#
533function parse_file_list() {
Bruno Martins3b96ba52016-07-27 15:00:05 +0100534 if [ -z "$1" ]; then
535 echo "An input file is expected!"
536 exit 1
537 elif [ ! -f "$1" ]; then
538 echo "Input file "$1" does not exist!"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700539 exit 1
540 fi
541
542 PRODUCT_PACKAGES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700543 PRODUCT_PACKAGES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700544 PRODUCT_COPY_FILES_LIST=()
Steve Kondik48f8df82016-08-14 03:55:08 -0700545 PRODUCT_COPY_FILES_HASHES=()
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700546
547 while read -r line; do
548 if [ -z "$line" ]; then continue; fi
549
Steve Kondik48f8df82016-08-14 03:55:08 -0700550 # If the line has a pipe delimiter, a sha1 hash should follow.
551 # This indicates the file should be pinned and not overwritten
552 # when extracting files.
553 local SPLIT=(${line//\|/ })
554 local COUNT=${#SPLIT[@]}
555 local SPEC=${SPLIT[0]}
556 local HASH="x"
557 if [ "$COUNT" -gt "1" ]; then
558 HASH=${SPLIT[1]}
559 fi
560
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700561 # if line starts with a dash, it needs to be packaged
Steve Kondik48f8df82016-08-14 03:55:08 -0700562 if [[ "$SPEC" =~ ^- ]]; then
563 PRODUCT_PACKAGES_LIST+=("${SPEC#-}")
564 PRODUCT_PACKAGES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700565 else
Steve Kondik48f8df82016-08-14 03:55:08 -0700566 PRODUCT_COPY_FILES_LIST+=("$SPEC")
567 PRODUCT_COPY_FILES_HASHES+=("$HASH")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700568 fi
569
570 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
571}
572
573#
574# write_makefiles:
575#
576# $1: file containing the list of items to extract
577#
578# Calls write_product_copy_files and write_product_packages on
579# the given file and appends to the Android.mk as well as
580# the product makefile.
581#
582function write_makefiles() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700583 parse_file_list "$1"
584 write_product_copy_files
585 write_product_packages
586}
587
588#
Louis Popia516c2f2016-07-25 15:51:13 +0200589# append_firmware_calls_to_makefiles:
590#
591# Appends to Android.mk the calls to all images present in radio folder
592# (filesmap file used by releasetools to map firmware images should be kept in the device tree)
593#
594function append_firmware_calls_to_makefiles() {
595 cat << EOF >> "$ANDROIDMK"
596ifeq (\$(LOCAL_PATH)/radio, \$(wildcard \$(LOCAL_PATH)/radio))
597
598RADIO_FILES := \$(wildcard \$(LOCAL_PATH)/radio/*)
599\$(foreach f, \$(notdir \$(RADIO_FILES)), \\
600 \$(call add-radio-file,radio/\$(f)))
601\$(call add-radio-file,../../../device/$VENDOR/$DEVICE/radio/filesmap)
602
603endif
604
605EOF
606}
607
608#
Luca Stefani7f9fff22016-07-18 13:47:55 +0200609# get_file:
610#
611# $1: input file
612# $2: target file/folder
613# $3: source of the file (can be "adb" or a local folder)
614#
615# Silently extracts the input file to defined target
616# Returns success if file can be pulled from the device or found locally
617#
618function get_file() {
619 local SRC="$3"
620
621 if [ "$SRC" = "adb" ]; then
622 # try to pull
623 adb pull "$1" "$2" >/dev/null 2>&1 && return 0
624
625 return 1
626 else
627 # try to copy
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700628 cp -r "$SRC/$1" "$2" 2>/dev/null && return 0
Luca Stefani7f9fff22016-07-18 13:47:55 +0200629
630 return 1
631 fi
632};
633
634#
635# oat2dex:
636#
637# $1: extracted apk|jar (to check if deodex is required)
638# $2: odexed apk|jar to deodex
639# $3: source of the odexed apk|jar
640#
641# Convert apk|jar .odex in the corresposing classes.dex
642#
643function oat2dex() {
644 local CM_TARGET="$1"
645 local OEM_TARGET="$2"
646 local SRC="$3"
647 local TARGET=
648 local OAT=
649
650 if [ -z "$BAKSMALIJAR" ] || [ -z "$SMALIJAR" ]; then
651 export BAKSMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/baksmali.jar
652 export SMALIJAR="$CM_ROOT"/vendor/cm/build/tools/smali/smali.jar
653 fi
654
655 # Extract existing boot.oats to the temp folder
656 if [ -z "$ARCHES" ]; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700657 echo "Checking if system is odexed and locating boot.oats..."
Luca Stefani7f9fff22016-07-18 13:47:55 +0200658 for ARCH in "arm64" "arm" "x86_64" "x86"; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700659 mkdir -p "$TMPDIR/system/framework/$ARCH"
660 if get_file "system/framework/$ARCH/" "$TMPDIR/system/framework/" "$SRC"; then
Luca Stefani7f9fff22016-07-18 13:47:55 +0200661 ARCHES+="$ARCH "
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700662 else
663 rmdir "$TMPDIR/system/framework/$ARCH"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200664 fi
665 done
666 fi
667
668 if [ -z "$ARCHES" ]; then
669 FULLY_DEODEXED=1 && return 0 # system is fully deodexed, return
670 fi
671
Steve Kondik48f8df82016-08-14 03:55:08 -0700672 if [ ! -f "$CM_TARGET" ]; then
673 return;
674 fi
675
Luca Stefani7f9fff22016-07-18 13:47:55 +0200676 if grep "classes.dex" "$CM_TARGET" >/dev/null; then
677 return 0 # target apk|jar is already odexed, return
678 fi
679
680 for ARCH in $ARCHES; do
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700681 BOOTOAT="$TMPDIR/system/framework/$ARCH/boot.oat"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200682
683 local OAT="$(dirname "$OEM_TARGET")/oat/$ARCH/$(basename "$OEM_TARGET" ."${OEM_TARGET##*.}").odex"
684
685 if get_file "$OAT" "$TMPDIR" "$SRC"; then
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700686 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" "$TMPDIR/$(basename "$OAT")"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200687 elif [[ "$CM_TARGET" =~ .jar$ ]]; then
688 # try to extract classes.dex from boot.oat for framework jars
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700689 java -jar "$BAKSMALIJAR" deodex -o "$TMPDIR/dexout" -b "$BOOTOAT" -d "$TMPDIR" -e "/$OEM_TARGET" "$BOOTOAT"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200690 else
691 continue
692 fi
693
Sam Mortimer2e994ce2016-10-05 09:50:49 -0700694 java -jar "$SMALIJAR" assemble "$TMPDIR/dexout" -o "$TMPDIR/classes.dex" && break
Luca Stefani7f9fff22016-07-18 13:47:55 +0200695 done
696
697 rm -rf "$TMPDIR/dexout"
698}
699
700#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700701# init_adb_connection:
702#
703# Starts adb server and waits for the device
704#
705function init_adb_connection() {
706 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
707 if ! _adb_connected; then
708 echo "No device is online. Waiting for one..."
709 echo "Please connect USB and/or enable USB debugging"
710 until _adb_connected; do
711 sleep 1
712 done
713 echo "Device Found."
714 fi
715
716 # Retrieve IP and PORT info if we're using a TCP connection
717 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
718 | head -1 | awk '{print $1}')
719 adb root &> /dev/null
720 sleep 0.3
721 if [ -n "$TCPIPPORT" ]; then
722 # adb root just killed our connection
723 # so reconnect...
724 adb connect "$TCPIPPORT"
725 fi
726 adb wait-for-device &> /dev/null
727 sleep 0.3
728}
729
730#
Luca Stefani3a030122016-07-30 12:08:25 +0200731# fix_xml:
732#
733# $1: xml file to fix
734#
735function fix_xml() {
736 local XML="$1"
737 local TEMP_XML="$TMPDIR/`basename "$XML"`.temp"
738
739 grep '^<?xml version' "$XML" > "$TEMP_XML"
740 grep -v '^<?xml version' "$XML" >> "$TEMP_XML"
741
742 mv "$TEMP_XML" "$XML"
743}
744
745#
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700746# extract:
747#
748# $1: file containing the list of items to extract
749# $2: path to extracted system folder, or "adb" to extract from device
750#
751function extract() {
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700752 if [ -z "$OUTDIR" ]; then
753 echo "Output dir not set!"
754 exit 1
755 fi
756
757 parse_file_list "$1"
758
759 # Allow failing, so we can try $DEST and/or $FILE
760 set +e
761
762 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
Steve Kondik48f8df82016-08-14 03:55:08 -0700763 local HASHLIST=( ${PRODUCT_COPY_FILES_HASHES[@]} ${PRODUCT_PACKAGES_HASHES[@]} )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700764 local COUNT=${#FILELIST[@]}
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700765 local SRC="$2"
Steve Kondika991cf12016-07-28 12:13:12 -0700766 local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary
Steve Kondik48f8df82016-08-14 03:55:08 -0700767 local OUTPUT_TMP="$TMPDIR"/"$OUTDIR"/proprietary
768
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700769 if [ "$SRC" = "adb" ]; then
770 init_adb_connection
771 fi
772
773 if [ "$VENDOR_STATE" -eq "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700774 echo "Cleaning output directory ($OUTPUT_ROOT).."
Steve Kondik48f8df82016-08-14 03:55:08 -0700775 rm -rf "${OUTPUT_TMP:?}"
776 mkdir -p "${OUTPUT_TMP:?}"
777 mv "${OUTPUT_ROOT:?}/"* "${OUTPUT_TMP:?}/"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700778 VENDOR_STATE=1
779 fi
780
781 echo "Extracting $COUNT files in $1 from $SRC:"
782
783 for (( i=1; i<COUNT+1; i++ )); do
Steve Kondika991cf12016-07-28 12:13:12 -0700784
785 local FROM=$(target_file "${FILELIST[$i-1]}")
786 local ARGS=$(target_args "${FILELIST[$i-1]}")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700787 local SPLIT=(${FILELIST[$i-1]//:/ })
788 local FILE="${SPLIT[0]#-}"
Steve Kondika991cf12016-07-28 12:13:12 -0700789 local OUTPUT_DIR="$OUTPUT_ROOT"
Steve Kondik48f8df82016-08-14 03:55:08 -0700790 local TMP_DIR="$OUTPUT_TMP"
Steve Kondika991cf12016-07-28 12:13:12 -0700791 local TARGET=
792
793 if [ "$ARGS" = "rootfs" ]; then
794 TARGET="$FROM"
795 OUTPUT_DIR="$OUTPUT_DIR/rootfs"
Steve Kondik48f8df82016-08-14 03:55:08 -0700796 TMP_DIR="$TMP_DIR/rootfs"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700797 else
Steve Kondika991cf12016-07-28 12:13:12 -0700798 TARGET="system/$FROM"
799 FILE="system/$FILE"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700800 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700801
802 if [ "$SRC" = "adb" ]; then
803 printf ' - %s .. ' "/$TARGET"
804 else
805 printf ' - %s \n' "/$TARGET"
806 fi
807
808 local DIR=$(dirname "$FROM")
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700809 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
810 mkdir -p "$OUTPUT_DIR/$DIR"
811 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700812 local DEST="$OUTPUT_DIR/$FROM"
813
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700814 if [ "$SRC" = "adb" ]; then
815 # Try CM target first
Steve Kondika991cf12016-07-28 12:13:12 -0700816 adb pull "/$TARGET" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700817 # if file does not exist try OEM target
818 if [ "$?" != "0" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700819 adb pull "/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700820 fi
821 else
822 # Try OEM target first
Steve Kondik48f8df82016-08-14 03:55:08 -0700823 if [ -f "$SRC/$FILE" ]; then
824 cp "$SRC/$FILE" "$DEST"
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700825 # if file does not exist try CM target
Steve Kondik48f8df82016-08-14 03:55:08 -0700826 elif [ -f "$SRC/$TARGET" ]; then
Steve Kondika991cf12016-07-28 12:13:12 -0700827 cp "$SRC/$TARGET" "$DEST"
Steve Kondik48f8df82016-08-14 03:55:08 -0700828 else
829 printf ' !! file not found in source\n'
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700830 fi
831 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700832
Luca Stefani7f9fff22016-07-18 13:47:55 +0200833 if [ "$?" == "0" ]; then
834 # Deodex apk|jar if that's the case
835 if [[ "$FULLY_DEODEXED" -ne "1" && "$DEST" =~ .(apk|jar)$ ]]; then
836 oat2dex "$DEST" "$FILE" "$SRC"
837 if [ -f "$TMPDIR/classes.dex" ]; then
838 zip -gjq "$DEST" "$TMPDIR/classes.dex"
839 rm "$TMPDIR/classes.dex"
840 printf ' (updated %s from odex files)\n' "/$FILE"
841 fi
Luca Stefani3a030122016-07-30 12:08:25 +0200842 elif [[ "$DEST" =~ .xml$ ]]; then
843 fix_xml "$DEST"
Luca Stefani7f9fff22016-07-18 13:47:55 +0200844 fi
845 fi
846
Steve Kondik48f8df82016-08-14 03:55:08 -0700847 # Check pinned files
848 local HASH="${HASHLIST[$i-1]}"
Steve Kondik79fa59b2016-09-02 21:10:02 -0700849 if [ "$DISABLE_PINNING" != "1" ] && [ ! -z "$HASH" ] && [ "$HASH" != "x" ]; then
Steve Kondik48f8df82016-08-14 03:55:08 -0700850 local KEEP=""
851 local TMP="$TMP_DIR/$FROM"
852 if [ -f "$TMP" ]; then
853 if [ ! -f "$DEST" ]; then
854 KEEP="1"
855 else
856 local DEST_HASH=$(sha1sum "$DEST" | awk '{print $1}' )
857 if [ "$DEST_HASH" != "$HASH" ]; then
858 KEEP="1"
859 fi
860 fi
861 if [ "$KEEP" = "1" ]; then
862 local TMP_HASH=$(sha1sum "$TMP" | awk '{print $1}' )
863 if [ "$TMP_HASH" = "$HASH" ]; then
864 printf ' + (keeping pinned file with hash %s)\n' "$HASH"
865 cp -p "$TMP" "$DEST"
866 fi
867 fi
868 fi
Steve Kondika991cf12016-07-28 12:13:12 -0700869 fi
Steve Kondik48f8df82016-08-14 03:55:08 -0700870
871 if [ -f "$DEST" ]; then
872 local TYPE="${DIR##*/}"
873 if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then
874 chmod 755 "$DEST"
875 else
876 chmod 644 "$DEST"
877 fi
878 fi
879
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700880 done
881
882 # Don't allow failing
883 set -e
884}
Louis Popia516c2f2016-07-25 15:51:13 +0200885
886#
887# extract_firmware:
888#
889# $1: file containing the list of items to extract
890# $2: path to extracted radio folder
891#
892function extract_firmware() {
893 if [ -z "$OUTDIR" ]; then
894 echo "Output dir not set!"
895 exit 1
896 fi
897
898 parse_file_list "$1"
899
900 # Don't allow failing
901 set -e
902
903 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} )
904 local COUNT=${#FILELIST[@]}
905 local SRC="$2"
906 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/radio
907
908 if [ "$VENDOR_RADIO_STATE" -eq "0" ]; then
909 echo "Cleaning firmware output directory ($OUTPUT_DIR).."
910 rm -rf "${OUTPUT_DIR:?}/"*
911 VENDOR_RADIO_STATE=1
912 fi
913
914 echo "Extracting $COUNT files in $1 from $SRC:"
915
916 for (( i=1; i<COUNT+1; i++ )); do
917 local FILE="${FILELIST[$i-1]}"
918 printf ' - %s \n' "/radio/$FILE"
919
920 if [ ! -d "$OUTPUT_DIR" ]; then
921 mkdir -p "$OUTPUT_DIR"
922 fi
923 cp "$SRC/$FILE" "$OUTPUT_DIR/$FILE"
924 chmod 644 "$OUTPUT_DIR/$FILE"
925 done
926}