Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1692a36d authored by Mårten Kongstad's avatar Mårten Kongstad
Browse files

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
parent 576e8188
Loading
Loading
Loading
Loading
+47 −7
Original line number Diff line number Diff line
@@ -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 <your-target> && source <this script>
# 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
+19 −0
Original line number Diff line number Diff line
@@ -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)
  }
}
+66 −1
Original line number Diff line number Diff line
@@ -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 "<fully-qualified-name-of-flag> <state-of-flag> <API>", 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<Pair<Symbol, Flag>> {
  val output = mutableSetOf<Pair<Symbol, Flag>>()
  val visitor =
@@ -460,4 +494,35 @@ internal fun findErrors(
  return errors
}

fun main(args: Array<String>) = 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
 * <pre>
 *   &lt;fully-qualified-name-of-flag&lt; &lt;state-of-flag&lt; &lt;API&lt;
 * </pre>
 *
 * @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<Pair<Symbol, Flag>>,
    flags: Map<Flag, Boolean>
): List<String> {
  val output = mutableListOf<String>()
  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<String>) = MainCommand().subcommands(CheckCommand(), ListCommand()).main(args)