Merge "Revert "Make RuleBuilder methods take Paths""
diff --git a/Android.bp b/Android.bp
index 36a428a..ee51942 100644
--- a/Android.bp
+++ b/Android.bp
@@ -67,6 +67,7 @@
         "android/testing.go",
         "android/util.go",
         "android/variable.go",
+        "android/vts_config.go",
         "android/writedocs.go",
 
         // Lock down environment access last
@@ -85,6 +86,7 @@
         "android/rule_builder_test.go",
         "android/util_test.go",
         "android/variable_test.go",
+        "android/vts_config_test.go",
     ],
 }
 
diff --git a/android/vts_config.go b/android/vts_config.go
new file mode 100644
index 0000000..f468ba8
--- /dev/null
+++ b/android/vts_config.go
@@ -0,0 +1,70 @@
+// Copyright 2016 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 android
+
+import (
+	"fmt"
+	"io"
+)
+
+// Implements vts_config module
+
+func init() {
+	RegisterModuleType("vts_config", VtsConfigFactory)
+}
+
+type vtsConfigProperties struct {
+	// Test manifest file name if different from AndroidTest.xml.
+	Test_config *string
+}
+
+type VtsConfig struct {
+	ModuleBase
+	properties     vtsConfigProperties
+	OutputFilePath OutputPath
+}
+
+func (me *VtsConfig) GenerateAndroidBuildActions(ctx ModuleContext) {
+	me.OutputFilePath = PathForModuleOut(ctx, me.BaseModuleName()).OutputPath
+}
+
+func (me *VtsConfig) AndroidMk() AndroidMkData {
+	androidMkData := AndroidMkData{
+		Class:      "FAKE",
+		Include:    "$(BUILD_SYSTEM)/android_vts_host_config.mk",
+		OutputFile: OptionalPathForPath(me.OutputFilePath),
+	}
+	if me.properties.Test_config != nil {
+		androidMkData.Extra = []AndroidMkExtraFunc{
+			func(w io.Writer, outputFile Path) {
+				fmt.Fprintf(w, "LOCAL_TEST_CONFIG := %s\n",
+					*me.properties.Test_config)
+			},
+		}
+	}
+	return androidMkData
+}
+
+func InitVtsConfigModule(me *VtsConfig) {
+	me.AddProperties(&me.properties)
+}
+
+// Defines VTS configuration.
+func VtsConfigFactory() Module {
+	module := &VtsConfig{}
+	InitVtsConfigModule(module)
+	InitAndroidArchModule(module /*TODO: or HostAndDeviceSupported? */, HostSupported, MultilibFirst)
+	return module
+}
diff --git a/android/vts_config_test.go b/android/vts_config_test.go
new file mode 100644
index 0000000..7d4c9b1
--- /dev/null
+++ b/android/vts_config_test.go
@@ -0,0 +1,61 @@
+// 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 android
+
+import (
+	"io/ioutil"
+	"os"
+	"testing"
+)
+
+func testVtsConfig(test *testing.T, bpFileContents string) *TestContext {
+	buildDir, err := ioutil.TempDir("", "soong_vts_config_test")
+	if err != nil {
+		test.Fatal(err)
+	}
+
+	config := TestArchConfig(buildDir, nil)
+	defer func() { os.RemoveAll(buildDir) }()
+
+	ctx := NewTestArchContext()
+	ctx.RegisterModuleType("vts_config", ModuleFactoryAdaptor(VtsConfigFactory))
+	ctx.Register()
+	mockFiles := map[string][]byte{
+		"Android.bp": []byte(bpFileContents),
+	}
+	ctx.MockFileSystem(mockFiles)
+	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
+	FailIfErrored(test, errs)
+	_, errs = ctx.PrepareBuildActions(config)
+	FailIfErrored(test, errs)
+	return ctx
+}
+
+func TestVtsConfig(t *testing.T) {
+	ctx := testVtsConfig(t, `
+vts_config { name: "plain"}
+vts_config { name: "with_manifest", test_config: "manifest.xml" }
+`)
+
+	variants := ctx.ModuleVariantsForTests("plain")
+	if len(variants) > 1 {
+		t.Errorf("expected 1, got %d", len(variants))
+	}
+	expectedOutputFilename := ctx.ModuleForTests(
+		"plain", variants[0]).Module().(*VtsConfig).OutputFilePath.Base()
+	if expectedOutputFilename != "plain" {
+		t.Errorf("expected plain, got %q", expectedOutputFilename)
+	}
+}
diff --git a/androidmk/cmd/androidmk/android.go b/androidmk/cmd/androidmk/android.go
index 1ecda2d..a5dfcd9 100644
--- a/androidmk/cmd/androidmk/android.go
+++ b/androidmk/cmd/androidmk/android.go
@@ -105,6 +105,7 @@
 			"LOCAL_MANIFEST_FILE":           "manifest",
 
 			"LOCAL_DEX_PREOPT_PROFILE_CLASS_LISTING": "dex_preopt.profile",
