Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 1 | #!/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 | |
| 18 | PRODUCT_COPY_FILES_LIST=() |
| 19 | PRODUCT_PACKAGES_LIST=() |
| 20 | PACKAGE_LIST=() |
| 21 | VENDOR_STATE=-1 |
| 22 | COMMON=-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 | # |
| 36 | function 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 | # |
| 84 | function 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 | # |
| 104 | function 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 | # |
| 123 | function 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 | # |
| 138 | function 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 |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 168 | # $3: type-specific extra flags |
| 169 | # $4: Name of the array holding the target list |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 170 | # |
| 171 | # Internal function which writes out the BUILD_PREBUILT stanzas |
| 172 | # for all modules in the list. This is called by write_product_packages |
| 173 | # after the modules are categorized. |
| 174 | # |
| 175 | function write_packages() { |
| 176 | |
| 177 | local CLASS="$1" |
| 178 | local VENDOR_PKG="$2" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 179 | local EXTRA="$3" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 180 | |
| 181 | # Yes, this is a horrible hack - we create a new array using indirection |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 182 | local ARR_NAME="$4[@]" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 183 | local FILELIST=("${!ARR_NAME}") |
| 184 | |
| 185 | local FILE= |
| 186 | local ARGS= |
| 187 | local BASENAME= |
| 188 | local EXTENSION= |
| 189 | local PKGNAME= |
| 190 | local SRC= |
| 191 | |
| 192 | for P in "${FILELIST[@]}"; do |
| 193 | FILE=$(target_file "$P") |
| 194 | ARGS=$(target_args "$P") |
| 195 | |
| 196 | BASENAME=$(basename "$FILE") |
| 197 | EXTENSION=${BASENAME##*.} |
| 198 | PKGNAME=${BASENAME%.*} |
| 199 | |
| 200 | # Add to final package list |
| 201 | PACKAGE_LIST+=("$PKGNAME") |
| 202 | |
| 203 | SRC="proprietary" |
| 204 | if [ "$VENDOR_PKG" = "true" ]; then |
| 205 | SRC+="/vendor" |
| 206 | fi |
| 207 | |
| 208 | printf 'include $(CLEAR_VARS)\n' |
| 209 | printf 'LOCAL_MODULE := %s\n' "$PKGNAME" |
| 210 | printf 'LOCAL_MODULE_OWNER := %s\n' "$VENDOR" |
| 211 | if [ "$CLASS" = "SHARED_LIBRARIES" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 212 | if [ "$EXTRA" = "both" ]; then |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 213 | printf 'LOCAL_SRC_FILES_64 := %s/lib64/%s\n' "$SRC" "$FILE" |
| 214 | printf 'LOCAL_SRC_FILES_32 := %s/lib/%s\n' "$SRC" "$FILE" |
| 215 | #if [ "$VENDOR_PKG" = "true" ]; then |
| 216 | # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_VENDOR_SHARED_LIBRARIES)" |
| 217 | # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_VENDOR_SHARED_LIBRARIES)" |
| 218 | #else |
| 219 | # echo "LOCAL_MODULE_PATH_64 := \$(TARGET_OUT_SHARED_LIBRARIES)" |
| 220 | # echo "LOCAL_MODULE_PATH_32 := \$(2ND_TARGET_OUT_SHARED_LIBRARIES)" |
| 221 | #fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 222 | elif [ "$EXTRA" = "64" ]; then |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 223 | printf 'LOCAL_SRC_FILES := %s/lib64/%s\n' "$SRC" "$FILE" |
| 224 | else |
| 225 | printf 'LOCAL_SRC_FILES := %s/lib/%s\n' "$SRC" "$FILE" |
| 226 | fi |
Steve Kondik | 03ce400 | 2016-07-29 00:00:16 -0700 | [diff] [blame] | 227 | if [ "$EXTRA" != "none" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 228 | printf 'LOCAL_MULTILIB := %s\n' "$EXTRA" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 229 | fi |
| 230 | elif [ "$CLASS" = "APPS" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 231 | if [ "$EXTRA" = "priv-app" ]; then |
| 232 | SRC="$SRC/priv-app" |
| 233 | else |
| 234 | SRC="$SRC/app" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 235 | fi |
| 236 | printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE" |
| 237 | local CERT=platform |
| 238 | if [ ! -z "$ARGS" ]; then |
| 239 | CERT="$ARGS" |
| 240 | fi |
| 241 | printf 'LOCAL_CERTIFICATE := %s\n' "$CERT" |
| 242 | elif [ "$CLASS" = "JAVA_LIBRARIES" ]; then |
| 243 | printf 'LOCAL_SRC_FILES := %s/framework/%s\n' "$SRC" "$FILE" |
| 244 | elif [ "$CLASS" = "ETC" ]; then |
| 245 | printf 'LOCAL_SRC_FILES := %s/etc/%s\n' "$SRC" "$FILE" |
| 246 | elif [ "$CLASS" = "EXECUTABLES" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 247 | if [ "$ARGS" = "rootfs" ]; then |
| 248 | SRC="$SRC/rootfs" |
| 249 | if [ "$EXTRA" = "sbin" ]; then |
| 250 | SRC="$SRC/sbin" |
| 251 | printf '%s\n' "LOCAL_MODULE_PATH := \$(TARGET_ROOT_OUT_SBIN)" |
| 252 | printf '%s\n' "LOCAL_UNSTRIPPED_PATH := \$(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)" |
| 253 | fi |
| 254 | else |
| 255 | SRC="$SRC/bin" |
| 256 | fi |
| 257 | printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE" |
| 258 | unset EXTENSION |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 259 | else |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 260 | printf 'LOCAL_SRC_FILES := %s/%s\n' "$SRC" "$FILE" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 261 | fi |
| 262 | printf 'LOCAL_MODULE_TAGS := optional\n' |
| 263 | printf 'LOCAL_MODULE_CLASS := %s\n' "$CLASS" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 264 | if [ ! -z "$EXTENSION" ]; then |
| 265 | printf 'LOCAL_MODULE_SUFFIX := .%s\n' "$EXTENSION" |
| 266 | fi |
| 267 | if [ "$EXTRA" = "priv-app" ]; then |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 268 | printf 'LOCAL_PRIVILEGED_MODULE := true\n' |
| 269 | fi |
| 270 | if [ "$VENDOR_PKG" = "true" ]; then |
| 271 | printf 'LOCAL_PROPRIETARY_MODULE := true\n' |
| 272 | fi |
| 273 | printf 'include $(BUILD_PREBUILT)\n\n' |
| 274 | done |
| 275 | } |
| 276 | |
| 277 | # |
| 278 | # write_product_packages: |
| 279 | # |
| 280 | # This function will create BUILD_PREBUILT entries in the |
| 281 | # Android.mk and associated PRODUCT_PACKAGES list in the |
| 282 | # product makefile for all files in the blob list which |
| 283 | # start with a single dash (-) character. |
| 284 | # |
| 285 | function write_product_packages() { |
| 286 | PACKAGE_LIST=() |
| 287 | |
| 288 | local COUNT=${#PRODUCT_PACKAGES_LIST[@]} |
| 289 | |
| 290 | if [ "$COUNT" = "0" ]; then |
| 291 | return 0 |
| 292 | fi |
| 293 | |
| 294 | # Figure out what's 32-bit, what's 64-bit, and what's multilib |
| 295 | # I really should not be doing this in bash due to shitty array passing :( |
| 296 | local T_LIB32=( $(prefix_match "lib/") ) |
| 297 | local T_LIB64=( $(prefix_match "lib64/") ) |
| 298 | local MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${T_LIB64[@]}")) ) |
| 299 | local LIB32=( $(comm -23 <(printf '%s\n' "${T_LIB32[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) ) |
Steve Kondik | 60ef86d | 2016-07-20 20:03:40 -0700 | [diff] [blame] | 300 | local LIB64=( $(comm -23 <(printf '%s\n' "${T_LIB64[@]}") <(printf '%s\n' "${MULTILIBS[@]}")) ) |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 301 | |
Steve Kondik | 03ce400 | 2016-07-29 00:00:16 -0700 | [diff] [blame] | 302 | if [ "${#MULTILIBS[@]}" -gt "0" ]; then |
| 303 | write_packages "SHARED_LIBRARIES" "false" "both" "MULTILIBS" >> "$ANDROIDMK" |
| 304 | fi |
| 305 | if [ "${#LIB32[@]}" -gt "0" ]; then |
| 306 | write_packages "SHARED_LIBRARIES" "false" "32" "LIB32" >> "$ANDROIDMK" |
| 307 | fi |
| 308 | if [ "${#LIB64[@]}" -gt "0" ]; then |
| 309 | write_packages "SHARED_LIBRARIES" "false" "64" "LIB64" >> "$ANDROIDMK" |
| 310 | fi |
| 311 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 312 | local T_V_LIB32=( $(prefix_match "vendor/lib/") ) |
| 313 | local T_V_LIB64=( $(prefix_match "vendor/lib64/") ) |
| 314 | local V_MULTILIBS=( $(comm -12 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${T_V_LIB64[@]}")) ) |
| 315 | local V_LIB32=( $(comm -23 <(printf '%s\n' "${T_V_LIB32[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) ) |
Steve Kondik | 60ef86d | 2016-07-20 20:03:40 -0700 | [diff] [blame] | 316 | local V_LIB64=( $(comm -23 <(printf '%s\n' "${T_V_LIB64[@]}") <(printf '%s\n' "${V_MULTILIBS[@]}")) ) |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 317 | |
| 318 | if [ "${#V_MULTILIBS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 319 | write_packages "SHARED_LIBRARIES" "true" "both" "V_MULTILIBS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 320 | fi |
| 321 | if [ "${#V_LIB32[@]}" -gt "0" ]; then |
Steve Kondik | 03ce400 | 2016-07-29 00:00:16 -0700 | [diff] [blame] | 322 | write_packages "SHARED_LIBRARIES" "true" "32" "V_LIB32" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 323 | fi |
| 324 | if [ "${#V_LIB64[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 325 | write_packages "SHARED_LIBRARIES" "true" "64" "V_LIB64" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 326 | fi |
| 327 | |
| 328 | # Apps |
| 329 | local APPS=( $(prefix_match "app/") ) |
| 330 | if [ "${#APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 331 | write_packages "APPS" "false" "" "APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 332 | fi |
| 333 | local PRIV_APPS=( $(prefix_match "priv-app/") ) |
| 334 | if [ "${#PRIV_APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 335 | write_packages "APPS" "false" "priv-app" "PRIV_APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 336 | fi |
| 337 | local V_APPS=( $(prefix_match "vendor/app/") ) |
| 338 | if [ "${#V_APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 339 | write_packages "APPS" "true" "" "V_APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 340 | fi |
| 341 | local V_PRIV_APPS=( $(prefix_match "vendor/priv-app/") ) |
| 342 | if [ "${#V_PRIV_APPS[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 343 | write_packages "APPS" "true" "priv-app" "V_PRIV_APPS" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 344 | fi |
| 345 | |
| 346 | # Framework |
| 347 | local FRAMEWORK=( $(prefix_match "framework/") ) |
| 348 | if [ "${#FRAMEWORK[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 349 | write_packages "JAVA_LIBRARIES" "false" "" "FRAMEWORK" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 350 | fi |
| 351 | |
| 352 | # Etc |
| 353 | local ETC=( $(prefix_match "etc/") ) |
| 354 | if [ "${#ETC[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 355 | write_packages "ETC" "false" "" "ETC" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 356 | fi |
| 357 | local V_ETC=( $(prefix_match "vendor/etc/") ) |
| 358 | if [ "${#V_ETC[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 359 | write_packages "ETC" "false" "" "V_ETC" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 360 | fi |
| 361 | |
| 362 | # Executables |
| 363 | local BIN=( $(prefix_match "bin/") ) |
| 364 | if [ "${#BIN[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 365 | write_packages "EXECUTABLES" "false" "" "BIN" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 366 | fi |
| 367 | local V_BIN=( $(prefix_match "vendor/bin/") ) |
| 368 | if [ "${#V_BIN[@]}" -gt "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 369 | write_packages "EXECUTABLES" "true" "" "V_BIN" >> "$ANDROIDMK" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 370 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 371 | local SBIN=( $(prefix_match "sbin/") ) |
| 372 | if [ "${#SBIN[@]}" -gt "0" ]; then |
| 373 | write_packages "EXECUTABLES" "false" "sbin" "SBIN" >> "$ANDROIDMK" |
| 374 | fi |
| 375 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 376 | |
| 377 | # Actually write out the final PRODUCT_PACKAGES list |
| 378 | local PACKAGE_COUNT=${#PACKAGE_LIST[@]} |
| 379 | |
| 380 | if [ "$PACKAGE_COUNT" -eq "0" ]; then |
| 381 | return 0 |
| 382 | fi |
| 383 | |
| 384 | printf '\n%s\n' "PRODUCT_PACKAGES += \\" >> "$PRODUCTMK" |
| 385 | for (( i=1; i<PACKAGE_COUNT+1; i++ )); do |
| 386 | local LINEEND=" \\" |
| 387 | if [ "$i" -eq "$PACKAGE_COUNT" ]; then |
| 388 | LINEEND="" |
| 389 | fi |
| 390 | printf ' %s%s\n' "${PACKAGE_LIST[$i-1]}" "$LINEEND" >> "$PRODUCTMK" |
| 391 | done |
| 392 | } |
| 393 | |
| 394 | # |
| 395 | # write_header: |
| 396 | # |
| 397 | # $1: file which will be written to |
| 398 | # |
| 399 | # writes out the copyright header with the current year. |
| 400 | # note that this is not an append operation, and should |
| 401 | # be executed first! |
| 402 | # |
| 403 | function write_header() { |
| 404 | YEAR=$(date +"%Y") |
| 405 | |
| 406 | [ "$COMMON" -eq 1 ] && local DEVICE="$DEVICE_COMMON" |
| 407 | |
| 408 | cat << EOF > $1 |
| 409 | # Copyright (C) $YEAR The CyanogenMod Project |
| 410 | # |
| 411 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 412 | # you may not use this file except in compliance with the License. |
| 413 | # You may obtain a copy of the License at |
| 414 | # |
| 415 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 416 | # |
| 417 | # Unless required by applicable law or agreed to in writing, software |
| 418 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 419 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 420 | # See the License for the specific language governing permissions and |
| 421 | # limitations under the License. |
| 422 | |
| 423 | # This file is generated by device/$VENDOR/$DEVICE/setup-makefiles.sh |
| 424 | |
| 425 | EOF |
| 426 | } |
| 427 | |
| 428 | # |
| 429 | # write_headers: |
| 430 | # |
| 431 | # $1: devices falling under common to be added to guard - optional |
| 432 | # |
| 433 | # Calls write_header for each of the makefiles and creates |
| 434 | # the initial path declaration and device guard for the |
| 435 | # Android.mk |
| 436 | # |
| 437 | function write_headers() { |
| 438 | write_header "$ANDROIDMK" |
| 439 | cat << EOF >> "$ANDROIDMK" |
| 440 | LOCAL_PATH := \$(call my-dir) |
| 441 | |
| 442 | EOF |
| 443 | if [ "$COMMON" -ne 1 ]; then |
| 444 | cat << EOF >> "$ANDROIDMK" |
| 445 | ifeq (\$(TARGET_DEVICE),$DEVICE) |
| 446 | |
| 447 | EOF |
| 448 | else |
| 449 | if [ -z "$1" ]; then |
| 450 | echo "Argument with devices to be added to guard must be set!" |
| 451 | exit 1 |
| 452 | fi |
| 453 | cat << EOF >> "$ANDROIDMK" |
| 454 | ifneq (\$(filter $1,\$(TARGET_DEVICE)),) |
| 455 | |
| 456 | EOF |
| 457 | fi |
| 458 | |
| 459 | write_header "$BOARDMK" |
| 460 | write_header "$PRODUCTMK" |
| 461 | } |
| 462 | |
| 463 | # |
| 464 | # write_footers: |
| 465 | # |
| 466 | # Closes the inital guard and any other finalization tasks. Must |
| 467 | # be called as the final step. |
| 468 | # |
| 469 | function write_footers() { |
| 470 | cat << EOF >> "$ANDROIDMK" |
| 471 | endif |
| 472 | EOF |
| 473 | } |
| 474 | |
| 475 | # Return success if adb is up and not in recovery |
| 476 | function _adb_connected { |
| 477 | { |
| 478 | if [[ "$(adb get-state)" == device && |
| 479 | "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]] |
| 480 | then |
| 481 | return 0 |
| 482 | fi |
| 483 | } 2>/dev/null |
| 484 | |
| 485 | return 1 |
| 486 | }; |
| 487 | |
| 488 | # |
Bruno Martins | 3b96ba5 | 2016-07-27 15:00:05 +0100 | [diff] [blame] | 489 | # parse_file_list: |
| 490 | # |
| 491 | # $1: input file |
| 492 | # |
| 493 | # Sets PRODUCT_PACKAGES and PRODUCT_COPY_FILES while parsing the input file |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 494 | # |
| 495 | function parse_file_list() { |
Bruno Martins | 3b96ba5 | 2016-07-27 15:00:05 +0100 | [diff] [blame] | 496 | if [ -z "$1" ]; then |
| 497 | echo "An input file is expected!" |
| 498 | exit 1 |
| 499 | elif [ ! -f "$1" ]; then |
| 500 | echo "Input file "$1" does not exist!" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 501 | exit 1 |
| 502 | fi |
| 503 | |
| 504 | PRODUCT_PACKAGES_LIST=() |
| 505 | PRODUCT_COPY_FILES_LIST=() |
| 506 | |
| 507 | while read -r line; do |
| 508 | if [ -z "$line" ]; then continue; fi |
| 509 | |
| 510 | # if line starts with a dash, it needs to be packaged |
| 511 | if [[ "$line" =~ ^- ]]; then |
| 512 | PRODUCT_PACKAGES_LIST+=("${line#-}") |
| 513 | else |
| 514 | PRODUCT_COPY_FILES_LIST+=("$line") |
| 515 | fi |
| 516 | |
| 517 | done < <(egrep -v '(^#|^[[:space:]]*$)' "$1" | sort | uniq) |
| 518 | } |
| 519 | |
| 520 | # |
| 521 | # write_makefiles: |
| 522 | # |
| 523 | # $1: file containing the list of items to extract |
| 524 | # |
| 525 | # Calls write_product_copy_files and write_product_packages on |
| 526 | # the given file and appends to the Android.mk as well as |
| 527 | # the product makefile. |
| 528 | # |
| 529 | function write_makefiles() { |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 530 | parse_file_list "$1" |
| 531 | write_product_copy_files |
| 532 | write_product_packages |
| 533 | } |
| 534 | |
| 535 | # |
| 536 | # init_adb_connection: |
| 537 | # |
| 538 | # Starts adb server and waits for the device |
| 539 | # |
| 540 | function init_adb_connection() { |
| 541 | adb start-server # Prevent unexpected starting server message from adb get-state in the next line |
| 542 | if ! _adb_connected; then |
| 543 | echo "No device is online. Waiting for one..." |
| 544 | echo "Please connect USB and/or enable USB debugging" |
| 545 | until _adb_connected; do |
| 546 | sleep 1 |
| 547 | done |
| 548 | echo "Device Found." |
| 549 | fi |
| 550 | |
| 551 | # Retrieve IP and PORT info if we're using a TCP connection |
| 552 | TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \ |
| 553 | | head -1 | awk '{print $1}') |
| 554 | adb root &> /dev/null |
| 555 | sleep 0.3 |
| 556 | if [ -n "$TCPIPPORT" ]; then |
| 557 | # adb root just killed our connection |
| 558 | # so reconnect... |
| 559 | adb connect "$TCPIPPORT" |
| 560 | fi |
| 561 | adb wait-for-device &> /dev/null |
| 562 | sleep 0.3 |
| 563 | } |
| 564 | |
| 565 | # |
| 566 | # extract: |
| 567 | # |
| 568 | # $1: file containing the list of items to extract |
| 569 | # $2: path to extracted system folder, or "adb" to extract from device |
| 570 | # |
| 571 | function extract() { |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 572 | if [ -z "$OUTDIR" ]; then |
| 573 | echo "Output dir not set!" |
| 574 | exit 1 |
| 575 | fi |
| 576 | |
| 577 | parse_file_list "$1" |
| 578 | |
| 579 | # Allow failing, so we can try $DEST and/or $FILE |
| 580 | set +e |
| 581 | |
| 582 | local FILELIST=( ${PRODUCT_COPY_FILES_LIST[@]} ${PRODUCT_PACKAGES_LIST[@]} ) |
| 583 | local COUNT=${#FILELIST[@]} |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 584 | local SRC="$2" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 585 | local OUTPUT_ROOT="$CM_ROOT"/"$OUTDIR"/proprietary |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 586 | if [ "$SRC" = "adb" ]; then |
| 587 | init_adb_connection |
| 588 | fi |
| 589 | |
| 590 | if [ "$VENDOR_STATE" -eq "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 591 | echo "Cleaning output directory ($OUTPUT_ROOT).." |
| 592 | rm -rf "${OUTPUT_ROOT:?}/"* |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 593 | VENDOR_STATE=1 |
| 594 | fi |
| 595 | |
| 596 | echo "Extracting $COUNT files in $1 from $SRC:" |
| 597 | |
| 598 | for (( i=1; i<COUNT+1; i++ )); do |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 599 | |
| 600 | local FROM=$(target_file "${FILELIST[$i-1]}") |
| 601 | local ARGS=$(target_args "${FILELIST[$i-1]}") |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 602 | local SPLIT=(${FILELIST[$i-1]//:/ }) |
| 603 | local FILE="${SPLIT[0]#-}" |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 604 | local OUTPUT_DIR="$OUTPUT_ROOT" |
| 605 | local TARGET= |
| 606 | |
| 607 | if [ "$ARGS" = "rootfs" ]; then |
| 608 | TARGET="$FROM" |
| 609 | OUTPUT_DIR="$OUTPUT_DIR/rootfs" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 610 | else |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 611 | TARGET="system/$FROM" |
| 612 | FILE="system/$FILE" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 613 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 614 | |
| 615 | if [ "$SRC" = "adb" ]; then |
| 616 | printf ' - %s .. ' "/$TARGET" |
| 617 | else |
| 618 | printf ' - %s \n' "/$TARGET" |
| 619 | fi |
| 620 | |
| 621 | local DIR=$(dirname "$FROM") |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 622 | if [ ! -d "$OUTPUT_DIR/$DIR" ]; then |
| 623 | mkdir -p "$OUTPUT_DIR/$DIR" |
| 624 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 625 | local DEST="$OUTPUT_DIR/$FROM" |
| 626 | |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 627 | if [ "$SRC" = "adb" ]; then |
| 628 | # Try CM target first |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 629 | adb pull "/$TARGET" "$DEST" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 630 | # if file does not exist try OEM target |
| 631 | if [ "$?" != "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 632 | adb pull "/$FILE" "$DEST" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 633 | fi |
| 634 | else |
| 635 | # Try OEM target first |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 636 | cp "$SRC/$FILE" "$DEST" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 637 | # if file does not exist try CM target |
| 638 | if [ "$?" != "0" ]; then |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 639 | cp "$SRC/$TARGET" "$DEST" |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 640 | fi |
| 641 | fi |
Steve Kondik | a991cf1 | 2016-07-28 12:13:12 -0700 | [diff] [blame] | 642 | |
| 643 | local TYPE="${DIR##*/}" |
| 644 | if [ "$TYPE" = "bin" -o "$TYPE" = "sbin" ]; then |
| 645 | chmod 755 "$DEST" |
| 646 | else |
| 647 | chmod 644 "$DEST" |
| 648 | fi |
Steve Kondik | 4e2aaab | 2016-07-15 10:39:58 -0700 | [diff] [blame] | 649 | done |
| 650 | |
| 651 | # Don't allow failing |
| 652 | set -e |
| 653 | } |