Generalize ART APEX flavor handling in `art_apex_test.py`.

Introduce flag `--flavor` in `build/apex/art_apex_test.py` to select
the flavor of the ART APEX to check (`release`, `debug`, `testing`,
`auto`). Default to `auto`, which auto-detects the flavor based on the
name of the APEX passed as argument to the script.

Update `build/apex/runtests.sh` and `build/apex/Android.bp` to use
these new options instead of `--debug` and `--testing`.

Test: art/build/apex/runtests.sh
Bug: 139277987
Change-Id: I463ae6504576f77ee70b3ac4111bd43c49e450d0
diff --git a/build/apex/Android.bp b/build/apex/Android.bp
index 284cc18..c9263f3 100644
--- a/build/apex/Android.bp
+++ b/build/apex/Android.bp
@@ -460,6 +460,7 @@
     defaults: ["art-check-apex-gen-defaults"],
     srcs: [":com.android.art.release"],
     cmd: art_check_apex_gen_stem +
+        " --flavor release" +
         " $(in)" +
         " && touch $(out)",
     out: ["art-check-release-apex-gen.dummy"],
@@ -476,7 +477,7 @@
     defaults: ["art-check-apex-gen-defaults"],
     srcs: [":com.android.art.debug"],
     cmd: art_check_apex_gen_stem +
-        " --debug" +
+        " --flavor debug" +
         " $(in)" +
         " && touch $(out)",
     out: ["art-check-debug-apex-gen.dummy"],
@@ -493,7 +494,7 @@
     defaults: ["art-check-apex-gen-defaults"],
     srcs: [":com.android.art.testing"],
     cmd: art_check_apex_gen_stem +
-        " --testing" +
+        " --flavor testing" +
         " $(in)" +
         " && touch $(out)",
     out: ["art-check-testing-apex-gen.dummy"],
diff --git a/build/apex/art_apex_test.py b/build/apex/art_apex_test.py
index 2b42404..6d29fc9 100755
--- a/build/apex/art_apex_test.py
+++ b/build/apex/art_apex_test.py
@@ -1047,9 +1047,21 @@
   if test_args.size and not (test_args.list or test_args.tree):
     logging.error("--size set but neither --list nor --tree set")
     return 1
+  if test_args.host and test_args.flavor:
+    logging.error("Both of --host and --flavor set")
+    return 1
   if test_args.host and test_args.testing:
     logging.error("Both of --host and --testing set")
     return 1
+  if test_args.debug and test_args.testing:
+    logging.error("Both of --debug and --testing set")
+    return 1
+  if test_args.flavor and test_args.debug:
+    logging.error("Both of --flavor and --debug set")
+    return 1
+  if test_args.flavor and test_args.testing:
+    logging.error("Both of --flavor and --testing set")
+    return 1
   if not test_args.flattened and not test_args.tmpdir:
     logging.error("Need a tmpdir.")
     return 1
@@ -1078,6 +1090,30 @@
     List(apex_provider, test_args.size).print_list()
     return 0
 
+  # Handle legacy flavor flags.
+  if test_args.debug:
+    logging.warning('Using deprecated option --debug')
+    test_args.flavor='debug'
+  if test_args.testing:
+    logging.warning('Using deprecated option --testing')
+    test_args.flavor='testing'
+  if test_args.flavor == 'auto':
+    logging.warning('--flavor=auto, trying to autodetect. This may be incorrect!')
+    if fnmatch.fnmatch(test_args.apex, '*.release*'):
+      logging.warning('  Detected Release APEX')
+      test_args.flavor='release'
+    elif fnmatch.fnmatch(test_args.apex, '*.debug*'):
+      logging.warning('  Detected Debug APEX')
+      test_args.flavor='debug'
+    elif fnmatch.fnmatch(test_args.apex, '*.testing*'):
+      logging.warning('  Detected Testing APEX')
+      test_args.flavor='testing'
+    else:
+      logging.error('  Could not detect APEX flavor, neither \'release\', \'debug\' nor ' +
+                    '\'testing\' in \'%s\'',
+          test_args.apex)
+      return 1
+
   checkers = []
   if test_args.bitness == 'auto':
     logging.warning('--bitness=auto, trying to autodetect. This may be incorrect!')
