Skip to content

Commit e9ce56f

Browse files
committed
rustbuild changes to build LLVM
1 parent 3b342fa commit e9ce56f

File tree

6 files changed

+76
-16
lines changed

6 files changed

+76
-16
lines changed

src/bootstrap/build/config.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::io::prelude::*;
1515
use std::path::PathBuf;
1616
use std::process;
1717

18+
use build::util::build_path;
1819
use num_cpus;
1920
use rustc_serialize::Decodable;
2021
use toml::{Parser, Decoder, Value};
@@ -305,7 +306,7 @@ impl Config {
305306
.collect();
306307
}
307308
"CFG_MUSL_ROOT" if value.len() > 0 => {
308-
self.musl_root = Some(PathBuf::from(value));
309+
self.musl_root = Some(build_path(value));
309310
}
310311
"CFG_DEFAULT_AR" if value.len() > 0 => {
311312
self.rustc_default_ar = Some(value.to_string());
@@ -322,31 +323,31 @@ impl Config {
322323
"CFG_LLVM_ROOT" if value.len() > 0 => {
323324
let target = self.target_config.entry(self.build.clone())
324325
.or_insert(Target::default());
325-
let root = PathBuf::from(value);
326+
let root = build_path(value);
326327
target.llvm_config = Some(root.join("bin/llvm-config"));
327328
}
328329
"CFG_JEMALLOC_ROOT" if value.len() > 0 => {
329330
let target = self.target_config.entry(self.build.clone())
330331
.or_insert(Target::default());
331-
target.jemalloc = Some(PathBuf::from(value));
332+
target.jemalloc = Some(build_path(value));
332333
}
333334
"CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
334335
let target = "arm-linux-androideabi".to_string();
335336
let target = self.target_config.entry(target)
336337
.or_insert(Target::default());
337-
target.ndk = Some(PathBuf::from(value));
338+
target.ndk = Some(build_path(value));
338339
}
339340
"CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
340341
let target = "i686-linux-androideabi".to_string();
341342
let target = self.target_config.entry(target)
342343
.or_insert(Target::default());
343-
target.ndk = Some(PathBuf::from(value));
344+
target.ndk = Some(build_path(value));
344345
}
345346
"CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
346347
let target = "aarch64-linux-androideabi".to_string();
347348
let target = self.target_config.entry(target)
348349
.or_insert(Target::default());
349-
target.ndk = Some(PathBuf::from(value));
350+
target.ndk = Some(build_path(value));
350351
}
351352
_ => {}
352353
}

src/bootstrap/build/native.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,20 @@ pub fn compiler_rt(build: &Build, target: &str) {
146146
}
147147
let _ = fs::remove_dir_all(&dst);
148148
t!(fs::create_dir_all(&dst));
149-
let build_llvm_config = build.llvm_out(&build.config.build)
150-
.join("bin")
151-
.join(exe("llvm-config", &build.config.build));
149+
150+
let internal_config = build.llvm_out(&build.config.build)
151+
.join("bin")
152+
.join("llvm-config");
153+
let external_config = build.config.target_config.get(target).and_then(|config| {
154+
config.llvm_config.clone()
155+
});
156+
let llvm_config = external_config.unwrap_or(internal_config);
152157
let mut cfg = cmake::Config::new(build.src.join("src/compiler-rt"));
153158
cfg.target(target)
154159
.host(&build.config.build)
155160
.out_dir(&dst)
156161
.profile(mode)
157-
.define("LLVM_CONFIG_PATH", build_llvm_config)
162+
.define("LLVM_CONFIG_PATH", llvm_config)
158163
.define("COMPILER_RT_DEFAULT_TARGET_TRIPLE", target)
159164
.define("COMPILER_RT_BUILD_SANITIZERS", "OFF")
160165
.define("COMPILER_RT_BUILD_EMUTLS", "OFF")

src/bootstrap/build/sanity.rs

+19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::collections::HashSet;
1212
use std::env;
1313
use std::ffi::{OsStr, OsString};
1414
use std::fs;
15+
use std::path::PathBuf;
1516
use std::process::Command;
1617

1718
use build_helper::output;
@@ -78,6 +79,24 @@ pub fn check(build: &mut Build) {
7879
panic!("the iOS target is only supported on OSX");
7980
}
8081

