Merge "Don't strip when dexpreopt is disabled"
diff --git a/android/config.go b/android/config.go
index f046318..b3469a9 100644
--- a/android/config.go
+++ b/android/config.go
@@ -862,6 +862,24 @@
 	return c.config.productVariables.BoardPlatPrivateSepolicyDirs
 }
 
+func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
+	overrides := c.config.productVariables.ManifestPackageNameOverrides
+	if overrides == nil || len(overrides) == 0 {
+		return "", false
+	}
+	for _, o := range overrides {
+		split := strings.Split(o, ":")
+		if len(split) != 2 {
+			// This shouldn't happen as this is first checked in make, but just in case.
+			panic(fmt.Errorf("invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>", o))
+		}
+		if matchPattern(split[0], name) {
+			return substPattern(split[0], split[1], name), true
+		}
+	}
+	return "", false
+}
+
 func (c *config) SecondArchIsTranslated() bool {
 	deviceTargets := c.Targets[Android]
 	if len(deviceTargets) < 2 {
diff --git a/android/util.go b/android/util.go
index e9b9764..92ab845 100644
--- a/android/util.go
+++ b/android/util.go
@@ -202,3 +202,44 @@
 	}
 	return v
 }
+
+// copied from build/kati/strutil.go
+func substPattern(pat, repl, str string) string {
+	ps := strings.SplitN(pat, "%", 2)
+	if len(ps) != 2 {
+		if str == pat {
+			return repl
+		}
+		return str
+	}
+	in := str
+	trimed := str
+	if ps[0] != "" {
+		trimed = strings.TrimPrefix(in, ps[0])
+		if trimed == in {
+			return str
+		}
+	}
+	in = trimed
+	if ps[1] != "" {
+		trimed = strings.TrimSuffix(in, ps[1])
+		if trimed == in {
+			return str
+		}
+	}
+
+	rs := strings.SplitN(repl, "%", 2)
+	if len(rs) != 2 {
+		return repl
+	}
+	return rs[0] + trimed + rs[1]
+}
+
+// copied from build/kati/strutil.go
+func matchPattern(pat, str string) bool {
+	i := strings.IndexByte(pat, '%')
+	if i < 0 {
+		return pat == str
+	}
+	return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
+}
diff --git a/android/variable.go b/android/variable.go
index a1cdea1..264869a 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -261,6 +261,8 @@
 	FlattenApex *bool `json:",omitempty"`
 
 	DexpreoptGlobalConfig *string `json:",omitempty"`
+
+	ManifestPackageNameOverrides []string `json:",omitempty"`
 }
 
 func boolPtr(v bool) *bool {
diff --git a/apex/apex.go b/apex/apex.go
index c6d2e8e..b324644 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -492,6 +492,19 @@
 	if !cc.Arch().Native {
 		dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
 	}
+	switch cc.Name() {
+	case "libc", "libm", "libdl":
+		// Special case for bionic libs. This is to prevent the bionic libs
+		// from being included in the search path /apex/com.android.apex/lib.
+		// This exclusion is required because bionic libs in the runtime APEX
+		// are available via the legacy paths /system/lib/libc.so, etc. By the
+		// init process, the bionic libs in the APEX are bind-mounted to the
+		// legacy paths and thus will be loaded into the default linker namespace.
+		// If the bionic libs are directly in /apex/com.android.apex/lib then
+		// the same libs will be again loaded to the runtime linker namespace,
+		// which will result double loading of bionic libs that isn't supported.
+		dirInApex = filepath.Join(dirInApex, "bionic")
+	}
 
 	fileToCopy = cc.OutputFile().Path()
 	return
@@ -757,6 +770,11 @@
 			optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
 		}
 
+		manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
+		if overridden {
+			optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
+		}
+
 		ctx.Build(pctx, android.BuildParams{
 			Rule:        apexRule,
 			Implicits:   implicitInputs,
diff --git a/apex/apex_test.go b/apex/apex_test.go
index f8f9b33..d4f0f7e 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -492,6 +492,13 @@
 				versions: ["27", "28", "29"],
 			},
 		}
