]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/native.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[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::fs::{self, File};
23 use std::io::{Read, Write};
24 use std::path::Path;
25 use std::process::Command;
26
27 use build_helper::output;
28 use cmake;
29 use gcc;
30
31 use Build;
32 use util;
33 use build_helper::up_to_date;
34
35 /// Compile LLVM for `target`.
36 pub fn llvm(build: &Build, target: &str) {
37     // If we're using a custom LLVM bail out here, but we can only use a
38     // custom LLVM for the build triple.
39     if let Some(config) = build.config.target_config.get(target) {
40         if let Some(ref s) = config.llvm_config {
41             return check_llvm_version(build, s);
42         }
43     }
44
45     let rebuild_trigger = build.src.join("src/rustllvm/llvm-rebuild-trigger");
46     let mut rebuild_trigger_contents = String::new();
47     t!(t!(File::open(&rebuild_trigger)).read_to_string(&mut rebuild_trigger_contents));
48
49     let out_dir = build.llvm_out(target);
50     let done_stamp = out_dir.join("llvm-finished-building");
51     if done_stamp.exists() {
52         let mut done_contents = String::new();
53         t!(t!(File::open(&done_stamp)).read_to_string(&mut done_contents));
54
55         // If LLVM was already built previously and contents of the rebuild-trigger file
56         // didn't change from the previous build, then no action is required.
57         if done_contents == rebuild_trigger_contents {
58             return
59         }
60     }
61     if build.config.llvm_clean_rebuild {
62         drop(fs::remove_dir_all(&out_dir));
63     }
64
65     println!("Building LLVM for {}", target);
66     let _time = util::timeit();
67     t!(fs::create_dir_all(&out_dir));
68
69     // http://llvm.org/docs/CMake.html
70     let mut cfg = cmake::Config::new(build.src.join("src/llvm"));
71     if build.config.ninja {
72         cfg.generator("Ninja");
73     }
74
75     let profile = match (build.config.llvm_optimize, build.config.llvm_release_debuginfo) {
76         (false, _) => "Debug",
77         (true, false) => "Release",
78         (true, true) => "RelWithDebInfo",
79     };
80
81     // NOTE: remember to also update `config.toml.example` when changing the defaults!
82     let llvm_targets = match build.config.llvm_targets {
83         Some(ref s) => s,
84         None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX",
85     };
86
87     let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"};
88
89     cfg.target(target)
90        .host(&build.config.build)
91        .out_dir(&out_dir)
92        .profile(profile)
93        .define("LLVM_ENABLE_ASSERTIONS", assertions)
94        .define("LLVM_TARGETS_TO_BUILD", llvm_targets)
95        .define("LLVM_INCLUDE_EXAMPLES", "OFF")
96        .define("LLVM_INCLUDE_TESTS", "OFF")
97        .define("LLVM_INCLUDE_DOCS", "OFF")
98        .define("LLVM_ENABLE_ZLIB", "OFF")
99        .define("WITH_POLLY", "OFF")
100        .define("LLVM_ENABLE_TERMINFO", "OFF")
101        .define("LLVM_ENABLE_LIBEDIT", "OFF")
102        .define("LLVM_PARALLEL_COMPILE_JOBS", build.jobs().to_string())
103        .define("LLVM_TARGET_ARCH", target.split('-').next().unwrap())
104        .define("LLVM_DEFAULT_TARGET_TRIPLE", target);
105
106     if target.contains("msvc") {
107         cfg.define("LLVM_USE_CRT_DEBUG", "MT");
108         cfg.define("LLVM_USE_CRT_RELEASE", "MT");
109         cfg.define("LLVM_USE_CRT_RELWITHDEBINFO", "MT");
110     }
111
112     if target.starts_with("i686") {
113         cfg.define("LLVM_BUILD_32_BITS", "ON");
114     }
115
116     if let Some(num_linkers) = build.config.llvm_link_jobs {
117         if num_linkers > 0 {
118             cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
119         }
120     }
121
122     // http://llvm.org/docs/HowToCrossCompileLLVM.html
123     if target != build.config.build {
124         // FIXME: if the llvm root for the build triple is overridden then we
125         //        should use llvm-tblgen from there, also should verify that it
126         //        actually exists most of the time in normal installs of LLVM.
127         let host = build.llvm_out(&build.config.build).join("bin/llvm-tblgen");
128         cfg.define("CMAKE_CROSSCOMPILING", "True")
129            .define("LLVM_TABLEGEN", &host);
130     }
131
132     // MSVC handles compiler business itself
133     if !target.contains("msvc") {
134         if let Some(ref ccache) = build.config.ccache {
135            cfg.define("CMAKE_C_COMPILER", ccache)
136               .define("CMAKE_C_COMPILER_ARG1", build.cc(target))
137               .define("CMAKE_CXX_COMPILER", ccache)
138               .define("CMAKE_CXX_COMPILER_ARG1", build.cxx(target));
139         } else {
140            cfg.define("CMAKE_C_COMPILER", build.cc(target))
141               .define("CMAKE_CXX_COMPILER", build.cxx(target));
142         }
143         cfg.build_arg("-j").build_arg(build.jobs().to_string());
144
145         cfg.define("CMAKE_C_FLAGS", build.cflags(target).join(" "));
146         cfg.define("CMAKE_CXX_FLAGS", build.cflags(target).join(" "));
147     }
148
149     if env::var_os("SCCACHE_ERROR_LOG").is_some() {
150         cfg.env("RUST_LOG", "sccache=info");
151     }
152
153     // FIXME: we don't actually need to build all LLVM tools and all LLVM
154     //        libraries here, e.g. we just want a few components and a few
155     //        tools. Figure out how to filter them down and only build the right
156     //        tools and libs on all platforms.
157     cfg.build();
158
159     t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes()));
160 }
161
162 fn check_llvm_version(build: &Build, llvm_config: &Path) {
163     if !build.config.llvm_version_check {
164         return
165     }
166
167     let mut cmd = Command::new(llvm_config);
168     let version = output(cmd.arg("--version"));
169     if version.starts_with("3.5") || version.starts_with("3.6") ||
170        version.starts_with("3.7") {
171         return
172     }
173     panic!("\n\nbad LLVM version: {}, need >=3.5\n\n", version)
174 }
175
176 /// Compiles the `rust_test_helpers.c` library which we used in various
177 /// `run-pass` test suites for ABI testing.
178 pub fn test_helpers(build: &Build, target: &str) {
179     let dst = build.test_helpers_out(target);
180     let src = build.src.join("src/rt/rust_test_helpers.c");
181     if up_to_date(&src, &dst.join("librust_test_helpers.a")) {
182         return
183     }
184
185     println!("Building test helpers");
186     t!(fs::create_dir_all(&dst));
187     let mut cfg = gcc::Config::new();
188
189     // We may have found various cross-compilers a little differently due to our
190     // extra configuration, so inform gcc of these compilers. Note, though, that
191     // on MSVC we still need gcc's detection of env vars (ugh).
192     if !target.contains("msvc") {
193         if let Some(ar) = build.ar(target) {
194             cfg.archiver(ar);
195         }
196         cfg.compiler(build.cc(target));
197     }
198
199     cfg.cargo_metadata(false)
200        .out_dir(&dst)
201        .target(target)
202        .host(&build.config.build)
203        .opt_level(0)
204        .debug(false)
205        .file(build.src.join("src/rt/rust_test_helpers.c"))
206        .compile("librust_test_helpers.a");
207 }
208 const OPENSSL_VERS: &'static str = "1.0.2k";
209 const OPENSSL_SHA256: &'static str =
210     "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0";
211
212 pub fn openssl(build: &Build, target: &str) {
213     let out = match build.openssl_dir(target) {
214         Some(dir) => dir,
215         None => return,
216     };
217
218     let stamp = out.join(".stamp");
219     let mut contents = String::new();
220     drop(File::open(&stamp).and_then(|mut f| f.read_to_string(&mut contents)));
221     if contents == OPENSSL_VERS {
222         return
223     }
224     t!(fs::create_dir_all(&out));
225
226     let name = format!("openssl-{}.tar.gz", OPENSSL_VERS);
227     let tarball = out.join(&name);
228     if !tarball.exists() {
229         let tmp = tarball.with_extension("tmp");
230         // originally from https://www.openssl.org/source/...
231         let url = format!("https://s3.amazonaws.com/rust-lang-ci/rust-ci-mirror/{}",
232                           name);
233         let mut ok = false;
234         for _ in 0..3 {
235             let status = Command::new("curl")
236                             .arg("-o").arg(&tmp)
237                             .arg(&url)
238                             .status()
239                             .expect("failed to spawn curl");
240             if status.success() {
241                 ok = true;
242                 break
243             }
244         }
245         if !ok {
246             panic!("failed to download openssl source")
247         }
248         let mut shasum = if target.contains("apple") {
249             let mut cmd = Command::new("shasum");
250             cmd.arg("-a").arg("256");
251             cmd
252         } else {
253             Command::new("sha256sum")
254         };
255         let output = output(&mut shasum.arg(&tmp));
256         let found = output.split_whitespace().next().unwrap();
257         if found != OPENSSL_SHA256 {
258             panic!("downloaded openssl sha256 different\n\
259                     expected: {}\n\
260                     found:    {}\n", OPENSSL_SHA256, found);
261         }
262         t!(fs::rename(&tmp, &tarball));
263     }
264     let obj = out.join(format!("openssl-{}", OPENSSL_VERS));
265     let dst = build.openssl_install_dir(target).unwrap();
266     drop(fs::remove_dir_all(&obj));
267     drop(fs::remove_dir_all(&dst));
268     build.run(Command::new("tar").arg("xf").arg(&tarball).current_dir(&out));
269
270     let mut configure = Command::new(obj.join("Configure"));
271     configure.arg(format!("--prefix={}", dst.display()));
272     configure.arg("no-dso");
273     configure.arg("no-ssl2");
274     configure.arg("no-ssl3");
275
276     let os = match target {
277         "aarch64-unknown-linux-gnu" => "linux-aarch64",
278         "arm-unknown-linux-gnueabi" => "linux-armv4",
279         "arm-unknown-linux-gnueabihf" => "linux-armv4",
280         "armv7-unknown-linux-gnueabihf" => "linux-armv4",
281         "i686-apple-darwin" => "darwin-i386-cc",
282         "i686-unknown-freebsd" => "BSD-x86-elf",
283         "i686-unknown-linux-gnu" => "linux-elf",
284         "i686-unknown-linux-musl" => "linux-elf",
285         "mips-unknown-linux-gnu" => "linux-mips32",
286         "mips64-unknown-linux-gnuabi64" => "linux64-mips64",
287         "mips64el-unknown-linux-gnuabi64" => "linux64-mips64",
288         "mipsel-unknown-linux-gnu" => "linux-mips32",
289         "powerpc-unknown-linux-gnu" => "linux-ppc",
290         "powerpc64-unknown-linux-gnu" => "linux-ppc64",
291         "powerpc64le-unknown-linux-gnu" => "linux-ppc64le",
292         "s390x-unknown-linux-gnu" => "linux64-s390x",
293         "x86_64-apple-darwin" => "darwin64-x86_64-cc",
294         "x86_64-unknown-freebsd" => "BSD-x86_64",
295         "x86_64-unknown-linux-gnu" => "linux-x86_64",
296         "x86_64-unknown-linux-musl" => "linux-x86_64",
297         "x86_64-unknown-netbsd" => "BSD-x86_64",
298         _ => panic!("don't know how to configure OpenSSL for {}", target),
299     };
300     configure.arg(os);
301     configure.env("CC", build.cc(target));
302     for flag in build.cflags(target) {
303         configure.arg(flag);
304     }
305     configure.current_dir(&obj);
306     println!("Configuring openssl for {}", target);
307     build.run_quiet(&mut configure);
308     println!("Building openssl for {}", target);
309     build.run_quiet(Command::new("make").current_dir(&obj));
310     println!("Installing openssl for {}", target);
311     build.run_quiet(Command::new("make").arg("install").current_dir(&obj));
312
313     let mut f = t!(File::create(&stamp));
314     t!(f.write_all(OPENSSL_VERS.as_bytes()));
315 }