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 the
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}
;
25 use std
::path
::{Path, PathBuf}
;
26 use std
::process
::Command
;
28 use build_helper
::output
;
33 use util
::{self, exe}
;
34 use build_helper
::up_to_date
;
35 use builder
::{Builder, RunConfig, ShouldRun, Step}
;
38 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
40 pub target
: Interned
<String
>,
45 type Output
= PathBuf
; // path to llvm-config
47 const ONLY_HOSTS
: bool
= true;
49 fn should_run(run
: ShouldRun
) -> ShouldRun
{
50 run
.path("src/llvm").path("src/llvm-emscripten")
53 fn make_run(run
: RunConfig
) {
54 let emscripten
= run
.path
.ends_with("llvm-emscripten");
55 run
.builder
.ensure(Llvm
{
61 /// Compile LLVM for `target`.
62 fn run(self, builder
: &Builder
) -> PathBuf
{
63 let build
= builder
.build
;
64 let target
= self.target
;
65 let emscripten
= self.emscripten
;
67 // If we're using a custom LLVM bail out here, but we can only use a
68 // custom LLVM for the build triple.
70 if let Some(config
) = build
.config
.target_config
.get(&target
) {
71 if let Some(ref s
) = config
.llvm_config
{
72 check_llvm_version(build
, s
);
73 return s
.to_path_buf()
78 let rebuild_trigger
= build
.src
.join("src/rustllvm/llvm-rebuild-trigger");
79 let mut rebuild_trigger_contents
= String
::new();
80 t
!(t
!(File
::open(&rebuild_trigger
)).read_to_string(&mut rebuild_trigger_contents
));
82 let (out_dir
, llvm_config_ret_dir
) = if emscripten
{
83 let dir
= build
.emscripten_llvm_out(target
);
84 let config_dir
= dir
.join("bin");
87 let mut dir
= build
.llvm_out(build
.config
.build
);
88 if !build
.config
.build
.contains("msvc") || build
.config
.ninja
{
91 (build
.llvm_out(target
), dir
.join("bin"))
93 let done_stamp
= out_dir
.join("llvm-finished-building");
94 let build_llvm_config
= llvm_config_ret_dir
95 .join(exe("llvm-config", &*build
.config
.build
));
96 if done_stamp
.exists() {
97 let mut done_contents
= String
::new();
98 t
!(t
!(File
::open(&done_stamp
)).read_to_string(&mut done_contents
));
100 // If LLVM was already built previously and contents of the rebuild-trigger file
101 // didn't change from the previous build, then no action is required.
102 if done_contents
== rebuild_trigger_contents
{
103 return build_llvm_config
107 let _folder
= build
.fold_output(|| "llvm");
108 let descriptor
= if emscripten { "Emscripten " }
else { "" }
;
109 println
!("Building {}LLVM for {}", descriptor
, target
);
110 let _time
= util
::timeit();
111 t
!(fs
::create_dir_all(&out_dir
));
113 // http://llvm.org/docs/CMake.html
114 let root
= if self.emscripten { "src/llvm-emscripten" }
else { "src/llvm" }
;
115 let mut cfg
= cmake
::Config
::new(build
.src
.join(root
));
117 let profile
= match (build
.config
.llvm_optimize
, build
.config
.llvm_release_debuginfo
) {
118 (false, _
) => "Debug",
119 (true, false) => "Release",
120 (true, true) => "RelWithDebInfo",
123 // NOTE: remember to also update `config.toml.example` when changing the
125 let llvm_targets
= if self.emscripten
{
128 match build
.config
.llvm_targets
{
130 None
=> "X86;ARM;AArch64;Mips;PowerPC;SystemZ;MSP430;Sparc;NVPTX;Hexagon",
134 let llvm_exp_targets
= if self.emscripten
{
137 &build
.config
.llvm_experimental_targets
[..]
140 let assertions
= if build
.config
.llvm_assertions {"ON"}
else {"OFF"}
;
142 cfg
.out_dir(&out_dir
)
144 .define("LLVM_ENABLE_ASSERTIONS", assertions
)
145 .define("LLVM_TARGETS_TO_BUILD", llvm_targets
)
146 .define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets
)
147 .define("LLVM_INCLUDE_EXAMPLES", "OFF")
148 .define("LLVM_INCLUDE_TESTS", "OFF")
149 .define("LLVM_INCLUDE_DOCS", "OFF")
150 .define("LLVM_ENABLE_ZLIB", "OFF")
151 .define("WITH_POLLY", "OFF")
152 .define("LLVM_ENABLE_TERMINFO", "OFF")
153 .define("LLVM_ENABLE_LIBEDIT", "OFF")
154 .define("LLVM_PARALLEL_COMPILE_JOBS", build
.jobs().to_string())
155 .define("LLVM_TARGET_ARCH", target
.split('
-'
).next().unwrap())
156 .define("LLVM_DEFAULT_TARGET_TRIPLE", target
);
158 // By default, LLVM will automatically find OCaml and, if it finds it,
159 // install the LLVM bindings in LLVM_OCAML_INSTALL_PATH, which defaults
160 // to /usr/bin/ocaml.
161 // This causes problem for non-root builds of Rust. Side-step the issue
162 // by setting LLVM_OCAML_INSTALL_PATH to a relative path, so it installs
164 cfg
.define("LLVM_OCAML_INSTALL_PATH",
165 env
::var_os("LLVM_OCAML_INSTALL_PATH").unwrap_or_else(|| "usr/lib/ocaml".into()));
167 // This setting makes the LLVM tools link to the dynamic LLVM library,
168 // which saves both memory during parallel links and overall disk space
169 // for the tools. We don't distribute any of those tools, so this is
170 // just a local concern. However, it doesn't work well everywhere.
171 if target
.contains("linux-gnu") || target
.contains("apple-darwin") {
172 cfg
.define("LLVM_LINK_LLVM_DYLIB", "ON");
175 if target
.contains("msvc") {
176 cfg
.define("LLVM_USE_CRT_DEBUG", "MT");
177 cfg
.define("LLVM_USE_CRT_RELEASE", "MT");
178 cfg
.define("LLVM_USE_CRT_RELWITHDEBINFO", "MT");
179 cfg
.static_crt(true);
182 if target
.starts_with("i686") {
183 cfg
.define("LLVM_BUILD_32_BITS", "ON");
186 if let Some(num_linkers
) = build
.config
.llvm_link_jobs
{
188 cfg
.define("LLVM_PARALLEL_LINK_JOBS", num_linkers
.to_string());
192 // http://llvm.org/docs/HowToCrossCompileLLVM.html
193 if target
!= build
.build
&& !emscripten
{
194 builder
.ensure(Llvm
{
198 // FIXME: if the llvm root for the build triple is overridden then we
199 // should use llvm-tblgen from there, also should verify that it
200 // actually exists most of the time in normal installs of LLVM.
201 let host
= build
.llvm_out(build
.build
).join("bin/llvm-tblgen");
202 cfg
.define("CMAKE_CROSSCOMPILING", "True")
203 .define("LLVM_TABLEGEN", &host
);
205 if target
.contains("netbsd") {
206 cfg
.define("CMAKE_SYSTEM_NAME", "NetBSD");
207 } else if target
.contains("freebsd") {
208 cfg
.define("CMAKE_SYSTEM_NAME", "FreeBSD");
211 cfg
.define("LLVM_NATIVE_BUILD", build
.llvm_out(build
.build
).join("build"));
214 configure_cmake(build
, target
, &mut cfg
, false);
216 // FIXME: we don't actually need to build all LLVM tools and all LLVM
217 // libraries here, e.g. we just want a few components and a few
218 // tools. Figure out how to filter them down and only build the right
219 // tools and libs on all platforms.
222 t
!(t
!(File
::create(&done_stamp
)).write_all(rebuild_trigger_contents
.as_bytes()));
228 fn check_llvm_version(build
: &Build
, llvm_config
: &Path
) {
229 if !build
.config
.llvm_version_check
{
233 let mut cmd
= Command
::new(llvm_config
);
234 let version
= output(cmd
.arg("--version"));
235 let mut parts
= version
.split('
.'
).take(2)
236 .filter_map(|s
| s
.parse
::<u32>().ok());
237 if let (Some(major
), Some(minor
)) = (parts
.next(), parts
.next()) {
238 if major
> 3 || (major
== 3 && minor
>= 9) {
242 panic
!("\n\nbad LLVM version: {}, need >=3.9\n\n", version
)
245 fn configure_cmake(build
: &Build
,
246 target
: Interned
<String
>,
247 cfg
: &mut cmake
::Config
,
248 building_dist_binaries
: bool
) {
249 if build
.config
.ninja
{
250 cfg
.generator("Ninja");
253 .host(&build
.config
.build
);
255 let sanitize_cc
= |cc
: &Path
| {
256 if target
.contains("msvc") {
257 OsString
::from(cc
.to_str().unwrap().replace("\\", "/"))
259 cc
.as_os_str().to_owned()
263 // MSVC with CMake uses msbuild by default which doesn't respect these
264 // vars that we'd otherwise configure. In that case we just skip this
266 if target
.contains("msvc") && !build
.config
.ninja
{
270 let cc
= build
.cc(target
);
271 let cxx
= build
.cxx(target
).unwrap();
273 // Handle msvc + ninja + ccache specially (this is what the bots use)
274 if target
.contains("msvc") &&
275 build
.config
.ninja
&&
276 build
.config
.ccache
.is_some() {
277 let mut cc
= env
::current_exe().expect("failed to get cwd");
278 cc
.set_file_name("sccache-plus-cl.exe");
280 cfg
.define("CMAKE_C_COMPILER", sanitize_cc(&cc
))
281 .define("CMAKE_CXX_COMPILER", sanitize_cc(&cc
));
282 cfg
.env("SCCACHE_PATH",
283 build
.config
.ccache
.as_ref().unwrap())
284 .env("SCCACHE_TARGET", target
);
286 // If ccache is configured we inform the build a little differently hwo
287 // to invoke ccache while also invoking our compilers.
288 } else if let Some(ref ccache
) = build
.config
.ccache
{
289 cfg
.define("CMAKE_C_COMPILER", ccache
)
290 .define("CMAKE_C_COMPILER_ARG1", sanitize_cc(cc
))
291 .define("CMAKE_CXX_COMPILER", ccache
)
292 .define("CMAKE_CXX_COMPILER_ARG1", sanitize_cc(cxx
));
294 cfg
.define("CMAKE_C_COMPILER", sanitize_cc(cc
))
295 .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx
));
298 cfg
.build_arg("-j").build_arg(build
.jobs().to_string());
299 cfg
.define("CMAKE_C_FLAGS", build
.cflags(target
).join(" "));
300 let mut cxxflags
= build
.cflags(target
).join(" ");
301 if building_dist_binaries
{
302 if build
.config
.llvm_static_stdcpp
&& !target
.contains("windows") {
303 cxxflags
.push_str(" -static-libstdc++");
306 cfg
.define("CMAKE_CXX_FLAGS", cxxflags
);
307 if let Some(ar
) = build
.ar(target
) {
308 if ar
.is_absolute() {
309 // LLVM build breaks if `CMAKE_AR` is a relative path, for some reason it
310 // tries to resolve this path in the LLVM build directory.
311 cfg
.define("CMAKE_AR", sanitize_cc(ar
));
315 if env
::var_os("SCCACHE_ERROR_LOG").is_some() {
316 cfg
.env("RUST_LOG", "sccache=warn");
320 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
322 pub target
: Interned
<String
>,
326 type Output
= PathBuf
;
327 const ONLY_HOSTS
: bool
= true;
329 fn should_run(run
: ShouldRun
) -> ShouldRun
{
330 run
.path("src/tools/lld")
333 fn make_run(run
: RunConfig
) {
334 run
.builder
.ensure(Lld { target: run.target }
);
337 /// Compile LLVM for `target`.
338 fn run(self, builder
: &Builder
) -> PathBuf
{
339 let target
= self.target
;
340 let build
= builder
.build
;
342 let llvm_config
= builder
.ensure(Llvm
{
347 let out_dir
= build
.lld_out(target
);
348 let done_stamp
= out_dir
.join("lld-finished-building");
349 if done_stamp
.exists() {
353 let _folder
= build
.fold_output(|| "lld");
354 println
!("Building LLD for {}", target
);
355 let _time
= util
::timeit();
356 t
!(fs
::create_dir_all(&out_dir
));
358 let mut cfg
= cmake
::Config
::new(build
.src
.join("src/tools/lld"));
359 configure_cmake(build
, target
, &mut cfg
, true);
361 cfg
.out_dir(&out_dir
)
363 .define("LLVM_CONFIG_PATH", llvm_config
)
364 .define("LLVM_INCLUDE_TESTS", "OFF");
368 t
!(File
::create(&done_stamp
));
373 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
374 pub struct TestHelpers
{
375 pub target
: Interned
<String
>,
378 impl Step
for TestHelpers
{
381 fn should_run(run
: ShouldRun
) -> ShouldRun
{
382 run
.path("src/test/auxiliary/rust_test_helpers.c")
385 fn make_run(run
: RunConfig
) {
386 run
.builder
.ensure(TestHelpers { target: run.target }
)
389 /// Compiles the `rust_test_helpers.c` library which we used in various
390 /// `run-pass` test suites for ABI testing.
391 fn run(self, builder
: &Builder
) {
392 let build
= builder
.build
;
393 let target
= self.target
;
394 let dst
= build
.test_helpers_out(target
);
395 let src
= build
.src
.join("src/test/auxiliary/rust_test_helpers.c");
396 if up_to_date(&src
, &dst
.join("librust_test_helpers.a")) {
400 let _folder
= build
.fold_output(|| "build_test_helpers");
401 println
!("Building test helpers");
402 t
!(fs
::create_dir_all(&dst
));
403 let mut cfg
= cc
::Build
::new();
405 // We may have found various cross-compilers a little differently due to our
406 // extra configuration, so inform gcc of these compilers. Note, though, that
407 // on MSVC we still need gcc's detection of env vars (ugh).
408 if !target
.contains("msvc") {
409 if let Some(ar
) = build
.ar(target
) {
412 cfg
.compiler(build
.cc(target
));
415 cfg
.cargo_metadata(false)
422 .file(build
.src
.join("src/test/auxiliary/rust_test_helpers.c"))
423 .compile("rust_test_helpers");
427 const OPENSSL_VERS
: &'
static str = "1.0.2n";
428 const OPENSSL_SHA256
: &'
static str =
429 "370babb75f278c39e0c50e8c4e7493bc0f18db6867478341a832a982fd15a8fe";
431 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
433 pub target
: Interned
<String
>,
436 impl Step
for Openssl
{
439 fn should_run(run
: ShouldRun
) -> ShouldRun
{
443 fn run(self, builder
: &Builder
) {
444 let build
= builder
.build
;
445 let target
= self.target
;
446 let out
= match build
.openssl_dir(target
) {
451 let stamp
= out
.join(".stamp");
452 let mut contents
= String
::new();
453 drop(File
::open(&stamp
).and_then(|mut f
| f
.read_to_string(&mut contents
)));
454 if contents
== OPENSSL_VERS
{
457 t
!(fs
::create_dir_all(&out
));
459 let name
= format
!("openssl-{}.tar.gz", OPENSSL_VERS
);
460 let tarball
= out
.join(&name
);
461 if !tarball
.exists() {
462 let tmp
= tarball
.with_extension("tmp");
463 // originally from https://www.openssl.org/source/...
464 let url
= format
!("https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rust-ci-mirror/{}",
466 let mut last_error
= None
;
468 let status
= Command
::new("curl")
470 .arg("-f") // make curl fail if the URL does not return HTTP 200
473 .expect("failed to spawn curl");
475 // Retry if download failed.
476 if !status
.success() {
477 last_error
= Some(status
.to_string());
481 // Ensure the hash is correct.
482 let mut shasum
= if target
.contains("apple") || build
.build
.contains("netbsd") {
483 let mut cmd
= Command
::new("shasum");
484 cmd
.arg("-a").arg("256");
487 Command
::new("sha256sum")
489 let output
= output(&mut shasum
.arg(&tmp
));
490 let found
= output
.split_whitespace().next().unwrap();
492 // If the hash is wrong, probably the download is incomplete or S3 served an error
493 // page. In any case, retry.
494 if found
!= OPENSSL_SHA256
{
495 last_error
= Some(format
!(
496 "downloaded openssl sha256 different\n\
505 // Everything is fine, so exit the retry loop.
509 if let Some(error
) = last_error
{
510 panic
!("failed to download openssl source: {}", error
);
512 t
!(fs
::rename(&tmp
, &tarball
));
514 let obj
= out
.join(format
!("openssl-{}", OPENSSL_VERS
));
515 let dst
= build
.openssl_install_dir(target
).unwrap();
516 drop(fs
::remove_dir_all(&obj
));
517 drop(fs
::remove_dir_all(&dst
));
518 build
.run(Command
::new("tar").arg("zxf").arg(&tarball
).current_dir(&out
));
520 let mut configure
= Command
::new("perl");
521 configure
.arg(obj
.join("Configure"));
522 configure
.arg(format
!("--prefix={}", dst
.display()));
523 configure
.arg("no-dso");
524 configure
.arg("no-ssl2");
525 configure
.arg("no-ssl3");
527 let os
= match &*target
{
528 "aarch64-linux-android" => "linux-aarch64",
529 "aarch64-unknown-linux-gnu" => "linux-aarch64",
530 "aarch64-unknown-linux-musl" => "linux-aarch64",
531 "arm-linux-androideabi" => "android",
532 "arm-unknown-linux-gnueabi" => "linux-armv4",
533 "arm-unknown-linux-gnueabihf" => "linux-armv4",
534 "armv7-linux-androideabi" => "android-armv7",
535 "armv7-unknown-linux-gnueabihf" => "linux-armv4",
536 "i586-unknown-linux-gnu" => "linux-elf",
537 "i586-unknown-linux-musl" => "linux-elf",
538 "i686-apple-darwin" => "darwin-i386-cc",
539 "i686-linux-android" => "android-x86",
540 "i686-unknown-freebsd" => "BSD-x86-elf",
541 "i686-unknown-linux-gnu" => "linux-elf",
542 "i686-unknown-linux-musl" => "linux-elf",
543 "i686-unknown-netbsd" => "BSD-x86-elf",
544 "mips-unknown-linux-gnu" => "linux-mips32",
545 "mips64-unknown-linux-gnuabi64" => "linux64-mips64",
546 "mips64el-unknown-linux-gnuabi64" => "linux64-mips64",
547 "mipsel-unknown-linux-gnu" => "linux-mips32",
548 "powerpc-unknown-linux-gnu" => "linux-ppc",
549 "powerpc-unknown-linux-gnuspe" => "linux-ppc",
550 "powerpc-unknown-netbsd" => "BSD-generic32",
551 "powerpc64-unknown-linux-gnu" => "linux-ppc64",
552 "powerpc64le-unknown-linux-gnu" => "linux-ppc64le",
553 "s390x-unknown-linux-gnu" => "linux64-s390x",
554 "sparc-unknown-linux-gnu" => "linux-sparcv9",
555 "sparc64-unknown-linux-gnu" => "linux64-sparcv9",
556 "sparc64-unknown-netbsd" => "BSD-sparc64",
557 "x86_64-apple-darwin" => "darwin64-x86_64-cc",
558 "x86_64-linux-android" => "linux-x86_64",
559 "x86_64-unknown-freebsd" => "BSD-x86_64",
560 "x86_64-unknown-dragonfly" => "BSD-x86_64",
561 "x86_64-unknown-linux-gnu" => "linux-x86_64",
562 "x86_64-unknown-linux-gnux32" => "linux-x32",
563 "x86_64-unknown-linux-musl" => "linux-x86_64",
564 "x86_64-unknown-netbsd" => "BSD-x86_64",
565 _
=> panic
!("don't know how to configure OpenSSL for {}", target
),
568 configure
.env("CC", build
.cc(target
));
569 for flag
in build
.cflags(target
) {
572 // There is no specific os target for android aarch64 or x86_64,
573 // so we need to pass some extra cflags
574 if target
== "aarch64-linux-android" || target
== "x86_64-linux-android" {
575 configure
.arg("-mandroid");
576 configure
.arg("-fomit-frame-pointer");
578 if target
== "sparc64-unknown-netbsd" {
579 // Need -m64 to get assembly generated correctly for sparc64.
580 configure
.arg("-m64");
581 if build
.build
.contains("netbsd") {
582 // Disable sparc64 asm on NetBSD builders, it uses
583 // m4(1)'s -B flag, which NetBSD m4 does not support.
584 configure
.arg("no-asm");
588 // Non-PIE linker support was removed in Lollipop
589 // https://source.android.com/security/enhancements/enhancements50
590 if target
== "i686-linux-android" {
591 configure
.arg("no-asm");
593 configure
.current_dir(&obj
);
594 println
!("Configuring openssl for {}", target
);
595 build
.run_quiet(&mut configure
);
596 println
!("Building openssl for {}", target
);
597 build
.run_quiet(Command
::new("make").arg("-j1").current_dir(&obj
));
598 println
!("Installing openssl for {}", target
);
599 build
.run_quiet(Command
::new("make").arg("install").arg("-j1").current_dir(&obj
));
601 let mut f
= t
!(File
::create(&stamp
));
602 t
!(f
.write_all(OPENSSL_VERS
.as_bytes()));