Use FUCHSIA_DIR instead of FUCHSIA_ROOT

But still fall back to FUCHSIA_ROOT for a while.

Change-Id: I97e9a46df4aa4665531bf171cad78f91f04c8476
diff --git a/src/device.rs b/src/device.rs
index 02c8b32..4224e6a 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -3,7 +3,7 @@
 // found in the LICENSE file.
 
 use failure::{err_msg, Error, ResultExt};
-use sdk::{fuchsia_root, target_out_dir, TargetOptions};
+use sdk::{fuchsia_dir, target_out_dir, TargetOptions};
 use std::{str, thread, time};
 use std::env;
 use std::path::{Path, PathBuf};
@@ -11,8 +11,8 @@
 use utils::is_mac;
 
 pub fn netaddr(verbose: bool, target_options: &TargetOptions) -> Result<String, Error> {
-    let fuchsia_root = fuchsia_root(target_options)?;
-    let netaddr_binary = fuchsia_root.join("out/build-zircon/tools/netaddr");
+    let fuchsia_dir = fuchsia_dir(target_options)?;
+    let netaddr_binary = fuchsia_dir.join("out/build-zircon/tools/netaddr");
     let mut args = vec!["--fuchsia"];
     if let Some(device_name) = target_options.device_name {
         args.push(device_name);
@@ -43,8 +43,8 @@
 }
 
 pub fn netls(verbose: bool, target_options: &TargetOptions) -> Result<(), Error> {
-    let fuchsia_root = fuchsia_root(target_options)?;
-    let netls_binary = fuchsia_root.join("out/build-zircon/tools/netls");
+    let fuchsia_dir = fuchsia_dir(target_options)?;
+    let netls_binary = fuchsia_dir.join("out/build-zircon/tools/netls");
     let mut netls_command = Command::new(netls_binary);
     netls_command.arg("--nowait").arg("--timeout=500");
     if verbose {
@@ -230,8 +230,8 @@
 pub fn start_emulator(
     with_graphics: bool, with_networking: bool, target_options: &TargetOptions
 ) -> Result<(), Error> {
-    let fuchsia_root = fuchsia_root(target_options)?;
-    let fx_script = fuchsia_root.join("scripts/fx");
+    let fuchsia_dir = fuchsia_dir(target_options)?;
+    let fx_script = fuchsia_dir.join("scripts/fx");
     if !fx_script.exists() {
         bail!("fx script not found at {:?}", fx_script);
     }
@@ -246,7 +246,7 @@
         .args(&args)
         .stdout(Stdio::null())
         .stderr(Stdio::null())
-        .current_dir(&fuchsia_root)
+        .current_dir(&fuchsia_dir)
         .spawn()
         .context("unable to run qemu")?;
 
diff --git a/src/facade.rs b/src/facade.rs
index 58e1d75..191da39 100644
--- a/src/facade.rs
+++ b/src/facade.rs
@@ -1,5 +1,5 @@
 use failure::{Error, ResultExt};
-use sdk::{fuchsia_root, fx_path, TargetOptions};
+use sdk::{fuchsia_dir, fx_path, TargetOptions};
 use std::collections::BTreeMap;
 use std::fs;
 use std::fs::File;
@@ -132,10 +132,10 @@
 }
 
 fn format_gn_file(path: &Path, target_options: &TargetOptions) -> Result<(), Error> {
-    let fuchsia_root = fuchsia_root(target_options)?;
+    let fuchsia_dir = fuchsia_dir(target_options)?;
     let fx_path = fx_path(target_options)?;
     Command::new(fx_path)
-        .current_dir(&fuchsia_root)
+        .current_dir(&fuchsia_dir)
         .arg("gn")
         .arg("format")
         .arg(path)
@@ -164,16 +164,16 @@
 }
 
 fn update_workspace_file(
-    crate_path: &Path, crate_name: &str, fuchsia_root: &Path
+    crate_path: &Path, crate_name: &str, fuchsia_dir: &Path
 ) -> Result<(), Error> {
-    let garnet_root = fuchsia_root.join("garnet");
+    let garnet_root = fuchsia_dir.join("garnet");
     let workspace_path = garnet_root.join("Cargo.toml");
     let mut workspace_file = File::open(&workspace_path)?;
     let mut workspace_contents_str = String::new();
     workspace_file.read_to_string(&mut workspace_contents_str)?;
     let mut decoded: Manifest = toml::from_str(&workspace_contents_str)?;
     {
-        let garnet_path = fuchsia_root.join("garnet");
+        let garnet_path = fuchsia_dir.join("garnet");
         let relative_crate_path = crate_path.strip_prefix(&garnet_path)?;
         let ref mut workspace = decoded.workspace.as_mut().unwrap();
         let ref mut members = workspace.members.as_mut().unwrap();
@@ -196,13 +196,13 @@
 
 pub fn create_facade(path_to_interface: &str, options: &TargetOptions) -> Result<(), Error> {
     let facade_target = FacadeTarget::parse(path_to_interface)?;
-    let fuchsia_root = fuchsia_root(options)?;
+    let fuchsia_dir = fuchsia_dir(options)?;
 
-    let interface_full_path = fuchsia_root.join(facade_target.fs_path);
-    let interface_relative_path = interface_full_path.strip_prefix(&fuchsia_root)?;
+    let interface_full_path = fuchsia_dir.join(facade_target.fs_path);
+    let interface_relative_path = interface_full_path.strip_prefix(&fuchsia_dir)?;
     let interface_relative = interface_relative_path.to_str().unwrap();
     let crate_name = facade_target.crate_name();
-    let crate_path = fuchsia_root
+    let crate_path = fuchsia_dir
         .join("garnet/public/rust/fidl_crates")
         .join(&crate_name);
 
@@ -215,7 +215,7 @@
         facade_target.label,
         options,
     )?;
-    update_workspace_file(&crate_path, &crate_name, &fuchsia_root)?;
+    update_workspace_file(&crate_path, &crate_name, &fuchsia_dir)?;
 
     println!("Created or updated facade crate at {:?}.", crate_path);
     Ok(())
diff --git a/src/sdk.rs b/src/sdk.rs
index 69060ec..e2dcb82 100644
--- a/src/sdk.rs
+++ b/src/sdk.rs
@@ -42,16 +42,35 @@
     }
 }
 
-pub fn fuchsia_root(options: &TargetOptions) -> Result<PathBuf, Error> {
-    let fuchsia_root_value = if let Ok(fuchsia_root_value) = env::var("FUCHSIA_ROOT") {
-        let fuchsia_root_path = PathBuf::from(&fuchsia_root_value);
-        if !fuchsia_root_path.is_dir() {
+fn get_path_from_env(env_name: &str, require_dir: bool) -> Result<Option<PathBuf>, Error> {
+    if let Ok(file_value) = env::var(env_name) {
+        let file_path = PathBuf::from(&file_value);
+        if !file_path.exists() {
             bail!(
-                "FUCHSIA_ROOT is set to '{}' but that path does not point to a directory.",
-                &fuchsia_root_value
+                "{} is set to '{}' but nothing exists at that path.",
+                env_name,
+                &file_value
             );
         }
-        fuchsia_root_path
+        if require_dir {
+            if !file_path.is_dir() {
+                bail!(
+                    "{} is set to '{}' but that path does not point to a directory.",
+                    env_name,
+                    &file_value
+                );
+            }
+        }
+        return Ok(Some(file_path));
+    }
+    Ok(None)
+}
+
+pub fn fuchsia_dir(options: &TargetOptions) -> Result<PathBuf, Error> {
+    let fuchsia_dir = if let Some(fuchsia_root) = get_path_from_env("FUCHSIA_ROOT", true)? {
+        fuchsia_root
+    } else if let Some(fuchsia_dir) = get_path_from_env("FUCHSIA_DIR", true)? {
+        fuchsia_dir
     } else {
         let mut path = env::current_dir().unwrap();
         loop {
@@ -62,19 +81,19 @@
                 path.to_path_buf()
             } else {
                 bail!(
-                    "FUCHSIA_ROOT not set and current directory is not in a Fuchsia tree with a \
-                     release-x64 build. You must set the environmental variable FUCHSIA_ROOT to \
+                    "FUCHSIA_DIR not set and current directory is not in a Fuchsia tree with a \
+                     release-x64 build. You must set the environmental variable FUCHSIA_DIR to \
                      point to a Fuchsia tree with a release-x64 build."
                 )
             }
         }
     };
 
-    Ok(PathBuf::from(fuchsia_root_value))
+    Ok(fuchsia_dir)
 }
 
 pub fn possible_target_out_dir(
-    fuchsia_root: &PathBuf, options: &TargetOptions
+    fuchsia_dir: &PathBuf, options: &TargetOptions
 ) -> Result<PathBuf, Error> {
     let out_dir_name_prefix = if options.release_os {
         "release"
@@ -82,7 +101,7 @@
         "debug"
     };
     let out_dir_name = format!("{}-{}", out_dir_name_prefix, options.target_cpu);
-    let target_out_dir = fuchsia_root.join("out").join(out_dir_name);
+    let target_out_dir = fuchsia_dir.join("out").join(out_dir_name);
     if !target_out_dir.exists() {
         bail!("no target out directory found at  {:?}", target_out_dir);
     }
@@ -90,8 +109,8 @@
 }
 
 pub fn target_out_dir(options: &TargetOptions) -> Result<PathBuf, Error> {
-    let fuchsia_root = fuchsia_root(options)?;
-    possible_target_out_dir(&fuchsia_root, options)
+    let fuchsia_dir = fuchsia_dir(options)?;
+    possible_target_out_dir(&fuchsia_dir, options)
 }
 
 pub fn target_gen_dir(options: &TargetOptions) -> Result<PathBuf, Error> {
@@ -100,9 +119,9 @@
 }
 
 pub fn cargo_out_dir(options: &TargetOptions) -> Result<PathBuf, Error> {
-    let fuchsia_root = fuchsia_root(options)?;
+    let fuchsia_dir = fuchsia_dir(options)?;
     let target_triple = format!("{}-unknown-fuchsia", options.target_cpu_linker);
-    Ok(fuchsia_root
+    Ok(fuchsia_dir
         .join("garnet")
         .join("target")
         .join(target_triple)
@@ -119,7 +138,7 @@
     } else {
         "build-arm64"
     };
-    Ok(fuchsia_root(&options)?
+    Ok(fuchsia_dir(&options)?
         .join("out")
         .join("build-zircon")
         .join(zircon_name)
@@ -132,25 +151,18 @@
     } else {
         "arm64-shared"
     };
-    Ok(fuchsia_root(&options)?.join("out").join(shared_name))
+    Ok(fuchsia_dir(&options)?.join("out").join(shared_name))
 }
 
 fn buildtools_path(target_options: &TargetOptions) -> Result<PathBuf, Error> {
     let platform_name = if is_mac() { "mac-x64" } else { "linux-x64" };
-    Ok(fuchsia_root(target_options)?
+    Ok(fuchsia_dir(target_options)?
         .join("buildtools")
         .join(platform_name))
 }
 
 pub fn cargo_path(target_options: &TargetOptions) -> Result<PathBuf, Error> {
-    if let Ok(cargo_path_value) = env::var("FARGO_CARGO") {
-        let cargo_path = PathBuf::from(&cargo_path_value);
-        if !cargo_path.exists() {
-            bail!(
-                "FARGO_CARGO is set to '{}' but that path does not point to a file.",
-                &cargo_path_value
-            );
-        }
+    if let Some(cargo_path) = get_path_from_env("FARGO_CARGO", false)? {
         Ok(cargo_path)
     } else {
         Ok(buildtools_path(target_options)?.join("rust/bin/cargo"))
@@ -158,14 +170,7 @@
 }
 
 pub fn rustc_path(target_options: &TargetOptions) -> Result<PathBuf, Error> {
-    if let Ok(rustc_path_value) = env::var("FARGO_RUSTC") {
-        let rustc_path = PathBuf::from(&rustc_path_value);
-        if !rustc_path.exists() {
-            bail!(
-                "FARGO_RUSTC is set to '{}' but that path does not point to a file.",
-                &rustc_path_value
-            );
-        }
+    if let Some(rustc_path) = get_path_from_env("FARGO_RUSTC", false)? {
         Ok(rustc_path)
     } else {
         Ok(buildtools_path(target_options)?.join("rust/bin/rustc"))
@@ -199,8 +204,8 @@
 }
 
 pub fn fx_path(target_options: &TargetOptions) -> Result<PathBuf, Error> {
-    let fuchsia_root = fuchsia_root(target_options)?;
-    Ok(fuchsia_root.join("scripts/fx"))
+    let fuchsia_dir = fuchsia_dir(target_options)?;
+    Ok(fuchsia_dir.join("scripts/fx"))
 }
 
 #[derive(Debug)]
@@ -219,8 +224,8 @@
             fuchsia_arch: String::from(""),
             zircon_project: String::from(""),
         };
-        let fuchsia_root = fuchsia_root(target_options)?;
-        let config_path = fuchsia_root.join(".config");
+        let fuchsia_dir = fuchsia_dir(target_options)?;
+        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)?;