From c169f52b2542886ed2244e4f7d903da7ba15463a Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Fri, 20 Oct 2017 09:03:21 -0700 Subject: [PATCH] Use env::var_os to read paths from the environment This avoids unnecessary UTF-8 validation on OsStrings that we just pass back to the OS. --- components/atoms/build.rs | 4 ++-- components/config/basedir.rs | 7 ++++--- components/script/build.rs | 4 ++-- components/selectors/build.rs | 2 +- components/style/build.rs | 2 +- components/style/build_gecko.rs | 14 +++++++------- ports/servo/build.rs | 4 ++-- 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/components/atoms/build.rs b/components/atoms/build.rs index c8edc9bcd5a..8a6ba8fd936 100644 --- a/components/atoms/build.rs +++ b/components/atoms/build.rs @@ -10,7 +10,7 @@ use std::io::{BufReader, BufRead}; use std::path::Path; fn main() { - let static_atoms = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("static_atoms.txt"); + let static_atoms = Path::new(&env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("static_atoms.txt"); let static_atoms = BufReader::new(File::open(&static_atoms).unwrap()); let mut atom_type = string_cache_codegen::AtomType::new("Atom", "atom!"); @@ -27,6 +27,6 @@ fn main() { atom_type .atoms(static_atoms.lines().map(Result::unwrap)) - .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("atom.rs")) + .write_to_file(&Path::new(&env::var_os("OUT_DIR").unwrap()).join("atom.rs")) .unwrap(); } diff --git a/components/config/basedir.rs b/components/config/basedir.rs index 521eb4e9dfd..0f403b81473 100644 --- a/components/config/basedir.rs +++ b/components/config/basedir.rs @@ -77,9 +77,10 @@ pub fn default_config_dir() -> Option { #[cfg(target_os = "windows")] pub fn default_config_dir() -> Option { - let mut config_dir = match env::var("APPDATA") { - Ok(appdata_path) => PathBuf::from(appdata_path), - Err(_) => { let mut dir = env::home_dir().unwrap(); + let mut config_dir = match env::var_os("APPDATA") { + Some(appdata_path) => PathBuf::from(appdata_path), + None => { + let mut dir = env::home_dir().unwrap(); dir.push("Appdata"); dir.push("Roaming"); dir diff --git a/components/script/build.rs b/components/script/build.rs index a19e286ba25..88317ca8f31 100644 --- a/components/script/build.rs +++ b/components/script/build.rs @@ -46,13 +46,13 @@ fn main() { println!("Binding generation completed in {}s", start.elapsed().as_secs()); - let json = PathBuf::from(env::var("OUT_DIR").unwrap()).join("build").join("InterfaceObjectMapData.json"); + let json = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("build").join("InterfaceObjectMapData.json"); let json: Value = serde_json::from_reader(File::open(&json).unwrap()).unwrap(); let mut map = phf_codegen::Map::new(); for (key, value) in json.as_object().unwrap() { map.entry(Bytes(key), value.as_str().unwrap()); } - let phf = PathBuf::from(env::var("OUT_DIR").unwrap()).join("InterfaceObjectMapPhf.rs"); + let phf = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("InterfaceObjectMapPhf.rs"); let mut phf = File::create(&phf).unwrap(); write!(&mut phf, "pub static MAP: phf::Map<&'static [u8], unsafe fn(*mut JSContext, HandleObject)> = ").unwrap(); map.build(&mut phf).unwrap(); diff --git a/components/selectors/build.rs b/components/selectors/build.rs index 11b72f01b86..a7c58c64a86 100644 --- a/components/selectors/build.rs +++ b/components/selectors/build.rs @@ -10,7 +10,7 @@ use std::io::{BufWriter, Write}; use std::path::Path; fn main() { - let path = Path::new(&env::var("OUT_DIR").unwrap()) + let path = Path::new(&env::var_os("OUT_DIR").unwrap()) .join("ascii_case_insensitive_html_attributes.rs"); let mut file = BufWriter::new(File::create(&path).unwrap()); diff --git a/components/style/build.rs b/components/style/build.rs index 607f895c297..da3d0587eb4 100644 --- a/components/style/build.rs +++ b/components/style/build.rs @@ -69,7 +69,7 @@ fn generate_properties() { } } - let script = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()) + let script = Path::new(&env::var_os("CARGO_MANIFEST_DIR").unwrap()) .join("properties").join("build.py"); let product = if cfg!(feature = "gecko") { "gecko" } else { "servo" }; let status = Command::new(&*PYTHON) diff --git a/components/style/build_gecko.rs b/components/style/build_gecko.rs index b8b0a56f72d..dd319555185 100644 --- a/components/style/build_gecko.rs +++ b/components/style/build_gecko.rs @@ -7,7 +7,7 @@ mod common { use std::path::{Path, PathBuf}; lazy_static! { - pub static ref OUTDIR_PATH: PathBuf = PathBuf::from(env::var("OUT_DIR").unwrap()).join("gecko"); + pub static ref OUTDIR_PATH: PathBuf = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("gecko"); } /// Copy contents of one directory into another. @@ -74,7 +74,7 @@ mod bindings { lazy_static! { static ref CONFIG: toml::Table = { // Load Gecko's binding generator config from the source tree. - let path = PathBuf::from(env::var("MOZ_SRC").unwrap()) + let path = PathBuf::from(env::var_os("MOZ_SRC").unwrap()) .join("layout/style/ServoBindings.toml"); read_config(&path) }; @@ -82,7 +82,7 @@ mod bindings { // Load build-specific config overrides. // FIXME: We should merge with CONFIG above instead of // forcing callers to do it. - let path = PathBuf::from(env::var("MOZ_TOPOBJDIR").unwrap()) + let path = PathBuf::from(env::var_os("MOZ_TOPOBJDIR").unwrap()) .join("layout/style/bindgen.toml"); read_config(&path) }; @@ -98,7 +98,7 @@ mod bindings { }; static ref INCLUDE_RE: Regex = Regex::new(r#"#include\s*"(.+?)""#).unwrap(); static ref DISTDIR_PATH: PathBuf = { - let path = PathBuf::from(env::var("MOZ_DIST").unwrap()); + let path = PathBuf::from(env::var_os("MOZ_DIST").unwrap()); if !path.is_absolute() || !path.is_dir() { panic!("MOZ_DIST must be an absolute directory, was: {}", path.display()); } @@ -452,7 +452,7 @@ mod bindings { } } - if let Ok(path) = env::var("STYLO_BUILD_LOG") { + if let Some(path) = env::var_os("STYLO_BUILD_LOG") { log::set_logger(|log_level| { log_level.set(log::LogLevelFilter::Debug); Box::new(BuildLogger { @@ -536,7 +536,7 @@ mod bindings { } fn generate_atoms() { - let script = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) + let script = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()) .join("gecko").join("regen_atoms.py"); println!("cargo:rerun-if-changed={}", script.display()); let status = Command::new(&*PYTHON) @@ -587,7 +587,7 @@ mod bindings { use super::common::*; pub fn generate() { - let dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("gecko/generated"); + let dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("gecko/generated"); println!("cargo:rerun-if-changed={}", dir.display()); copy_dir(&dir, &*OUTDIR_PATH, |path| { println!("cargo:rerun-if-changed={}", path.display()); diff --git a/ports/servo/build.rs b/ports/servo/build.rs index 9c121ead68a..860617cdaf2 100644 --- a/ports/servo/build.rs +++ b/ports/servo/build.rs @@ -17,7 +17,7 @@ fn main() { fn android_main() { // Get the NDK path from NDK_HOME env. - let ndk_path = env::var("ANDROID_NDK").ok().expect("Please set the ANDROID_NDK environment variable"); + let ndk_path = env::var_os("ANDROID_NDK").expect("Please set the ANDROID_NDK environment variable"); let ndk_path = Path::new(&ndk_path); // Build up the path to the NDK compilers @@ -62,7 +62,7 @@ fn android_main() { println!("toolchain path is: {}", toolchain_path.to_str().unwrap()); // Get the output directory. - let out_dir = env::var("OUT_DIR").ok().expect("Cargo should have set the OUT_DIR environment variable"); + let out_dir = env::var("OUT_DIR").expect("Cargo should have set the OUT_DIR environment variable"); let directory = Path::new(&out_dir); // compiling android_native_app_glue.c