From: Alex Crichton Date: Fri, 29 Apr 2016 23:36:55 +0000 (-0700) Subject: Pass RUSTFLAGS when we learn about target info X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~22^2~14^2~33^2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=c25c1ae1242e8293e4fc1d5e63b3542fefb72cd0;p=cargo.git Pass RUSTFLAGS when we learn about target info This should help us learn about `target_feature` additions! Closes #2628 --- diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 824ba1344..c33864c3c 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -67,11 +67,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> { profiles: &'a Profiles) -> CargoResult> { let target = build_config.requested_target.clone(); let target = target.as_ref().map(|s| &s[..]); - let target_info = try!(Context::target_info(target, config)); + let target_info = try!(Context::target_info(target, config, &build_config)); let host_info = if build_config.requested_target.is_none() { target_info.clone() } else { - try!(Context::target_info(None, config)) + try!(Context::target_info(None, config, &build_config)) }; let target_triple = target.unwrap_or_else(|| { &config.rustc_info().host[..] @@ -103,8 +103,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> { /// Run `rustc` to discover the dylib prefix/suffix for the target /// specified as well as the exe suffix - fn target_info(target: Option<&str>, cfg: &Config) + fn target_info(target: Option<&str>, + cfg: &Config, + build_config: &BuildConfig) -> CargoResult { + let kind = if target.is_none() {Kind::Host} else {Kind::Target}; let mut process = util::process(cfg.rustc()); process.arg("-") .arg("--crate-name").arg("_") @@ -112,6 +115,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> { .arg("--crate-type").arg("staticlib") .arg("--crate-type").arg("bin") .arg("--print=file-names") + .args(&try!(rustflags_args(cfg, build_config, kind))) .env_remove("RUST_LOG"); if let Some(s) = target { process.arg("--target").arg(s); @@ -617,53 +621,59 @@ impl<'a, 'cfg> Context<'a, 'cfg> { &self.profiles.dev } - // Acquire extra flags to pass to the compiler from the - // RUSTFLAGS environment variable and similar config values pub fn rustflags_args(&self, unit: &Unit) -> CargoResult> { - // We *want* to apply RUSTFLAGS only to builds for the - // requested target architecture, and not to things like build - // scripts and plugins, which may be for an entirely different - // architecture. Cargo's present architecture makes it quite - // hard to only apply flags to things that are not build - // scripts and plugins though, so we do something more hacky - // instead to avoid applying the same RUSTFLAGS to multiple targets - // arches: - // - // 1) If --target is not specified we just apply RUSTFLAGS to - // all builds; they are all going to have the same target. - // - // 2) If --target *is* specified then we only apply RUSTFLAGS - // to compilation units with the Target kind, which indicates - // it was chosen by the --target flag. - // - // This means that, e.g. even if the specified --target is the - // same as the host, build scripts in plugins won't get - // RUSTFLAGS. - let compiling_with_target = self.build_config.requested_target.is_some(); - let is_target_kind = unit.kind == Kind::Target; - - if compiling_with_target && ! is_target_kind { - // This is probably a build script or plugin and we're - // compiling with --target. In this scenario there are - // no rustflags we can apply. - return Ok(Vec::new()); - } + rustflags_args(self.config, &self.build_config, unit.kind) + } +} - // First try RUSTFLAGS from the environment - if let Some(a) = env::var("RUSTFLAGS").ok() { - let args = a.split(" ") - .map(str::trim) - .filter(|s| !s.is_empty()) - .map(str::to_string); - return Ok(args.collect()); - } +// Acquire extra flags to pass to the compiler from the +// RUSTFLAGS environment variable and similar config values +fn rustflags_args(config: &Config, + build_config: &BuildConfig, + kind: Kind) -> CargoResult> { + // We *want* to apply RUSTFLAGS only to builds for the + // requested target architecture, and not to things like build + // scripts and plugins, which may be for an entirely different + // architecture. Cargo's present architecture makes it quite + // hard to only apply flags to things that are not build + // scripts and plugins though, so we do something more hacky + // instead to avoid applying the same RUSTFLAGS to multiple targets + // arches: + // + // 1) If --target is not specified we just apply RUSTFLAGS to + // all builds; they are all going to have the same target. + // + // 2) If --target *is* specified then we only apply RUSTFLAGS + // to compilation units with the Target kind, which indicates + // it was chosen by the --target flag. + // + // This means that, e.g. even if the specified --target is the + // same as the host, build scripts in plugins won't get + // RUSTFLAGS. + let compiling_with_target = build_config.requested_target.is_some(); + let is_target_kind = kind == Kind::Target; + + if compiling_with_target && !is_target_kind { + // This is probably a build script or plugin and we're + // compiling with --target. In this scenario there are + // no rustflags we can apply. + return Ok(Vec::new()); + } - // Then the build.rustflags value - if let Some(args) = try!(self.config.get_list("build.rustflags")) { - let args = args.val.into_iter().map(|a| a.0); - return Ok(args.collect()); - } + // First try RUSTFLAGS from the environment + if let Some(a) = env::var("RUSTFLAGS").ok() { + let args = a.split(" ") + .map(str::trim) + .filter(|s| !s.is_empty()) + .map(str::to_string); + return Ok(args.collect()); + } - Ok(Vec::new()) + // Then the build.rustflags value + if let Some(args) = try!(config.get_list("build.rustflags")) { + let args = args.val.into_iter().map(|a| a.0); + return Ok(args.collect()); } + + Ok(Vec::new()) }