Skip to content

Commit 9823bc6

Browse files
Jon GjengsetMark-Simulacrum
Jon Gjengset
authored andcommitted
bootstrap: correct reading of flags for llvm
First, this reverts the `CFLAGS`/`CXXFLAGS` of rust-lang#93918. Those flags are already read by `cc` and populated into `Build` earlier on in the process. We shouldn't be overriding that based on `CFLAGS`, since `cc` also respects overrides like `CFLAGS_{TARGET}` and `HOST_CFLAGS`, which we want to take into account. Second, this adds the same capability to specify target-specific versions of `LDFLAGS` as we have through `cc` for the `C*` flags: https://github.com/alexcrichton/cc-rs#external-configuration-via-environment-variables Note that this also necessitated an update to compiletest to treat CXXFLAGS separately from CFLAGS.
1 parent 7237f15 commit 9823bc6

File tree

10 files changed

+70
-29
lines changed

10 files changed

+70
-29
lines changed

src/bootstrap/builder.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::run;
2626
use crate::test;
2727
use crate::tool::{self, SourceType};
2828
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir};
29-
use crate::{Build, DocTests, GitRepo, Mode};
29+
use crate::{Build, CLang, DocTests, GitRepo, Mode};
3030

3131
pub use crate::Compiler;
3232
// FIXME: replace with std::lazy after it gets stabilized and reaches beta
@@ -1511,7 +1511,7 @@ impl<'a> Builder<'a> {
15111511
let cc = ccacheify(&self.cc(target));
15121512
cargo.env(format!("CC_{}", target.triple), &cc);
15131513

1514-
let cflags = self.cflags(target, GitRepo::Rustc).join(" ");
1514+
let cflags = self.cflags(target, GitRepo::Rustc, CLang::C).join(" ");
15151515
cargo.env(format!("CFLAGS_{}", target.triple), &cflags);
15161516

15171517
if let Some(ar) = self.ar(target) {
@@ -1523,9 +1523,10 @@ impl<'a> Builder<'a> {
15231523

15241524
if let Ok(cxx) = self.cxx(target) {
15251525
let cxx = ccacheify(&cxx);
1526+
let cxxflags = self.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
15261527
cargo
15271528
.env(format!("CXX_{}", target.triple), &cxx)
1528-
.env(format!("CXXFLAGS_{}", target.triple), cflags);
1529+
.env(format!("CXXFLAGS_{}", target.triple), cxxflags);
15291530
}
15301531
}
15311532

src/bootstrap/cc_detect.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::{env, iter};
2929
use build_helper::output;
3030

3131
use crate::config::{Target, TargetSelection};
32-
use crate::{Build, GitRepo};
32+
use crate::{Build, CLang, GitRepo};
3333

3434
// The `cc` crate doesn't provide a way to obtain a path to the detected archiver,
3535
// so use some simplified logic here. First we respect the environment variable `AR`, then
@@ -109,7 +109,7 @@ pub fn find(build: &mut Build) {
109109
};
110110

111111
build.cc.insert(target, compiler.clone());
112-
let cflags = build.cflags(target, GitRepo::Rustc);
112+
let cflags = build.cflags(target, GitRepo::Rustc, CLang::C);
113113

114114
// If we use llvm-libunwind, we will need a C++ compiler as well for all targets
115115
// We'll need one anyways if the target triple is also a host triple
@@ -142,8 +142,9 @@ pub fn find(build: &mut Build) {
142142
build.verbose(&format!("CC_{} = {:?}", &target.triple, build.cc(target)));
143143
build.verbose(&format!("CFLAGS_{} = {:?}", &target.triple, cflags));
144144
if let Ok(cxx) = build.cxx(target) {
145+
let cxxflags = build.cflags(target, GitRepo::Rustc, CLang::Cxx);
145146
build.verbose(&format!("CXX_{} = {:?}", &target.triple, cxx));
146-
build.verbose(&format!("CXXFLAGS_{} = {:?}", &target.triple, cflags));
147+
build.verbose(&format!("CXXFLAGS_{} = {:?}", &target.triple, cxxflags));
147148
}
148149
if let Some(ar) = ar {
149150
build.verbose(&format!("AR_{} = {:?}", &target.triple, ar));

src/bootstrap/compile.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::native;
2929
use crate::tool::SourceType;
3030
use crate::util::{exe, is_debug_info, is_dylib, symlink_dir};
3131
use crate::LLVM_TOOLS;
32-
use crate::{Compiler, DependencyType, GitRepo, Mode};
32+
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode};
3333

3434
#[derive(Debug, PartialOrd, Ord, Copy, Clone, PartialEq, Eq, Hash)]
3535
pub struct Std {
@@ -250,7 +250,7 @@ fn copy_self_contained_objects(
250250
}
251251
} else if target.contains("windows-gnu") {
252252
for obj in ["crt2.o", "dllcrt2.o"].iter() {
253-
let src = compiler_file(builder, builder.cc(target), target, obj);
253+
let src = compiler_file(builder, builder.cc(target), target, CLang::C, obj);
254254
let target = libdir_self_contained.join(obj);
255255
builder.copy(&src, &target);
256256
target_deps.push((target, DependencyType::TargetSelfContained));
@@ -724,7 +724,13 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
724724
&& !target.contains("msvc")
725725
&& !target.contains("apple")
726726
{
727-
let file = compiler_file(builder, builder.cxx(target).unwrap(), target, "libstdc++.a");
727+
let file = compiler_file(
728+
builder,
729+
builder.cxx(target).unwrap(),
730+
target,
731+
CLang::Cxx,
732+
"libstdc++.a",
733+
);
728734
cargo.env("LLVM_STATIC_STDCPP", file);
729735
}
730736
if builder.config.llvm_link_shared {
@@ -945,10 +951,11 @@ pub fn compiler_file(
945951
builder: &Builder<'_>,
946952
compiler: &Path,
947953
target: TargetSelection,
954+
c: CLang,
948955
file: &str,
949956
) -> PathBuf {
950957
let mut cmd = Command::new(compiler);
951-
cmd.args(builder.cflags(target, GitRepo::Rustc));
958+
cmd.args(builder.cflags(target, GitRepo::Rustc, c));
952959
cmd.arg(format!("-print-file-name={}", file));
953960
let out = output(&mut cmd);
954961
PathBuf::from(out.trim())

src/bootstrap/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ impl Mode {
338338
}
339339
}
340340

341+
pub enum CLang {
342+
C,
343+
Cxx,
344+
}
345+
341346
impl Build {
342347
/// Creates a new set of build configuration from the `flags` on the command
343348
/// line and the filesystem `config`.
@@ -940,10 +945,15 @@ impl Build {
940945

941946
/// Returns a list of flags to pass to the C compiler for the target
942947
/// specified.
943-
fn cflags(&self, target: TargetSelection, which: GitRepo) -> Vec<String> {
948+
fn cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
949+
let base = match c {
950+
CLang::C => &self.cc[&target],
951+
CLang::Cxx => &self.cxx[&target],
952+
};
953+
944954
// Filter out -O and /O (the optimization flags) that we picked up from
945955
// cc-rs because the build scripts will determine that for themselves.
946-
let mut base = self.cc[&target]
956+
let mut base = base
947957
.args()
948958
.iter()
949959
.map(|s| s.to_string_lossy().into_owned())

src/bootstrap/native.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use build_helper::{output, t};
2121
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
2222
use crate::config::TargetSelection;
2323
use crate::util::{self, exe};
24-
use crate::GitRepo;
24+
use crate::{CLang, GitRepo};
2525
use build_helper::up_to_date;
2626

2727
pub struct Meta {
@@ -517,7 +517,7 @@ fn configure_cmake(
517517
}
518518

519519
cfg.build_arg("-j").build_arg(builder.jobs().to_string());
520-
let mut cflags: OsString = builder.cflags(target, GitRepo::Llvm).join(" ").into();
520+
let mut cflags: OsString = builder.cflags(target, GitRepo::Llvm, CLang::C).join(" ").into();
521521
if let Some(ref s) = builder.config.llvm_cflags {
522522
cflags.push(" ");
523523
cflags.push(s);
@@ -533,23 +533,15 @@ fn configure_cmake(
533533
if builder.config.llvm_clang_cl.is_some() {
534534
cflags.push(&format!(" --target={}", target));
535535
}
536-
if let Some(flags) = env::var_os("CFLAGS") {
537-
cflags.push(" ");
538-
cflags.push(flags);
539-
}
540536
cfg.define("CMAKE_C_FLAGS", cflags);
541-
let mut cxxflags: OsString = builder.cflags(target, GitRepo::Llvm).join(" ").into();
537+
let mut cxxflags: OsString = builder.cflags(target, GitRepo::Llvm, CLang::Cxx).join(" ").into();
542538
if let Some(ref s) = builder.config.llvm_cxxflags {
543539
cxxflags.push(" ");
544540
cxxflags.push(s);
545541
}
546542
if builder.config.llvm_clang_cl.is_some() {
547543
cxxflags.push(&format!(" --target={}", target));
548544
}
549-
if let Some(flags) = env::var_os("CXXFLAGS") {
550-
cxxflags.push(" ");
551-
cxxflags.push(flags);
552-
}
553545
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
554546
if let Some(ar) = builder.ar(target) {
555547
if ar.is_absolute() {
@@ -571,7 +563,7 @@ fn configure_cmake(
571563
ldflags.push_all(flags);
572564
}
573565

574-
if let Some(flags) = env::var_os("LDFLAGS") {
566+
if let Some(flags) = get_var("LDFLAGS", &builder.config.build.triple, &target.triple) {
575567
ldflags.push_all(&flags);
576568
}
577569

@@ -596,6 +588,16 @@ fn configure_cmake(
596588
}
597589
}
598590

591+
// Adapted from https://github.com/alexcrichton/cc-rs/blob/fba7feded71ee4f63cfe885673ead6d7b4f2f454/src/lib.rs#L2347-L2365
592+
fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
593+
let kind = if host == target { "HOST" } else { "TARGET" };
594+
let target_u = target.replace("-", "_");
595+
env::var_os(&format!("{}_{}", var_base, target))
596+
.or_else(|| env::var_os(&format!("{}_{}", var_base, target_u)))
597+
.or_else(|| env::var_os(&format!("{}_{}", kind, var_base)))
598+
.or_else(|| env::var_os(var_base))
599+
}
600+
599601
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
600602
pub struct Lld {
601603
pub target: TargetSelection,

src/bootstrap/test.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::tool::{self, SourceType, Tool};
2424
use crate::toolstate::ToolState;
2525
use crate::util::{self, add_link_lib_path, dylib_path, dylib_path_var};
2626
use crate::Crate as CargoCrate;
27-
use crate::{envify, DocTests, GitRepo, Mode};
27+
use crate::{envify, CLang, DocTests, GitRepo, Mode};
2828

2929
const ADB_TEST_DIR: &str = "/data/tmp/work";
3030

@@ -1509,7 +1509,9 @@ note: if you're sure you want to do this, please open an issue as to why. In the
15091509
.arg("--cxx")
15101510
.arg(builder.cxx(target).unwrap())
15111511
.arg("--cflags")
1512-
.arg(builder.cflags(target, GitRepo::Rustc).join(" "));
1512+
.arg(builder.cflags(target, GitRepo::Rustc, CLang::C).join(" "))
1513+
.arg("--cxxflags")
1514+
.arg(builder.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" "));
15131515
copts_passed = true;
15141516
if let Some(ar) = builder.ar(target) {
15151517
cmd.arg("--ar").arg(ar);
@@ -1520,7 +1522,14 @@ note: if you're sure you want to do this, please open an issue as to why. In the
15201522
cmd.arg("--llvm-components").arg("");
15211523
}
15221524
if !copts_passed {
1523-
cmd.arg("--cc").arg("").arg("--cxx").arg("").arg("--cflags").arg("");
1525+
cmd.arg("--cc")
1526+
.arg("")
1527+
.arg("--cxx")
1528+
.arg("")
1529+
.arg("--cflags")
1530+
.arg("")
1531+
.arg("--cxxflags")
1532+
.arg("");
15241533
}
15251534

15261535
if builder.remote_tested(target) {

src/tools/compiletest/src/common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ pub struct Config {
357357
pub cc: String,
358358
pub cxx: String,
359359
pub cflags: String,
360+
pub cxxflags: String,
360361
pub ar: String,
361362
pub linker: Option<String>,
362363
pub llvm_components: String,

src/tools/compiletest/src/header/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn config() -> Config {
5252
"--cc=c",
5353
"--cxx=c++",
5454
"--cflags=",
55+
"--cxxflags=",
5556
"--llvm-components=",
5657
"--android-cross-path=",
5758
"--target=x86_64-unknown-linux-gnu",

src/tools/compiletest/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
126126
.reqopt("", "cc", "path to a C compiler", "PATH")
127127
.reqopt("", "cxx", "path to a C++ compiler", "PATH")
128128
.reqopt("", "cflags", "flags for the C compiler", "FLAGS")
129+
.reqopt("", "cxxflags", "flags for the CXX compiler", "FLAGS")
129130
.optopt("", "ar", "path to an archiver", "PATH")
130131
.optopt("", "linker", "path to a linker", "PATH")
131132
.reqopt("", "llvm-components", "list of LLVM components built in", "LIST")
@@ -288,6 +289,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
288289
cc: matches.opt_str("cc").unwrap(),
289290
cxx: matches.opt_str("cxx").unwrap(),
290291
cflags: matches.opt_str("cflags").unwrap(),
292+
cxxflags: matches.opt_str("cxxflags").unwrap(),
291293
ar: matches.opt_str("ar").unwrap_or_else(|| String::from("ar")),
292294
linker: matches.opt_str("linker"),
293295
llvm_components: matches.opt_str("llvm-components").unwrap(),

src/tools/compiletest/src/runtest.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2919,15 +2919,22 @@ impl<'test> TestCx<'test> {
29192919
.map(|s| s.replace("/", "-"))
29202920
.collect::<Vec<_>>()
29212921
.join(" ");
2922+
let cxxflags = self
2923+
.config
2924+
.cxxflags
2925+
.split(' ')
2926+
.map(|s| s.replace("/", "-"))
2927+
.collect::<Vec<_>>()
2928+
.join(" ");
29222929

29232930
cmd.env("IS_MSVC", "1")
29242931
.env("IS_WINDOWS", "1")
29252932
.env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
29262933
.env("CC", format!("'{}' {}", self.config.cc, cflags))
2927-
.env("CXX", format!("'{}'", &self.config.cxx));
2934+
.env("CXX", format!("'{}' {}", &self.config.cxx, cxxflags));
29282935
} else {
29292936
cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2930-
.env("CXX", format!("{} {}", self.config.cxx, self.config.cflags))
2937+
.env("CXX", format!("{} {}", self.config.cxx, self.config.cxxflags))
29312938
.env("AR", &self.config.ar);
29322939

29332940
if self.config.target.contains("windows") {

0 commit comments

Comments
 (0)