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;
35 use builder::{Builder, Step};
38 // rules.build("llvm", "src/llvm")
41 // if s.target == build.build {
44 // s.target(&build.build)
47 // .run(move |s| native::llvm(build, s.target));
49 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
51 pub target: Interned<String>,
56 const ONLY_HOSTS: bool = true;
58 /// Compile LLVM for `target`.
59 fn run(self, builder: &Builder) {
60 let build = builder.build;
61 let target = self.target;
62 // If we're using a custom LLVM bail out here, but we can only use a
63 // custom LLVM for the build triple.
64 if let Some(config) = build.config.target_config.get(&target) {
65 if let Some(ref s) = config.llvm_config {
66 return check_llvm_version(build, s);
70 let rebuild_trigger = build.src.join("src/rustllvm/llvm-rebuild-trigger");
71 let mut rebuild_trigger_contents = String::new();
72 t!(t!(File::open(&rebuild_trigger)).read_to_string(&mut rebuild_trigger_contents));
74 let out_dir = build.llvm_out(target);
75 let done_stamp = out_dir.join("llvm-finished-building");
76 if done_stamp.exists() {
77 let mut done_contents = String::new();
78 t!(t!(File::open(&done_stamp)).read_to_string(&mut done_contents));
80 // If LLVM was already built previously and contents of the rebuild-trigger file
81 // didn't change from the previous build, then no action is required.
82 if done_contents == rebuild_trigger_contents {
86 if build.config.llvm_clean_rebuild {
87 drop(fs::remove_dir_all(&out_dir));
90 let _folder = build.fold_output(|| "llvm");
91 println!("Building LLVM for {}", target);
92 let _time = util::timeit();
93 t!(fs::create_dir_all(&out_dir));
95 // http://llvm.org/docs/CMake.html
96 let mut cfg = cmake::Config::new(build.src.join("src/llvm"));
97 if build.config.ninja {
98 cfg.generator("Ninja");
101 let profile = match (build.config.llvm_optimize, build.config.llvm_release_debuginfo) {
102 (false, _) => "Debug",
103 (true, false) => "Release",
104 (true, true) => "RelWithDebInfo",
107 // NOTE: remember to also update `config.toml.example` when changing the defaults!
108 let llvm_targets = match build.config.llvm_targets {
110 None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon",
113 let llvm_exp_targets = match build.config.llvm_experimental_targets {
118 let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"};
124 .define("LLVM_ENABLE_ASSERTIONS", assertions)
125 .define("LLVM_TARGETS_TO_BUILD", llvm_targets)
126 .define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets)
127 .define("LLVM_INCLUDE_EXAMPLES", "OFF")
128 .define("LLVM_INCLUDE_TESTS", "OFF")
129 .define("LLVM_INCLUDE_DOCS", "OFF")
130 .define("LLVM_ENABLE_ZLIB", "OFF")
131 .define("WITH_POLLY", "OFF")
132 .define("LLVM_ENABLE_TERMINFO", "OFF")
133 .define("LLVM_ENABLE_LIBEDIT", "OFF")
134 .define("LLVM_PARALLEL_COMPILE_JOBS", build.jobs().to_string())
135 .define("LLVM_TARGET_ARCH", target.split('-').next().unwrap())
136 .define("LLVM_DEFAULT_TARGET_TRIPLE", target);
138 if target.contains("msvc") {
139 cfg.define("LLVM_USE_CRT_DEBUG", "MT");
140 cfg.define("LLVM_USE_CRT_RELEASE", "MT");
141 cfg.define("LLVM_USE_CRT_RELWITHDEBINFO", "MT");
142 cfg.static_crt(true);
145 if target.starts_with("i686") {
146 cfg.define("LLVM_BUILD_32_BITS", "ON");
149 if let Some(num_linkers) = build.config.llvm_link_jobs {
151 cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
155 // http://llvm.org/docs/HowToCrossCompileLLVM.html
156 if target != build.build {
157 builder.ensure(Llvm { target: build.build });
158 // FIXME: if the llvm root for the build triple is overridden then we
159 // should use llvm-tblgen from there, also should verify that it
160 // actually exists most of the time in normal installs of LLVM.
161 let host = build.llvm_out(build.build).join("bin/llvm-tblgen");
162 cfg.define("CMAKE_CROSSCOMPILING", "True")
163 .define("LLVM_TABLEGEN", &host);
166 let sanitize_cc = |cc: &Path| {
167 if target.contains("msvc") {
168 OsString::from(cc.to_str().unwrap().replace("\\", "/"))
170 cc.as_os_str().to_owned()
174 let configure_compilers = |cfg: &mut cmake::Config| {
175 // MSVC with CMake uses msbuild by default which doesn't respect these
176 // vars that we'd otherwise configure. In that case we just skip this
178 if target.contains("msvc") && !build.config.ninja {
182 let cc = build.cc(target);
183 let cxx = build.cxx(target).unwrap();
185 // Handle msvc + ninja + ccache specially (this is what the bots use)
186 if target.contains("msvc") &&
187 build.config.ninja &&
188 build.config.ccache.is_some() {
189 let mut cc = env::current_exe().expect("failed to get cwd");
190 cc.set_file_name("sccache-plus-cl.exe");
192 cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
193 .define("CMAKE_CXX_COMPILER", sanitize_cc(&cc));
194 cfg.env("SCCACHE_PATH",
195 build.config.ccache.as_ref().unwrap())
196 .env("SCCACHE_TARGET", target);
198 // If ccache is configured we inform the build a little differently hwo
199 // to invoke ccache while also invoking our compilers.
200 } else if let Some(ref ccache) = build.config.ccache {
201 cfg.define("CMAKE_C_COMPILER", ccache)
202 .define("CMAKE_C_COMPILER_ARG1", sanitize_cc(cc))
203 .define("CMAKE_CXX_COMPILER", ccache)
204 .define("CMAKE_CXX_COMPILER_ARG1", sanitize_cc(cxx));
206 cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc))
207 .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx));
210 cfg.build_arg("-j").build_arg(build.jobs().to_string());
211 cfg.define("CMAKE_C_FLAGS", build.cflags(target).join(" "));
212 cfg.define("CMAKE_CXX_FLAGS", build.cflags(target).join(" "));
215 configure_compilers(&mut cfg);
217 if env::var_os("SCCACHE_ERROR_LOG").is_some() {
218 cfg.env("RUST_LOG", "sccache=warn");
221 // FIXME: we don't actually need to build all LLVM tools and all LLVM
222 // libraries here, e.g. we just want a few components and a few
223 // tools. Figure out how to filter them down and only build the right
224 // tools and libs on all platforms.
227 t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes()));
231 fn check_llvm_version(build: &Build, llvm_config: &Path) {
232 if !build.config.llvm_version_check {
236 let mut cmd = Command::new(llvm_config);
237 let version = output(cmd.arg("--version"));
238 if version.starts_with("3.5") || version.starts_with("3.6") ||
239 version.starts_with("3.7") {
242 panic!("\n\nbad LLVM version: {}, need >=3.5\n\n", version)
245 //rules.build("test-helpers", "src/rt/rust_test_helpers.c")
246 // .run(move |s| native::test_helpers(build, s.target));
248 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
249 pub struct TestHelpers {
250 pub target: Interned<String>,
253 impl Step for TestHelpers {
256 fn should_run(_builder: &Builder, path: &Path) -> bool {
257 path.ends_with("src/rt/rust_test_helpers.c")
262 _path: Option<&Path>,
263 _host: Interned<String>,
264 target: Interned<String>,
266 builder.ensure(TestHelpers { target })
269 /// Compiles the `rust_test_helpers.c` library which we used in various
270 /// `run-pass` test suites for ABI testing.
271 fn run(self, builder: &Builder) {
272 let build = builder.build;
273 let target = self.target;
274 let dst = build.test_helpers_out(target);
275 let src = build.src.join("src/rt/rust_test_helpers.c");
276 if up_to_date(&src, &dst.join("librust_test_helpers.a")) {
280 let _folder = build.fold_output(|| "build_test_helpers");
281 println!("Building test helpers");
282 t!(fs::create_dir_all(&dst));
283 let mut cfg = gcc::Config::new();
285 // We may have found various cross-compilers a little differently due to our
286 // extra configuration, so inform gcc of these compilers. Note, though, that
287 // on MSVC we still need gcc's detection of env vars (ugh).
288 if !target.contains("msvc") {
289 if let Some(ar) = build.ar(target) {
292 cfg.compiler(build.cc(target));
295 cfg.cargo_metadata(false)
301 .file(build.src.join("src/rt/rust_test_helpers.c"))
302 .compile("librust_test_helpers.a");
306 const OPENSSL_VERS: &'static str = "1.0.2k";
307 const OPENSSL_SHA256: &'static str =
308 "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0";
310 //rules.build("openssl", "path/to/nowhere")
311 // .run(move |s| native::openssl(build, s.target));
313 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
315 pub target: Interned<String>,
318 impl Step for Openssl {
321 fn should_run(_builder: &Builder, _path: &Path) -> bool {
325 fn run(self, builder: &Builder) {
326 let build = builder.build;
327 let target = self.target;
328 let out = match build.openssl_dir(target) {
333 let stamp = out.join(".stamp");
334 let mut contents = String::new();
335 drop(File::open(&stamp).and_then(|mut f| f.read_to_string(&mut contents)));
336 if contents == OPENSSL_VERS {
339 t!(fs::create_dir_all(&out));
341 let name = format!("openssl-{}.tar.gz", OPENSSL_VERS);
342 let tarball = out.join(&name);
343 if !tarball.exists() {
344 let tmp = tarball.with_extension("tmp");
345 // originally from https://www.openssl.org/source/...
346 let url = format!("https://s3.amazonaws.com/rust-lang-ci/rust-ci-mirror/{}",
350 let status = Command::new("curl")
354 .expect("failed to spawn curl");
355 if status.success() {
361 panic!("failed to download openssl source")
363 let mut shasum = if target.contains("apple") {
364 let mut cmd = Command::new("shasum");
365 cmd.arg("-a").arg("256");
368 Command::new("sha256sum")
370 let output = output(&mut shasum.arg(&tmp));
371 let found = output.split_whitespace().next().unwrap();
372 if found != OPENSSL_SHA256 {
373 panic!("downloaded openssl sha256 different\n\
375 found: {}\n", OPENSSL_SHA256, found);
377 t!(fs::rename(&tmp, &tarball));
379 let obj = out.join(format!("openssl-{}", OPENSSL_VERS));
380 let dst = build.openssl_install_dir(target).unwrap();
381 drop(fs::remove_dir_all(&obj));
382 drop(fs::remove_dir_all(&dst));
383 build.run(Command::new("tar").arg("xf").arg(&tarball).current_dir(&out));
385 let mut configure = Command::new(obj.join("Configure"));
386 configure.arg(format!("--prefix={}", dst.display()));
387 configure.arg("no-dso");
388 configure.arg("no-ssl2");
389 configure.arg("no-ssl3");
391 let os = match &*target {
392 "aarch64-linux-android" => "linux-aarch64",
393 "aarch64-unknown-linux-gnu" => "linux-aarch64",
394 "arm-linux-androideabi" => "android",
395 "arm-unknown-linux-gnueabi" => "linux-armv4",
396 "arm-unknown-linux-gnueabihf" => "linux-armv4",
397 "armv7-linux-androideabi" => "android-armv7",
398 "armv7-unknown-linux-gnueabihf" => "linux-armv4",
399 "i686-apple-darwin" => "darwin-i386-cc",
400 "i686-linux-android" => "android-x86",
401 "i686-unknown-freebsd" => "BSD-x86-elf",
402 "i686-unknown-linux-gnu" => "linux-elf",
403 "i686-unknown-linux-musl" => "linux-elf",
404 "mips-unknown-linux-gnu" => "linux-mips32",
405 "mips64-unknown-linux-gnuabi64" => "linux64-mips64",
406 "mips64el-unknown-linux-gnuabi64" => "linux64-mips64",
407 "mipsel-unknown-linux-gnu" => "linux-mips32",
408 "powerpc-unknown-linux-gnu" => "linux-ppc",
409 "powerpc64-unknown-linux-gnu" => "linux-ppc64",
410 "powerpc64le-unknown-linux-gnu" => "linux-ppc64le",
411 "s390x-unknown-linux-gnu" => "linux64-s390x",
412 "x86_64-apple-darwin" => "darwin64-x86_64-cc",
413 "x86_64-linux-android" => "linux-x86_64",
414 "x86_64-unknown-freebsd" => "BSD-x86_64",
415 "x86_64-unknown-linux-gnu" => "linux-x86_64",
416 "x86_64-unknown-linux-musl" => "linux-x86_64",
417 "x86_64-unknown-netbsd" => "BSD-x86_64",
418 _ => panic!("don't know how to configure OpenSSL for {}", target),
421 configure.env("CC", build.cc(target));
422 for flag in build.cflags(target) {
425 // There is no specific os target for android aarch64 or x86_64,
426 // so we need to pass some extra cflags
427 if target == "aarch64-linux-android" || target == "x86_64-linux-android" {
428 configure.arg("-mandroid");
429 configure.arg("-fomit-frame-pointer");
432 // Non-PIE linker support was removed in Lollipop
433 // https://source.android.com/security/enhancements/enhancements50
434 if target == "i686-linux-android" {
435 configure.arg("no-asm");
437 configure.current_dir(&obj);
438 println!("Configuring openssl for {}", target);
439 build.run_quiet(&mut configure);
440 println!("Building openssl for {}", target);
441 build.run_quiet(Command::new("make").arg("-j1").current_dir(&obj));
442 println!("Installing openssl for {}", target);
443 build.run_quiet(Command::new("make").arg("install").current_dir(&obj));
445 let mut f = t!(File::create(&stamp));
446 t!(f.write_all(OPENSSL_VERS.as_bytes()));