+			"LOCAL_TEST_CONFIG":                      "test_config",
 		})
 	addStandardProperties(bpparser.ListType,
 		map[string]string{
@@ -513,8 +514,8 @@
 
 func prebuiltClass(ctx variableAssignmentContext) error {
 	class := ctx.mkvalue.Value(ctx.file.scope)
-	if v, ok := prebuiltTypes[class]; ok {
-		ctx.file.scope.Set("BUILD_PREBUILT", v)
+	if _, ok := prebuiltTypes[class]; ok {
+		ctx.file.scope.Set("BUILD_PREBUILT", class)
 	} else {
 		// reset to default
 		ctx.file.scope.Set("BUILD_PREBUILT", "prebuilt")
@@ -873,6 +874,19 @@
 
 var soongModuleTypes = map[string]bool{}
 
+var includePathToModule = map[string]string{
+	"test/vts/tools/build/Android.host_config.mk": "vts_config",
+	// The rest will be populated dynamically in androidScope below
+}
+
+func mapIncludePath(path string) (string, bool) {
+	if path == clear_vars || path == include_ignored {
+		return path, true
+	}
+	module, ok := includePathToModule[path]
+	return module, ok
+}
+
 func androidScope() mkparser.Scope {
 	globalScope := mkparser.NewScope(nil)
 	globalScope.Set("CLEAR_VARS", clear_vars)
@@ -887,12 +901,17 @@
 	globalScope.SetFunc("first-makefiles-under", includeIgnored)
 	globalScope.SetFunc("all-named-subdir-makefiles", includeIgnored)
 	globalScope.SetFunc("all-subdir-makefiles", includeIgnored)
-	for k, v := range moduleTypes {
-		globalScope.Set(k, v)
-		soongModuleTypes[v] = true
+
+	// The scope maps each known variable to a path, and then includePathToModule maps a path
+	// to a module. We don't care what the actual path value is so long as the value in scope
+	// is mapped, so we might as well use variable name as key, too.
+	for varName, moduleName := range moduleTypes {
+		path := varName
+		globalScope.Set(varName, path)
+		includePathToModule[path] = moduleName
 	}
-	for _, v := range prebuiltTypes {
-		soongModuleTypes[v] = true
+	for varName, moduleName := range prebuiltTypes {
+		includePathToModule[varName] = moduleName
 	}
 
 	return globalScope
diff --git a/androidmk/cmd/androidmk/androidmk.go b/androidmk/cmd/androidmk/androidmk.go
index b6a973c..0426b43 100644
--- a/androidmk/cmd/androidmk/androidmk.go
+++ b/androidmk/cmd/androidmk/androidmk.go
@@ -169,20 +169,21 @@
 			handleAssignment(file, x, assignmentCond)
 		case *mkparser.Directive:
 			switch x.Name {
-			case "include":
-				val := x.Args.Value(file.scope)
-				switch {
-				case soongModuleTypes[val]:
-					handleModuleConditionals(file, x, conds)
-					makeModule(file, val)
-				case val == clear_vars:
+			case "include", "-include":
+				module, ok := mapIncludePath(x.Args.Value(file.scope))
+				if !ok {
+					file.errorf(x, "unsupported include")
+					continue
+				}
+				switch module {
+				case clear_vars:
 					resetModule(file)
-				case val == include_ignored:
+				case include_ignored:
 					// subdirs are already automatically included in Soong
 					continue
 				default:
-					file.errorf(x, "unsupported include")
-					continue
+					handleModuleConditionals(file, x, conds)
+					makeModule(file, module)
 				}
 			case "ifeq", "ifneq", "ifdef", "ifndef":
 				args := x.Args.Dump()
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index 52b8476..618dd42 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -1028,6 +1028,19 @@
 }
 `,
 	},
+	{
+		desc: "vts_config",
+		in: `
+include $(CLEAR_VARS)
+LOCAL_MODULE := vtsconf
+include test/vts/tools/build/Android.host_config.mk
+`,
+		expected: `
+vts_config {
+	name: "vtsconf",
+}
+`,
+	},
 }
 
 func TestEndToEnd(t *testing.T) {
diff --git a/apex/apex.go b/apex/apex.go
index 408415e..8639337 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -389,6 +389,9 @@
 	// list of files to be included in this apex
 	filesInfo []apexFile
 
+	// list of module names that this APEX is depending on
+	externalDeps []string
+
 	flattened bool
 
 	testApex bool
@@ -731,6 +734,14 @@
 			if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
 				if cc, ok := child.(*cc.Module); ok {
 					if cc.IsStubs() || cc.HasStubsVariants() {
+						// If the dependency is a stubs lib, don't include it in this APEX,
+						// but make sure that the lib is installed on the device.
+						// In case no APEX is having the lib, the lib is installed to the system
+						// partition.
+						if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
+							a.externalDeps = append(a.externalDeps, cc.Name())
+						}
+						// Don't track further
 						return false
 					}
 					depName := ctx.OtherModuleName(child)
@@ -1126,6 +1137,9 @@
 				if len(moduleNames) > 0 {
 					fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
 				}
+				if len(a.externalDeps) > 0 {
+					fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
+				}
 				fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
 
 				if apexType == imageApex {
diff --git a/apex/key.go b/apex/key.go
index 4c83861..5d7c2fa 100644
--- a/apex/key.go
+++ b/apex/key.go
@@ -29,7 +29,6 @@
 func init() {
 	android.RegisterModuleType("apex_key", apexKeyFactory)
 	android.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
-	android.RegisterMakeVarsProvider(pctx, apexKeysFileProvider)
 }
 
 type apexKey struct {
@@ -108,11 +107,12 @@
 
 ////////////////////////////////////////////////////////////////////////
 // apex_keys_text
-type apexKeysText struct{}
+type apexKeysText struct {
+	output android.OutputPath
+}
 
 func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
-	output := android.PathForOutput(ctx, "apexkeys.txt")
-	*apexKeysFile(ctx.Config()) = output.String()
+	s.output = android.PathForOutput(ctx, "apexkeys.txt")
 	var filecontent strings.Builder
 	ctx.VisitAllModules(func(module android.Module) {
 		if m, ok := module.(android.Module); ok && !m.Enabled() {
@@ -131,27 +131,18 @@
 	})
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        android.WriteFile,
-		Description: "apex_keys.txt",
-		Output:      output,
+		Description: "apexkeys.txt",
+		Output:      s.output,
 		Args: map[string]string{
 			"content": filecontent.String(),
 		},
 	})
 }
 
-var apexKeysFileKey = android.NewOnceKey("apexKeysFile")
-
-func apexKeysFile(config android.Config) *string {
-	return config.Once(apexKeysFileKey, func() interface{} {
-		str := ""
-		return &str
-	}).(*string)
-}
-
 func apexKeysTextFactory() android.Singleton {
 	return &apexKeysText{}
 }
 
-func apexKeysFileProvider(ctx android.MakeVarsContext) {
-	ctx.Strict("SOONG_APEX_KEYS_FILE", *apexKeysFile(ctx.Config()))
+func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
+	ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
 }
diff --git a/cc/config/x86_darwin_host.go b/cc/config/x86_darwin_host.go
index 09632db..1026370 100644
--- a/cc/config/x86_darwin_host.go
+++ b/cc/config/x86_darwin_host.go
@@ -79,6 +79,7 @@
 		"-framework Foundation",
 		"-framework IOKit",
 		"-framework Security",
+		"-framework SystemConfiguration",
 	)
 )