]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/build.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / librustc_llvm / build.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 extern crate gcc;
12 extern crate build_helper;
13
14 use std::process::Command;
15 use std::env;
16 use std::path::{PathBuf, Path};
17
18 use build_helper::output;
19
20 fn main() {
21     println!("cargo:rustc-cfg=cargobuild");
22
23     let target = env::var("TARGET").unwrap();
24     let llvm_config = env::var_os("LLVM_CONFIG")
25                           .map(PathBuf::from)
26                           .unwrap_or_else(|| {
27                               if let Some(dir) = env::var_os("CARGO_TARGET_DIR")
28                                       .map(PathBuf::from) {
29                                   let to_test = dir.parent()
30                                                    .unwrap()
31                                                    .parent()
32                                                    .unwrap()
33                                                    .join(&target)
34                                                    .join("llvm/bin/llvm-config");
35                                   if Command::new(&to_test).output().is_ok() {
36                                       return to_test;
37                                   }
38                               }
39                               PathBuf::from("llvm-config")
40                           });
41
42     println!("cargo:rerun-if-changed={}", llvm_config.display());
43
44     // Test whether we're cross-compiling LLVM. This is a pretty rare case
45     // currently where we're producing an LLVM for a different platform than
46     // what this build script is currently running on.
47     //
48     // In that case, there's no guarantee that we can actually run the target,
49     // so the build system works around this by giving us the LLVM_CONFIG for
50     // the host platform. This only really works if the host LLVM and target
51     // LLVM are compiled the same way, but for us that's typically the case.
52     //
53     // We *want* detect this cross compiling situation by asking llvm-config
54     // what it's host-target is. If that's not the TARGET, then we're cross
55     // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
56     // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
57     // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
58     // tricks us into thinking we're doing a cross build when we aren't, so
59     // havoc ensues.
60     //
61     // In any case, if we're cross compiling, this generally just means that we
62     // can't trust all the output of llvm-config becaues it might be targeted
63     // for the host rather than the target. As a result a bunch of blocks below
64     // are gated on `if !is_crossed`
65     let target = env::var("TARGET").unwrap();
66     let host = env::var("HOST").unwrap();
67     let is_crossed = target != host;
68
69     let optional_components = ["x86", "arm", "aarch64", "mips", "powerpc", "pnacl", "systemz"];
70
71     // FIXME: surely we don't need all these components, right? Stuff like mcjit
72     //        or interpreter the compiler itself never uses.
73     let required_components = &["ipo",
74                                 "bitreader",
75                                 "bitwriter",
76                                 "linker",
77                                 "asmparser",
78                                 "mcjit",
79                                 "interpreter",
80                                 "instrumentation"];
81
82     let components = output(Command::new(&llvm_config).arg("--components"));
83     let mut components = components.split_whitespace().collect::<Vec<_>>();
84     components.retain(|c| optional_components.contains(c) || required_components.contains(c));
85
86     for component in required_components {
87         if !components.contains(component) {
88             panic!("require llvm component {} but wasn't found", component);
89         }
90     }
91
92     for component in components.iter() {
93         println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
94     }
95
96     // Link in our own LLVM shims, compiled with the same flags as LLVM
97     let mut cmd = Command::new(&llvm_config);
98     cmd.arg("--cxxflags");
99     let cxxflags = output(&mut cmd);
100     let mut cfg = gcc::Config::new();
101     for flag in cxxflags.split_whitespace() {
102         // Ignore flags like `-m64` when we're doing a cross build
103         if is_crossed && flag.starts_with("-m") {
104             continue;
105         }
106         cfg.flag(flag);
107     }
108
109     for component in &components[..] {
110         let mut flag = String::from("-DLLVM_COMPONENT_");
111         flag.push_str(&component.to_uppercase());
112         cfg.flag(&flag);
113     }
114
115     if env::var_os("LLVM_RUSTLLVM").is_some() {
116         cfg.flag("-DLLVM_RUSTLLVM");
117     }
118
119     cfg.file("../rustllvm/PassWrapper.cpp")
120        .file("../rustllvm/RustWrapper.cpp")
121        .file("../rustllvm/ArchiveWrapper.cpp")
122        .cpp(true)
123        .cpp_link_stdlib(None) // we handle this below
124        .compile("librustllvm.a");
125
126     // Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
127     // we don't pick up system libs because unfortunately they're for the host
128     // of llvm-config, not the target that we're attempting to link.
129     let mut cmd = Command::new(&llvm_config);
130     cmd.arg("--libs");
131     if !is_crossed {
132         cmd.arg("--system-libs");
133     }
134     cmd.args(&components[..]);
135
136     for lib in output(&mut cmd).split_whitespace() {
137         let name = if lib.starts_with("-l") {
138             &lib[2..]
139         } else if lib.starts_with("-") {
140             &lib[1..]
141         } else if Path::new(lib).exists() {
142             // On MSVC llvm-config will print the full name to libraries, but
143             // we're only interested in the name part
144             let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
145             name.trim_right_matches(".lib")
146         } else if lib.ends_with(".lib") {
147             // Some MSVC libraries just come up with `.lib` tacked on, so chop
148             // that off
149             lib.trim_right_matches(".lib")
150         } else {
151             continue
152         };
153
154         // Don't need or want this library, but LLVM's CMake build system
155         // doesn't provide a way to disable it, so filter it here even though we
156         // may or may not have built it. We don't reference anything from this
157         // library and it otherwise may just pull in extra dependencies on
158         // libedit which we don't want
159         if name == "LLVMLineEditor" {
160             continue
161         }
162
163         let kind = if name.starts_with("LLVM") {
164             "static"
165         } else {
166             "dylib"
167         };
168         println!("cargo:rustc-link-lib={}={}", kind, name);
169     }
170
171     // LLVM ldflags
172     //
173     // If we're a cross-compile of LLVM then unfortunately we can't trust these
174     // ldflags (largely where all the LLVM libs are located). Currently just
175     // hack around this by replacing the host triple with the target and pray
176     // that those -L directories are the same!
177     let mut cmd = Command::new(&llvm_config);
178     cmd.arg("--ldflags");
179     for lib in output(&mut cmd).split_whitespace() {
180         if lib.starts_with("-LIBPATH:") {
181                 println!("cargo:rustc-link-search=native={}", &lib[9..]);
182         } else if is_crossed {
183             if lib.starts_with("-L") {
184                 println!("cargo:rustc-link-search=native={}",
185                          lib[2..].replace(&host, &target));
186             }
187         } else if lib.starts_with("-l") {
188             println!("cargo:rustc-link-lib={}", &lib[2..]);
189         } else if lib.starts_with("-L") {
190             println!("cargo:rustc-link-search=native={}", &lib[2..]);
191         }
192     }
193
194     // C++ runtime library
195     if !target.contains("msvc") {
196         if let Some(s) = env::var_os("LLVM_STATIC_STDCPP") {
197             assert!(!cxxflags.contains("stdlib=libc++"));
198             let path = PathBuf::from(s);
199             println!("cargo:rustc-link-search=native={}",
200                      path.parent().unwrap().display());
201             println!("cargo:rustc-link-lib=static=stdc++");
202         } else if cxxflags.contains("stdlib=libc++") {
203             println!("cargo:rustc-link-lib=c++");
204         } else {
205             println!("cargo:rustc-link-lib=stdc++");
206         }
207     }
208 }