1#!/usr/bin/env bash
2
3# e.x. convertFqName "android.hardware.foo@1.0::foo::IFoo" "\5" -> IFoo
4function convertFqName() {
5    # android.hardware.foo@1.0::foo::IFoo
6    # \1 => android.hardware.foo
7    # \3 => 1
8    # \4 => 0
9    # \5 => IFoo # optional capture group
10    local FQNAME="^(([a-zA-Z0-9]+\.)*[a-zA-Z0-9]+)@([0-9]+).([0-9]+)(::([a-zA-Z0-9]+))?$"
11
12    local fqName="$1"
13    local pattern="$2"
14
15    echo $1 | sed -E "s/$FQNAME/$2/g"
16}
17function assertInterfaceFqName() {
18    if [ -n "$(convertFqName "$1")" ]; then
19        echo "$1 is not an interface fqname"
20        return 1
21    fi
22}
23function javaTarget() { convertFqName "$1" "\1-V\3.\4-java"; }
24function cppTarget()  { convertFqName "$1" "\1@\3.\4"; }
25
26function hidl-gen-output() {
27    local out="$1"
28
29    while read -r fqName; do
30        echo "Generating output for $fqName."
31        hidl-gen -Lc++-headers -o "$out/cpp" "$fqName" || exit 1
32        hidl-gen -Ljava -o "$out/java" "$fqName" 2>/dev/null
33    done
34}
35
36# for a package root path + package, get unreleased interfaces
37function all-interfaces() {
38    [ $# = 2 ] || { echo "usage: all-interfaces <package root directory> <package root path>" && return 1; }
39
40    local package_root="$1"
41    [ -d "$package_root" ] || { echo "all-interfaces: directory $package_root does not exist" && return 1; }
42
43    local package="$2"
44
45    find $package_root -name '*.hal' -printf '%P\n' \
46        | sed "s,^,$package.,g" \
47        | sed s,/,.,g \
48        | sed "s/\.\([0-9]\+\.[0-9]\+\)\.\([a-zA-Z0-9]\+\)\.hal$/@\1::\2/g" \
49        | sort # should be unique
50}
51
52# for a package root path, get released interface names
53function current-interfaces() {
54    [ $# = 1 ] || { echo "usage: current-interfaces <package root directory>" && return 1; }
55
56    local package_root="$1"
57    [ -d "$package_root" ] || { echo "current-interfaces: directory $package_root does not exist" && return 1; }
58
59    local current_file="$package_root/current.txt"
60    [ -f "$current_file" ] || { echo "current-interfaces: current file $current_file does not exist" && return 1; }
61
62    cat "$current_file" | cut -d '#' -f1 | awk '{print $2}' | sed "/^ *$/d" | sort | uniq
63}
64
65# for a package root path + package, get interfaces in tree but not in current.txt
66function changed-interfaces() {
67    [ $# = 2 ] || { echo "usage: changed-interfaces <package root directory> <package root path>" && return 1; }
68
69    local package_root="$1"
70    [ -d "$package_root" ] || { echo "current-interfaces: directory $package_root does not exist" && return 1; }
71
72    local package="$2"
73
74    echo "CHANGES FOR $2: ($1)"
75
76    # not excluding '2' since 2 should be a subset of 1, but it might help catch some errors
77    comm -3 <(all-interfaces "$package_root" "$package") <(current-interfaces "$package_root")
78}
79
80# see all of the interfaces which are unreleased
81function aosp-changes-report() {
82    echo "Warning: ignoring test/automotive interfaces."
83    echo
84
85    changed-interfaces $ANDROID_BUILD_TOP/hardware/interfaces android.hardware \
86        | grep -v "android.hardware.tests" \
87        | grep -v "android.hardware.automotive"
88    changed-interfaces $ANDROID_BUILD_TOP/frameworks/hardware/interfaces android.frameworks
89    changed-interfaces $ANDROID_BUILD_TOP/system/hardware/interfaces android.system
90    changed-interfaces $ANDROID_BUILD_TOP/system/libhidl/transport android.hidl
91}
92
93function aosp-interfaces() {
94    declare -A roots
95    roots["android.hardware"]="hardware/interfaces"
96    roots["android.system"]="system/hardware/interfaces"
97    roots["android.frameworks"]="frameworks/hardware/interfaces"
98    roots["android.hidl"]="system/libhidl/transport"
99
100    for root in "${!roots[@]}"; do
101        local path="${roots["$root"]}"
102        all-interfaces "$ANDROID_BUILD_TOP/$path" "$root"
103    done | sort -u
104}
105
106function aosp-released-interfaces() {
107    local roots=(
108        hardware/interfaces
109        system/hardware/interfaces
110        frameworks/hardware/interfaces
111        system/libhidl/transport
112    )
113
114    for root in "${roots[@]}"; do
115        current-interfaces "$ANDROID_BUILD_TOP/$root"
116    done | sort -u
117}
118
119function aosp-released-packages() {
120    aosp-released-interfaces | cut -d ':' -f1 | sort -u
121}
122
123function hidl-doc-generate-sources() {
124    local outputDir="$1"
125    [ -z "$1" ] && outputDir="gen"
126
127    echo "Generating sources in $(realpath $outputDir)"
128
129    aosp-released-packages | hidl-gen-output "$outputDir"
130
131    echo "Deleting implementation-related files from $outputDir."
132    rm $(find "$outputDir/cpp" -\( -name "IHw*.h" \
133                                   -o -name "BnHw*.h" \
134                                   -o -name "BpHw*.h" \
135                                   -o -name "Bs*.h" \
136                                   -o -name "hwtypes.h" \))
137
138    echo "Done: generated sources are in $outputDir"
139}
140
141# WARNING, must have all branches in question downloaded
142# Example:
143# $ cd $ANDROID_BUILD_TOP/hardware/interfaces
144# $ repo sync -j128 . # must not use -c
145# $ hidl_current_check aosp/oreo-release aosp/oreo-mr1-release aosp/pie-release # etc..
146#
147# This can be used to make sure interfaces are not removed.
148function hidl_current_check() {
149    local args=("$@")
150    for ((i=0;i<${#args[@]} - 1;++i)); do
151        local parent="${args[i]}"
152        local child="${args[i+1]}"
153        git show-ref -q $parent || { echo "$parent doesn't exist" && continue; }
154        echo "$parent .. $child"
155        diff <(git show "$parent":current.txt | grep -oP "^[^ ]+ [^ ]+" | sort) \
156             <(git show "$child":current.txt | grep -oP "^[^ ]+ [^ ]+" | sort) |
157             grep "<"
158    done
159}
160
161