+
+		cc_library {
+			name: "libBootstrap",
+			srcs: ["mylib.cpp"],
+			stl: "none",
+			bootstrap: true,
+		}
 	`)
 
 	apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
@@ -499,11 +506,11 @@
 
 	// Ensure that mylib, libm, libdl are included.
 	ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
-	ensureContains(t, copyCmds, "image.apex/lib64/libm.so")
-	ensureContains(t, copyCmds, "image.apex/lib64/libdl.so")
+	ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
+	ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
 
 	// Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
-	ensureNotContains(t, copyCmds, "image.apex/lib64/libc.so")
+	ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
 
 	mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
 	mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
@@ -538,6 +545,12 @@
 	// ... Cflags from stub is correctly exported to mylib
 	ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
 	ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
+
+	// Ensure that libBootstrap is depending on the platform variant of bionic libs
+	libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
+	ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
+	ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
+	ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
 }
 
 func TestFilesInSubDir(t *testing.T) {
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 82feec8..7844756 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -29,9 +29,12 @@
 )
 
 type AndroidMkContext interface {
+	Name() string
 	Target() android.Target
 	subAndroidMk(*android.AndroidMkData, interface{})
 	useVndk() bool
+	static() bool
+	inRecovery() bool
 }
 
 type subAndroidMkProvider interface {
@@ -59,8 +62,12 @@
 
 	ret := android.AndroidMkData{
 		OutputFile: c.outputFile,
-		Required:   append(c.Properties.AndroidMkRuntimeLibs, c.Properties.ApexesProvidingSharedLibs...),
-		Include:    "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
+		// TODO(jiyong): add the APEXes providing shared libs to the required modules
+		// Currently, adding c.Properties.ApexesProvidingSharedLibs is causing multiple
+		// runtime APEXes (com.android.runtime.debug|release) to be installed. And this
+		// is breaking some older devices (like marlin) where system.img is small.
+		Required: c.Properties.AndroidMkRuntimeLibs,
+		Include:  "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
 
 		Extra: []android.AndroidMkExtraFunc{
 			func(w io.Writer, outputFile android.Path) {
@@ -172,13 +179,23 @@
 		}
 	})
 
-	if library.shared() {
+	if library.shared() && !library.buildStubs() {
 		ctx.subAndroidMk(ret, library.baseInstaller)
 	} else {
 		ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
 			fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
+			if library.buildStubs() {
+				fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
+			}
 		})
 	}
+
+	if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx.Name()) &&
+		!ctx.inRecovery() && !ctx.useVndk() && !ctx.static() {
+		if !library.buildStubs() {
+			ret.SubName = ".bootstrap"
+		}
+	}
 }
 
 func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
diff --git a/cc/cc.go b/cc/cc.go
index ce28a3b..85e3a67 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -201,6 +201,10 @@
 	Recovery_available *bool
 
 	InRecovery bool `blueprint:"mutated"`
+
+	// Allows this module to use non-APEX version of libraries. Useful
+	// for building binaries that are started before APEXes are activated.
+	Bootstrap *bool
 }
 
 type VendorProperties struct {
@@ -250,6 +254,8 @@
 	isPgoCompile() bool
 	useClangLld(actx ModuleContext) bool
 	isApex() bool
+	hasStubsVariants() bool
+	isStubs() bool
 }
 
 type ModuleContext interface {
@@ -676,6 +682,14 @@
 	return ctx.mod.ApexName() != ""
 }
 
+func (ctx *moduleContextImpl) hasStubsVariants() bool {
+	return ctx.mod.HasStubsVariants()
+}
+
+func (ctx *moduleContextImpl) isStubs() bool {
+	return ctx.mod.IsStubs()
+}
+
 func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
 	return &Module{
 		hod:      hod,
@@ -855,6 +869,18 @@
 			return
 		}
 		c.outputFile = android.OptionalPathForPath(outputFile)
+
+		// If a lib is directly included in any of the APEXes, unhide the stubs
+		// variant having the latest version gets visible to make. In addition,
+		// the non-stubs variant is renamed to <libname>.bootstrap. This is to
+		// force anything in the make world to link against the stubs library.
+		// (unless it is explicitly referenced via .bootstrap suffix or the
+		// module is marked with 'bootstrap: true').
+		if c.HasStubsVariants() && android.DirectlyInAnyApex(ctx.baseModuleName()) &&
+			!c.inRecovery() && !c.useVndk() && !c.static() && c.IsStubs() {
+			c.Properties.HideFromMake = false // unhide
+			// Note: this is still non-installable
+		}
 	}
 
 	if c.installer != nil && !c.Properties.PreventInstall && c.IsForPlatform() && c.outputFile.Valid() {
@@ -1446,8 +1472,8 @@
 					// If not building for APEX, use stubs only when it is from
 					// an APEX (and not from platform)
 					useThisDep = (depInPlatform != depIsStubs)
-					if c.inRecovery() {
-						// However, for recovery modules, since there is no APEX there,
+					if c.inRecovery() || Bool(c.Properties.Bootstrap) {
+						// However, for recovery or bootstrap modules,
 						// always link to non-stub variant
 						useThisDep = !depIsStubs
 					}
diff --git a/java/app.go b/java/app.go
index f99b299..3b2305f 100644
--- a/java/app.go
+++ b/java/app.go
@@ -184,6 +184,11 @@
 	// TODO: LOCAL_PACKAGE_OVERRIDES
 	//    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
 
+	manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
+	if overridden {
+		linkFlags = append(linkFlags, "--rename-manifest-package "+manifestPackageName)
+	}
+
 	a.aapt.buildActions(ctx, sdkContext(a), linkFlags...)
 
 	// apps manifests are handled by aapt, don't let Module see them
diff --git a/ui/build/Android.bp b/ui/build/Android.bp
index 0eba5ce..1ddaf68 100644
--- a/ui/build/Android.bp
+++ b/ui/build/Android.bp
@@ -47,6 +47,7 @@
         "environment.go",
         "exec.go",
         "finder.go",
+        "goma.go",
         "kati.go",
         "ninja.go",
         "path.go",
diff --git a/ui/build/build.go b/ui/build/build.go
index c902a0f..0ae06d6 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -156,6 +156,11 @@
 
 	SetupPath(ctx, config)
 
+	if config.StartGoma() {
+		// Ensure start Goma compiler_proxy
+		startGoma(ctx, config)
+	}
+
 	if what&BuildProductConfig != 0 {
 		// Run make for product config
 		runMakeProductConfig(ctx, config)
diff --git a/ui/build/config.go b/ui/build/config.go
index 97b009a..64270f8 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -478,6 +478,20 @@
 	return false
 }
 
+func (c *configImpl) StartGoma() bool {
+	if !c.UseGoma() {
+		return false
+	}
+
+	if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
+		v = strings.TrimSpace(v)
+		if v != "" && v != "false" {
+			return false
+		}
+	}
+	return true
+}
+
 // RemoteParallel controls how many remote jobs (i.e., commands which contain
 // gomacc) are run in parallel.  Note the parallelism of all other jobs is
 // still limited by Parallel()
diff --git a/ui/build/goma.go b/ui/build/goma.go
new file mode 100644
index 0000000..d0dc9cf
--- /dev/null
+++ b/ui/build/goma.go
@@ -0,0 +1,48 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package build
+
+import (
+	"errors"
+	"path/filepath"
+
+	"android/soong/ui/metrics"
+)
+
+const gomaCtlScript = "goma_ctl.py"
+
+var gomaCtlNotFound = errors.New("goma_ctl.py not found")
+
+func startGoma(ctx Context, config Config) error {
+	ctx.BeginTrace(metrics.RunSetupTool, "goma_ctl")
+	defer ctx.EndTrace()
+
+	var gomaCtl string
+	if gomaDir, ok := config.Environment().Get("GOMA_DIR"); ok {
+		gomaCtl = filepath.Join(gomaDir, gomaCtlScript)
+	} else if home, ok := config.Environment().Get("HOME"); ok {
+		gomaCtl = filepath.Join(home, "goma", gomaCtlScript)
+	} else {
+		return gomaCtlNotFound
+	}
+
+	cmd := Command(ctx, config, "goma_ctl.py ensure_start", gomaCtl, "ensure_start")
+
+	if err := cmd.Run(); err != nil {
+		ctx.Fatalf("goma_ctl.py ensure_start failed with: %v", err)
+	}
+
+	return nil
+}
diff --git a/ui/build/paths/config.go b/ui/build/paths/config.go
index 604bf90..ec5c028 100644
--- a/ui/build/paths/config.go
+++ b/ui/build/paths/config.go
@@ -81,7 +81,6 @@
 	"date":      Allowed,
 	"dd":        Allowed,
 	"diff":      Allowed,
-	"du":        Allowed,
 	"echo":      Allowed,
 	"egrep":     Allowed,
 	"expr":      Allowed,
@@ -158,6 +157,7 @@
 	"cp":       Toybox,
 	"cut":      Toybox,
 	"dirname":  Toybox,
+	"du":       Toybox,
 	"env":      Toybox,
 	"head":     Toybox,
 	"id":       Toybox,