-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
221 lines (186 loc) · 7.15 KB
/
build.rs
File metadata and controls
221 lines (186 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::env;
use std::ffi::OsStr;
use std::io::{self};
use std::path::{Path, PathBuf};
use std::process::Command;
lazy_static::lazy_static! {
static ref LLVM_CONFIG_PATH: PathBuf = {
let abort_with_message = |msg: &str| -> ! {
panic!("{}", msg);
};
let sys_user_home: PathBuf = if cfg!(target_os = "linux") {
env::var("HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| abort_with_message("Could not determine user's HOME directory."))
} else if cfg!(target_os = "windows") {
env::var("APPDATA")
.map(PathBuf::from)
.unwrap_or_else(|_| abort_with_message("Could not determine user's APPDATA directory."))
} else {
abort_with_message("Unsupported OS for fetching the LLVM config binary. This build script only supports Linux and Windows.");
};
let llvm_build_path: PathBuf = sys_user_home.join(".thrushlang/backends/llvm/build");
if !llvm_build_path.exists() {
abort_with_message(&format!(
"No LLVM build folder found at '{}'; ensure the compiler installer was used first.",
llvm_build_path.display()
));
}
let llvm_config_extension: &str = if cfg!(target_os = "windows") { ".exe" } else { "" };
let llvm_config_path: PathBuf = llvm_build_path.join(format!("bin/llvm-config{}", llvm_config_extension));
if !llvm_config_path.exists() {
abort_with_message(&format!(
"LLVM config binary not found at '{}'. Please check your LLVM installation.",
llvm_config_path.display()
));
}
llvm_config_path
};
}
#[inline]
fn llvm_config(arg: &str) -> String {
llvm_config_ex(&*LLVM_CONFIG_PATH, arg).expect("Surprising failure from llvm-config.")
}
fn llvm_config_ex<S: AsRef<OsStr>>(binary: S, arg: &str) -> io::Result<String> {
Command::new(binary)
.arg(arg)
.arg("--link-static")
.arg("core")
.output()
.and_then(|output| {
if output.stdout.is_empty() {
Err(io::Error::new(
io::ErrorKind::NotFound,
"llvm-config returned empty output",
))
} else {
Ok(String::from_utf8(output.stdout)
.expect("Output from llvm-config was not valid UTF-8."))
}
})
}
fn get_system_libraries() -> Vec<String> {
self::llvm_config("--system-libs")
.split_whitespace()
.filter(|s| !s.is_empty())
.filter(|s| !s.starts_with('/'))
.map(|flag| {
if self::target_env_is("msvc") {
flag.strip_suffix(".lib")
.expect("MSVC system library does not end with '.lib'.")
.to_owned()
} else {
flag.strip_prefix("-l")
.unwrap_or_else(|| {
let maybe_lib_path: &Path = Path::new(&flag);
if maybe_lib_path.is_file() {
println!(
"cargo:rustc-link-search=native={}",
maybe_lib_path.parent().unwrap_or_else(|| Path::new("")).display()
);
let soname: &str = maybe_lib_path
.file_name()
.expect("Shared library path has no file name.")
.to_str()
.expect("Shared library path is not valid UTF-8.");
let stem = soname
.rsplit_once(self::get_target_dylib_extension())
.expect("Shared library should have a recognized extension (.so or .dylib).")
.0;
stem.trim_start_matches("lib")
} else {
panic!(
"Unable to parse result of llvm-config --system-libs: unexpected flag '{}'.",
flag
);
}
}).to_owned()
}
})
.chain(self::get_system_libcpp().map(str::to_owned))
.collect::<Vec<String>>()
}
fn get_link_libraries() -> Vec<String> {
self::llvm_config("--libnames")
.split_whitespace()
.filter(|s| !s.is_empty())
.map(|name| {
if self::target_env_is("msvc") {
name.strip_suffix(".lib")
.expect("library name does not appear to be a MSVC library file.")
.to_owned()
} else {
name.strip_prefix("lib")
.and_then(|s| s.strip_suffix(".a"))
.expect("library name does not appear to be a static library (libNAME.a).")
.to_owned()
}
})
.collect::<Vec<String>>()
}
fn get_llvm_cxxflags() -> String {
let output: String = llvm_config("--cxxflags");
if self::target_env_is("msvc") {
return output;
}
self::llvm_config("--cxxflags")
.split(&[' ', '\n'][..])
.filter(|word| !word.starts_with("-W"))
.collect::<Vec<_>>()
.join(" ")
}
#[inline]
fn get_target_dylib_extension() -> &'static str {
".so"
}
#[inline]
fn get_system_libcpp() -> Option<&'static str> {
if self::target_env_is("msvc") {
return None;
}
Some("stdc++")
}
#[inline]
fn target_env_is(name: &str) -> bool {
env::var_os("CARGO_CFG_TARGET_ENV").is_some_and(|s| s == name)
}
#[inline]
fn is_llvm_debug() -> bool {
self::llvm_config("--build-mode").contains("Debug")
}
fn main() {
unsafe { std::env::set_var("CXXFLAGS", get_llvm_cxxflags()) };
let mut build: cc::Build = cc::Build::new();
build.cpp(true).file("wrapper/lld.cpp");
if build.get_compiler().is_like_msvc() {
build.flag("/std:c++17");
} else {
build.flag("-std=c++17");
}
build.compile("lldwrapper");
println!("cargo:rerun-if-changed=wrapper/lld.cpp");
let libdir: String = self::llvm_config("--libdir");
println!("cargo:config_path={}", LLVM_CONFIG_PATH.display());
println!("cargo:libdir={}", libdir);
println!("cargo:rustc-link-search=native={}", libdir);
self::get_link_libraries()
.iter()
.filter(|lib| !lib.contains("LLVMLineEditor"))
.for_each(|libname| {
println!("cargo:rustc-link-lib=static={}", libname);
});
self::get_system_libraries().iter().for_each(|libname| {
println!("cargo:rustc-link-lib=dylib={}", libname);
});
if cfg!(target_env = "msvc") && self::is_llvm_debug() {
println!("cargo:rustc-link-lib=msvcrtd");
}
println!("cargo:rustc-link-lib=static=lldCOFF");
println!("cargo:rustc-link-lib=static=lldCommon");
println!("cargo:rustc-link-lib=static=lldELF");
println!("cargo:rustc-link-lib=static=lldMachO");
println!("cargo:rustc-link-lib=static=lldWasm");
if cfg!(not(target_os = "windows")) {
println!("cargo:rustc-link-lib=dylib=ffi");
}
}