Copy shared_libs and system_shared_libs to module snapshot

This change ensures that the runtime dependencies between a
binary/shared library are correctly specified in the snapshot so that
the build can ensure that shared libraries are built before the targets
that use them.

It adds support for differentiating between references that are
required to refer to another sdk member (required) and those that may
refer to either an sdk member or a non-sdk member (optional). The
latter is used for shared library references as the libraries used by
an sdk member may be provided from outside the sdk. e.g. liblog is not
part of the ART module but is used by some members of the ART sdk.

Bug: 142935992
Test: m nothing
Change-Id: Ia8509ffe79b208c23beba1880fe9c8a92b732685
diff --git a/sdk/cc_sdk_test.go b/sdk/cc_sdk_test.go
index 2b0fd3c..0f84c4c 100644
--- a/sdk/cc_sdk_test.go
+++ b/sdk/cc_sdk_test.go
@@ -309,7 +309,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_shared {
@@ -326,7 +325,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 sdk_snapshot {
@@ -555,7 +553,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_shared {
@@ -577,7 +574,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 sdk_snapshot {
@@ -599,6 +595,189 @@
 	)
 }
 
+func TestSnapshotWithCcSharedLibrarySharedLibs(t *testing.T) {
+	result := testSdkWithCc(t, `
+		sdk {
+			name: "mysdk",
+			native_shared_libs: [
+				"mynativelib",
+				"myothernativelib",
+				"mysystemnativelib",
+			],
+		}
+
+		cc_library {
+			name: "mysystemnativelib",
+			srcs: [
+				"Test.cpp",
+			],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		cc_library_shared {
+			name: "myothernativelib",
+			srcs: [
+				"Test.cpp",
+			],
+			system_shared_libs: [
+				// A reference to a library that is not an sdk member. Uses libm as that
+				// is in the default set of modules available to this test and so is available
+				// both here and also when the generated Android.bp file is tested in
+				// CheckSnapshot(). This ensures that the system_shared_libs property correctly
+				// handles references to modules that are not sdk members.
+				"libm",
+			],
+			stl: "none",
+		}
+
+		cc_library {
+			name: "mynativelib",
+			srcs: [
+				"Test.cpp",
+			],
+			shared_libs: [
+				// A reference to another sdk member.
+				"myothernativelib",
+			],
+			target: {
+				android: {
+					shared: {
+						shared_libs: [
+							// A reference to a library that is not an sdk member. The libc library
+							// is used here to check that the shared_libs property is handled correctly
+							// in a similar way to how libm is used to check system_shared_libs above.
+							"libc",
+						],
+					},
+				},
+			},
+			system_shared_libs: [],
+			stl: "none",
+		}
+	`)
+
+	result.CheckSnapshot("mysdk", "",
+		checkAndroidBpContents(`
+// This is auto-generated. DO NOT EDIT.
+
+cc_prebuilt_library_shared {
+    name: "mysdk_mynativelib@current",
+    sdk_member_name: "mynativelib",
+    installable: false,
+    shared_libs: [
+        "mysdk_myothernativelib@current",
+        "libc",
+    ],
+    arch: {
+        arm64: {
+            srcs: ["arm64/lib/mynativelib.so"],
+        },
+        arm: {
+            srcs: ["arm/lib/mynativelib.so"],
+        },
+    },
+    stl: "none",
+}
+
+cc_prebuilt_library_shared {
+    name: "mynativelib",
+    prefer: false,
+    shared_libs: [
+        "myothernativelib",
+        "libc",
+    ],
+    arch: {
+        arm64: {
+            srcs: ["arm64/lib/mynativelib.so"],
+        },
+        arm: {
+            srcs: ["arm/lib/mynativelib.so"],
+        },
+    },
+    stl: "none",
+}
+
+cc_prebuilt_library_shared {
+    name: "mysdk_myothernativelib@current",
+    sdk_member_name: "myothernativelib",
+    installable: false,
+    system_shared_libs: ["libm"],
+    arch: {
+        arm64: {
+            srcs: ["arm64/lib/myothernativelib.so"],
+        },
+        arm: {
+            srcs: ["arm/lib/myothernativelib.so"],
+        },
+    },
+    stl: "none",
+}
+
+cc_prebuilt_library_shared {
+    name: "myothernativelib",
+    prefer: false,
+    system_shared_libs: ["libm"],
+    arch: {
+        arm64: {
+            srcs: ["arm64/lib/myothernativelib.so"],
+        },
+        arm: {
+            srcs: ["arm/lib/myothernativelib.so"],
+        },
+    },
+    stl: "none",
+}
+
+cc_prebuilt_library_shared {
+    name: "mysdk_mysystemnativelib@current",
+    sdk_member_name: "mysystemnativelib",
+    installable: false,
+    arch: {
+        arm64: {
+            srcs: ["arm64/lib/mysystemnativelib.so"],
+        },
+        arm: {
+            srcs: ["arm/lib/mysystemnativelib.so"],
+        },
+    },
+    stl: "none",
+}
+
+cc_prebuilt_library_shared {
+    name: "mysystemnativelib",
+    prefer: false,
+    arch: {
+        arm64: {
+            srcs: ["arm64/lib/mysystemnativelib.so"],
+        },
+        arm: {
+            srcs: ["arm/lib/mysystemnativelib.so"],
+        },
+    },
+    stl: "none",
+}
+
+sdk_snapshot {
+    name: "mysdk@current",
+    native_shared_libs: [
+        "mysdk_mynativelib@current",
+        "mysdk_myothernativelib@current",
+        "mysdk_mysystemnativelib@current",
+    ],
+}
+`),
+		checkAllCopyRules(`
+.intermediates/mynativelib/android_arm64_armv8-a_shared/mynativelib.so -> arm64/lib/mynativelib.so
+.intermediates/mynativelib/android_arm_armv7-a-neon_shared/mynativelib.so -> arm/lib/mynativelib.so
+.intermediates/myothernativelib/android_arm64_armv8-a_shared/myothernativelib.so -> arm64/lib/myothernativelib.so
+.intermediates/myothernativelib/android_arm_armv7-a-neon_shared/myothernativelib.so -> arm/lib/myothernativelib.so
+.intermediates/mysystemnativelib/android_arm64_armv8-a_shared/mysystemnativelib.so -> arm64/lib/mysystemnativelib.so
+.intermediates/mysystemnativelib/android_arm_armv7-a-neon_shared/mysystemnativelib.so -> arm/lib/mysystemnativelib.so
+`),
+	)
+}
+
 func TestHostSnapshotWithCcSharedLibrary(t *testing.T) {
 	// b/145598135 - Generating host snapshots for anything other than linux is not supported.
 	SkipIfNotLinux(t)
@@ -652,7 +831,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_shared {
@@ -673,7 +851,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 sdk_snapshot {
@@ -753,7 +930,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_shared {
@@ -773,7 +949,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 sdk_snapshot {
@@ -833,7 +1008,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_static {
@@ -851,7 +1025,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 module_exports_snapshot {
@@ -924,7 +1097,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_static {
@@ -944,7 +1116,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 module_exports_snapshot {
@@ -1020,7 +1191,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_static {
@@ -1036,7 +1206,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 module_exports_snapshot {
@@ -1084,7 +1253,6 @@
     sdk_member_name: "mynativeheaders",
     export_include_dirs: ["include/include"],
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_headers {
@@ -1092,7 +1260,6 @@
     prefer: false,
     export_include_dirs: ["include/include"],
     stl: "none",
-    system_shared_libs: [],
 }
 
 sdk_snapshot {
@@ -1139,7 +1306,6 @@
     host_supported: true,
     export_include_dirs: ["include/include"],
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_headers {
@@ -1149,7 +1315,6 @@
     host_supported: true,
     export_include_dirs: ["include/include"],
     stl: "none",
-    system_shared_libs: [],
 }
 
 sdk_snapshot {
@@ -1211,7 +1376,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 cc_prebuilt_library_headers {
@@ -1228,7 +1392,6 @@
         },
     },
     stl: "none",
-    system_shared_libs: [],
 }
 
 sdk_snapshot {