@@ -1110,11 +1146,11 @@
     checkers.append(ReleaseHostChecker(base_checker))
   else:
     checkers.append(ReleaseTargetChecker(base_checker))
-  if test_args.debug or test_args.testing:
+  if test_args.flavor == 'debug' or test_args.flavor == 'testing':
     checkers.append(DebugChecker(base_checker))
     if not test_args.host:
       checkers.append(DebugTargetChecker(base_checker))
-  if test_args.testing:
+  if test_args.flavor == 'testing':
     checkers.append(TestingTargetChecker(base_checker))
 
   # These checkers must be last.
@@ -1162,11 +1198,12 @@
                   test_args.debugfs)
     sys.exit(1)
 
-  # TODO: Add host support
+  # TODO: Add host support.
+  # TODO: Add support for flattened APEX packages.
   configs = [
-    {'name': 'com.android.art.release', 'debug': False, 'testing': False, 'host': False},
-    {'name': 'com.android.art.debug',   'debug': True,  'testing': False, 'host': False},
-    {'name': 'com.android.art.testing', 'debug': False, 'testing': True,  'host': False},
+    {'name': 'com.android.art.release', 'flavor': 'release', 'host': False},
+    {'name': 'com.android.art.debug',   'flavor': 'debug',   'host': False},
+    {'name': 'com.android.art.testing', 'flavor': 'testing', 'host': False},
   ]
 
   for config in configs:
@@ -1177,8 +1214,7 @@
       failed = True
       logging.error("Cannot find APEX %s. Please build it first.", test_args.apex)
       continue
-    test_args.debug = config['debug']
-    test_args.testing = config['testing']
+    test_args.flavor = config['flavor']
     test_args.host = config['host']
     failed = art_apex_test_main(test_args) != 0
 
@@ -1189,14 +1225,18 @@
 if __name__ == "__main__":
   parser = argparse.ArgumentParser(description='Check integrity of a Runtime APEX.')
 
-  parser.add_argument('apex', help='apex file input')
+  parser.add_argument('apex', help='APEX file input')
 
-  parser.add_argument('--host', help='Check as host apex', action='store_true')
+  parser.add_argument('--host', help='Check as host APEX', action='store_true')
 
-  parser.add_argument('--flattened', help='Check as flattened (target) apex', action='store_true')
+  parser.add_argument('--flattened', help='Check as flattened (target) APEX', action='store_true')
 
-  parser.add_argument('--debug', help='Check as debug apex', action='store_true')
-  parser.add_argument('--testing', help='Check as testing apex', action='store_true')
+  parser.add_argument('--flavor', help='Check as FLAVOR APEX, release|debug|testing|auto',
+                      default='auto')
+  # Deprecated flavor flags.
+  # TODO: Stop supporting those flags eventually.
+  parser.add_argument('--debug', help='Check as debug APEX', action='store_true')
+  parser.add_argument('--testing', help='Check as testing APEX', action='store_true')
 
   parser.add_argument('--list', help='List all files', action='store_true')
   parser.add_argument('--tree', help='Print directory tree', action='store_true')
diff --git a/build/apex/runtests.sh b/build/apex/runtests.sh
index 7acdaca..c51e930 100755
--- a/build/apex/runtests.sh
+++ b/build/apex/runtests.sh
@@ -159,7 +159,7 @@
   if [[ $apex_module = *.host ]]; then
     apex_path="$ANDROID_HOST_OUT/apex/${apex_module}.zipapex"
     art_apex_test_args="$art_apex_test_args --host"
-    test_only_args="--debug"
+    test_only_args="--flavor debug"
   else
     if $flattened_apex_p; then
       apex_path="$ANDROID_PRODUCT_OUT/system/apex/${apex_module}"
@@ -171,8 +171,9 @@
       art_apex_test_args="$art_apex_test_args --debugfs $ANDROID_HOST_OUT/bin/debugfs"
     fi
     case $apex_module in
-      (*.debug)   test_only_args="--debug";;
-      (*.testing) test_only_args="--testing";;
+      (*.release) test_only_args="--flavor release";;
+      (*.debug)   test_only_args="--flavor debug";;
+      (*.testing) test_only_args="--flavor testing";;
     esac
   fi
   say "APEX package path: $apex_path"