Handle static binary repetition of system deps

Test: b build `bmod toybox-static`
Test: go test bp2build tests
Change-Id: Id728f6fd08832a4fc153f0ff5282cdfb6b19c2f6
diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go
index d320599..974245e 100644
--- a/android/allowlists/allowlists.go
+++ b/android/allowlists/allowlists.go
@@ -931,9 +931,6 @@
 		"test_fips",                     // depends on unconverted modules: adb
 		"timezone-host",                 // depends on unconverted modules: art.module.api.annotations
 
-		// '//bionic/libc:libc_bp2build_cc_library_static' is duplicated in the 'deps' attribute of rule
-		"toybox-static",
-
 		// aidl files not created
 		"overlayable_policy_aidl_interface",
 
diff --git a/bp2build/cc_binary_conversion_test.go b/bp2build/cc_binary_conversion_test.go
index 18cb9e1..18fc6e7 100644
--- a/bp2build/cc_binary_conversion_test.go
+++ b/bp2build/cc_binary_conversion_test.go
@@ -1214,3 +1214,82 @@
 		},
 	})
 }
+
+func TestCcBinaryStatic_SystemSharedLibUsedAsDep(t *testing.T) {
+	runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
+		description: "cc_library_static system_shared_lib empty for linux_bionic variant",
+		blueprint: soongCcLibraryStaticPreamble +
+			simpleModuleDoNotConvertBp2build("cc_library", "libc") + `
+
+cc_library {
+    name: "libm",
+    bazel_module: { bp2build_available: false },
+}
+
+cc_binary {
+    name: "used_in_bionic_oses",
+    target: {
+        android: {
+            static_libs: ["libc"],
+        },
+        linux_bionic: {
+            static_libs: ["libc"],
+        },
+    },
+    include_build_directory: false,
+    static_executable: true,
+}
+
+cc_binary {
+    name: "all",
+    static_libs: ["libc"],
+    include_build_directory: false,
+    static_executable: true,
+}
+
+cc_binary {
+    name: "keep_for_empty_system_shared_libs",
+    static_libs: ["libc"],
+    system_shared_libs: [],
+    include_build_directory: false,
+    static_executable: true,
+}
+
+cc_binary {
+    name: "used_with_stubs",
+    static_libs: ["libm"],
+    include_build_directory: false,
+    static_executable: true,
+}
+
+cc_binary {
+    name: "keep_with_stubs",
+    static_libs: ["libm"],
+    system_shared_libs: [],
+    include_build_directory: false,
+    static_executable: true,
+}
+`,
+		targets: []testBazelTarget{
+			{"cc_binary", "all", AttrNameToString{
+				"linkshared": "False",
+			}},
+			{"cc_binary", "keep_for_empty_system_shared_libs", AttrNameToString{
+				"deps":        `[":libc_bp2build_cc_library_static"]`,
+				"system_deps": `[]`,
+				"linkshared":  "False",
+			}},
+			{"cc_binary", "keep_with_stubs", AttrNameToString{
+				"linkshared":  "False",
+				"deps":        `[":libm_bp2build_cc_library_static"]`,
+				"system_deps": `[]`,
+			}},
+			{"cc_binary", "used_in_bionic_oses", AttrNameToString{
+				"linkshared": "False",
+			}},
+			{"cc_binary", "used_with_stubs", AttrNameToString{
+				"linkshared": "False",
+			}},
+		},
+	})
+}
diff --git a/cc/bp2build.go b/cc/bp2build.go
index 5459595..48ed7ac 100644
--- a/cc/bp2build.go
+++ b/cc/bp2build.go
@@ -1139,6 +1139,7 @@
 	wholeArchiveDeps                 bazel.LabelListAttribute
 	implementationWholeArchiveDeps   bazel.LabelListAttribute
 	systemDynamicDeps                bazel.LabelListAttribute
+	usedSystemDynamicDepAsStaticDep  map[string]bool
 	usedSystemDynamicDepAsDynamicDep map[string]bool
 
 	useVersionLib                 bazel.BoolAttribute
@@ -1201,6 +1202,18 @@
 	// https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
 	la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
 
+	if isBinary && module.StaticExecutable() {
+		usedSystemStatic := android.FilterListPred(staticLibs, func(s string) bool {
+			return android.InList(s, soongSystemSharedLibs) && !android.InList(s, props.Exclude_static_libs)
+		})
+
+		for _, el := range usedSystemStatic {
+			if la.usedSystemDynamicDepAsStaticDep == nil {
+				la.usedSystemDynamicDepAsStaticDep = map[string]bool{}
+			}
+			la.usedSystemDynamicDepAsStaticDep[el] = true
+		}
+	}
 	staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(
 		ctx,
 		!isBinary,
@@ -1233,6 +1246,7 @@
 	usedSystem := android.FilterListPred(sharedLibs, func(s string) bool {
 		return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs)
 	})
+
 	for _, el := range usedSystem {
 		if la.usedSystemDynamicDepAsDynamicDep == nil {
 			la.usedSystemDynamicDepAsDynamicDep = map[string]bool{}
@@ -1625,6 +1639,15 @@
 		}
 		la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList(stubsToRemove))
 	}
+	if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsStaticDep) > 0 {
+		toRemove := bazelLabelForStaticDeps(ctx, android.SortedKeys(la.usedSystemDynamicDepAsStaticDep))
+		la.deps.Exclude(bazel.NoConfigAxis, "", toRemove)
+		la.deps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
+		la.deps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
+		la.implementationDeps.Exclude(bazel.NoConfigAxis, "", toRemove)
+		la.implementationDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
+		la.implementationDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
+	}
 
 	la.deps.ResolveExcludes()
 	la.implementationDeps.ResolveExcludes()