|
| 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; |
| 17 | + |
| 18 | +use build_helper::output; |
| 19 | + |
| 20 | +fn main() { |
| 21 | + let target = env::var("TARGET").unwrap(); |
| 22 | + let llvm_config = env::var_os("LLVM_CONFIG").map(PathBuf::from) |
| 23 | + .unwrap_or_else(|| { |
| 24 | + match env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) { |
| 25 | + Some(dir) => { |
| 26 | + let to_test = dir.parent().unwrap().parent().unwrap() |
| 27 | + .join(&target).join("llvm/bin/llvm-config"); |
| 28 | + if Command::new(&to_test).output().is_ok() { |
| 29 | + return to_test |
| 30 | + } |
| 31 | + } |
| 32 | + None => {} |
| 33 | + } |
| 34 | + PathBuf::from("llvm-config") |
| 35 | + }); |
| 36 | + |
| 37 | + println!("cargo:rerun-if-changed={}", llvm_config.display()); |
| 38 | + |
| 39 | + let optional_components = ["x86", "arm", "aarch64", "mips", "powerpc", |
| 40 | + "pnacl"]; |
| 41 | + |
| 42 | + // FIXME: surely we don't need all these components, right? Stuff like mcjit |
| 43 | + // or interpreter the compiler itself never uses. |
| 44 | + let required_components = &["ipo", "bitreader", "bitwriter", "linker", |
| 45 | + "asmparser", "mcjit", "interpreter", |
| 46 | + "instrumentation"]; |
| 47 | + |
| 48 | + let components = output(Command::new(&llvm_config).arg("--components")); |
| 49 | + let mut components = components.split_whitespace().collect::<Vec<_>>(); |
| 50 | + components.retain(|c| { |
| 51 | + optional_components.contains(c) || required_components.contains(c) |
| 52 | + }); |
| 53 | + |
| 54 | + for component in required_components { |
| 55 | + if !components.contains(component) { |
| 56 | + panic!("require llvm component {} but wasn't found", component); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + for component in components.iter() { |
| 61 | + println!("cargo:rustc-cfg=llvm_component=\"{}\"", component); |
| 62 | + } |
| 63 | + |
| 64 | + // Link in our own LLVM shims, compiled with the same flags as LLVM |
| 65 | + let mut cmd = Command::new(&llvm_config); |
| 66 | + cmd.arg("--cxxflags"); |
| 67 | + let cxxflags = output(&mut cmd); |
| 68 | + let mut cfg = gcc::Config::new(); |
| 69 | + for flag in cxxflags.split_whitespace() { |
| 70 | + cfg.flag(flag); |
| 71 | + } |
| 72 | + cfg.file("../rustllvm/ExecutionEngineWrapper.cpp") |
| 73 | + .file("../rustllvm/PassWrapper.cpp") |
| 74 | + .file("../rustllvm/RustWrapper.cpp") |
| 75 | + .file("../rustllvm/ArchiveWrapper.cpp") |
| 76 | + .cpp(true) |
| 77 | + .cpp_link_stdlib(None) // we handle this below |
| 78 | + .compile("librustllvm.a"); |
| 79 | + |
| 80 | + // Link in all LLVM libraries |
| 81 | + let mut cmd = Command::new(&llvm_config); |
| 82 | + cmd.arg("--libs").arg("--system-libs").args(&components[..]); |
| 83 | + for lib in output(&mut cmd).split_whitespace() { |
| 84 | + let name = if lib.starts_with("-l") { |
| 85 | + &lib[2..] |
| 86 | + } else if lib.starts_with("-") { |
| 87 | + &lib[1..] |
| 88 | + } else { |
| 89 | + continue |
| 90 | + }; |
| 91 | + |
| 92 | + // Don't need or want this library, but LLVM's CMake build system |
| 93 | + // doesn't provide a way to disable it, so filter it here even though we |
| 94 | + // may or may not have built it. We don't reference anything from this |
| 95 | + // library and it otherwise may just pull in extra dependencies on |
| 96 | + // libedit which we don't want |
| 97 | + if name == "LLVMLineEditor" { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + let kind = if name.starts_with("LLVM") {"static"} else {"dylib"}; |
| 102 | + println!("cargo:rustc-link-lib={}={}", kind, name); |
| 103 | + } |
| 104 | + |
| 105 | + // LLVM ldflags |
| 106 | + let mut cmd = Command::new(&llvm_config); |
| 107 | + cmd.arg("--ldflags"); |
| 108 | + for lib in output(&mut cmd).split_whitespace() { |
| 109 | + if lib.starts_with("-l") { |
| 110 | + println!("cargo:rustc-link-lib={}", &lib[2..]); |
| 111 | + } else if lib.starts_with("-L") { |
| 112 | + println!("cargo:rustc-link-search=native={}", &lib[2..]); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // C++ runtime library |
| 117 | + if !target.contains("msvc") { |
| 118 | + if let Some(s) = env::var_os("LLVM_STATIC_STDCPP") { |
| 119 | + assert!(!cxxflags.contains("stdlib=libc++")); |
| 120 | + let path = PathBuf::from(s); |
| 121 | + println!("cargo:rustc-link-search=native={}", |
| 122 | + path.parent().unwrap().display()); |
| 123 | + println!("cargo:rustc-link-lib=static=stdc++"); |
| 124 | + } else if cxxflags.contains("stdlib=libc++") { |
| 125 | + println!("cargo:rustc-link-lib=c++"); |
| 126 | + } else { |
| 127 | + println!("cargo:rustc-link-lib=stdc++"); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments