Add support for symlink_preferred_arch in apex

Some modules rely on symlink_preferred_arch to have expected files
present. This change makes apexs include these symlinks.

Test: m com.android.runtime.debug
      pushd $(mktemp -d)
      mkdir mnt
      unzip $OUT/apex/system/com.android.runtime.debug.apex
      sudo mount -o loop,ro apex_payload.img mnt
      Ensure that mnt/bin/dalvikvm and mnt/bin/dex2oatd both exist and
      are symlinks to mnt/bin/dalvikvm64 and mnt/bin/dex2oatd32
      respectively.

Bug: 119942078
Bug: 122373634
Bug: 123079311
Change-Id: I47868fbedc5bdd3141a836c488f79e91e0a6ddfe
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 4ca5b14..7ae49f6 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -107,6 +107,16 @@
 			recovery_available: true,
 		}
 
+		cc_object {
+			name: "crtbegin_static",
+			stl: "none",
+		}
+
+		cc_object {
+			name: "crtend_android",
+			stl: "none",
+		}
+
 		llndk_library {
 			name: "libc",
 			symbol_file: "",
@@ -194,6 +204,11 @@
 			name: "myapex",
 			key: "myapex.key",
 			native_shared_libs: ["mylib"],
+			multilib: {
+				both: {
+					binaries: ["foo",],
+				}
+			}
 		}
 
 		apex_key {
@@ -210,6 +225,25 @@
 			stl: "none",
 		}
 
+		cc_binary {
+			name: "foo",
+			srcs: ["mylib.cpp"],
+			compile_multilib: "both",
+			multilib: {
+					lib32: {
+							suffix: "32",
+					},
+					lib64: {
+							suffix: "64",
+					},
+			},
+			symlinks: ["foo_link_"],
+			symlink_preferred_arch: true,
+			system_shared_libs: [],
+			static_executable: true,
+			stl: "none",
+		}
+
 		cc_library {
 			name: "mylib2",
 			srcs: ["mylib.cpp"],
@@ -237,6 +271,23 @@
 	// Ensure that the platform variant ends with _core_shared
 	ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
 	ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
+
+	// Ensure that all symlinks are present.
+	found_foo_link_64 := false
+	found_foo := false
+	for _, cmd := range strings.Split(copyCmds, " && ") {
+		if strings.HasPrefix(cmd, "ln -s foo64") {
+			if strings.HasSuffix(cmd, "bin/foo") {
+				found_foo = true
+			} else if strings.HasSuffix(cmd, "bin/foo_link_64") {
+				found_foo_link_64 = true
+			}
+		}
+	}
+	good := found_foo && found_foo_link_64
+	if !good {
+		t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
+	}
 }
 
 func TestBasicZipApex(t *testing.T) {
@@ -671,17 +722,6 @@
 			system_shared_libs: [],
 			stl: "none",
 		}
-
-		cc_object {
-			name: "crtbegin_static",
-			stl: "none",
-		}
-
-		cc_object {
-			name: "crtend_android",
-			stl: "none",
-		}
-
 	`)
 
 	ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]