From 1692a36da78d0c2444157c713cc62310473442ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Kongstad?= Date: Mon, 10 Jun 2024 16:04:34 +0200 Subject: [PATCH] check-flagged-apis: add list subcommand Add a new subcommand to list all flagged APIs and corresponding flags. This provides an overview of what flagged APIs exist in the Android tree. Bug: 345207706 Test: build/tools/check-flagged-apis/check-flagged-apis.sh list Test: atest check-flagged-apis-test Flag: EXEMPT host side tool Change-Id: Icc224f3787480353baabbd3946f36f003f35db59 --- .../check-flagged-apis/check-flagged-apis.sh | 54 +++++++++++++-- .../checkflaggedapis/CheckFlaggedApisTest.kt | 19 ++++++ .../src/com/android/checkflaggedapis/Main.kt | 67 ++++++++++++++++++- 3 files changed, 132 insertions(+), 8 deletions(-) diff --git a/tools/check-flagged-apis/check-flagged-apis.sh b/tools/check-flagged-apis/check-flagged-apis.sh index dad1236f71..8078cd80bf 100755 --- a/tools/check-flagged-apis/check-flagged-apis.sh +++ b/tools/check-flagged-apis/check-flagged-apis.sh @@ -14,8 +14,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Run check-flagged-apis for public APIs and the three @SystemApi flavours -# Usage: lunch && source +# Run check-flagged-apis for public APIs and the three @SystemApi flavours. +# +# This script expects an argument to tell it which subcommand of +# check-flagged-apis to execute. Run the script without any arguments to see +# the valid options. +# +# Remember to lunch to select the relevant release config before running this script. source $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../../shell_utils.sh require_top @@ -43,6 +48,10 @@ function build() { $MODULE_LIB_XML_VERSIONS } +function noop() { + true +} + function aninja() { local T="$(gettop)" (\cd "${T}" && prebuilts/build-tools/linux-x86/bin/ninja -f out/combined-${TARGET_PRODUCT}.ninja "$@") @@ -52,7 +61,7 @@ function path_to_api_signature_file { aninja -t query device_"$1"_all_targets | grep -A1 -e input: | tail -n1 } -function run() { +function run_check() { local errors=0 echo "# current" @@ -89,8 +98,39 @@ function run() { return $errors } -if [[ "$1" != "--skip-build" ]]; then - build && run -else - run +function run_list() { + echo "# current" + check-flagged-apis list \ + --api-signature $(path_to_api_signature_file "frameworks-base-api-current.txt") \ + --flag-values $(gettop)/out/soong/.intermediates/all_aconfig_declarations.pb + + echo + echo "# system-current" + check-flagged-apis list \ + --api-signature $(path_to_api_signature_file "frameworks-base-api-system-current.txt") \ + --flag-values $(gettop)/out/soong/.intermediates/all_aconfig_declarations.pb + + echo + echo "# system-server-current" + check-flagged-apis list \ + --api-signature $(path_to_api_signature_file "frameworks-base-api-system-server-current.txt") \ + --flag-values $(gettop)/out/soong/.intermediates/all_aconfig_declarations.pb + + echo + echo "# module-lib" + check-flagged-apis list \ + --api-signature $(path_to_api_signature_file "frameworks-base-api-module-lib-current.txt") \ + --flag-values $(gettop)/out/soong/.intermediates/all_aconfig_declarations.pb +} + +build_cmd=build +if [[ "$1" == "--skip-build" ]]; then + build_cmd=noop + shift 1 fi + +case "$1" in + check) $build_cmd && run_check ;; + list) $build_cmd && run_list ;; + *) echo "usage: $(basename $0): [--skip-build] check|list"; exit 1 +esac diff --git a/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt b/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt index 8e285f6216..e07ac1dfd4 100644 --- a/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt +++ b/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt @@ -358,4 +358,23 @@ class CheckFlaggedApisTest { parseApiVersions(API_VERSIONS.byteInputStream())) assertEquals(expected, actual) } + + @Test + fun testListFlaggedApis() { + val expected = + listOf( + "android.flag.bar DISABLED android/Clazz/Builder", + "android.flag.foo ENABLED android/Clazz", + "android.flag.foo ENABLED android/Clazz/Clazz()", + "android.flag.foo ENABLED android/Clazz/FOO", + "android.flag.foo ENABLED android/Clazz/getErrorCode()", + "android.flag.foo ENABLED android/Clazz/innerClassArg(Landroid/Clazz/Builder;)", + "android.flag.foo ENABLED android/Clazz/setData(I[[ILandroid/util/Utility;)", + "android.flag.foo ENABLED android/Clazz/setVariableData(I[Landroid/util/Atom;)") + val actual = + listFlaggedApis( + parseApiSignature("in-memory", API_SIGNATURE.byteInputStream()), + parseFlagValues(generateFlagsProto(ENABLED, DISABLED))) + assertEquals(expected, actual) + } } diff --git a/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt b/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt index 4696efd136..1125d393bf 100644 --- a/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt +++ b/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt @@ -210,6 +210,40 @@ The tool will exit with a non-zero exit code if any flagged APIs are found to be } } +class ListCommand : + CliktCommand( + help = + """ +List all flagged APIs and corresponding flags. + +The output format is " ", one line per API. + +The output can be post-processed by e.g. piping it to grep to filter out only enabled APIs, or all APIs guarded by a given flag. +""") { + private val apiSignaturePath by + option(ARG_API_SIGNATURE) + .help(ARG_API_SIGNATURE_HELP) + .path(mustExist = true, canBeDir = false, mustBeReadable = true) + .required() + private val flagValuesPath by + option(ARG_FLAG_VALUES) + .help(ARG_FLAG_VALUES_HELP) + .path(mustExist = true, canBeDir = false, mustBeReadable = true) + .required() + + override fun run() { + val flaggedSymbols = + apiSignaturePath.toFile().inputStream().use { + parseApiSignature(apiSignaturePath.toString(), it) + } + val flags = flagValuesPath.toFile().inputStream().use { parseFlagValues(it) } + val output = listFlaggedApis(flaggedSymbols, flags) + if (output.isNotEmpty()) { + println(output.joinToString("\n")) + } + } +} + internal fun parseApiSignature(path: String, input: InputStream): Set> { val output = mutableSetOf>() val visitor = @@ -460,4 +494,35 @@ internal fun findErrors( return errors } -fun main(args: Array) = MainCommand().subcommands(CheckCommand()).main(args) +/** + * Collect all known info about all @FlaggedApi annotated APIs. + * + * Each API will be represented as a String, on the format + *
+ *   <fully-qualified-name-of-flag< <state-of-flag< <API<
+ * 
+ * + * @param flaggedSymbolsInSource the set of symbols that are flagged in the source code + * @param flags the set of flags and their values + * @return a list of Strings encoding API data using the format described above, sorted + * alphabetically + */ +internal fun listFlaggedApis( + flaggedSymbolsInSource: Set>, + flags: Map +): List { + val output = mutableListOf() + for ((symbol, flag) in flaggedSymbolsInSource) { + val flagState = + when (flags.get(flag)) { + true -> "ENABLED" + false -> "DISABLED" + null -> "UNKNOWN" + } + output.add("$flag $flagState ${symbol.toPrettyString()}") + } + output.sort() + return output +} + +fun main(args: Array) = MainCommand().subcommands(CheckCommand(), ListCommand()).main(args)