Skip to content

Commit c25c1ae

Browse files
committed
Pass RUSTFLAGS when we learn about target info
This should help us learn about `target_feature` additions! Closes #2628
1 parent 27161ec commit c25c1ae

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
6767
profiles: &'a Profiles) -> CargoResult<Context<'a, 'cfg>> {
6868
let target = build_config.requested_target.clone();
6969
let target = target.as_ref().map(|s| &s[..]);
70-
let target_info = try!(Context::target_info(target, config));
70+
let target_info = try!(Context::target_info(target, config, &build_config));
7171
let host_info = if build_config.requested_target.is_none() {
7272
target_info.clone()
7373
} else {
74-
try!(Context::target_info(None, config))
74+
try!(Context::target_info(None, config, &build_config))
7575
};
7676
let target_triple = target.unwrap_or_else(|| {
7777
&config.rustc_info().host[..]
@@ -103,15 +103,19 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
103103

104104
/// Run `rustc` to discover the dylib prefix/suffix for the target
105105
/// specified as well as the exe suffix
106-
fn target_info(target: Option<&str>, cfg: &Config)
106+
fn target_info(target: Option<&str>,
107+
cfg: &Config,
108+
build_config: &BuildConfig)
107109
-> CargoResult<TargetInfo> {
110+
let kind = if target.is_none() {Kind::Host} else {Kind::Target};
108111
let mut process = util::process(cfg.rustc());
109112
process.arg("-")
110113
.arg("--crate-name").arg("_")
111114
.arg("--crate-type").arg("dylib")
112115
.arg("--crate-type").arg("staticlib")
113116
.arg("--crate-type").arg("bin")
114117
.arg("--print=file-names")
118+
.args(&try!(rustflags_args(cfg, build_config, kind)))
115119
.env_remove("RUST_LOG");
116120
if let Some(s) = target {
117121
process.arg("--target").arg(s);
@@ -617,53 +621,59 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
617621
&self.profiles.dev
618622
}
619623

620-
// Acquire extra flags to pass to the compiler from the
621-
// RUSTFLAGS environment variable and similar config values
622624
pub fn rustflags_args(&self, unit: &Unit) -> CargoResult<Vec<String>> {
623-
// We *want* to apply RUSTFLAGS only to builds for the
624-
// requested target architecture, and not to things like build
625-
// scripts and plugins, which may be for an entirely different
626-
// architecture. Cargo's present architecture makes it quite
627-
// hard to only apply flags to things that are not build
628-
// scripts and plugins though, so we do something more hacky
629-
// instead to avoid applying the same RUSTFLAGS to multiple targets
630-
// arches:
631-
//
632-
// 1) If --target is not specified we just apply RUSTFLAGS to
633-
// all builds; they are all going to have the same target.
634-
//
635-
// 2) If --target *is* specified then we only apply RUSTFLAGS
636-
// to compilation units with the Target kind, which indicates
637-
// it was chosen by the --target flag.
638-
//
639-
// This means that, e.g. even if the specified --target is the
640-
// same as the host, build scripts in plugins won't get
641-
// RUSTFLAGS.
642-
let compiling_with_target = self.build_config.requested_target.is_some();
643-
let is_target_kind = unit.kind == Kind::Target;
644-
645-
if compiling_with_target && ! is_target_kind {
646-
// This is probably a build script or plugin and we're
647-
// compiling with --target. In this scenario there are
648-
// no rustflags we can apply.
649-
return Ok(Vec::new());
650-
}
625+
rustflags_args(self.config, &self.build_config, unit.kind)
626+
}
627+
}
651628

652-
// First try RUSTFLAGS from the environment
653-
if let Some(a) = env::var("RUSTFLAGS").ok() {
654-
let args = a.split(" ")
655-
.map(str::trim)
656-
.filter(|s| !s.is_empty())
657-
.map(str::to_string);
658-
return Ok(args.collect());
659-
}
629+
// Acquire extra flags to pass to the compiler from the
630+
// RUSTFLAGS environment variable and similar config values
631+
fn rustflags_args(config: &Config,
632+
build_config: &BuildConfig,
633+
kind: Kind) -> CargoResult<Vec<String>> {
634+
// We *want* to apply RUSTFLAGS only to builds for the
635+
// requested target architecture, and not to things like build
636+
// scripts and plugins, which may be for an entirely different
637+
// architecture. Cargo's present architecture makes it quite
638+
// hard to only apply flags to things that are not build
639+
// scripts and plugins though, so we do something more hacky
640+
// instead to avoid applying the same RUSTFLAGS to multiple targets
641+
// arches:
642+
//
643+
// 1) If --target is not specified we just apply RUSTFLAGS to
644+
// all builds; they are all going to have the same target.
645+
//
646+
// 2) If --target *is* specified then we only apply RUSTFLAGS
647+
// to compilation units with the Target kind, which indicates
648+
// it was chosen by the --target flag.
649+
//
650+
// This means that, e.g. even if the specified --target is the
651+
// same as the host, build scripts in plugins won't get
652+
// RUSTFLAGS.
653+
let compiling_with_target = build_config.requested_target.is_some();
654+
let is_target_kind = kind == Kind::Target;
655+
656+
if compiling_with_target && !is_target_kind {
657+
// This is probably a build script or plugin and we're
658+
// compiling with --target. In this scenario there are
659+
// no rustflags we can apply.
660+
return Ok(Vec::new());
661+
}
660662

661-
// Then the build.rustflags value
662-
if let Some(args) = try!(self.config.get_list("build.rustflags")) {
663-
let args = args.val.into_iter().map(|a| a.0);
664-
return Ok(args.collect());
665-
}
663+
// First try RUSTFLAGS from the environment
664+
if let Some(a) = env::var("RUSTFLAGS").ok() {
665+
let args = a.split(" ")
666+
.map(str::trim)
667+
.filter(|s| !s.is_empty())
668+
.map(str::to_string);
669+
return Ok(args.collect());
670+
}
666671

667-
Ok(Vec::new())
672+
// Then the build.rustflags value
673+
if let Some(args) = try!(config.get_list("build.rustflags")) {
674+
let args = args.val.into_iter().map(|a| a.0);
675+
return Ok(args.collect());
668676
}
677+
678+
Ok(Vec::new())
669679
}

0 commit comments

Comments
 (0)