Make RuleBuilder methods take Paths

There are no more Make paths being used in Soong now that
dexpreopting and hiddenapi are in Soong. Use the Path types
in the inputs to RuleBuilder, and fix all users of RuleBuilder.

Test: all soong tests
Test: m checkbuild
Change-Id: I886f803d9a3419a43b2cae412537645f94c5dfbf
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
index ee3cc8d..6f8ea3a 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -17,6 +17,7 @@
 import (
 	"encoding/json"
 	"io/ioutil"
+	"strings"
 
 	"android/soong/android"
 )
@@ -74,12 +75,13 @@
 	InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
 
 	// Only used for boot image
-	DirtyImageObjects string   // path to a dirty-image-objects file
-	PreloadedClasses  string   // path to a preloaded-classes file
-	BootImageProfiles []string // path to a boot-image-profile.txt file
-	BootFlags         string   // extra flags to pass to dex2oat for the boot image
-	Dex2oatImageXmx   string   // max heap size for dex2oat for the boot image
-	Dex2oatImageXms   string   // initial heap size for dex2oat for the boot image
+	DirtyImageObjects      android.OptionalPath // path to a dirty-image-objects file
+	PreloadedClasses       android.OptionalPath // path to a preloaded-classes file
+	BootImageProfiles      android.Paths        // path to a boot-image-profile.txt file
+	UseProfileForBootImage bool                 // whether a profile should be used to compile the boot image
+	BootFlags              string               // extra flags to pass to dex2oat for the boot image
+	Dex2oatImageXmx        string               // max heap size for dex2oat for the boot image
+	Dex2oatImageXms        string               // initial heap size for dex2oat for the boot image
 
 	Tools Tools // paths to tools possibly used by the generated commands
 }
@@ -87,38 +89,38 @@
 // Tools contains paths to tools possibly used by the generated commands.  If you add a new tool here you MUST add it
 // to the order-only dependency list in DEXPREOPT_GEN_DEPS.
 type Tools struct {
-	Profman  string
-	Dex2oat  string
-	Aapt     string
-	SoongZip string
-	Zip2zip  string
+	Profman  android.Path
+	Dex2oat  android.Path
+	Aapt     android.Path
+	SoongZip android.Path
+	Zip2zip  android.Path
 
-	VerifyUsesLibraries string
-	ConstructContext    string
+	VerifyUsesLibraries android.Path
+	ConstructContext    android.Path
 }
 
 type ModuleConfig struct {
 	Name            string
 	DexLocation     string // dex location on device
-	BuildPath       string
-	DexPath         string
+	BuildPath       android.OutputPath
+	DexPath         android.Path
 	UncompressedDex bool
 	HasApkLibraries bool
 	PreoptFlags     []string
 
-	ProfileClassListing  string
+	ProfileClassListing  android.OptionalPath
 	ProfileIsTextListing bool
 
 	EnforceUsesLibraries  bool
 	OptionalUsesLibraries []string
 	UsesLibraries         []string
-	LibraryPaths          map[string]string
+	LibraryPaths          map[string]android.Path
 
 	Archs           []android.ArchType
-	DexPreoptImages []string
+	DexPreoptImages []android.Path
 
-	PreoptBootClassPathDexFiles     []string // file paths of boot class path files
-	PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
+	PreoptBootClassPathDexFiles     android.Paths // file paths of boot class path files
+	PreoptBootClassPathDexLocations []string      // virtual locations of boot class path files
 
 	PreoptExtractedApk bool // Overrides OnlyPreoptModules
 
@@ -128,24 +130,137 @@
 	PresignedPrebuilt bool
 
 	NoStripping     bool
-	StripInputPath  string
-	StripOutputPath string
+	StripInputPath  android.Path
+	StripOutputPath android.WritablePath
 }
 
