]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/native.rs
rustdoc: Hide `self: Box<Self>` in list of deref methods
[rust.git] / src / bootstrap / native.rs
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.
4 //
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.
10
11 //! Compilation of native dependencies like LLVM.
12 //!
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).
17 //!
18 //! LLVM and compiler-rt are essentially just wired up to everything else to
19 //! ensure that they're always in place if needed.
20
21 use std::env;
22 use std::ffi::OsString;
23 use std::fs::{self, File};
24 use std::io::{Read, Write};
25 use std::path::Path;
26 use std::process::Command;
27
28 use build_helper::output;
29 use cmake;
30 use gcc;
31
32 use Build;
33 use util;
34 use build_helper::up_to_date;
35
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);
43         }
44     }
45
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));
49
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));
55
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 {
59             return
60         }
61     }
62     if build.config.llvm_clean_rebuild {
63         drop(fs::remove_dir_all(&out_dir));
64     }
65
66     println!("Building LLVM for {}", target);
67     let _time = util::timeit();
68     t!(fs::create_dir_all(&out_dir));
69
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");
74     }
75
76     let profile = match (build.config.llvm_optimize, build.config.llvm_release_debuginfo) {
77         (false, _) => "Debug",
78         (true, false) => "Release",
79         (true, true) => "RelWithDebInfo",
80     };
81
82     // NOTE: remember to also update `config.toml.example` when changing the defaults!
83     let llvm_targets = match build.config.llvm_targets {
84         Some(ref s) => s,
85         None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon",
86     };
87
88     let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"};
89
90     cfg.target(target)
91        .host(&build.config.build)
92        .out_dir(&out_dir)
93        .profile(profile)
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);
106
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");
111         cfg.static_crt(true);
112     }
113
114     if target.starts_with("i686") {
115         cfg.define("LLVM_BUILD_32_BITS", "ON");
116     }
117
118     if let Some(num_linkers) = build.config.llvm_link_jobs {
119         if num_linkers > 0 {
120             cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
121         }
122     }
123
124     // http://llvm.org/docs/HowToCrossCompileLLVM.html
125     if target != build.config.build {
126         // FIXME: if the llvm root for the build triple is overridden then we
127         //        should use llvm-tblgen from there, also should verify that it
128         //        actually exists most of the time in normal installs of LLVM.
129         let host = build.llvm_out(&build.config.build).join("bin/llvm-tblgen");
130         cfg.define("CMAKE_CROSSCOMPILING", "True")
131            .define("LLVM_TABLEGEN", &host);
132     }
133
134     let sanitize_cc = |cc: &Path| {
135         if target.contains("msvc") {
136             OsString::from(cc.to_str().unwrap().replace("\\", "/"))
137         } else {
138             cc.as_os_str().to_owned()
139         }
140     };
141
142     let configure_compilers = |cfg: &mut cmake::Config| {
143         // MSVC with CMake uses msbuild by default which doesn't respect these
144         // vars that we'd otherwise configure. In that case we just skip this
145         // entirely.
146         if target.contains("msvc") && !build.config.ninja {
147             return
148         }
149
150         let cc = build.cc(target);
151         let cxx = build.cxx(target);
152
153         // Handle msvc + ninja + ccache specially (this is what the bots use)
154         if target.contains("msvc") &&
155            build.config.ninja &&
156            build.config.ccache.is_some() {
157             let mut cc = env::current_exe().expect("failed to get cwd");
158             cc.set_file_name("sccache-plus-cl.exe");
159
160            cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
161               .define("CMAKE_CXX_COMPILER", sanitize_cc(&cc));
162            cfg.env("SCCACHE_PATH",
163                    build.config.ccache.as_ref().unwrap())
164               .env("SCCACHE_TARGET", target);
165
166         // If ccache is configured we inform the build a little differently hwo
167         // to invoke ccache while also invoking our compilers.
168         } else if let Some(ref ccache) = build.config.ccache {
169            cfg.define("CMAKE_C_COMPILER", ccache)
170               .define("CMAKE_C_COMPILER_ARG1", sanitize_cc(cc))
171               .define("CMAKE_CXX_COMPILER", ccache)
172               .define("CMAKE_CXX_COMPILER_ARG1", sanitize_cc(cxx));
173         } else {
174            cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc))
175               .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx));
176         }
177
178         cfg.build_arg("-j").build_arg(build.jobs().to_string());
179         cfg.define("CMAKE_C_FLAGS", build.cflags(target).join(" "));
180         cfg.define("CMAKE_CXX_FLAGS", build.cflags(target).join(" "));
181     };
182
183     configure_compilers(&mut cfg);
184
185     if env::var_os("SCCACHE_ERROR_LOG").is_some() {
186         cfg.env("RUST_LOG", "sccache=info");
187     }
188
189     // FIXME: we don't actually need to build all LLVM tools and all LLVM
190     //        libraries here, e.g. we just want a few components and a few
191     //        tools. Figure out how to filter them down and only build the right
192     //        tools and libs on all platforms.
193     cfg.build();
194
195     t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes()));
196 }
197
198 fn check_llvm_version(build: &Build, llvm_config: &Path) {
199     if !build.config.llvm_version_check {
200         return
201     }
202
203     let mut cmd = Command::new(llvm_config);
204     let version = output(cmd.arg("--version"));
205     if version.starts_with("3.5") || version.starts_with("3.6") ||
206        version.starts_with("3.7") {
207         return
208     }
209     panic!("\n\nbad LLVM version: {}, need >=3.5\n\n", version)
210 }
211
212 /// Compiles the `rust_test_helpers.c` library which we used in various
213 /// `run-pass` test suites for ABI testing.
214 pub fn test_helpers(build: &Build, target: &str) {
215     let dst = build.test_helpers_out(target);
216     let src = build.src.join("src/rt/rust_test_helpers.c");
217     if up_to_date(&src, &dst.join("librust_test_helpers.a")) {
218         return
219     }
220
221     println!("Building test helpers");
222     t!(fs::create_dir_all(&dst));
223     let mut cfg = gcc::Config::new();
224
225     // We may have found various cross-compilers a little differently due to our
226     // extra configuration, so inform gcc of these compilers. Note, though, that
227     // on MSVC we still need gcc's detection of env vars (ugh).
228     if !target.contains("msvc") {
229         if let Some(ar) = build.ar(target) {
230             cfg.archiver(ar);
231         }
232         cfg.compiler(build.cc(target));
233     }
234
235     cfg.cargo_metadata(false)
236        .out_dir(&dst)
237        .target(target)
238        .host(&build.config.build)
239        .opt_level(0)
240        .debug(false)
241        .file(build.src.join("src/rt/rust_test_helpers.c"))
242        .compile("librust_test_helpers.a");
243 }
244 const OPENSSL_VERS: &'static str = "1.0.2k";
245 const OPENSSL_SHA256: &'static str =
246     "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0";
247
248 pub fn openssl(build: &Build, target: &str) {
249     let out = match build.openssl_dir(target) {
250         Some(dir) => dir,
251         None => return,
252     };
253
254     let stamp = out.join(".stamp");
255     let mut contents = String::new();
256     drop(File::open(&stamp).and_then(|mut f| f.read_to_string(&mut contents)));
257     if contents == OPENSSL_VERS {
258         return
259     }
260     t!(fs::create_dir_all(&out));
261
262     let name = format!("openssl-{}.tar.gz", OPENSSL_VERS);
263     let tarball = out.join(&name);
264     if !tarball.exists() {
265         let tmp = tarball.with_extension("tmp");
266         // originally from https://www.openssl.org/source/...
267         let url = format!("https://s3.amazonaws.com/rust-lang-ci/rust-ci-mirror/{}",
268                           name);
269         let mut ok = false;
270         for _ in 0..3 {
271             let status = Command::new("curl")
272                             .arg("-o").arg(&tmp)
273                             .arg(&url)
274                             .status()
275                             .expect("failed to spawn curl");
276             if status.success() {
277                 ok = true;
278                 break
279             }
280         }
281         if !ok {
282             panic!("failed to download openssl source")
283         }
284         let mut shasum = if target.contains("apple") {
285             let mut cmd = Command::new("shasum");
286             cmd.arg("-a").arg("256");
287             cmd
288         } else {
289             Command::new("sha256sum")
290         };
291         let output = output(&mut shasum.arg(&tmp));
292         let found = output.split_whitespace().next().unwrap();
293         if found != OPENSSL_SHA256 {
294             panic!("downloaded openssl sha256 different\n\
295                     expected: {}\n\
296                     found:    {}\n", OPENSSL_SHA256, found);
297         }
298         t!(fs::rename(&tmp, &tarball));
299     }
300     let obj = out.join(format!("openssl-{}", OPENSSL_VERS));
301     let dst = build.openssl_install_dir(target).unwrap();
302     drop(fs::remove_dir_all(&obj));
303     drop(fs::remove_dir_all(&dst));
304     build.run(Command::new("tar").arg("xf").arg(&tarball).current_dir(&out));
305
306     let mut configure = Command::new(obj.join("Configure"));
307     configure.arg(format!("--prefix={}", dst.display()));
308     configure.arg("no-dso");
309     configure.arg("no-ssl2");
310     configure.arg("no-ssl3");
311
312     let os = match target {
313         "aarch64-linux-android" => "linux-aarch64",
314         "aarch64-unknown-linux-gnu" => "linux-aarch64",
315         "arm-linux-androideabi" => "android",
316         "arm-unknown-linux-gnueabi" => "linux-armv4",
317         "arm-unknown-linux-gnueabihf" => "linux-armv4",
318         "armv7-linux-androideabi" => "android-armv7",
319         "armv7-unknown-linux-gnueabihf" => "linux-armv4",
320         "i686-apple-darwin" => "darwin-i386-cc",
321         "i686-linux-android" => "android-x86",
322         "i686-unknown-freebsd" => "BSD-x86-elf",
323         "i686-unknown-linux-gnu" => "linux-elf",
324         "i686-unknown-linux-musl" => "linux-elf",
325         "mips-unknown-linux-gnu" => "linux-mips32",
326         "mips64-unknown-linux-gnuabi64" => "linux64-mips64",
327         "mips64el-unknown-linux-gnuabi64" => "linux64-mips64",
328         "mipsel-unknown-linux-gnu" => "linux-mips32",
329         "powerpc-unknown-linux-gnu" => "linux-ppc",
330         "powerpc64-unknown-linux-gnu" => "linux-ppc64",
331         "powerpc64le-unknown-linux-gnu" => "linux-ppc64le",
332         "s390x-unknown-linux-gnu" => "linux64-s390x",
333         "x86_64-apple-darwin" => "darwin64-x86_64-cc",
334         "x86_64-linux-android" => "linux-x86_64",
335         "x86_64-unknown-freebsd" => "BSD-x86_64",
336         "x86_64-unknown-linux-gnu" => "linux-x86_64",
337         "x86_64-unknown-linux-musl" => "linux-x86_64",
338         "x86_64-unknown-netbsd" => "BSD-x86_64",
339         _ => panic!("don't know how to configure OpenSSL for {}", target),
340     };
341     configure.arg(os);
342     configure.env("CC", build.cc(target));
343     for flag in build.cflags(target) {
344         configure.arg(flag);
345     }
346     // There is no specific os target for android aarch64 or x86_64,
347     // so we need to pass some extra cflags
348     if target == "aarch64-linux-android" || target == "x86_64-linux-android" {
349         configure.arg("-mandroid");
350         configure.arg("-fomit-frame-pointer");
351     }
352     // Make PIE binaries
353     // Non-PIE linker support was removed in Lollipop
354     // https://source.android.com/security/enhancements/enhancements50
355     if target == "i686-linux-android" {
356         configure.arg("no-asm");
357     }
358     configure.current_dir(&obj);
359     println!("Configuring openssl for {}", target);
360     build.run_quiet(&mut configure);
361     println!("Building openssl for {}", target);
362     build.run_quiet(Command::new("make").arg("-j1").current_dir(&obj));
363     println!("Installing openssl for {}", target);
364     build.run_quiet(Command::new("make").arg("install").current_dir(&obj));
365
366     let mut f = t!(File::create(&stamp));
367     t!(f.write_all(OPENSSL_VERS.as_bytes()));
368 }