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