-func LoadGlobalConfig(path string) (GlobalConfig, error) {
-	config := GlobalConfig{}
-	err := loadConfig(path, &config)
-	return config, err
+func constructPath(ctx android.PathContext, path string) android.Path {
+	buildDirPrefix := ctx.Config().BuildDir() + "/"
+	if path == "" {
+		return nil
+	} else if strings.HasPrefix(path, buildDirPrefix) {
+		return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
+	} else {
+		return android.PathForSource(ctx, path)
+	}
 }
 
-func LoadModuleConfig(path string) (ModuleConfig, error) {
-	config := ModuleConfig{}
-	err := loadConfig(path, &config)
-	return config, err
+func constructPaths(ctx android.PathContext, paths []string) android.Paths {
+	var ret android.Paths
+	for _, path := range paths {
+		ret = append(ret, constructPath(ctx, path))
+	}
+	return ret
 }
 
-func loadConfig(path string, config interface{}) error {
-	data, err := ioutil.ReadFile(path)
+func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
+	ret := map[string]android.Path{}
+	for key, path := range paths {
+		ret[key] = constructPath(ctx, path)
+	}
+	return ret
+}
+
+func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
+	if path == "" {
+		return nil
+	}
+	return constructPath(ctx, path).(android.WritablePath)
+}
+
+// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct.  It is used directly in Soong
+// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
+func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, error) {
+	type GlobalJSONConfig struct {
+		GlobalConfig
+
+		// Copies of entries in GlobalConfig that are not constructable without extra parameters.  They will be
+		// used to construct the real value manually below.
+		DirtyImageObjects string
+		PreloadedClasses  string
+		BootImageProfiles []string
+
+		Tools struct {
+			Profman  string
+			Dex2oat  string
+			Aapt     string
+			SoongZip string
+			Zip2zip  string
+
+			VerifyUsesLibraries string
+			ConstructContext    string
+		}
+	}
+
+	config := GlobalJSONConfig{}
+	err := loadConfig(ctx, path, &config)
+	if err != nil {
+		return config.GlobalConfig, err
+	}
+
+	// Construct paths that require a PathContext.
+	config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
+	config.GlobalConfig.PreloadedClasses = android.OptionalPathForPath(constructPath(ctx, config.PreloadedClasses))
+	config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
+
+	config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
+	config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
+	config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
+	config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
+	config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
+	config.GlobalConfig.Tools.VerifyUsesLibraries = constructPath(ctx, config.Tools.VerifyUsesLibraries)
+	config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
+
+	return config.GlobalConfig, nil
+}
+
+// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct.  It is not used in Soong, which
+// receives a ModuleConfig struct directly from java/dexpreopt.go.  It is used in dexpreopt_gen called from oMake to
+// read the module dexpreopt.config written by Make.
+func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
+	type ModuleJSONConfig struct {
+		ModuleConfig
+
+		// Copies of entries in ModuleConfig that are not constructable without extra parameters.  They will be
+		// used to construct the real value manually below.
+		BuildPath                   string
+		DexPath                     string
+		ProfileClassListing         string
+		LibraryPaths                map[string]string
+		DexPreoptImages             []string
+		PreoptBootClassPathDexFiles []string
+		StripInputPath              string
+		StripOutputPath             string
+	}
+
+	config := ModuleJSONConfig{}
+
+	err := loadConfig(ctx, path, &config)
+	if err != nil {
+		return config.ModuleConfig, err
+	}
+
+	// Construct paths that require a PathContext.
+	config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
+	config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
+	config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
+	config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
+	config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
+	config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
+	config.ModuleConfig.StripInputPath = constructPath(ctx, config.StripInputPath)
+	config.ModuleConfig.StripOutputPath = constructWritablePath(ctx, config.StripOutputPath)
+
+	return config.ModuleConfig, nil
+}
+
+func loadConfig(ctx android.PathContext, path string, config interface{}) error {
+	r, err := ctx.Fs().Open(path)
+	if err != nil {
+		return err
+	}
+	defer r.Close()
+
+	data, err := ioutil.ReadAll(r)
 	if err != nil {
 		return err
 	}
@@ -157,3 +272,56 @@
 
 	return nil
 }
+
+func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
+	return GlobalConfig{
+		DefaultNoStripping:                 false,
+		DisablePreoptModules:               nil,
+		OnlyPreoptBootImageAndSystemServer: false,
+		HasSystemOther:                     false,
+		PatternsOnSystemOther:              nil,
+		DisableGenerateProfile:             false,
+		BootJars:                           nil,
+		RuntimeApexJars:                    nil,
+		ProductUpdatableBootModules:        nil,
+		ProductUpdatableBootLocations:      nil,
+		SystemServerJars:                   nil,
+		SystemServerApps:                   nil,
+		SpeedApps:                          nil,
+		PreoptFlags:                        nil,
+		DefaultCompilerFilter:              "",
+		SystemServerCompilerFilter:         "",
+		GenerateDMFiles:                    false,
+		NeverAllowStripping:                false,
+		NoDebugInfo:                        false,
+		AlwaysSystemServerDebugInfo:        false,
+		NeverSystemServerDebugInfo:         false,
+		AlwaysOtherDebugInfo:               false,
+		NeverOtherDebugInfo:                false,
+		MissingUsesLibraries:               nil,
+		IsEng:                              false,
+		SanitizeLite:                       false,
+		DefaultAppImages:                   false,
+		Dex2oatXmx:                         "",
+		Dex2oatXms:                         "",
+		EmptyDirectory:                     "empty_dir",
+		CpuVariant:                         nil,
+		InstructionSetFeatures:             nil,
+		DirtyImageObjects:                  android.OptionalPath{},
+		PreloadedClasses:                   android.OptionalPath{},
+		BootImageProfiles:                  nil,
+		UseProfileForBootImage:             false,
+		BootFlags:                          "",
+		Dex2oatImageXmx:                    "",
+		Dex2oatImageXms:                    "",
+		Tools: Tools{
+			Profman:             android.PathForTesting("profman"),
+			Dex2oat:             android.PathForTesting("dex2oat"),
+			Aapt:                android.PathForTesting("aapt"),
+			SoongZip:            android.PathForTesting("soong_zip"),
+			Zip2zip:             android.PathForTesting("zip2zip"),
+			VerifyUsesLibraries: android.PathForTesting("verify_uses_libraries.sh"),
+			ConstructContext:    android.PathForTesting("construct_context.sh"),
+		},
+	}
+}