]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/native.rs
Utilize interning to allow Copy/Clone steps
[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 use builder::{Builder, Step};
36 use cache::Interned;
37
38 // rules.build("llvm", "src/llvm")
39 //      .host(true)
40 //      .dep(move |s| {
41 //          if s.target == build.build {
42 //              Step::noop()
43 //          } else {
44 //              s.target(&build.build)
45 //          }
46 //      })
47 //      .run(move |s| native::llvm(build, s.target));
48
49 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
50 pub struct Llvm {
51     pub target: Interned<String>,
52 }
53
54 impl Step for Llvm {
55     type Output = ();
56     const ONLY_HOSTS: bool = true;
57
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);
67             }
68         }
69
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));
73
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));
79
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 {
83                 return
84             }
85         }
86         if build.config.llvm_clean_rebuild {
87             drop(fs::remove_dir_all(&out_dir));
88         }
89
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));
94
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");
99         }
100
101         let profile = match (build.config.llvm_optimize, build.config.llvm_release_debuginfo) {
102             (false, _) => "Debug",
103             (true, false) => "Release",
104             (true, true) => "RelWithDebInfo",
105         };
106
107         // NOTE: remember to also update `config.toml.example` when changing the defaults!
108         let llvm_targets = match build.config.llvm_targets {
109             Some(ref s) => s,
110             None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon",
111         };
112
113         let llvm_exp_targets = match build.config.llvm_experimental_targets {
114             Some(ref s) => s,
115             None => "",
116         };
117
118         let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"};
119
120         cfg.target(&target)
121            .host(&build.build)
122            .out_dir(&out_dir)
123            .profile(profile)
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);
137
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);
143         }
144
145         if target.starts_with("i686") {
146             cfg.define("LLVM_BUILD_32_BITS", "ON");
147         }
148
149         if let Some(num_linkers) = build.config.llvm_link_jobs {
150             if num_linkers > 0 {
151                 cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string());
152             }
153         }
154
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);
164         }
165
166         let sanitize_cc = |cc: &Path| {
167             if target.contains("msvc") {
168                 OsString::from(cc.to_str().unwrap().replace("\\", "/"))
169             } else {
170                 cc.as_os_str().to_owned()
171             }
172         };
173
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
177             // entirely.
178             if target.contains("msvc") && !build.config.ninja {
179                 return
180             }
181
182             let cc = build.cc(target);
183             let cxx = build.cxx(target).unwrap();
184
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");
191
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);
197
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));
205             } else {
206                cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc))
207                   .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx));
208             }
209
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(" "));
213         };
214
215         configure_compilers(&mut cfg);
216
217         if env::var_os("SCCACHE_ERROR_LOG").is_some() {
218             cfg.env("RUST_LOG", "sccache=warn");
219         }
220
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.
225         cfg.build();
226
227         t!(t!(File::create(&done_stamp)).write_all(rebuild_trigger_contents.as_bytes()));
228     }
229 }
230
231 fn check_llvm_version(build: &Build, llvm_config: &Path) {
232     if !build.config.llvm_version_check {
233         return
234     }
235
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") {
240         return
241     }
242     panic!("\n\nbad LLVM version: {}, need >=3.5\n\n", version)
243 }
244
245 //rules.build("test-helpers", "src/rt/rust_test_helpers.c")
246 //     .run(move |s| native::test_helpers(build, s.target));
247
248 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
249 pub struct TestHelpers {
250     pub target: Interned<String>,
251 }
252
253 impl Step for TestHelpers {
254     type Output = ();
255
256     fn should_run(_builder: &Builder, path: &Path) -> bool {
257         path.ends_with("src/rt/rust_test_helpers.c")
258     }
259
260     fn make_run(
261         builder: &Builder,
262         _path: Option<&Path>,
263         _host: Interned<String>,
264         target: Interned<String>,
265     ) {
266         builder.ensure(TestHelpers { target })
267     }
268
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")) {
277             return
278         }
279
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();
284
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) {
290                 cfg.archiver(ar);
291             }
292             cfg.compiler(build.cc(target));
293         }
294
295         cfg.cargo_metadata(false)
296            .out_dir(&dst)
297            .target(&target)
298            .host(&build.build)
299            .opt_level(0)
300            .debug(false)
301            .file(build.src.join("src/rt/rust_test_helpers.c"))
302            .compile("librust_test_helpers.a");
303     }
304 }
305
306 const OPENSSL_VERS: &'static str = "1.0.2k";
307 const OPENSSL_SHA256: &'static str =
308     "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0";
309
310 //rules.build("openssl", "path/to/nowhere")
311 //     .run(move |s| native::openssl(build, s.target));
312
313     #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
314 pub struct Openssl {
315     pub target: Interned<String>,
316 }
317
318 impl Step for Openssl {
319     type Output = ();
320
321     fn should_run(_builder: &Builder, _path: &Path) -> bool {
322         false
323     }
324
325     fn run(self, builder: &Builder) {
326         let build = builder.build;
327         let target = self.target;
328         let out = match build.openssl_dir(target) {
329             Some(dir) => dir,
330             None => return,
331         };
332
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 {
337             return
338         }
339         t!(fs::create_dir_all(&out));
340
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/{}",
347                               name);
348             let mut ok = false;
349             for _ in 0..3 {
350                 let status = Command::new("curl")
351                                 .arg("-o").arg(&tmp)
352                                 .arg(&url)
353                                 .status()
354                                 .expect("failed to spawn curl");
355                 if status.success() {
356                     ok = true;
357                     break
358                 }
359             }
360             if !ok {
361                 panic!("failed to download openssl source")
362             }
363             let mut shasum = if target.contains("apple") {
364                 let mut cmd = Command::new("shasum");
365                 cmd.arg("-a").arg("256");
366                 cmd
367             } else {
368                 Command::new("sha256sum")
369             };
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\
374                         expected: {}\n\
375                         found:    {}\n", OPENSSL_SHA256, found);
376             }
377             t!(fs::rename(&tmp, &tarball));
378         }
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));
384
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");
390
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),
419         };
420         configure.arg(os);
421         configure.env("CC", build.cc(target));
422         for flag in build.cflags(target) {
423             configure.arg(flag);
424         }
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");
430         }
431         // Make PIE binaries
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");
436         }
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));
444
445         let mut f = t!(File::create(&stamp));
446         t!(f.write_all(OPENSSL_VERS.as_bytes()));
447     }
448 }