blob: e7bdd1b2c5c54a833ec09b10b92ba3f326762f56 [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=()
19PRODUCT_PACKAGES_LIST=()
20PACKAGE_LIST=()
21VENDOR_STATE=-1
22COMMON=-1
23
24#
25# setup_vendor
26#
27# $1: device name
28# $2: vendor name
29# $3: CM root directory
30# $4: is common device - optional, default to false
31# $5: cleanup - optional, default to true
32#
33# Must be called before any other functions can be used. This
34# sets up the internal state for a new vendor configuration.
35#
36function setup_vendor() {
37 local DEVICE="$1"
38 if [ -z "$DEVICE" ]; then
39 echo "\$DEVICE must be set before including this script!"
40 exit 1
41 fi
42
43 export VENDOR="$2"
44 if [ -z "$VENDOR" ]; then
45 echo "\$VENDOR must be set before including this script!"
46 exit 1
47 fi
48
49 export CM_ROOT="$3"
50 if [ ! -d "$CM_ROOT" ]; then
51 echo "\$CM_ROOT must be set and valid before including this script!"
52 exit 1
53 fi
54
55 export OUTDIR=vendor/"$VENDOR"/"$DEVICE"
56 if [ ! -d "$CM_ROOT/$OUTDIR" ]; then
57 mkdir -p "$CM_ROOT/$OUTDIR"
58 fi
59
60 export PRODUCTMK="$CM_ROOT"/"$OUTDIR"/"$DEVICE"-vendor.mk
61 export ANDROIDMK="$CM_ROOT"/"$OUTDIR"/Android.mk
62 export BOARDMK="$CM_ROOT"/"$OUTDIR"/BoardConfigVendor.mk
63
64 if [ "$4" == "true" ] || [ "$4" == "1" ]; then
65 COMMON=1
66 else
67 COMMON=0
68 fi
69
70 if [ "$5" == "true" ] || [ "$5" == "1" ]; then
71 VENDOR_STATE=1
72 else
73 VENDOR_STATE=0
74 fi
75}
76
77#
78# target_file:
79#
80# $1: colon delimited list
81#
82# Returns destination filename without args
83#
84function target_file() {
85 local LINE="$1"
86 local SPLIT=(${LINE//:/ })
87 local COUNT=${#SPLIT[@]}
88 if [ "$COUNT" -gt "1" ]; then
89 if [[ "${SPLIT[1]}" =~ .*/.* ]]; then
90 printf '%s\n' "${SPLIT[1]}"
91 return 0
92 fi
93 fi
94 printf '%s\n' "${SPLIT[0]}"
95}
96
97#
98# target_args:
99#
100# $1: colon delimited list
101#
102# Returns optional arguments (last value) for given target
103#
104function target_args() {
105 local LINE="$1"
106 local SPLIT=(${LINE//:/ })
107 local COUNT=${#SPLIT[@]}
108 if [ "$COUNT" -gt "1" ]; then
109 if [[ ! "${SPLIT[$COUNT-1]}" =~ .*/.* ]]; then
110 printf '%s\n' "${SPLIT[$COUNT-1]}"
111 fi
112 fi
113}
114
115#
116# prefix_match:
117#
118# $1: the prefix to match on
119#
120# Internal function which loops thru the packages list and returns a new
121# list containing the matched files with the prefix stripped away.
122#
123function prefix_match() {
124 local PREFIX="$1"
125 for FILE in "${PRODUCT_PACKAGES_LIST[@]}"; do
126 if [[ "$FILE" =~ ^"$PREFIX" ]]; then
127 printf '%s\n' "${FILE#$PREFIX}"
128 fi
129 done
130}
131
132#
133# write_product_copy_files:
134#
135# Creates the PRODUCT_COPY_FILES section in the product makefile for all
136# items in the list which do not start with a dash (-).
137#
138function write_product_copy_files() {
139 local COUNT=${#PRODUCT_COPY_FILES_LIST[@]}
140 local TARGET=
141 local FILE=
142 local LINEEND=
143
144 if [ "$COUNT" -eq "0" ]; then
145 return 0
146 fi
147
148 printf '%s\n' "PRODUCT_COPY_FILES += \\" >> "$PRODUCTMK"
149 for (( i=1; i<COUNT+1; i++ )); do
150 FILE="${PRODUCT_COPY_FILES_LIST[$i-1]}"
151 LINEEND=" \\"
152 if [ "$i" -eq "$COUNT" ]; then
153 LINEEND=""
154 fi
155
156 TARGET=$(target_file "$FILE")
157 printf ' %s/proprietary/%s:system/%s%s\n' \
158 "$OUTDIR" "$TARGET" "$TARGET" "$LINEEND" >> "$PRODUCTMK"
159 done
160 return 0
161}
162
163#
164# write_packages:
165#
166# $1: The LOCAL_MODULE_CLASS for the given module list
167# $2: "true" if this package is part of the vendor/ path
168# $3: "true" if this is a privileged module (only valid for APPS)
169# $4: The multilib mode, "32", "64", "both", or "none"
170# $5: Name of the array holding the target list
171#
172# Internal function which writes out the BUILD_PREBUILT stanzas
173# for all modules in the list. This is called by write_product_packages
174# after the modules are categorized.
175#
176function write_packages() {
177
178 local CLASS="$1"
179 local VENDOR_PKG="$2"
180 local PRIVILEGED="$3"
181 local MULTILIB="$4"
182
183 # Yes, this is a horrible hack - we create a new array using indirection
184 local ARR_NAME="$5[@]"
185 local FILELIST=("${!ARR_NAME}")
186
187 local FILE=
188 local ARGS=
189 local BASENAME=
190 local EXTENSION=
191 local PKGNAME=
192 local SRC=
193
194 for P in "${FILELIST[@]}"; do
195 FILE=$(target_file "$P")
196 ARGS=$(target_args "$P")
197
198 BASENAME=$(basename "$FILE")
199 EXTENSION=${BASENAME##*.}
200 PKGNAME=${BASENAME%.*}
201
202 # Add to final package list
203 PACKAGE_LIST+=("$PKGNAME")
204
205 SRC="proprietary"
206 if [ "$VENDOR_PKG" = "true" ]; then
207 SRC+="/vendor"
208 fi
209
210 printf 'include $(CLEAR_VARS)\n'
211 printf 'LOCAL_MODULE := %s\n' "$PKGNAME"
212 printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR"
213 if [ "$CLASS" = "SHARED_LIBRARIES" ]; then
214 if [ "$MULTILIB" = "both" ]; then
215 printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE"
216 printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE"
217 #if [ "$VENDOR_PKG" = "true" ]; then
218 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
219 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)"
220 #else
221 # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)"
222 # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)"
223 #fi
224 elif [ "$MULTILIB" = "64" ]; then
225 printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE"
226 else
227 printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE"
228 fi
229 if [ "$MULTILIB" != "none" ]; then
230 printf 'LOCAL_MULTILIB := %s\n' "$MULTILIB"
231 fi
232 elif [ "$CLASS" = "APPS" ]; then
233 if [ -z "$ARGS" ]; then
234 if [ "$PRIVILEGED" = "true" ]; then
235 SRC="$SRC/priv-app"
236 else
237 SRC="$SRC/app"
238 fi
239 fi
240 printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE"
241 local CERT=platform
242 if [ ! -z "$ARGS" ]; then
243 CERT="$ARGS"
244 fi
245 printf 'LOCAL_CERTIFICATE := %s\n' "$CERT"
246 elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then
247 printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE"
248 elif [ "$CLASS" = "ETC" ]; then
249 printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE"
250 elif [ "$CLASS" = "EXECUTABLES" ]; then
251 printf 'LOCAL_SRC_FILES := %s/bin/%s\n' "$SRC" "$FILE"
252 else
253 printf 'LOCAL_SRC_FILES := %s/%s' "$SRC" "$FILE"
254 fi
255 printf 'LOCAL_MODULE_TAGS := optional\n'
256 printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS"
257 printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION"
258 if [ "$PRIVILEGED" = "true" ]; then
259 printf 'LOCAL_PRIVILEGED_MODULE := true\n'
260 fi
261 if [ "$VENDOR_PKG" = "true" ]; then
262 printf 'LOCAL_PROPRIETARY_MODULE := true\n'
263 fi
264 printf 'include $(BUILD_PREBUILT)\n\n'
265 done
266}
267
268#
269# write_product_packages:
270#
271# This function will create BUILD_PREBUILT entries in the
272# Android.mk and associated PRODUCT_PACKAGES list in the
273# product makefile for all files in the blob list which
274# start with a single dash (-) character.
275#
276function write_product_packages() {
277 PACKAGE_LIST=()
278
279 local COUNT=${#PRODUCT_PACKAGES_LIST[@]}
280
281 if [ "$COUNT" = "0" ]; then
282 return 0
283 fi
284
285 # Figure out what's 32-bit, what's 64-bit, and what's multilib
286 # I really should not be doing this in bash due to shitty array passing :(
287 local T_LIB32=( $(prefix_match "lib/") )
288 local T_LIB64=( $(prefix_match "lib64/") )
289 local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) )
290 local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700291 local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700292
293 if [ "${#MULTILIBS[@]}" -gt "0" ]; then
294 write_packages "SHARED_LIBRARIES" "false" "false" "both" "MULTILIBS" >> "$ANDROIDMK"
295 fi
296 if [ "${#LIB32[@]}" -gt "0" ]; then
297 write_packages "SHARED_LIBRARIES" "false" "false" "32" "LIB32" >> "$ANDROIDMK"
298 fi
Steve Kondik60ef86d2016-07-20 20:03:40 -0700299 if [ "${#LIB64[@]}" -gt "0" ]; then
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700300 write_packages "SHARED_LIBRARIES" "false" "false" "64" "LIB64" >> "$ANDROIDMK"
301 fi
302
303 local T_V_LIB32=( $(prefix_match "vendor/lib/") )
304 local T_V_LIB64=( $(prefix_match "vendor/lib64/") )
305 local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) )
306 local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik60ef86d2016-07-20 20:03:40 -0700307 local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) )
Steve Kondik4e2aaab2016-07-15 10:39:58 -0700308
309 if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then
310 write_packages "SHARED_LIBRARIES" "true" "false" "both" "V_MULTILIBS" >> "$ANDROIDMK"
311 fi
312 if [ "${#V_LIB32[@]}" -gt "0" ]; then
313 write_packages "SHARED_LIBRARIES" "true" "false" "32" "V_LIB32" >> "$ANDROIDMK"
314 fi
315 if [ "${#V_LIB64[@]}" -gt "0" ]; then
316 write_packages "SHARED_LIBRARIES" "true" "false" "64" "V_LIB64" >> "$ANDROIDMK"
317 fi
318
319 # Apps
320 local APPS=( $(prefix_match "app/") )
321 if [ "${#APPS[@]}" -gt "0" ]; then
322 write_packages "APPS" "false" "false" "none" "APPS" >> "$ANDROIDMK"
323 fi
324 local PRIV_APPS=( $(prefix_match "priv-app/") )
325 if [ "${#PRIV_APPS[@]}" -gt "0" ]; then
326 write_packages "APPS" "false" "true" "none" "PRIV_APPS" >> "$ANDROIDMK"
327 fi
328 local V_APPS=( $(prefix_match "vendor/app/") )
329 if [ "${#V_APPS[@]}" -gt "0" ]; then
330 write_packages "APPS" "true" "false" "none" "V_APPS" >> "$ANDROIDMK"
331 fi
332 local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") )
333 if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then
334 write_packages "APPS" "true" "true" "none" "V_PRIV_APPS" >> "$ANDROIDMK"
335 fi
336
337 # Framework
338 local FRAMEWORK=( $(prefix_match "framework/") )
339 if [ "${#FRAMEWORK[@]}" -gt "0" ]; then
340 write_packages "JAVA_LIBRARIES" "false" "false" "none" "FRAMEWORK" >> "$ANDROIDMK"
341 fi
342
343 # Etc
344 local ETC=( $(prefix_match "etc/") )
345 if [ "${#ETC[@]}" -gt "0" ]; then
346 write_packages "ETC" "false" "false" "none" "ETC" >> "$ANDROIDMK"
347 fi
348 local V_ETC=( $(prefix_match "vendor/etc/") )
349 if [ "${#V_ETC[@]}" -gt "0" ]; then
350 write_packages "ETC" "true" "false" "none" "V_ETC" >> "$ANDROIDMK"
351 fi
352
353 # Executables
354 local BIN=( $(prefix_match "bin/") )
355 if [ "${#BIN[@]}" -gt "0" ]; then
356 write_packages "EXECUTABLES" "false" "false" "none" "BIN" >> "$ANDROIDMK"
357 fi
358 local V_BIN=( $(prefix_match "vendor/bin/") )
359 if [ "${#V_BIN[@]}" -gt "0" ]; then
360 write_packages "EXECUTABLES" "true" "false" "none" "V_BIN" >> "$ANDROIDMK"
361 fi
362
363 # Actually write out the final PRODUCT_PACKAGES list
364 local PACKAGE_COUNT=${#PACKAGE_LIST[@]}
365
366 if [ "$PACKAGE_COUNT" -eq "0" ]; then
367 return 0
368 fi
369
370 printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK"
371 for (( i=1; i<PACKAGE_COUNT+1; i++ )); do
372 local LINEEND=" \\"
373 if [ "$i" -eq "$PACKAGE_COUNT" ]; then
374 LINEEND=""
375 fi
376 printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK"
377 done
378}
379
380#
381# write_header:
382#
383# $1: file which will be written to
384#
385# writes out the copyright header with the current year.
386# note that this is not an append operation, and should
387# be executed first!
388#
389function write_header() {
390 YEAR=$(date +"%Y")
391
392 [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON"
393
394 cat << EOF > $1
395# Copyright (C) $YEAR The CyanogenMod Project
396#
397# Licensed under the Apache License, Version 2.0 (the "License");
398# you may not use this file except in compliance with the License.
399# You may obtain a copy of the License at
400#
401# http://www.apache.org/licenses/LICENSE-2.0
402#
403# Unless required by applicable law or agreed to in writing, software
404# distributed under the License is distributed on an "AS IS" BASIS,
405# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
406# See the License for the specific language governing permissions and
407# limitations under the License.
408
409# This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh
410
411EOF
412}
413
414#
415# write_headers:
416#
417# $1: devices falling under common to be added to guard - optional
418#
419# Calls write_header for each of the makefiles and creates
420# the initial path declaration and device guard for the
421# Android.mk
422#
423function write_headers() {
424 write_header "$ANDROIDMK"
425 cat << EOF >> "$ANDROIDMK"
426LOCAL_PATH := \$(call my-dir)
427
428EOF
429 if [ "$COMMON" -ne 1 ]; then
430 cat << EOF >> "$ANDROIDMK"
431ifeq (\$(TARGET_DEVICE),$DEVICE)
432
433EOF
434 else
435 if [ -z "$1" ]; then
436 echo "Argument with devices to be added to guard must be set!"
437 exit 1
438 fi
439 cat << EOF >> "$ANDROIDMK"
440ifneq (\$(filter $1,\$(TARGET_DEVICE)),)
441
442EOF
443 fi
444
445 write_header "$BOARDMK"
446 write_header "$PRODUCTMK"
447}
448
449#
450# write_footers:
451#
452# Closes the inital guard and any other finalization tasks. Must
453# be called as the final step.
454#
455function write_footers() {
456 cat << EOF >> "$ANDROIDMK"
457endif
458EOF
459}
460
461# Return success if adb is up and not in recovery
462function _adb_connected {
463 {
464 if [[ "$(adb get-state)" == device &&
465 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
466 then
467 return 0
468 fi
469 } 2>/dev/null
470
471 return 1
472};
473
474#
475# parse_file_list
476#
477function parse_file_list() {
478 if [ ! -e "$1" ]; then
479 echo "$1 does not exist!"
480 exit 1
481 fi
482
483 PRODUCT_PACKAGES_LIST=()
484 PRODUCT_COPY_FILES_LIST=()
485
486 while read -r line; do
487 if [ -z "$line" ]; then continue; fi
488
489 # if line starts with a dash, it needs to be packaged
490 if [[ "$line" =~ ^- ]]; then
491 PRODUCT_PACKAGES_LIST+=("${line#-}")
492 else
493 PRODUCT_COPY_FILES_LIST+=("$line")
494 fi
495
496 done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq)
497}
498
499#
500# write_makefiles:
501#
502# $1: file containing the list of items to extract
503#
504# Calls write_product_copy_files and write_product_packages on
505# the given file and appends to the Android.mk as well as
506# the product makefile.
507#
508function write_makefiles() {
509 if [ ! -e "$1" ]; then
510 echo "$1 does not exist!"
511 exit 1
512 fi
513 parse_file_list "$1"
514 write_product_copy_files
515 write_product_packages
516}
517
518#
519# init_adb_connection:
520#
521# Starts adb server and waits for the device
522#
523function init_adb_connection() {
524 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
525 if ! _adb_connected; then
526 echo "No device is online. Waiting for one..."
527 echo "Please connect USB and/or enable USB debugging"
528 until _adb_connected; do
529 sleep 1
530 done
531 echo "Device Found."
532 fi
533
534 # Retrieve IP and PORT info if we're using a TCP connection
535 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
536 | head -1 | awk '{print $1}')
537 adb root &> /dev/null
538 sleep 0.3
539 if [ -n "$TCPIPPORT" ]; then
540 # adb root just killed our connection
541 # so reconnect...
542 adb connect "$TCPIPPORT"
543 fi
544 adb wait-for-device &> /dev/null
545 sleep 0.3
546}
547
548#
549# extract:
550#
551# $1: file containing the list of items to extract
552# $2: path to extracted system folder, or "adb" to extract from device
553#
554function extract() {
555 if [ ! -e "$1" ]; then
556 echo "$1 does not exist!"
557 exit 1
558 fi
559
560 if [ -z "$OUTDIR" ]; then
561 echo "Output dir not set!"
562 exit 1
563 fi
564
565 parse_file_list "$1"
566
567 # Allow failing, so we can try $DEST and/or $FILE
568 set +e
569
570 local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} )
571 local COUNT=${#FILELIST[@]}
572 local FILE=
573 local DEST=
574 local SRC="$2"
575 local OUTPUT_DIR="$CM_ROOT"/"$OUTDIR"/proprietary
576 local DIR=
577
578 if [ "$SRC" = "adb" ]; then
579 init_adb_connection
580 fi
581
582 if [ "$VENDOR_STATE" -eq "0" ]; then
583 echo "Cleaning output directory ($OUTPUT_DIR).."
584 rm -rf "${OUTPUT_DIR:?}/"*
585 VENDOR_STATE=1
586 fi
587
588 echo "Extracting $COUNT files in $1 from $SRC:"
589
590 for (( i=1; i<COUNT+1; i++ )); do
591 local SPLIT=(${FILELIST[$i-1]//:/ })
592 local FILE="${SPLIT[0]#-}"
593 local DEST="${SPLIT[1]}"
594 if [ -z "$DEST" ]; then
595 DEST="$FILE"
596 fi
597 if [ "$SRC" = "adb" ]; then
598 printf ' - %s .. ' "/system/$FILE"
599 else
600 printf ' - %s \n' "/system/$FILE"
601 fi
602 DIR=$(dirname "$DEST")
603 if [ ! -d "$OUTPUT_DIR/$DIR" ]; then
604 mkdir -p "$OUTPUT_DIR/$DIR"
605 fi
606 if [ "$SRC" = "adb" ]; then
607 # Try CM target first
608 adb pull "/system/$DEST" "$OUTPUT_DIR/$DEST"
609 # if file does not exist try OEM target
610 if [ "$?" != "0" ]; then
611 adb pull "/system/$FILE" "$OUTPUT_DIR/$DEST"
612 fi
613 else
614 # Try OEM target first
615 cp "$SRC/system/$FILE" "$OUTPUT_DIR/$DEST"
616 # if file does not exist try CM target
617 if [ "$?" != "0" ]; then
618 cp "$SRC/system/$DEST" "$OUTPUT_DIR/$DEST"
619 fi
620 fi
621 chmod 644 "$OUTPUT_DIR/$DEST"
622 done
623
624 # Don't allow failing
625 set -e
626}