Get build directory from fx

Run fx exec printenv and pull needed variables.

Change-Id: I92ccd74a0a38a36cbc88f52bec0be09394813304
diff --git a/src/build_rustc.rs b/src/build_rustc.rs
index 76b6f93..31fdd99 100644
--- a/src/build_rustc.rs
+++ b/src/build_rustc.rs
@@ -1,4 +1,5 @@
-use crate::sdk::{clang_base_path, clang_resource_dir, shared_libraries_path, sysroot_path, TargetOptions};
+use crate::sdk::{clang_base_path, clang_resource_dir, shared_libraries_path, sysroot_path,
+                 TargetOptions};
 use crate::X64;
 use crate::{get_target_triple, get_triple_cpu};
 
diff --git a/src/lib.rs b/src/lib.rs
index e56a31e..30664ea 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -494,9 +494,11 @@
         fs::canonicalize(std::env::current_exe()?)?
     };
 
-    let mut runner_args = vec![fargo_path
-        .to_str()
-        .ok_or_else(|| err_msg("unable to convert path to utf8 encoding"))?];
+    let mut runner_args = vec![
+        fargo_path
+            .to_str()
+            .ok_or_else(|| err_msg("unable to convert path to utf8 encoding"))?,
+    ];
 
     if options.verbose {
         runner_args.push("-v");
@@ -1136,7 +1138,7 @@
     let verbose = matches.is_present("verbose");
     let disable_cross = matches.is_present(DISABLE_CROSS_ENV);
 
-    let fuchsia_config = FuchsiaConfig::new()?;
+    let fuchsia_config = FuchsiaConfig::new_from_fx_exec()?;
     if verbose {
         println!("fuchsia_config = {:#?}", fuchsia_config);
     }
diff --git a/src/sdk.rs b/src/sdk.rs
index 46c6ac6..a608eb9 100644
--- a/src/sdk.rs
+++ b/src/sdk.rs
@@ -6,11 +6,9 @@
 use crate::utils::is_mac;
 use crate::X64;
 use failure::{bail, Error, ResultExt};
-use std::env;
-use std::fs::File;
-use std::io::Read;
 use std::path::PathBuf;
 use std::process::Command;
+use std::{env, str};
 
 /// The `TargetOptions` struct bundles together a number of parameters specific to
 /// the Fuchsia target that need to be passed through various internal functions. For
@@ -67,7 +65,7 @@
 }
 
 fn looks_like_fuchsia_dir(path: &PathBuf) -> bool {
-    for name in [".config", ".jiri_manifest"].iter() {
+    for name in [".fx-build-dir", ".jiri_manifest"].iter() {
         let config_path = path.join(name);
         if !config_path.exists() {
             return false;
@@ -233,49 +231,37 @@
 #[derive(Debug, Default)]
 pub struct FuchsiaConfig {
     pub fuchsia_build_dir: String,
-    pub fuchsia_variant: String,
     pub fuchsia_arch: String,
-    pub zircon_project: String,
 }
 
 impl FuchsiaConfig {
-    pub fn new() -> Result<FuchsiaConfig, Error> {
+    pub fn new_from_fx_exec() -> Result<FuchsiaConfig, Error> {
+        let fuchsia_dir = fuchsia_dir()?;
+        let fx_script = fx_path()?;
+        if !fx_script.exists() {
+            bail!("fx script not found at {:?}", fx_script);
+        }
+        let args = vec!["exec", "printenv"];
+        let fx_exec_result = Command::new(fx_script)
+            .args(args)
+            .current_dir(&fuchsia_dir)
+            .output()?;
+        let result = str::from_utf8(&fx_exec_result.stdout)?.trim().to_string();
         let mut config = FuchsiaConfig {
             fuchsia_build_dir: String::from(""),
-            fuchsia_variant: String::from(""),
             fuchsia_arch: String::from(""),
-            zircon_project: String::from(""),
         };
-        let fuchsia_dir = fuchsia_dir()?;
-        let config_path = fuchsia_dir.join(".config");
-        let mut config_file = File::open(&config_path)?;
-        let mut config_file_contents_str = String::new();
-        config_file.read_to_string(&mut config_file_contents_str)?;
-        for one_line in config_file_contents_str.lines() {
+        for one_line in result.lines() {
             let parts: Vec<&str> = one_line.split("=").collect();
-            if parts.len() == 2 {
-                const QUOTE: char = '\'';
-                match parts[0] {
-                    "FUCHSIA_BUILD_DIR" => {
-                        config.fuchsia_build_dir = String::from(parts[1].trim_matches(QUOTE))
-                    }
-                    "FUCHSIA_VARIANT" => {
-                        config.fuchsia_variant = String::from(parts[1].trim_matches(QUOTE))
-                    }
-                    "FUCHSIA_ARCH" => {
-                        config.fuchsia_arch = String::from(parts[1].trim_matches(QUOTE))
-                    }
-                    "ZIRCON_PROJECT" => {
-                        config.zircon_project = String::from(parts[1].trim_matches(QUOTE))
-                    }
-                    _ => (),
+            const QUOTE: char = '\'';
+            match parts[0] {
+                "FUCHSIA_BUILD_DIR" => {
+                    config.fuchsia_build_dir = String::from(parts[1].trim_matches(QUOTE))
                 }
+                "FUCHSIA_ARCH" => config.fuchsia_arch = String::from(parts[1].trim_matches(QUOTE)),
+                _ => (),
             }
         }
         Ok(config)
     }
-
-    pub fn is_release(&self) -> bool {
-        self.fuchsia_variant != "debug"
-    }
 }