1#!/bin/bash -ex
2
3# Usage:
4# generate_ml_bundle.sh [args] source_tree dest_tree api_level
5#
6# Args:
7# -p Sets prefer=true in generated java_sdk_library_import rules
8# -a Also update artifects; default behaviour is just to generate build rules.
9
10PREFER=false
11UPDATE_ARTIFACTS=false
12while getopts "pa" opt; do
13    case ${opt} in
14          p ) PREFER=true;;
15          a ) UPDATE_ARTIFACTS=true;;
16          \? ) echo "Invalid option."
17            exit
18            ;;
19    esac
20done
21shift $((OPTIND -1))
22
23SOURCE_TREE=$1
24DESTINATION_TREE=$2
25API_LEVEL=$3
26SOURCE_TREE_TYPE=$(test -e ${DESTINATION_TREE}/vendor/unbundled_google/modules && echo GOOGLE || echo PARTNER)
27
28function sdk_lib_bp() {
29    modulename=$1
30    apexname=$2
31    shared_library=$(test \( "$apexname" == com.android.ipsec \) -a \( "$API_LEVEL" -lt 31 \) && echo true || echo false)
32    cat << EOF
33java_sdk_library_import {
34    name: "${modulename}",
35    owner: "google",
36    prefer: ${PREFER},
37    shared_library: ${shared_library},
38    apex_available: [
39        "${apexname}",
40        "test_${apexname}",
41    ],
42    public: {
43        jars: ["current/public/${modulename}.jar"],
44        stub_srcs: ["current/public/${modulename}.srcjar"],
45        current_api: "current/public/${modulename}.txt",
46        removed_api: "current/public/${modulename}-removed.txt",
47        sdk_version: "module_current",
48    },
49    system: {
50        jars: ["current/system/${modulename}.jar"],
51        stub_srcs: ["current/system/${modulename}.srcjar"],
52        current_api: "current/system/${modulename}.txt",
53        removed_api: "current/system/${modulename}-removed.txt",
54        sdk_version: "module_current",
55    },
56    module_lib: {
57        jars: ["current/module_lib/${modulename}.jar"],
58        stub_srcs: ["current/module_lib/${modulename}.srcjar"],
59        current_api: "current/module_lib/${modulename}.txt",
60        removed_api: "current/module_lib/${modulename}-removed.txt",
61        sdk_version: "module_current",
62    },
63}
64EOF
65}
66
67function make_sdk_library() {
68    libnames=$1
69    destdir=$2
70    apexname=$3
71    bp=${destdir}/Android.bp
72
73    #test -e "$bp"
74    mkdir -p "$(dirname "$bp")"
75
76    # Remove existing java_sdk_library_import and double newlines
77    existing_bp="$(sed -e '/java_sdk_library_import {/,/^}/d' "$bp" | tr -s \\n)"
78    echo "$existing_bp" > "$bp"
79    for libname in $libnames; do
80        echo >> "$bp"
81        echo "$(sdk_lib_bp ${libname} ${apexname})" | grep -v test_com.android.tethering >> "$bp"
82    done
83    #echo >> "$bp"
84
85    if [ "${UPDATE_ARTIFACTS}" == "true" ] ; then
86        rm -rf "${destdir}/current"
87        mkdir -p "${destdir}/current/"{public,system,module_lib}
88        for libname in $libnames; do
89            cp public/{${libname}.jar,api/${libname}.txt,api/${libname}-removed.txt} "${destdir}/current/public"
90            cp system/{${libname}.jar,api/${libname}.txt,api/${libname}-removed.txt} "${destdir}/current/system"
91            cp module-lib/{${libname}.jar,api/${libname}.txt,api/${libname}-removed.txt} "${destdir}/current/module_lib"
92        done
93    fi
94}
95
96function copy_notices() {
97    destdir=$1
98    apks=$(find $destdir -name '*.apks' | head -1)
99    if [[ -z "$apks" ]]; then
100        echo "no apks found in $destdir" >&2
101        exit
102    fi
103    filename=$(zipinfo -1 "$apks" *.apex */base-master.apk | head -1)
104    # com.android.ipsec is not expected to have a LICENSE, so don't fail if it doesn't.
105    unzip -p "$apks" "$filename" | bsdtar -xOf- assets/NOTICE.html.gz > "${destdir}/NOTICE.html.gz" || test "$2" == "com.android.ipsec"
106}
107
108function get_dest_dir() {
109    if [[ "$SOURCE_TREE_TYPE" == "PARTNER" ]]; then
110        echo "${DESTINATION_TREE}/vendor/partner_modules/${1}"
111    elif [[ "$SOURCE_TREE_TYPE" == "GOOGLE" ]]; then
112        echo -n "${DESTINATION_TREE}/prebuilts/module_sdk/"
113        case "$1" in
114            "IKEPrebuilt") echo "IPsec";;
115            "MediaFrameworkPrebuilt") echo "Media";;
116            "MediaProviderPrebuilt") echo "MediaProvider";;
117            "PermissionControllerPrebuilt") echo "Permission";;
118            "SdkExtensionsPrebuilt") echo "SdkExtensions";;
119            "StatsdPrebuilt") echo "StatsD";;
120            "TetheringPrebuilt") echo "Connectivity";;
121            "WiFiPrebuilt") echo "Wifi";;
122            *) exit 1;;
123        esac
124    else
125        # Unknown tree type
126        exit 2
127    fi
128    #ConscryptPrebuilt ???
129}
130
131function setup_stub_prebuilts() {
132    destdir=$(get_dest_dir $2)
133
134    make_sdk_library "$1" "$destdir" "$3"
135    #copy_notices "$destdir" "$3"
136}
137
138cd "$SOURCE_TREE/prebuilts/sdk/$API_LEVEL"
139setup_stub_prebuilts android.net.ipsec.ike IKEPrebuilt com.android.ipsec
140setup_stub_prebuilts framework-media MediaFrameworkPrebuilt com.android.media
141setup_stub_prebuilts framework-mediaprovider MediaProviderPrebuilt com.android.mediaprovider
142if [[ "$API_LEVEL" -ge 31 ]]; then
143    setup_stub_prebuilts 'framework-permission framework-permission-s' PermissionControllerPrebuilt com.android.permission
144else
145    setup_stub_prebuilts framework-permission PermissionControllerPrebuilt com.android.permission
146fi
147setup_stub_prebuilts framework-sdkextensions SdkExtensionsPrebuilt com.android.sdkext
148setup_stub_prebuilts framework-statsd StatsdPrebuilt com.android.os.statsd
149setup_stub_prebuilts framework-tethering TetheringPrebuilt com.android.tethering
150setup_stub_prebuilts framework-wifi WiFiPrebuilt com.android.wifi
151