Reexport vndk for vendor snapshot modules

Bug: 184795475
Test: run update.py with fake snapshot and see Android.bp
Change-Id: I5f3032574b957edaecf0dfa9f5e0c7981670d25a
diff --git a/vendor_snapshot/update.py b/vendor_snapshot/update.py
index fcc8433..9a4bb26 100644
--- a/vendor_snapshot/update.py
+++ b/vendor_snapshot/update.py
@@ -46,7 +46,6 @@
 def get_variation(json_rel_path):
     return json_rel_path.split('/')[2]
 
-
 # convert .bp prop dictionary to .bp prop string
 def gen_bp_prop(prop, ind):
     bp = ''
@@ -55,9 +54,10 @@
 
         # Skip empty list or dict, rather than printing empty prop like
         # "key: []," or "key: {},"
-        if type(val) == list or type(val) == dict:
-            if len(val) == 0:
-                continue
+        if type(val) == list and len(val) == 0:
+            continue
+        if type(val) == dict and gen_bp_prop(val, '') == '':
+            continue
 
         bp += ind + key + ': '
         if type(val) == bool:
@@ -116,6 +116,12 @@
     'src',
 }
 
+EXPORTED_FLAGS_PROPS = {
+    'export_include_dirs',
+    'export_system_include_dirs',
+    'export_flags',
+}
+
 
 # Converts parsed json dictionary (which is intermediate) to Android.bp prop
 # dictionary. This validates paths such as include directories and init_rc
@@ -147,7 +153,24 @@
 def is_64bit_arch(arch):
     return '64' in arch # arm64, x86_64
 
-def gen_bp_module(image, variation, name, version, target_arch, arch_props, bp_dir):
+def remove_keys_from_dict(keys, d):
+    # May contain subdictionaries (e.g. cfi), so recursively erase
+    for k in list(d.keys()):
+        if k in keys:
+            del d[k]
+        elif type(d[k]) == dict:
+            remove_keys_from_dict(keys, d[k])
+
+def reexport_vndk_header(name, arch_props):
+    remove_keys_from_dict(EXPORTED_FLAGS_PROPS, arch_props)
+    for arch in arch_props:
+        arch_props[arch]['shared_libs'] = [name]
+        arch_props[arch]['export_shared_lib_headers'] = [name]
+
+def gen_bp_module(image, variation, name, version, target_arch, vndk_list, arch_props, bp_dir):
+    # Generate Android.bp module for given snapshot.
+    # If a vndk library with the same name exists, reuses exported flags of the vndk library,
+    # instead of the snapshot's own flags.
     prop = {
         # These three are common for all snapshot modules.
         'version': str(version),
@@ -156,6 +179,15 @@
         'arch': {},
     }
 
+    reexport_vndk_name = name
+    if reexport_vndk_name == "libc++_static":
+        reexport_vndk_name = "libc++"
+
+    if reexport_vndk_name in vndk_list:
+        if variation == 'shared':
+            logging.error("Module %s is both vendor snapshot shared and vndk" % name)
+        reexport_vndk_header(reexport_vndk_name, arch_props)
+
     # Factor out common prop among architectures to minimize Android.bp.
     common_prop = None
     for arch in arch_props:
@@ -243,7 +275,7 @@
 
     return []
 
-def gen_bp_list_module(image, snapshot_version, vndk_dir, target_arch, arch_props):
+def gen_bp_list_module(image, snapshot_version, vndk_list, target_arch, arch_props):
     """Generates a {image}_snapshot module which contains lists of snapshots.
     For vendor snapshot, vndk list is also included, extracted from vndk_dir.
     """
@@ -254,7 +286,7 @@
     bp_props['name'] = '%s_snapshot' % image
     bp_props['version'] = str(snapshot_version)
     if image == 'vendor':
-        bp_props['vndk_libs'] = get_vndk_list(vndk_dir, target_arch)
+        bp_props['vndk_libs'] = vndk_list
 
     variant_to_property = {
         'shared': 'shared_libs',
@@ -364,17 +396,21 @@
     for target_arch in sorted(props):
         androidbp = ''
         bp_dir = os.path.join(install_dir, target_arch)
+        vndk_list = []
+        if image == 'vendor':
+            vndk_list = get_vndk_list(vndk_dir, target_arch)
 
         # Generate snapshot modules.
         for variation in sorted(props[target_arch]):
             for name in sorted(props[target_arch][variation]):
                 androidbp += gen_bp_module(image, variation, name,
                                            snapshot_version, target_arch,
+                                           vndk_list,
                                            props[target_arch][variation][name],
                                            bp_dir)
 
         # Generate {image}_snapshot module which contains the list of modules.
-        androidbp += gen_bp_list_module(image, snapshot_version, vndk_dir,
+        androidbp += gen_bp_list_module(image, snapshot_version, vndk_list,
                                         target_arch, props[target_arch])
         with open(os.path.join(bp_dir, 'Android.bp'), 'w') as f:
             logging.info('Generating Android.bp to: {}'.format(f.name))