82+
if cfg!(windows) {
83+
if let Some(config) = build.config.target_config.get(target) {
84+
if let Some(ref llvm_config) = config.llvm_config {
85+
let llvm_mode = output(Command::new(&llvm_config).arg("--shared-mode"));
86+
if llvm_mode == "shared" {
87+
let bin = output(Command::new(&llvm_config).arg("--bindir"));
88+
let bin_canonical = PathBuf::from(bin.trim()).canonicalize().unwrap();
89+
let bin_in_path = env::split_paths(&path).find(|p| {
90+
p.canonicalize().ok().map_or(false, |c| c.eq(&bin_canonical))
91+
});
92+
if bin_in_path.is_none() {
93+
panic!("Unable to find LLVM's binary folder {:?} in PATH", bin);
94+
}
95+
}
96+
}
97+
}
98+
}
99+
81100
// Make sure musl-root is valid if specified
82101
if target.contains("musl") && (target.contains("x86_64") || target.contains("i686")) {
83102
match build.config.musl_root {

src/bootstrap/build/step.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ fn top_level(build: &Build) -> Vec<Step> {
144144
src: Source::Llvm { _dummy: () },
145145
target: &build.config.build,
146146
};
147-
targets.push(t.doc(stage));
147+
if build.config.docs {
148+
targets.push(t.doc(stage));
149+
}
148150
for host in build.config.host.iter() {
149151
if !build.flags.host.contains(host) {
150152
continue
@@ -320,7 +322,9 @@ impl<'a> Step<'a> {
320322

321323
Source::Dist { stage } => {
322324
let mut base = Vec::new();
323-
base.push(self.dist_docs(stage));
325+
if build.config.docs {
326+
base.push(self.dist_docs(stage));
327+
}
324328

325329
for host in build.config.host.iter() {
326330
let host = self.target(host);

src/bootstrap/build/util.rs

+23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ use std::process::Command;
1616
use bootstrap::{dylib_path, dylib_path_var};
1717
use filetime::FileTime;
1818

19+
#[cfg(not(windows))]
20+
pub fn build_path(path: &str) -> PathBuf {
21+
PathBuf::from(path)
22+
}
23+
24+
#[cfg(windows)]
25+
pub fn build_path(path: &str) -> PathBuf {
26+
use std::io::{stderr, Write};
27+
use build_helper::output;
28+
29+
if path.chars().next() == Some('/') {
30+
let output = output(&mut Command::new("cygpath").arg("-w").arg(path));
31+
let win_path = output.trim_right();
32+
writeln!(&mut stderr(),
33+
"note: Converted Unix path '{}' to Windows path '{}'",
34+
path,
35+
win_path).ok();
36+
PathBuf::from(win_path)
37+
} else {
38+
PathBuf::from(path)
39+
}
40+
}
41+
1942
pub fn staticlib(name: &str, target: &str) -> String {
2043
if target.contains("windows-msvc") {
2144
format!("{}.lib", name)

src/librustc_llvm/build.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ fn main() {
2121
println!("cargo:rustc-cfg=cargobuild");
2222

2323
let target = env::var("TARGET").unwrap();
24-
let llvm_config = env::var_os("LLVM_CONFIG").map(PathBuf::from)
25-
.unwrap_or_else(|| {
24+
let mut llvm_config = env::var_os("LLVM_CONFIG").map(PathBuf::from)
25+
.unwrap_or_else(|| {
2626
match env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
2727
Some(dir) => {
2828
let to_test = dir.parent().unwrap().parent().unwrap()
@@ -35,7 +35,7 @@ fn main() {
3535
}
3636
PathBuf::from("llvm-config")
3737
});
38-
38+
llvm_config.set_extension(env::consts::EXE_EXTENSION);
3939
println!("cargo:rerun-if-changed={}", llvm_config.display());
4040

4141
// Test whether we're cross-compiling LLVM. This is a pretty rare case
@@ -111,6 +111,7 @@ fn main() {
111111
// Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
112112
// we don't pick up system libs because unfortunately they're for the host
113113
// of llvm-config, not the target that we're attempting to link.
114+
let llvm_static = output(Command::new(&llvm_config).arg("--shared-mode")) == "static";
114115
let mut cmd = Command::new(&llvm_config);
115116
cmd.arg("--libs");
116117
if !is_crossed {
@@ -136,7 +137,7 @@ fn main() {
136137
continue
137138
}
138139

139-
let kind = if name.starts_with("LLVM") {"static"} else {"dylib"};
140+
let kind = if name.starts_with("LLVM") && llvm_static {"static"} else {"dylib"};
140141
println!("cargo:rustc-link-lib={}={}", kind, name);
141142
}
142143

@@ -161,6 +162,13 @@ fn main() {
161162
}
162163
}
163164

165+
if !llvm_static && cfg!(windows) {
166+
// Use --bindir as the search path. DLLs will be placed here.
167+
// llvm-config is bugged and doesn't doesn't indicate this on Windows
168+
let bin = output(Command::new(&llvm_config).arg("--bindir"));
169+
println!("cargo:rustc-link-search=native={}", bin.trim());
170+
}
171+
164172
// C++ runtime library
165173
if !target.contains("msvc") {
166174
if let Some(s) = env::var_os("LLVM_STATIC_STDCPP") {

0 commit comments

Comments
 (0)