Merge "product_specific support for apex_key"
diff --git a/android/arch.go b/android/arch.go
index 151cabd..797c84c 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -155,18 +155,32 @@
 		"mips64r6",
 	},
 	X86: {
+		"amberlake",
 		"atom",
+		"broadwell",
 		"haswell",
+		"icelake",
 		"ivybridge",
+		"kabylake",
 		"sandybridge",
 		"silvermont",
+		"skylake",
+		"tigerlake",
+		"whiskeylake",
 		"x86_64",
 	},
 	X86_64: {
+		"amberlake",
+		"broadwell",
 		"haswell",
+		"icelake",
 		"ivybridge",
+		"kabylake",
 		"sandybridge",
 		"silvermont",
+		"skylake",
+		"tigerlake",
+		"whiskeylake",
 	},
 }
 
@@ -190,6 +204,8 @@
 		"sse4_2",
 		"aes_ni",
 		"avx",
+		"avx2",
+		"avx512",
 		"popcnt",
 		"movbe",
 	},
@@ -200,6 +216,8 @@
 		"sse4_2",
 		"aes_ni",
 		"avx",
+		"avx2",
+		"avx512",
 		"popcnt",
 	},
 }
@@ -230,10 +248,30 @@
 		},
 	},
 	X86: {
+		"amberlake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"aes_ni",
+			"popcnt",
+		},
 		"atom": {
 			"ssse3",
 			"movbe",
 		},
+		"broadwell": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"aes_ni",
+			"popcnt",
+		},
 		"haswell": {
 			"ssse3",
 			"sse4",
@@ -244,6 +282,17 @@
 			"popcnt",
 			"movbe",
 		},
+		"icelake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
 		"ivybridge": {
 			"ssse3",
 			"sse4",
@@ -253,6 +302,16 @@
 			"avx",
 			"popcnt",
 		},
+		"kabylake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"aes_ni",
+			"popcnt",
+		},
 		"sandybridge": {
 			"ssse3",
 			"sse4",
@@ -269,6 +328,39 @@
 			"popcnt",
 			"movbe",
 		},
+		"skylake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
+		"tigerlake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
+		"whiskeylake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
 		"x86_64": {
 			"ssse3",
 			"sse4",
@@ -278,6 +370,26 @@
 		},
 	},
 	X86_64: {
+		"amberlake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"aes_ni",
+			"popcnt",
+		},
+		"broadwell": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"aes_ni",
+			"popcnt",
+		},
 		"haswell": {
 			"ssse3",
 			"sse4",
@@ -287,6 +399,17 @@
 			"avx",
 			"popcnt",
 		},
+		"icelake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
 		"ivybridge": {
 			"ssse3",
 			"sse4",
@@ -296,6 +419,16 @@
 			"avx",
 			"popcnt",
 		},
+		"kabylake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"aes_ni",
+			"popcnt",
+		},
 		"sandybridge": {
 			"ssse3",
 			"sse4",
@@ -311,6 +444,39 @@
 			"aes_ni",
 			"popcnt",
 		},
+		"skylake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
+		"tigerlake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
+		"whiskeylake": {
+			"ssse3",
+			"sse4",
+			"sse4_1",
+			"sse4_2",
+			"avx",
+			"avx2",
+			"avx512",
+			"aes_ni",
+			"popcnt",
+		},
 	},
 }
 
diff --git a/android/module.go b/android/module.go
index c1f3e79..c5252c8 100644
--- a/android/module.go
+++ b/android/module.go
@@ -124,6 +124,7 @@
 	InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
 	InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
 	InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
+	InstallAbsoluteSymlink(installPath OutputPath, name string, absPath string) OutputPath
 	CheckbuildFile(srcPath Path)
 
 	AddMissingDependencies(deps []string)
@@ -1316,6 +1317,28 @@
 	return fullInstallPath
 }
 
+// installPath/name -> absPath where absPath might be a path that is available only at runtime
+// (e.g. /apex/...)
+func (a *androidModuleContext) InstallAbsoluteSymlink(installPath OutputPath, name string, absPath string) OutputPath {
+	fullInstallPath := installPath.Join(a, name)
+	a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
+
+	if !a.skipInstall(fullInstallPath) {
+		a.Build(pctx, BuildParams{
+			Rule:        Symlink,
+			Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
+			Output:      fullInstallPath,
+			Default:     !a.Config().EmbeddedInMake(),
+			Args: map[string]string{
+				"fromPath": absPath,
+			},
+		})
+
+		a.installFiles = append(a.installFiles, fullInstallPath)
+	}
+	return fullInstallPath
+}
+
 func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
 	a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
 }
