Merge "Add Respfile support for soong_zip."
diff --git a/android/config.go b/android/config.go
index b5ec975..1e5dd52 100644
--- a/android/config.go
+++ b/android/config.go
@@ -710,6 +710,10 @@
 	return coverage
 }
 
+func (c *deviceConfig) PgoAdditionalProfileDirs() []string {
+	return c.config.ProductVariables.PgoAdditionalProfileDirs
+}
+
 func (c *config) IntegerOverflowDisabledForPath(path string) bool {
 	if c.ProductVariables.IntegerOverflowExcludePaths == nil {
 		return false
diff --git a/android/variable.go b/android/variable.go
index 2c2a0cf..328074a 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -205,6 +205,8 @@
 	ExtraVndkVersions []string `json:",omitempty"`
 
 	NamespacesToExport []string `json:",omitempty"`
+
+	PgoAdditionalProfileDirs []string `json:",omitempty"`
 }
 
 func boolPtr(v bool) *bool {
diff --git a/cc/pgo.go b/cc/pgo.go
index 3ce67be..1e70339 100644
--- a/cc/pgo.go
+++ b/cc/pgo.go
@@ -27,17 +27,24 @@
 	// some functions
 	profileUseOtherFlags = []string{"-Wno-backend-plugin"}
 
-	pgoProfileProjects = []string{
+	globalPgoProfileProjects = []string{
 		"toolchain/pgo-profiles",
 		"vendor/google_data/pgo-profiles",
 	}
 )
 
+const pgoProfileProjectsConfigKey = "PgoProfileProjects"
 const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
 const profileSamplingFlag = "-gline-tables-only"
 const profileUseInstrumentFormat = "-fprofile-use=%s"
 const profileUseSamplingFormat = "-fprofile-sample-use=%s"
 
+func getPgoProfileProjects(config android.DeviceConfig) []string {
+	return config.OnceStringSlice(pgoProfileProjectsConfigKey, func() []string {
+		return append(globalPgoProfileProjects, config.PgoAdditionalProfileDirs()...)
+	})
+}
+
 func recordMissingProfileFile(ctx ModuleContext, missing string) {
 	getNamedMapForConfig(ctx.Config(), modulesMissingProfileFile).Store(missing, true)
 }
@@ -92,8 +99,8 @@
 }
 
 func (props *PgoProperties) getPgoProfileFile(ctx ModuleContext) android.OptionalPath {
-	// Test if the profile_file is present in any of the pgoProfileProjects
-	for _, profileProject := range pgoProfileProjects {
+	// Test if the profile_file is present in any of the PGO profile projects
+	for _, profileProject := range getPgoProfileProjects(ctx.DeviceConfig()) {
 		path := android.ExistentPathForSource(ctx, "", profileProject, *props.Pgo.Profile_file)
 		if path.Valid() {
 			return path
diff --git a/cmd/pom2mk/pom2mk.go b/cmd/pom2mk/pom2mk.go
index 721e250..63bcbb6 100644
--- a/cmd/pom2mk/pom2mk.go
+++ b/cmd/pom2mk/pom2mk.go
@@ -98,8 +98,7 @@
 	ArtifactId string `xml:"artifactId"`
 	Version    string `xml:"version"`
 	Type       string `xml:"type"`
-
-	Scope string `xml:"scope"`
+	Scope      string `xml:"scope"`
 }
 
 func (d Dependency) MkName() string {
@@ -140,17 +139,19 @@
 }
 
 func (p Pom) MkJarDeps() []string {
-	return p.MkDeps("jar")
+	return p.MkDeps("jar", "compile")
 }
 
 func (p Pom) MkAarDeps() []string {
-	return p.MkDeps("aar")
+	return p.MkDeps("aar", "compile")
 }
 
-func (p Pom) MkDeps(typeExt string) []string {
+// MkDeps obtains dependencies filtered by type and scope. The results of this
+// method are formatted as Make targets, e.g. run through MavenToMk rules.
+func (p Pom) MkDeps(typeExt string, scope string) []string {
 	var ret []string
 	for _, d := range p.Dependencies {
-		if d.Type != typeExt {
+		if d.Type != typeExt || d.Scope != scope {
 			continue
 		}
 		name := rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
@@ -164,13 +165,22 @@
 	return sdkVersion
 }
 
-func (p *Pom) FixDepTypes(modules map[string]*Pom) {
+func (p *Pom) FixDeps(modules map[string]*Pom) {
 	for _, d := range p.Dependencies {
-		if d.Type != "" {
-			continue
+		if d.Type == "" {
+			if depPom, ok := modules[d.MkName()]; ok {
+				// We've seen the POM for this dependency, use its packaging
+				// as the dependency type rather than Maven spec default.
+				d.Type = depPom.Packaging
+			} else {
+				// Dependency type was not specified and we don't have the POM
+				// for this artifact, use the default from Maven spec.
+				d.Type = "jar"
+			}
 		}
-		if depPom, ok := modules[d.MkName()]; ok {
-			d.Type = depPom.Packaging
+		if d.Scope == "" {
+			// Scope was not specified, use the default from Maven spec.
+			d.Scope = "compile"
 		}
 	}
 }
@@ -360,7 +370,7 @@
 	}
 
 	for _, pom := range poms {
-		pom.FixDepTypes(modules)
+		pom.FixDeps(modules)
 	}
 
 	fmt.Println("# Automatically generated with:")