1#!/bin/bash
2
3set -ex
4
5function apply_droidstubs_hack() {
6    if ! grep -q 'STOPSHIP: RESTORE THIS LOGIC WHEN DECLARING "REL" BUILD' "$top/build/soong/java/droidstubs.go" ; then
7        local build_soong_git_root="$(readlink -f $top/build/soong)"
8        patch --strip=1 --no-backup-if-mismatch --directory="$build_soong_git_root" --input=../../build/make/tools/finalization/build_soong_java_droidstubs.go.apply_hack.diff
9    fi
10}
11
12function finalize_bionic_ndk() {
13    # Adding __ANDROID_API_<>__.
14    # If this hasn't done then it's not used and not really needed. Still, let's check and add this.
15    local api_level="$top/bionic/libc/include/android/api-level.h"
16    if ! grep -q "\__.*$((${FINAL_PLATFORM_SDK_VERSION}))" $api_level ; then
17        local tmpfile=$(mktemp /tmp/finalization.XXXXXX)
18        echo "
19/** Names the \"${FINAL_PLATFORM_CODENAME:0:1}\" API level ($FINAL_PLATFORM_SDK_VERSION), for comparison against \`__ANDROID_API__\`. */
20#define __ANDROID_API_${FINAL_PLATFORM_CODENAME:0:1}__ $FINAL_PLATFORM_SDK_VERSION" > "$tmpfile"
21
22        local api_level="$top/bionic/libc/include/android/api-level.h"
23        sed -i -e "/__.*$((${FINAL_PLATFORM_SDK_VERSION}-1))/r""$tmpfile" $api_level
24
25        rm "$tmpfile"
26    fi
27}
28
29function finalize_modules_utils() {
30    local shortCodename="${FINAL_PLATFORM_CODENAME:0:1}"
31    local methodPlaceholder="INSERT_NEW_AT_LEAST_${shortCodename}_METHOD_HERE"
32
33    local tmpfile=$(mktemp /tmp/finalization.XXXXXX)
34    echo "    /** Checks if the device is running on a release version of Android $FINAL_PLATFORM_CODENAME or newer */
35    @ChecksSdkIntAtLeast(api = $FINAL_PLATFORM_SDK_VERSION /* BUILD_VERSION_CODES.$FINAL_PLATFORM_CODENAME */)
36    public static boolean isAtLeast${FINAL_PLATFORM_CODENAME:0:1}() {
37        return SDK_INT >= $FINAL_PLATFORM_SDK_VERSION ||
38                (SDK_INT == $(($FINAL_PLATFORM_SDK_VERSION - 1)) && isAtLeastPreReleaseCodename(\"$FINAL_PLATFORM_CODENAME\"));
39    }" > "$tmpfile"
40
41    local javaFuncRegex='\/\*\*[^{]*isAtLeast'"${shortCodename}"'() {[^{}]*}'
42    local javaFuncReplace="N;N;N;N;N;N;N;N; s/$javaFuncRegex/$methodPlaceholder/; /$javaFuncRegex/!{P;D};"
43
44    local javaSdkLevel="$top/frameworks/libs/modules-utils/java/com/android/modules/utils/build/SdkLevel.java"
45    sed -i "$javaFuncReplace" $javaSdkLevel
46
47    sed -i "/${methodPlaceholder}"'/{
48           r '"$tmpfile"'
49           d}' $javaSdkLevel
50
51    echo "// Checks if the device is running on release version of Android ${FINAL_PLATFORM_CODENAME:0:1} or newer.
52inline bool IsAtLeast${FINAL_PLATFORM_CODENAME:0:1}() {
53  return android_get_device_api_level() >= $FINAL_PLATFORM_SDK_VERSION ||
54         (android_get_device_api_level() == $(($FINAL_PLATFORM_SDK_VERSION - 1)) &&
55          detail::IsAtLeastPreReleaseCodename(\"$FINAL_PLATFORM_CODENAME\"));
56}" > "$tmpfile"
57
58    local cppFuncRegex='\/\/[^{]*IsAtLeast'"${shortCodename}"'() {[^{}]*}'
59    local cppFuncReplace="N;N;N;N;N;N; s/$cppFuncRegex/$methodPlaceholder/; /$cppFuncRegex/!{P;D};"
60
61    local cppSdkLevel="$top/frameworks/libs/modules-utils/build/include/android-modules-utils/sdk_level.h"
62    sed -i "$cppFuncReplace" $cppSdkLevel
63    sed -i "/${methodPlaceholder}"'/{
64           r '"$tmpfile"'
65           d}' $cppSdkLevel
66
67    rm "$tmpfile"
68}
69
70function bumpSdkExtensionsVersion() {
71    local SDKEXT="packages/modules/SdkExtensions/"
72
73    # This used to call bump_sdk.sh utility.
74    # However due to TS, we have to build the gen_sdk with a correct set of settings.
75
76    # "$top/packages/modules/SdkExtensions/gen_sdk/bump_sdk.sh" ${FINAL_MAINLINE_EXTENSION}
77    # Leave the last commit as a set of modified files.
78    # The code to create a finalization topic will pick it up later.
79    # git -C ${SDKEXT} reset HEAD~1
80
81    local sdk="${FINAL_MAINLINE_EXTENSION}"
82    local modules_arg=
83
84    TARGET_PRODUCT=aosp_arm64 \
85        TARGET_RELEASE=fina_1 \
86        TARGET_BUILD_VARIANT=userdebug \
87        DIST_DIR=out/dist \
88        $top/build/soong/soong_ui.bash --make-mode --soong-only gen_sdk
89
90    ANDROID_BUILD_TOP="$top" out/soong/host/linux-x86/bin/gen_sdk \
91        --database ${SDKEXT}/gen_sdk/extensions_db.textpb \
92        --action new_sdk \
93        --sdk "$sdk" \
94        $modules_arg
95}
96
97function finalize_sdk_resources() {
98    local top="$(dirname "$0")"/../../../..
99    source $top/build/make/tools/finalization/environment.sh
100
101    local SDK_CODENAME="public static final int $FINAL_PLATFORM_CODENAME_JAVA = CUR_DEVELOPMENT;"
102    local SDK_VERSION="public static final int $FINAL_PLATFORM_CODENAME_JAVA = $FINAL_PLATFORM_SDK_VERSION;"
103
104    # The full process can be found at (INTERNAL) go/android-sdk-finalization.
105
106    # apply droidstubs hack to prevent tools from incrementing an API version
107    apply_droidstubs_hack
108
109    # bionic/NDK
110    finalize_bionic_ndk
111
112    # Finalize SDK
113
114    # frameworks/libs/modules-utils
115    finalize_modules_utils
116
117    # development/sdk
118    local platform_source="$top/development/sdk/platform_source.prop_template"
119    sed -i -e 's/Pkg\.Revision.*/Pkg\.Revision=1/g' $platform_source
120    local build_tools_source="$top/development/sdk/build_tools_source.prop_template"
121    sed -i -e 's/Pkg\.Revision.*/Pkg\.Revision=${PLATFORM_SDK_VERSION}.0.0/g' $build_tools_source
122
123    # build/soong
124    local codename_version="\"${FINAL_PLATFORM_CODENAME}\":     ${FINAL_PLATFORM_SDK_VERSION}"
125    if ! grep -q "$codename_version" "$top/build/soong/android/api_levels.go" ; then
126        sed -i -e "/:.*$((${FINAL_PLATFORM_SDK_VERSION}-1)),/a \\\t\t$codename_version," "$top/build/soong/android/api_levels.go"
127    fi
128
129    # cts
130    if ! grep -q "${FINAL_PLATFORM_VERSION}" "$top/cts/tests/tests/os/assets/platform_releases.txt" ; then
131        echo ${FINAL_PLATFORM_VERSION} >> "$top/cts/tests/tests/os/assets/platform_releases.txt"
132    fi
133    if ! grep -q "$((${FINAL_PLATFORM_SDK_VERSION}-1)), ${FINAL_PLATFORM_VERSION}" "$top/cts/tests/tests/os/src/android/os/cts/BuildVersionTest.java" ; then
134        sed -i -e "s/.*EXPECTED_SDKS = List.of(.*$((${FINAL_PLATFORM_SDK_VERSION}-1))/&, $FINAL_PLATFORM_SDK_VERSION/" "$top/cts/tests/tests/os/src/android/os/cts/BuildVersionTest.java"
135    fi
136
137    # libcore
138    sed -i "s%$SDK_CODENAME%$SDK_VERSION%g" "$top/libcore/dalvik/src/main/java/dalvik/annotation/compat/VersionCodes.java"
139
140    # platform_testing
141    local version_codes="$top/platform_testing/libraries/compatibility-common-util/src/com/android/compatibility/common/util/VersionCodes.java"
142    sed -i -e "/=.*$((${FINAL_PLATFORM_SDK_VERSION}-1));/a \\    ${SDK_VERSION}" $version_codes
143
144    # tools/platform-compat
145    local class2nonsdklist="$top/tools/platform-compat/java/com/android/class2nonsdklist/Class2NonSdkList.java"
146    if ! grep -q "\.*map.put($((${FINAL_PLATFORM_SDK_VERSION}))" $class2nonsdklist ; then
147      local sdk_version="map.put(${FINAL_PLATFORM_SDK_VERSION}, FLAG_UNSUPPORTED);"
148      sed -i -e "/.*map.put($((${FINAL_PLATFORM_SDK_VERSION}-1))/a \\        ${sdk_version}" $class2nonsdklist
149    fi
150
151    # Finalize resources
152    "$top/frameworks/base/tools/aapt2/tools/finalize_res.py" \
153           "$top/frameworks/base/core/res/res/values/public-staging.xml" \
154           "$top/frameworks/base/core/res/res/values/public-final.xml"
155
156    # frameworks/base
157    sed -i "s%$SDK_CODENAME%$SDK_VERSION%g" "$top/frameworks/base/core/java/android/os/Build.java"
158    sed -i -e "/=.*$((${FINAL_PLATFORM_SDK_VERSION}-1)),/a \\    SDK_${FINAL_PLATFORM_CODENAME_JAVA} = ${FINAL_PLATFORM_SDK_VERSION}," "$top/frameworks/base/tools/aapt/SdkConstants.h"
159    sed -i -e "/=.*$((${FINAL_PLATFORM_SDK_VERSION}-1)),/a \\  SDK_${FINAL_PLATFORM_CODENAME_JAVA} = ${FINAL_PLATFORM_SDK_VERSION}," "$top/frameworks/base/tools/aapt2/SdkConstants.h"
160
161    # Bump Mainline SDK extension version.
162    bumpSdkExtensionsVersion
163
164    # target to build SDK
165    local sdk_m="$top/build/soong/soong_ui.bash --make-mode TARGET_PRODUCT=aosp_arm64 TARGET_RELEASE=fina_1 TARGET_BUILD_VARIANT=userdebug DIST_DIR=out/dist"
166
167    # Force update current.txt
168    $sdk_m clobber
169    $sdk_m update-api
170}
171
172finalize_sdk_resources
173
174