diff --git a/android/vts_config.go b/android/vts_config.go
index f468ba8..c44b3a3 100644
--- a/android/vts_config.go
+++ b/android/vts_config.go
@@ -19,14 +19,12 @@
 	"io"
 )
 
-// Implements vts_config module
-
 func init() {
 	RegisterModuleType("vts_config", VtsConfigFactory)
 }
 
 type vtsConfigProperties struct {
-	// Test manifest file name if different from AndroidTest.xml.
+	// Override the default (AndroidTest.xml) test manifest file name.
 	Test_config *string
 }
 
@@ -61,7 +59,8 @@
 	me.AddProperties(&me.properties)
 }
 
-// Defines VTS configuration.
+// vts_config generates a Vendor Test Suite (VTS) configuration file from the
+// <test_config> xml file and stores it in a subdirectory of $(HOST_OUT).
 func VtsConfigFactory() Module {
 	module := &VtsConfig{}
 	InitVtsConfigModule(module)
diff --git a/cc/androidmk.go b/cc/androidmk.go
index fc791fe..d229d0c 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -158,6 +158,9 @@
 			if len(library.Properties.Overrides) > 0 {
 				fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES := "+strings.Join(library.Properties.Overrides, " "))
 			}
+			if len(library.post_install_cmds) > 0 {
+				fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD := "+strings.Join(library.post_install_cmds, "&& "))
+			}
 		})
 	} else if library.header() {
 		ret.Class = "HEADER_LIBRARIES"
@@ -231,6 +234,9 @@
 		if len(binary.Properties.Overrides) > 0 {
 			fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES := "+strings.Join(binary.Properties.Overrides, " "))
 		}
+		if len(binary.post_install_cmds) > 0 {
+			fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD := "+strings.Join(binary.post_install_cmds, "&& "))
+		}
 	})
 }
 
diff --git a/cc/binary.go b/cc/binary.go
index 4794815..9952943 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -15,6 +15,8 @@
 package cc
 
 import (
+	"path/filepath"
+
 	"github.com/google/blueprint"
 
 	"android/soong/android"
@@ -91,6 +93,8 @@
 
 	// Location of the file that should be copied to dist dir when requested
 	distFile android.OptionalPath
+
+	post_install_cmds []string
 }
 
 var _ linker = (*binaryDecorator)(nil)
@@ -249,7 +253,7 @@
 				} else {
 					switch ctx.Os() {
 					case android.Android:
-						if ctx.bootstrap() {
+						if ctx.bootstrap() && !ctx.inRecovery() {
 							flags.DynamicLinker = "/system/bin/bootstrap/linker"
 						} else {
 							flags.DynamicLinker = "/system/bin/linker"
@@ -413,7 +417,29 @@
 	return binary.symlinks
 }
 
+// /system/bin/linker -> /apex/com.android.runtime/bin/linker
+func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
+	dir := binary.baseInstaller.installDir(ctx)
+	dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
+	target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
+
+	ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
+	binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
+
+	for _, symlink := range binary.symlinks {
+		ctx.InstallAbsoluteSymlink(dir, symlink, target)
+		binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, symlink, target))
+	}
+}
+
 func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
+	// Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
+	// The original path becomes a symlink to the corresponding file in the
+	// runtime APEX.
+	if isBionic(ctx.baseModuleName()) && ctx.Arch().Native && ctx.apexName() == "" && !ctx.inRecovery() {
+		binary.installSymlinkToRuntimeApex(ctx, file)
+		binary.baseInstaller.subDir = "bootstrap"
+	}
 	binary.baseInstaller.install(ctx, file)
 	for _, symlink := range binary.symlinks {
 		ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
diff --git a/cc/cc.go b/cc/cc.go
index 284b58d..c036b84 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -599,6 +599,14 @@
 	return Bool(c.Properties.Bootstrap)
 }
 
