Use SRCDIR as a working directory

The existing behavior of using the build directory as the working
directory is useful if you want to move/copy the output directory around
and SRCDIR still refers the the source. But, it's more useful to have
the source directory be the working directory. Tools like cpp(__FILE__)
and other debug prints embed relative paths from the working directory.
We also have tools that expect the working directory to be $TOP.

Change-Id: Ia0f1d3c6b7df72d61cf5628efa2baa98bd19775b
diff --git a/common/config.go b/common/config.go
index f8231f0..a7675b9 100644
--- a/common/config.go
+++ b/common/config.go
@@ -24,8 +24,8 @@
 )
 
 // The configuration file name
-const ConfigFileName = "soong.config"
-const ProductVariablesFileName = "soong.variables"
+const configFileName = "soong.config"
+const productVariablesFileName = "soong.variables"
 
 // A FileConfigurableOptions contains options which can be configured by the
 // config file. These will be included in the config struct.
@@ -46,7 +46,11 @@
 	FileConfigurableOptions
 	ProductVariables productVariables
 
-	srcDir string // the path of the root source directory
+	ConfigFileName           string
+	ProductVariablesFileName string
+
+	srcDir   string // the path of the root source directory
+	buildDir string // the path of the build output directory
 
 	envLock   sync.Mutex
 	envDeps   map[string]string
@@ -58,12 +62,12 @@
 }
 
 func loadConfig(config *config) error {
-	err := loadFromConfigFile(&config.FileConfigurableOptions, ConfigFileName)
+	err := loadFromConfigFile(&config.FileConfigurableOptions, config.ConfigFileName)
 	if err != nil {
 		return err
 	}
 
-	return loadFromConfigFile(&config.ProductVariables, ProductVariablesFileName)
+	return loadFromConfigFile(&config.ProductVariables, config.ProductVariablesFileName)
 }
 
 // loads configuration options from a JSON file in the cwd.
@@ -120,12 +124,16 @@
 
 // New creates a new Config object.  The srcDir argument specifies the path to
 // the root source directory. It also loads the config file, if found.
-func NewConfig(srcDir string) (Config, error) {
+func NewConfig(srcDir, buildDir string) (Config, error) {
 	// Make a config with default options
 	config := Config{
 		config: &config{
-			srcDir:  srcDir,
-			envDeps: make(map[string]string),
+			ConfigFileName:           filepath.Join(buildDir, configFileName),
+			ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
+
+			srcDir:   srcDir,
+			buildDir: buildDir,
+			envDeps:  make(map[string]string),
 		},
 	}
 
@@ -142,8 +150,12 @@
 	return c.srcDir
 }
 
+func (c *config) BuildDir() string {
+	return c.buildDir
+}
+
 func (c *config) IntermediatesDir() string {
-	return ".intermediates"
+	return filepath.Join(c.BuildDir(), ".intermediates")
 }
 
 // PrebuiltOS returns the name of the host OS used in prebuilts directories
@@ -204,12 +216,12 @@
 
 // DeviceOut returns the path to out directory for device targets
 func (c *config) DeviceOut() string {
-	return filepath.Join("target/product", c.DeviceName())
+	return filepath.Join(c.BuildDir(), "target/product", c.DeviceName())
 }
 
 // HostOut returns the path to out directory for host targets
 func (c *config) HostOut() string {
-	return filepath.Join("host", c.PrebuiltOS())
+	return filepath.Join(c.BuildDir(), "host", c.PrebuiltOS())
 }
 
 // HostBin returns the path to bin directory for host targets