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
;
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
);
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
));
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
));
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
{
62 if build
.config
.llvm_clean_rebuild
{
63 drop(fs
::remove_dir_all(&out_dir
));
66 let _folder
= build
.fold_output(|| "llvm");
67 println
!("Building LLVM for {}", target
);
68 let _time
= util
::timeit();
69 t
!(fs
::create_dir_all(&out_dir
));
71 // http://llvm.org/docs/CMake.html
72 let mut cfg
= cmake
::Config
::new(build
.src
.join("src/llvm"));
73 if build
.config
.ninja
{
74 cfg
.generator("Ninja");
77 let profile
= match (build
.config
.llvm_optimize
, build
.config
.llvm_release_debuginfo
) {
78 (false, _
) => "Debug",
79 (true, false) => "Release",
80 (true, true) => "RelWithDebInfo",
83 // NOTE: remember to also update `config.toml.example` when changing the defaults!
84 let llvm_targets
= match build
.config
.llvm_targets
{
86 None
=> "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX;Hexagon",
89 let llvm_exp_targets
= match build
.config
.llvm_experimental_targets
{
94 let assertions
= if build
.config
.llvm_assertions {"ON"}
else {"OFF"}
;
100 .define("LLVM_ENABLE_ASSERTIONS", assertions
)
101 .define("LLVM_TARGETS_TO_BUILD", llvm_targets
)
102 .define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets
)
103 .define("LLVM_INCLUDE_EXAMPLES", "OFF")
104 .define("LLVM_INCLUDE_TESTS", "OFF")
105 .define("LLVM_INCLUDE_DOCS", "OFF")
106 .define("LLVM_ENABLE_ZLIB", "OFF")
107 .define("WITH_POLLY", "OFF")
108 .define("LLVM_ENABLE_TERMINFO", "OFF")
109 .define("LLVM_ENABLE_LIBEDIT", "OFF")
110 .define("LLVM_PARALLEL_COMPILE_JOBS", build
.jobs().to_string())
111 .define("LLVM_TARGET_ARCH", target
.split('
-'
).next().unwrap())
112 .define("LLVM_DEFAULT_TARGET_TRIPLE", target
);
114 if target
.contains("msvc") {
115 cfg
.define("LLVM_USE_CRT_DEBUG", "MT");
116 cfg
.define("LLVM_USE_CRT_RELEASE", "MT");
117 cfg
.define("LLVM_USE_CRT_RELWITHDEBINFO", "MT");
118 cfg
.static_crt(true);
121 if target
.starts_with("i686") {
122 cfg
.define("LLVM_BUILD_32_BITS", "ON");
125 if let Some(num_linkers
) = build
.config
.llvm_link_jobs
{
127 cfg
.define("LLVM_PARALLEL_LINK_JOBS", num_linkers
.to_string());
131 // http://llvm.org/docs/HowToCrossCompileLLVM.html
132 if target
!= build
.build
{
133 // FIXME: if the llvm root for the build triple is overridden then we
134 // should use llvm-tblgen from there, also should verify that it
135 // actually exists most of the time in normal installs of LLVM.
136 let host
= build
.llvm_out(&build
.build
).join("bin/llvm-tblgen");
137 cfg
.define("CMAKE_CROSSCOMPILING", "True")
138 .define("LLVM_TABLEGEN", &host
);
141 let sanitize_cc
= |cc
: &Path
| {
142 if target
.contains("msvc") {
143 OsString
::from(cc
.to_str().unwrap().replace("\\", "/"))
145 cc
.as_os_str().to_owned()
149 let configure_compilers
= |cfg
: &mut cmake
::Config
| {
150 // MSVC with CMake uses msbuild by default which doesn't respect these
151 // vars that we'd otherwise configure. In that case we just skip this
153 if target
.contains("msvc") && !build
.config
.ninja
{
157 let cc
= build
.cc(target
);
158 let cxx
= build
.cxx(target
).unwrap();
160 // Handle msvc + ninja + ccache specially (this is what the bots use)
161 if target
.contains("msvc") &&
162 build
.config
.ninja
&&
163 build
.config
.ccache
.is_some() {
164 let mut cc
= env
::current_exe().expect("failed to get cwd");
165 cc
.set_file_name("sccache-plus-cl.exe");
167 cfg
.define("CMAKE_C_COMPILER", sanitize_cc(&cc
))
168 .define("CMAKE_CXX_COMPILER", sanitize_cc(&cc
));
169 cfg
.env("SCCACHE_PATH",
170 build
.config
.ccache
.as_ref().unwrap())
171 .env("SCCACHE_TARGET", target
);
173 // If ccache is configured we inform the build a little differently hwo
174 // to invoke ccache while also invoking our compilers.
175 } else if let Some(ref ccache
) = build
.config
.ccache
{
176 cfg
.define("CMAKE_C_COMPILER", ccache
)
177 .define("CMAKE_C_COMPILER_ARG1", sanitize_cc(cc
))
178 .define("CMAKE_CXX_COMPILER", ccache
)
179 .define("CMAKE_CXX_COMPILER_ARG1", sanitize_cc(cxx
));
181 cfg
.define("CMAKE_C_COMPILER", sanitize_cc(cc
))
182 .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx
));
185 cfg
.build_arg("-j").build_arg(build
.jobs().to_string());
186 cfg
.define("CMAKE_C_FLAGS", build
.cflags(target
).join(" "));
187 cfg
.define("CMAKE_CXX_FLAGS", build
.cflags(target
).join(" "));
190 configure_compilers(&mut cfg
);
192 if env
::var_os("SCCACHE_ERROR_LOG").is_some() {
193 cfg
.env("RUST_LOG", "sccache=warn");
196 // FIXME: we don't actually need to build all LLVM tools and all LLVM
197 // libraries here, e.g. we just want a few components and a few
198 // tools. Figure out how to filter them down and only build the right
199 // tools and libs on all platforms.
202 t
!(t
!(File
::create(&done_stamp
)).write_all(rebuild_trigger_contents
.as_bytes()));
205 fn check_llvm_version(build
: &Build
, llvm_config
: &Path
) {
206 if !build
.config
.llvm_version_check
{
210 let mut cmd
= Command
::new(llvm_config
);
211 let version
= output(cmd
.arg("--version"));
212 if version
.starts_with("3.5") || version
.starts_with("3.6") ||
213 version
.starts_with("3.7") {
216 panic
!("\n\nbad LLVM version: {}, need >=3.5\n\n", version
)
219 /// Compiles the `rust_test_helpers.c` library which we used in various
220 /// `run-pass` test suites for ABI testing.
221 pub fn test_helpers(build
: &Build
, target
: &str) {
222 let dst
= build
.test_helpers_out(target
);
223 let src
= build
.src
.join("src/rt/rust_test_helpers.c");
224 if up_to_date(&src
, &dst
.join("librust_test_helpers.a")) {
228 let _folder
= build
.fold_output(|| "build_test_helpers");
229 println
!("Building test helpers");
230 t
!(fs
::create_dir_all(&dst
));
231 let mut cfg
= gcc
::Config
::new();
233 // We may have found various cross-compilers a little differently due to our
234 // extra configuration, so inform gcc of these compilers. Note, though, that
235 // on MSVC we still need gcc's detection of env vars (ugh).
236 if !target
.contains("msvc") {
237 if let Some(ar
) = build
.ar(target
) {
240 cfg
.compiler(build
.cc(target
));
243 cfg
.cargo_metadata(false)
249 .file(build
.src
.join("src/rt/rust_test_helpers.c"))
250 .compile("librust_test_helpers.a");
252 const OPENSSL_VERS
: &'
static str = "1.0.2k";
253 const OPENSSL_SHA256
: &'
static str =
254 "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0";
256 pub fn openssl(build
: &Build
, target
: &str) {
257 let out
= match build
.openssl_dir(target
) {
262 let stamp
= out
.join(".stamp");
263 let mut contents
= String
::new();
264 drop(File
::open(&stamp
).and_then(|mut f
| f
.read_to_string(&mut contents
)));
265 if contents
== OPENSSL_VERS
{
268 t
!(fs
::create_dir_all(&out
));
270 let name
= format
!("openssl-{}.tar.gz", OPENSSL_VERS
);
271 let tarball
= out
.join(&name
);
272 if !tarball
.exists() {
273 let tmp
= tarball
.with_extension("tmp");
274 // originally from https://www.openssl.org/source/...
275 let url
= format
!("https://s3.amazonaws.com/rust-lang-ci/rust-ci-mirror/{}",
279 let status
= Command
::new("curl")
283 .expect("failed to spawn curl");
284 if status
.success() {
290 panic
!("failed to download openssl source")
292 let mut shasum
= if target
.contains("apple") {
293 let mut cmd
= Command
::new("shasum");
294 cmd
.arg("-a").arg("256");
297 Command
::new("sha256sum")
299 let output
= output(&mut shasum
.arg(&tmp
));
300 let found
= output
.split_whitespace().next().unwrap();
301 if found
!= OPENSSL_SHA256
{
302 panic
!("downloaded openssl sha256 different\n\
304 found: {}\n", OPENSSL_SHA256
, found
);
306 t
!(fs
::rename(&tmp
, &tarball
));
308 let obj
= out
.join(format
!("openssl-{}", OPENSSL_VERS
));
309 let dst
= build
.openssl_install_dir(target
).unwrap();
310 drop(fs
::remove_dir_all(&obj
));
311 drop(fs
::remove_dir_all(&dst
));
312 build
.run(Command
::new("tar").arg("xf").arg(&tarball
).current_dir(&out
));
314 let mut configure
= Command
::new(obj
.join("Configure"));
315 configure
.arg(format
!("--prefix={}", dst
.display()));
316 configure
.arg("no-dso");
317 configure
.arg("no-ssl2");
318 configure
.arg("no-ssl3");
320 let os
= match target
{
321 "aarch64-linux-android" => "linux-aarch64",
322 "aarch64-unknown-linux-gnu" => "linux-aarch64",
323 "arm-linux-androideabi" => "android",
324 "arm-unknown-linux-gnueabi" => "linux-armv4",
325 "arm-unknown-linux-gnueabihf" => "linux-armv4",
326 "armv7-linux-androideabi" => "android-armv7",
327 "armv7-unknown-linux-gnueabihf" => "linux-armv4",
328 "i686-apple-darwin" => "darwin-i386-cc",
329 "i686-linux-android" => "android-x86",
330 "i686-unknown-freebsd" => "BSD-x86-elf",
331 "i686-unknown-linux-gnu" => "linux-elf",
332 "i686-unknown-linux-musl" => "linux-elf",
333 "mips-unknown-linux-gnu" => "linux-mips32",
334 "mips64-unknown-linux-gnuabi64" => "linux64-mips64",
335 "mips64el-unknown-linux-gnuabi64" => "linux64-mips64",
336 "mipsel-unknown-linux-gnu" => "linux-mips32",
337 "powerpc-unknown-linux-gnu" => "linux-ppc",
338 "powerpc64-unknown-linux-gnu" => "linux-ppc64",
339 "powerpc64le-unknown-linux-gnu" => "linux-ppc64le",
340 "s390x-unknown-linux-gnu" => "linux64-s390x",
341 "x86_64-apple-darwin" => "darwin64-x86_64-cc",
342 "x86_64-linux-android" => "linux-x86_64",
343 "x86_64-unknown-freebsd" => "BSD-x86_64",
344 "x86_64-unknown-linux-gnu" => "linux-x86_64",
345 "x86_64-unknown-linux-musl" => "linux-x86_64",
346 "x86_64-unknown-netbsd" => "BSD-x86_64",
347 _
=> panic
!("don't know how to configure OpenSSL for {}", target
),
350 configure
.env("CC", build
.cc(target
));
351 for flag
in build
.cflags(target
) {
354 // There is no specific os target for android aarch64 or x86_64,
355 // so we need to pass some extra cflags
356 if target
== "aarch64-linux-android" || target
== "x86_64-linux-android" {
357 configure
.arg("-mandroid");
358 configure
.arg("-fomit-frame-pointer");
361 // Non-PIE linker support was removed in Lollipop
362 // https://source.android.com/security/enhancements/enhancements50
363 if target
== "i686-linux-android" {
364 configure
.arg("no-asm");
366 configure
.current_dir(&obj
);
367 println
!("Configuring openssl for {}", target
);
368 build
.run_quiet(&mut configure
);
369 println
!("Building openssl for {}", target
);
370 build
.run_quiet(Command
::new("make").arg("-j1").current_dir(&obj
));
371 println
!("Installing openssl for {}", target
);
372 build
.run_quiet(Command
::new("make").arg("install").current_dir(&obj
));
374 let mut f
= t
!(File
::create(&stamp
));
375 t
!(f
.write_all(OPENSSL_VERS
.as_bytes()));