+func isBionic(name string) bool {
+	switch name {
+	case "libc", "libm", "libdl", "linker":
+		return true
+	}
+	return false
+}
+
 type baseModuleContext struct {
 	android.BaseContext
 	moduleContextImpl
diff --git a/cc/config/x86_64_device.go b/cc/config/x86_64_device.go
index 4264eda..8a86db4 100644
--- a/cc/config/x86_64_device.go
+++ b/cc/config/x86_64_device.go
@@ -38,6 +38,10 @@
 		"": []string{
 			"-march=x86-64",
 		},
+		"broadwell": []string{
+			"-march=broadwell",
+		},
+
 		"haswell": []string{
 			"-march=core-avx2",
 		},
@@ -50,6 +54,9 @@
 		"silvermont": []string{
 			"-march=slm",
 		},
+		"skylake": []string{
+			"-march=skylake",
+		},
 	}
 
 	x86_64ArchFeatureCflags = map[string][]string{
@@ -59,6 +66,8 @@
 		"sse4_2": []string{"-msse4.2"},
 		"popcnt": []string{"-mpopcnt"},
 		"avx":    []string{"-mavx"},
+		"avx2":   []string{"-mavx2"},
+		"avx512": []string{"-mavx512"},
 		"aes_ni": []string{"-maes"},
 	}
 )
diff --git a/cc/config/x86_device.go b/cc/config/x86_device.go
index 34e7df8..b3d6a4d 100644
--- a/cc/config/x86_device.go
+++ b/cc/config/x86_device.go
@@ -51,6 +51,10 @@
 			"-march=atom",
 			"-mfpmath=sse",
 		},
+		"broadwell": []string{
+			"-march=broadwell",
+			"-mfpmath=sse",
+		},
 		"haswell": []string{
 			"-march=core-avx2",
 			"-mfpmath=sse",
@@ -67,6 +71,10 @@
 			"-march=slm",
 			"-mfpmath=sse",
 		},
+		"skylake": []string{
+			"-march=skylake",
+			"-mfpmath=sse",
+		},
 	}
 
 	x86ArchFeatureCflags = map[string][]string{
diff --git a/cc/library.go b/cc/library.go
index 71a9df6..6404906 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -15,6 +15,7 @@
 package cc
 
 import (
+	"path/filepath"
 	"regexp"
 	"sort"
 	"strconv"
@@ -290,6 +291,8 @@
 
 	versionScriptPath android.ModuleGenPath
 
+	post_install_cmds []string
+
 	// Decorated interafaces
 	*baseCompiler
 	*baseLinker
@@ -888,6 +891,14 @@
 	return library.tocFile
 }
 
+func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
+	dir := library.baseInstaller.installDir(ctx)
+	dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
+	target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), "bionic", file.Base())
+	ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
+	library.post_install_cmds = append(library.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
+}
+
 func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
 	if library.shared() {
 		if ctx.Device() && ctx.useVndk() {
@@ -905,15 +916,11 @@
 				}
 			}
 		} else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
-			// If a library in an APEX has stable versioned APIs, we basically don't need
-			// to have the platform variant of the library in /system partition because
-			// platform components can just use the lib from the APEX without fearing about
-			// compatibility. However, if the library is required for some early processes
-			// before the APEX is activated, the platform variant may also be required.
-			// In that case, it is installed to the subdirectory 'bootstrap' in order to
-			// be distinguished/isolated from other non-bootstrap libraries in /system/lib
-			// so that the bootstrap libraries are used only when the APEX isn't ready.
-			if !library.buildStubs() && ctx.Arch().Native {
+			// Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
+			// The original path becomes a symlink to the corresponding file in the
+			// runtime APEX.
+			if isBionic(ctx.baseModuleName()) && !library.buildStubs() && ctx.Arch().Native && !ctx.inRecovery() {
+				library.installSymlinkToRuntimeApex(ctx, file)
 				library.baseInstaller.subDir = "bootstrap"
 			}
 		}
diff --git a/cc/util.go b/cc/util.go
index 925dd74..782bf61 100644
--- a/cc/util.go
+++ b/cc/util.go
@@ -133,3 +133,10 @@
 
 	return root, suffix, ext
 }
+
+// linkDirOnDevice/linkName -> target
+func makeSymlinkCmd(linkDirOnDevice string, linkName string, target string) string {
+	dir := filepath.Join("$(PRODUCT_OUT)", linkDirOnDevice)
+	return "mkdir -p " + dir + " && " +
+		"ln -sf " + target + " " + filepath.Join(dir, linkName)
+}