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 let _folder = build.fold_output(|| "llvm");
67 println!("Building LLVM for {}", target);
68 let _time = util::timeit();
69 t!(fs::create_dir_all(&out_dir));
71 // http://llvm.org/docs/CMake.html
72 let mut cfg = cmake::Config::new(build.src.join("src/llvm"));
73 if build.config.ninja {
74 cfg.generator("Ninja");
77 let profile = match (build.config.llvm_optimize, build.config.llvm_release_debuginfo) {
78 (false, _) => "Debug",
79 (true, false) => "Release",
80 (true, true) => "RelWithDebInfo",
83 // NOTE: remember to also update `config.toml.example` when changing the defaults!
84 let llvm_targets = match build.config.llvm_targets {
86 None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon",
89 let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"};
92 .host(&build.config.build)
95 .define("LLVM_ENABLE_ASSERTIONS", assertions)
96 .define("LLVM_TARGETS_TO_BUILD", llvm_targets)
97 .define("LLVM_INCLUDE_EXAMPLES", "OFF")
98 .define("LLVM_INCLUDE_TESTS", "OFF")
99 .define("LLVM_INCLUDE_DOCS", "OFF")
100 .define("LLVM_ENABLE_ZLIB", "OFF")
101 .define("WITH_POLLY", "OFF")
102 .define("LLVM_ENABLE_TERMINFO", "OFF")
103 .define("LLVM_ENABLE_LIBEDIT", "OFF")
104 .define("LLVM_PARALLEL_COMPILE_JOBS", build.jobs().to_string())
105 .define("LLVM_TARGET_ARCH", target.split('-').next().unwrap())
106 .define("LLVM_DEFAULT_TARGET_TRIPLE", target);
108 if target.contains("msvc") {
109 cfg.define("LLVM_USE_CRT_DEBUG", "MT");
110 cfg.define("LLVM_USE_CRT_RELEASE", "MT");
111 cfg.define("LLVM_USE_CRT_RELWITHDEBINFO", "MT");
112 cfg.static_crt(true);
115 if target.starts_with("i686") {
116 cfg.define("LLVM_BUILD_32_BITS", "ON");
119 if let Some(num_linkers) = build.config.llvm_link_jobs {
121 cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
125 // http://llvm.org/docs/HowToCrossCompileLLVM.html
126 if target != build.config.build {
127 // FIXME: if the llvm root for the build triple is overridden then we
128 // should use llvm-tblgen from there, also should verify that it
129 // actually exists most of the time in normal installs of LLVM.
130 let host = build.llvm_out(&build.config.build).join("bin/llvm-tblgen");
131 cfg.define("CMAKE_CROSSCOMPILING", "True")
132 .define("LLVM_TABLEGEN", &host);
135 let sanitize_cc = |cc: &Path| {
136 if target.contains("msvc") {
137 OsString::from(cc.to_str().unwrap().replace("\\", "/"))
139 cc.as_os_str().to_owned()
143 let configure_compilers = |cfg: &mut cmake::Config| {
144 // MSVC with CMake uses msbuild by default which doesn't respect these
145 // vars that we'd otherwise configure. In that case we just skip this
147 if target.contains("msvc") && !build.config.ninja {
151 let cc = build.cc(target);
152 let cxx = build.cxx(target);
154 // Handle msvc + ninja + ccache specially (this is what the bots use)
155 if target.contains("msvc") &&
156 build.config.ninja &&
157 build.config.ccache.is_some() {
158 let mut cc = env::current_exe().expect("failed to get cwd");
159 cc.set_file_name("sccache-plus-cl.exe");
161 cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
162 .define("CMAKE_CXX_COMPILER", sanitize_cc(&cc));
163 cfg.env("SCCACHE_PATH",
164 build.config.ccache.as_ref().unwrap())
165 .env("SCCACHE_TARGET", target);
167 // If ccache is configured we inform the build a little differently hwo
168 // to invoke ccache while also invoking our compilers.
169 } else if let Some(ref ccache) = build.config.ccache {
170 cfg.define("CMAKE_C_COMPILER", ccache)
171 .define("CMAKE_C_COMPILER_ARG1", sanitize_cc(cc))
172 .define("CMAKE_CXX_COMPILER", ccache)
173 .define("CMAKE_CXX_COMPILER_ARG1", sanitize_cc(cxx));
175 cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc))
176 .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx));
179 cfg.build_arg("-j").build_arg(build.jobs().to_string());
180 cfg.define("CMAKE_C_FLAGS", build.cflags(target).join(" "));
181 cfg.define("CMAKE_CXX_FLAGS", build.cflags(target).join(" "));
184 configure_compilers(&mut cfg);
186 if env::var_os("SCCACHE_ERROR_LOG").is_some() {
187 cfg.env("RUST_LOG", "sccache=info");
190 // FIXME: we don't actually need to build all LLVM tools and all LLVM
191 // libraries here, e.g. we just want a few components and a few
192 // tools. Figure out how to filter them down and only build the right
193 // tools and libs on all platforms.
196 t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes()));
199 fn check_llvm_version(build: &Build, llvm_config: &Path) {
200 if !build.config.llvm_version_check {
204 let mut cmd = Command::new(llvm_config);
205 let version = output(cmd.arg("--version"));
206 if version.starts_with("3.5") || version.starts_with("3.6") ||
207 version.starts_with("3.7") {
210 panic!("\n\nbad LLVM version: {}, need >=3.5\n\n", version)
213 /// Compiles the `rust_test_helpers.c` library which we used in various
214 /// `run-pass` test suites for ABI testing.
215 pub fn test_helpers(build: &Build, target: &str) {
216 let dst = build.test_helpers_out(target);
217 let src = build.src.join("src/rt/rust_test_helpers.c");
218 if up_to_date(&src, &dst.join("librust_test_helpers.a")) {
222 let _folder = build.fold_output(|| "build_test_helpers");
223 println!("Building test helpers");
224 t!(fs::create_dir_all(&dst));
225 let mut cfg = gcc::Config::new();
227 // We may have found various cross-compilers a little differently due to our
228 // extra configuration, so inform gcc of these compilers. Note, though, that
229 // on MSVC we still need gcc's detection of env vars (ugh).
230 if !target.contains("msvc") {
231 if let Some(ar) = build.ar(target) {
234 cfg.compiler(build.cc(target));
237 cfg.cargo_metadata(false)
240 .host(&build.config.build)
243 .file(build.src.join("src/rt/rust_test_helpers.c"))
244 .compile("librust_test_helpers.a");
246 const OPENSSL_VERS: &'static str = "1.0.2k";
247 const OPENSSL_SHA256: &'static str =
248 "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0";
250 pub fn openssl(build: &Build, target: &str) {
251 let out = match build.openssl_dir(target) {
256 let stamp = out.join(".stamp");
257 let mut contents = String::new();
258 drop(File::open(&stamp).and_then(|mut f| f.read_to_string(&mut contents)));
259 if contents == OPENSSL_VERS {
262 t!(fs::create_dir_all(&out));
264 let name = format!("openssl-{}.tar.gz", OPENSSL_VERS);
265 let tarball = out.join(&name);
266 if !tarball.exists() {
267 let tmp = tarball.with_extension("tmp");
268 // originally from https://www.openssl.org/source/...
269 let url = format!("https://s3.amazonaws.com/rust-lang-ci/rust-ci-mirror/{}",
273 let status = Command::new("curl")
277 .expect("failed to spawn curl");
278 if status.success() {
284 panic!("failed to download openssl source")
286 let mut shasum = if target.contains("apple") {
287 let mut cmd = Command::new("shasum");
288 cmd.arg("-a").arg("256");
291 Command::new("sha256sum")
293 let output = output(&mut shasum.arg(&tmp));
294 let found = output.split_whitespace().next().unwrap();
295 if found != OPENSSL_SHA256 {
296 panic!("downloaded openssl sha256 different\n\
298 found: {}\n", OPENSSL_SHA256, found);
300 t!(fs::rename(&tmp, &tarball));
302 let obj = out.join(format!("openssl-{}", OPENSSL_VERS));
303 let dst = build.openssl_install_dir(target).unwrap();
304 drop(fs::remove_dir_all(&obj));
305 drop(fs::remove_dir_all(&dst));
306 build.run(Command::new("tar").arg("xf").arg(&tarball).current_dir(&out));
308 let mut configure = Command::new(obj.join("Configure"));
309 configure.arg(format!("--prefix={}", dst.display()));
310 configure.arg("no-dso");
311 configure.arg("no-ssl2");
312 configure.arg("no-ssl3");
314 let os = match target {
315 "aarch64-linux-android" => "linux-aarch64",
316 "aarch64-unknown-linux-gnu" => "linux-aarch64",
317 "arm-linux-androideabi" => "android",
318 "arm-unknown-linux-gnueabi" => "linux-armv4",
319 "arm-unknown-linux-gnueabihf" => "linux-armv4",
320 "armv7-linux-androideabi" => "android-armv7",
321 "armv7-unknown-linux-gnueabihf" => "linux-armv4",
322 "i686-apple-darwin" => "darwin-i386-cc",
323 "i686-linux-android" => "android-x86",
324 "i686-unknown-freebsd" => "BSD-x86-elf",
325 "i686-unknown-linux-gnu" => "linux-elf",
326 "i686-unknown-linux-musl" => "linux-elf",
327 "mips-unknown-linux-gnu" => "linux-mips32",
328 "mips64-unknown-linux-gnuabi64" => "linux64-mips64",
329 "mips64el-unknown-linux-gnuabi64" => "linux64-mips64",
330 "mipsel-unknown-linux-gnu" => "linux-mips32",
331 "powerpc-unknown-linux-gnu" => "linux-ppc",
332 "powerpc64-unknown-linux-gnu" => "linux-ppc64",
333 "powerpc64le-unknown-linux-gnu" => "linux-ppc64le",
334 "s390x-unknown-linux-gnu" => "linux64-s390x",
335 "x86_64-apple-darwin" => "darwin64-x86_64-cc",
336 "x86_64-linux-android" => "linux-x86_64",
337 "x86_64-unknown-freebsd" => "BSD-x86_64",
338 "x86_64-unknown-linux-gnu" => "linux-x86_64",
339 "x86_64-unknown-linux-musl" => "linux-x86_64",
340 "x86_64-unknown-netbsd" => "BSD-x86_64",
341 _ => panic!("don't know how to configure OpenSSL for {}", target),
344 configure.env("CC", build.cc(target));
345 for flag in build.cflags(target) {
348 // There is no specific os target for android aarch64 or x86_64,
349 // so we need to pass some extra cflags
350 if target == "aarch64-linux-android" || target == "x86_64-linux-android" {
351 configure.arg("-mandroid");
352 configure.arg("-fomit-frame-pointer");
355 // Non-PIE linker support was removed in Lollipop
356 // https://source.android.com/security/enhancements/enhancements50
357 if target == "i686-linux-android" {
358 configure.arg("no-asm");
360 configure.current_dir(&obj);
361 println!("Configuring openssl for {}", target);
362 build.run_quiet(&mut configure);
363 println!("Building openssl for {}", target);
364 build.run_quiet(Command::new("make").arg("-j1").current_dir(&obj));
365 println!("Installing openssl for {}", target);
366 build.run_quiet(Command::new("make").arg("install").current_dir(&obj));
368 let mut f = t!(File::create(&stamp));
369 t!(f.write_all(OPENSSL_VERS.as_bytes()));