1#/usr/bin/env bash
2
3set -euo pipefail
4
5if (echo "$@" |grep -qe -h); then
6    echo "This script will generate a new HAL version identical to an other one."
7    echo "This helps creating the boilerplate for a new version."
8    echo
9    echo "USAGE: $0 [BASE_VERSION] [NEW_VERSION]"
10    echo "       BASE_VERSION default value is the highest version currently existing"
11    echo "       NEW_VERSION default value is BASE_VERSION + 1"
12    echo
13    echo "Example: to generate a V6.0 by copying V5, do: $0 5.0 6.0"
14    exit
15fi
16readonly HAL_DIRECTORY=hardware/interfaces/audio
17readonly HAL_VTS_DIRECTORY=core/all-versions/vts/functional
18readonly HAL_VTS_FILE=AudioPrimaryHidlHalTest.cpp
19readonly HAL_SERVICE_DIRECTORY=common/all-versions/default/service/
20readonly HAL_SERVICE_CPP=service.cpp
21
22readonly FWK_DIRECTORY=frameworks/av/media/libaudiohal
23readonly IMPL_DIRECTORY=impl
24readonly IMPL_FACTORYHAL=FactoryHalHidl.cpp
25
26readonly VTS_DIRECTORY=test/vts-testcase/hal/audio
27readonly VTS_LIST=test/vts/tools/build/tasks/list/vts_test_lib_hidl_package_list.mk
28readonly WATCHDOG=frameworks/base/services/core/java/com/android/server/Watchdog.cpp
29readonly DUMP_UTILS=frameworks/native/libs/dumputils/dump_utils.cpp
30readonly GSI_CURRENT=build/make/target/product/gsi/current.txt
31
32readonly BASE_VERSION=${1:-$(ls $ANDROID_BUILD_TOP/$HAL_DIRECTORY | grep -E '[0-9]+\.[0-9]+' |
33                                  sort -n |tail -n1)}
34readonly BASE_MAJOR_VERSION=${BASE_VERSION%.*}
35readonly BASE_MINOR_VERSION=${BASE_VERSION##*.}
36
37readonly NEW_VERSION="${2:-$((${BASE_MAJOR_VERSION} + 1)).0}"
38readonly NEW_MAJOR_VERSION=${NEW_VERSION%.*}
39readonly NEW_MINOR_VERSION=${NEW_VERSION##*.}
40
41
42readonly BASE_VERSION_REGEX="${BASE_MAJOR_VERSION}[._]${BASE_MINOR_VERSION}"
43readonly NEW_VERSION_REGEX="${NEW_MAJOR_VERSION}[._]${NEW_MINOR_VERSION}"
44
45readonly BASE_VERSION_ESCAPE="${BASE_MAJOR_VERSION}\.${BASE_MINOR_VERSION}"
46readonly BASE_VERSION_UNDERSCORE="${BASE_MAJOR_VERSION}_${BASE_MINOR_VERSION}"
47readonly NEW_VERSION_UNDERSCORE="${NEW_MAJOR_VERSION}_${NEW_MINOR_VERSION}"
48updateVersion() {
49    if [ $1 == "-e" ]; then
50        local -r REGEX="$2"; shift 2
51    else
52        local -r REGEX="$BASE_VERSION_REGEX"
53    fi
54    awk -i inplace -e "{if (!/$REGEX/) print; else {
55                            if (original_before) print
56                            if (original_after) original_line=\$0;
57
58                            gsub(/$BASE_VERSION_ESCAPE/,\"$NEW_VERSION\");
59                            gsub(/$BASE_VERSION_UNDERSCORE/,\"$NEW_VERSION_UNDERSCORE\");
60                            gsub(/MAJOR_VERSION=$BASE_MAJOR_VERSION/,
61                                 \"MAJOR_VERSION=$NEW_MAJOR_VERSION\");
62                            gsub(/MINOR_VERSION=$BASE_MINOR_VERSION/,
63                                 \"MINOR_VERSION=$NEW_MINOR_VERSION\");
64
65                            print
66                            if (original_after) print original_line
67                       }}" "$@"
68}
69
70updateAudioVersion() {
71    updateVersion -e "audio.*$BASE_VERSION_REGEX" "$@"
72}
73
74updateLicenceDates() {
75    # Update date on the 2 first lines
76    sed -i "1,2 s/20[0-9][0-9]/$(date +"%Y")/g" "$@"
77}
78
79echo "Creating new audio HAL V$NEW_VERSION based on V$BASE_VERSION"
80echo "Press Ctrl-C to cancel, Enter to continue"
81read
82
83MODIFIED=
84runIfNeeded() {
85    local -r TARGET=$1; shift
86    cd $ANDROID_BUILD_TOP/$TARGET
87    if grep -q -r "audio.*$NEW_VERSION_REGEX"; then
88        echo "  Skipping $TARGET as already up to date"
89    else
90        echo "  Updating $PWD"
91        MODIFIED+=$'\n - '$TARGET
92        "$@"
93    fi
94}
95
96createHALVersion() {
97    local -r DIRS=". common effect"
98    local COPY=
99    echo "Copy $BASE_VERSION to $NEW_VERSION in $DIRS"
100    for DIR in $DIRS; do
101        cp -Tar $DIR/$BASE_VERSION $DIR/$NEW_VERSION
102        COPY+=" $DIR/$NEW_VERSION"
103    done
104
105    echo "Replacing $BASE_VERSION by $NEW_VERSION in the copied files"
106    updateVersion $(find $COPY -type f)
107    updateLicenceDates $(find $COPY -type f)
108
109    echo "Update implementation and VTS generic code"
110    local -r FILES="*/all-versions/default/Android.bp */all-versions/vts/functional/Android.bp"
111    updateVersion -v original_before=1 -v RS= -v ORS='\n\n' $FILES
112    sed -i '${/^$/d}' $FILES # Remove \n at the end of the files
113
114    updateVersion -v original_before=1 $HAL_SERVICE_DIRECTORY/Android.bp
115
116    updateVersion -e "audio::.*$BASE_VERSION_REGEX" -v original_after=1 \
117        $HAL_SERVICE_DIRECTORY/$HAL_SERVICE_CPP
118    updateVersion -e "audio\/.*$BASE_VERSION_REGEX" -v original_before=1 \
119        $HAL_SERVICE_DIRECTORY/$HAL_SERVICE_CPP
120
121    local -r HAL_VTS_PATH=$HAL_VTS_DIRECTORY/$NEW_VERSION/$HAL_VTS_FILE
122    mkdir -p $(dirname $HAL_VTS_PATH)
123    cat > $HAL_VTS_PATH <<EOF
124/*
125 * Copyright (C) $(date +"%Y") The Android Open Source Project
126 *
127 * Licensed under the Apache License, Version 2.0 (the "License");
128 * you may not use this file except in compliance with the License.
129 * You may obtain a copy of the License at
130 *
131 *      http://www.apache.org/licenses/LICENSE-2.0
132 *
133 * Unless required by applicable law or agreed to in writing, software
134 * distributed under the License is distributed on an "AS IS" BASIS,
135 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136 * See the License for the specific language governing permissions and
137 * limitations under the License.
138 */
139
140// pull in all the <= $BASE_VERSION tests
141#include "$BASE_VERSION/$(basename AudioPrimaryHidlHalTest.cpp)"
142EOF
143
144    echo "New HAL version $NEW_VERSION successfully created"
145}
146
147echo "Creating new audio HAL definition, default impl and VTS"
148runIfNeeded $HAL_DIRECTORY createHALVersion
149
150
151createFrameworkAdapter() {
152    updateVersion -v original_before=1 Android.bp
153    updateVersion -v original_before=1 -v RS= -v ORS='\n\n' $IMPL_DIRECTORY/Android.bp
154    updateVersion -v original_after=1 $IMPL_FACTORYHAL
155}
156echo "Now creating the framework adapter version"
157runIfNeeded $FWK_DIRECTORY createFrameworkAdapter
158
159createVTSXML() {
160    cp -Tar V$BASE_VERSION_UNDERSCORE V$NEW_VERSION_UNDERSCORE
161    cp -Tar effect/{V$BASE_VERSION_UNDERSCORE,V$NEW_VERSION_UNDERSCORE}
162    local -r FILES=$(find {.,effect}/V$NEW_VERSION_UNDERSCORE -type f)
163    updateVersion $FILES
164    updateLicenceDates $FILES
165}
166echo "Now update VTS XML"
167runIfNeeded $VTS_DIRECTORY createVTSXML
168
169echo "Now register new VTS"
170runIfNeeded $(dirname $VTS_LIST) updateAudioVersion -v original_before=1 $(basename $VTS_LIST)
171
172echo "Now update watchdog"
173runIfNeeded $(dirname $WATCHDOG) updateAudioVersion -v original_before=1 $(basename $WATCHDOG)
174
175echo "Now update dumputils"
176runIfNeeded $(dirname $DUMP_UTILS) updateAudioVersion -v original_before=1 $(basename $DUMP_UTILS)
177
178echo "Now update GSI current.txt"
179runIfNeeded $(dirname $GSI_CURRENT) update-vndk-list.sh
180
181if ! [ "$MODIFIED" ]; then
182    echo
183    echo "$NEW_VERSION already exist, nothing to do"
184    exit
185fi
186
187cat << EOF
188
189All File generated successfully. Please submit a patch in all those directories: $MODIFIED
190
191-----------------------------------------------------------
192WHAT WAS *NOT* DONE, and you need to do now:
193 1) You need to choose if the new HAL is optional or not for new devices.
194    Either add or replace $BASE_VERSION by $NEW_VERSION in
195    compatibility_matrices/compatibility_matrix.current.xml
196    Do not forget to update both the "audio" and "audio.effects" HAL'
197
198 2) Then you need to choose a device to update its audio HAL implementation:
199    a) Update the HAL manifest of your device: open your device manifest.xml
200       and replace $BASE_VERSION by $NEW_VERSION for both
201        - android.hardware.audio
202        - android.hardware.audio.effect
203    b) Go to your device device.mk (usually next to the manifest) and replace:
204        - android.hardware.audio@$BASE_VERSION-impl by
205          android.hardware.audio@$NEW_VERSION-impl
206        - android.hardware.audio.effect@$BASE_VERSION-impl by
207          android.hardware.audio.effect@$NEW_VERSION-impl
208EOF
209