1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Compilation of native dependencies like LLVM.
13 //! Native projects like LLVM unfortunately aren't suited just yet for
14 //! compilation in build scripts that Cargo has. This is because thie
15 //! compilation takes a *very* long time but also because we don't want to
16 //! compile LLVM 3 times as part of a normal bootstrap (we want it cached).
18 //! LLVM and compiler-rt are essentially just wired up to everything else to
19 //! ensure that they're always in place if needed.
22 use std::ffi::OsString;
23 use std::fs::{self, File};
24 use std::io::{Read, Write};
26 use std::process::Command;
28 use build_helper::output;
34 use build_helper::up_to_date;
36 /// Compile LLVM for `target`.
37 pub fn llvm(build: &Build, target: &str) {
38 // If we're using a custom LLVM bail out here, but we can only use a
39 // custom LLVM for the build triple.
40 if let Some(config) = build.config.target_config.get(target) {
41 if let Some(ref s) = config.llvm_config {
42 return check_llvm_version(build, s);
46 let rebuild_trigger = build.src.join("src/rustllvm/llvm-rebuild-trigger");
47 let mut rebuild_trigger_contents = String::new();
48 t!(t!(File::open(&rebuild_trigger)).read_to_string(&mut rebuild_trigger_contents));
50 let out_dir = build.llvm_out(target);
51 let done_stamp = out_dir.join("llvm-finished-building");
52 if done_stamp.exists() {
53 let mut done_contents = String::new();
54 t!(t!(File::open(&done_stamp)).read_to_string(&mut done_contents));
56 // If LLVM was already built previously and contents of the rebuild-trigger file
57 // didn't change from the previous build, then no action is required.
58 if done_contents == rebuild_trigger_contents {
62 if build.config.llvm_clean_rebuild {
63 drop(fs::remove_dir_all(&out_dir));
66 println!("Building LLVM for {}", target);
67 let _time = util::timeit();
68 t!(fs::create_dir_all(&out_dir));
70 // http://llvm.org/docs/CMake.html
71 let mut cfg = cmake::Config::new(build.src.join("src/llvm"));
72 if build.config.ninja {
73 cfg.generator("Ninja");
76 let profile = match (build.config.llvm_optimize, build.config.llvm_release_debuginfo) {
77 (false, _) => "Debug",
78 (true, false) => "Release",
79 (true, true) => "RelWithDebInfo",
82 // NOTE: remember to also update `config.toml.example` when changing the defaults!
83 let llvm_targets = match build.config.llvm_targets {
85 None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon",
88 let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"};
91 .host(&build.config.build)
94 .define("LLVM_ENABLE_ASSERTIONS", assertions)
95 .define("LLVM_TARGETS_TO_BUILD", llvm_targets)
96 .define("LLVM_INCLUDE_EXAMPLES", "OFF")
97 .define("LLVM_INCLUDE_TESTS", "OFF")
98 .define("LLVM_INCLUDE_DOCS", "OFF")
99 .define("LLVM_ENABLE_ZLIB", "OFF")
100 .define("WITH_POLLY", "OFF")
101 .define("LLVM_ENABLE_TERMINFO", "OFF")
102 .define("LLVM_ENABLE_LIBEDIT", "OFF")
103 .define("LLVM_PARALLEL_COMPILE_JOBS", build.jobs().to_string())
104 .define("LLVM_TARGET_ARCH", target.split('-').next().unwrap())
105 .define("LLVM_DEFAULT_TARGET_TRIPLE", target);
107 if target.contains("msvc") {
108 cfg.define("LLVM_USE_CRT_DEBUG", "MT");
109 cfg.define("LLVM_USE_CRT_RELEASE", "MT");
110 cfg.define("LLVM_USE_CRT_RELWITHDEBINFO", "MT");
113 if target.starts_with("i686") {
114 cfg.define("LLVM_BUILD_32_BITS", "ON");
117 if let Some(num_linkers) = build.config.llvm_link_jobs {
119 cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
123 // http://llvm.org/docs/HowToCrossCompileLLVM.html
124 if target != build.config.build {
125 // FIXME: if the llvm root for the build triple is overridden then we
126 // should use llvm-tblgen from there, also should verify that it
127 // actually exists most of the time in normal installs of LLVM.
128 let host = build.llvm_out(&build.config.build).join("bin/llvm-tblgen");
129 cfg.define("CMAKE_CROSSCOMPILING", "True")
130 .define("LLVM_TABLEGEN", &host);
133 let sanitize_cc = |cc: &Path| {
134 if target.contains("msvc") {
135 OsString::from(cc.to_str().unwrap().replace("\\", "/"))
137 cc.as_os_str().to_owned()
141 let configure_compilers = |cfg: &mut cmake::Config| {
142 // MSVC with CMake uses msbuild by default which doesn't respect these
143 // vars that we'd otherwise configure. In that case we just skip this
145 if target.contains("msvc") && !build.config.ninja {
149 let cc = build.cc(target);
150 let cxx = build.cxx(target);
152 // Handle msvc + ninja + ccache specially (this is what the bots use)
153 if target.contains("msvc") &&
154 build.config.ninja &&
155 build.config.ccache.is_some() {
156 let mut cc = env::current_exe().expect("failed to get cwd");
157 cc.set_file_name("sccache-plus-cl.exe");
159 cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
160 .define("CMAKE_CXX_COMPILER", sanitize_cc(&cc));
161 cfg.env("SCCACHE_PATH",
162 build.config.ccache.as_ref().unwrap())
163 .env("SCCACHE_TARGET", target);
165 // If ccache is configured we inform the build a little differently hwo
166 // to invoke ccache while also invoking our compilers.
167 } else if let Some(ref ccache) = build.config.ccache {
168 cfg.define("CMAKE_C_COMPILER", ccache)
169 .define("CMAKE_C_COMPILER_ARG1", sanitize_cc(cc))
170 .define("CMAKE_CXX_COMPILER", ccache)
171 .define("CMAKE_CXX_COMPILER_ARG1", sanitize_cc(cxx));
173 cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc))
174 .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx));
177 cfg.build_arg("-j").build_arg(build.jobs().to_string());
178 cfg.define("CMAKE_C_FLAGS", build.cflags(target).join(" "));
179 cfg.define("CMAKE_CXX_FLAGS", build.cflags(target).join(" "));
182 configure_compilers(&mut cfg);
184 if env::var_os("SCCACHE_ERROR_LOG").is_some() {
185 cfg.env("RUST_LOG", "sccache=info");
188 // FIXME: we don't actually need to build all LLVM tools and all LLVM
189 // libraries here, e.g. we just want a few components and a few
190 // tools. Figure out how to filter them down and only build the right
191 // tools and libs on all platforms.
194 t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes()));
197 fn check_llvm_version(build: &Build, llvm_config: &Path) {
198 if !build.config.llvm_version_check {
202 let mut cmd = Command::new(llvm_config);
203 let version = output(cmd.arg("--version"));
204 if version.starts_with("3.5") || version.starts_with("3.6") ||
205 version.starts_with("3.7") {
208 panic!("\n\nbad LLVM version: {}, need >=3.5\n\n", version)
211 /// Compiles the `rust_test_helpers.c` library which we used in various
212 /// `run-pass` test suites for ABI testing.
213 pub fn test_helpers(build: &Build, target: &str) {
214 let dst = build.test_helpers_out(target);
215 let src = build.src.join("src/rt/rust_test_helpers.c");
216 if up_to_date(&src, &dst.join("librust_test_helpers.a")) {
220 println!("Building test helpers");
221 t!(fs::create_dir_all(&dst));
222 let mut cfg = gcc::Config::new();
224 // We may have found various cross-compilers a little differently due to our
225 // extra configuration, so inform gcc of these compilers. Note, though, that
226 // on MSVC we still need gcc's detection of env vars (ugh).
227 if !target.contains("msvc") {
228 if let Some(ar) = build.ar(target) {
231 cfg.compiler(build.cc(target));
234 cfg.cargo_metadata(false)
237 .host(&build.config.build)
240 .file(build.src.join("src/rt/rust_test_helpers.c"))
241 .compile("librust_test_helpers.a");
243 const OPENSSL_VERS: &'static str = "1.0.2k";
244 const OPENSSL_SHA256: &'static str =
245 "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0";
247 pub fn openssl(build: &Build, target: &str) {
248 let out = match build.openssl_dir(target) {
253 let stamp = out.join(".stamp");
254 let mut contents = String::new();
255 drop(File::open(&stamp).and_then(|mut f| f.read_to_string(&mut contents)));
256 if contents == OPENSSL_VERS {
259 t!(fs::create_dir_all(&out));
261 let name = format!("openssl-{}.tar.gz", OPENSSL_VERS);
262 let tarball = out.join(&name);
263 if !tarball.exists() {
264 let tmp = tarball.with_extension("tmp");
265 // originally from https://www.openssl.org/source/...
266 let url = format!("https://s3.amazonaws.com/rust-lang-ci/rust-ci-mirror/{}",
270 let status = Command::new("curl")
274 .expect("failed to spawn curl");
275 if status.success() {
281 panic!("failed to download openssl source")
283 let mut shasum = if target.contains("apple") {
284 let mut cmd = Command::new("shasum");
285 cmd.arg("-a").arg("256");
288 Command::new("sha256sum")
290 let output = output(&mut shasum.arg(&tmp));
291 let found = output.split_whitespace().next().unwrap();
292 if found != OPENSSL_SHA256 {
293 panic!("downloaded openssl sha256 different\n\
295 found: {}\n", OPENSSL_SHA256, found);
297 t!(fs::rename(&tmp, &tarball));
299 let obj = out.join(format!("openssl-{}", OPENSSL_VERS));
300 let dst = build.openssl_install_dir(target).unwrap();
301 drop(fs::remove_dir_all(&obj));
302 drop(fs::remove_dir_all(&dst));
303 build.run(Command::new("tar").arg("xf").arg(&tarball).current_dir(&out));
305 let mut configure = Command::new(obj.join("Configure"));
306 configure.arg(format!("--prefix={}", dst.display()));
307 configure.arg("no-dso");
308 configure.arg("no-ssl2");
309 configure.arg("no-ssl3");
311 let os = match target {
312 "aarch64-unknown-linux-gnu" => "linux-aarch64",
313 "arm-unknown-linux-gnueabi" => "linux-armv4",
314 "arm-unknown-linux-gnueabihf" => "linux-armv4",
315 "armv7-unknown-linux-gnueabihf" => "linux-armv4",
316 "i686-apple-darwin" => "darwin-i386-cc",
317 "i686-unknown-freebsd" => "BSD-x86-elf",
318 "i686-unknown-linux-gnu" => "linux-elf",
319 "i686-unknown-linux-musl" => "linux-elf",
320 "mips-unknown-linux-gnu" => "linux-mips32",
321 "mips64-unknown-linux-gnuabi64" => "linux64-mips64",
322 "mips64el-unknown-linux-gnuabi64" => "linux64-mips64",
323 "mipsel-unknown-linux-gnu" => "linux-mips32",
324 "powerpc-unknown-linux-gnu" => "linux-ppc",
325 "powerpc64-unknown-linux-gnu" => "linux-ppc64",
326 "powerpc64le-unknown-linux-gnu" => "linux-ppc64le",
327 "s390x-unknown-linux-gnu" => "linux64-s390x",
328 "x86_64-apple-darwin" => "darwin64-x86_64-cc",
329 "x86_64-unknown-freebsd" => "BSD-x86_64",
330 "x86_64-unknown-linux-gnu" => "linux-x86_64",
331 "x86_64-unknown-linux-musl" => "linux-x86_64",
332 "x86_64-unknown-netbsd" => "BSD-x86_64",
333 _ => panic!("don't know how to configure OpenSSL for {}", target),
336 configure.env("CC", build.cc(target));
337 for flag in build.cflags(target) {
340 configure.current_dir(&obj);
341 println!("Configuring openssl for {}", target);
342 build.run_quiet(&mut configure);
343 println!("Building openssl for {}", target);
344 build.run_quiet(Command::new("make").arg("-j1").current_dir(&obj));
345 println!("Installing openssl for {}", target);
346 build.run_quiet(Command::new("make").arg("install").current_dir(&obj));
348 let mut f = t!(File::create(&stamp));
349 t!(f.write_all(OPENSSL_VERS.as_bytes()));