Update profile_file discovery rules

Bug: http://b/63768402

Profile files are searched in multiple directories in a pre-define list,
instead of just one.

PGO profile_file enforcement is also removed - profile_file dependency
and -fprofile-use are added iff the profile_file is present.

Test: Build https://android-review.googlesource.com/c/474805 with
.profdata files in vendor/google_data/pgo-profiles and
toolchain/pgo-profiles and verify expected behavior.

Change-Id: I2604713a8a7a682bd5c6bde569dc32335be5eca9
Merged-In: I2604713a8a7a682bd5c6bde569dc32335be5eca9
diff --git a/cc/pgo.go b/cc/pgo.go
index fef962e..7ac5b2a 100644
--- a/cc/pgo.go
+++ b/cc/pgo.go
@@ -26,9 +26,12 @@
 	// Add flags to ignore warnings that profiles are old or missing for
 	// some functions
 	profileUseOtherFlags = []string{"-Wno-backend-plugin"}
-)
 
-const pgoProfileProject = "toolchain/pgo-profiles"
+	pgoProfileProjects = []string{
+		"toolchain/pgo-profiles",
+		"vendor/google_data/pgo-profiles",
+	}
+)
 
 const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
 const profileSamplingFlag = "-gline-tables-only"
@@ -84,6 +87,18 @@
 	return flags
 }
 
+func (props *PgoProperties) getPgoProfileFile(ctx ModuleContext) android.OptionalPath {
+	// Test if the profile_file is present in any of the pgoProfileProjects
+	for _, profileProject := range pgoProfileProjects {
+		path := android.ExistentPathForSource(ctx, "", profileProject, *props.Pgo.Profile_file)
+		if path.Valid() {
+			return path
+		}
+	}
+
+	return android.OptionalPathForPath(nil)
+}
+
 func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string {
 	if props.isInstrumentation() {
 		return fmt.Sprintf(profileUseInstrumentFormat, file)
@@ -101,24 +116,28 @@
 }
 
 func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags {
+	// Return if 'pgo' property is not present in this module.
+	if !props.PgoPresent {
+		return flags
+	}
+
 	// Skip -fprofile-use if 'enable_profile_use' property is set
 	if props.Pgo.Enable_profile_use != nil && *props.Pgo.Enable_profile_use == false {
 		return flags
 	}
 
-	// If the PGO profiles project is found, and this module has PGO
-	// enabled, add flags to use the profile
-	if profilesDir := getPgoProfilesDir(ctx); props.PgoPresent && profilesDir.Valid() {
-		profileFile := android.PathForSource(ctx, profilesDir.String(), *props.Pgo.Profile_file)
-		profileUseFlags := props.profileUseFlags(ctx, profileFile.String())
+	// If the profile file is found, add flags to use the profile
+	if profileFile := props.getPgoProfileFile(ctx); profileFile.Valid() {
+		profileFilePath := profileFile.Path()
+		profileUseFlags := props.profileUseFlags(ctx, profileFilePath.String())
 
 		flags.CFlags = append(flags.CFlags, profileUseFlags...)
 		flags.LdFlags = append(flags.LdFlags, profileUseFlags...)
 
 		// Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
 		// if profileFile gets updated
-		flags.CFlagsDeps = append(flags.CFlagsDeps, profileFile)
-		flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFile)
+		flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath)
+		flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath)
 	}
 	return flags
 }
@@ -164,10 +183,6 @@
 	return true
 }
 
-func getPgoProfilesDir(ctx ModuleContext) android.OptionalPath {
-	return android.ExistentPathForSource(ctx, "", pgoProfileProject)
-}
-
 func (pgo *pgo) begin(ctx BaseModuleContext) {
 	// TODO Evaluate if we need to support PGO for host modules
 	if ctx.Host() {