forked from qir-alliance/pyqir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
378 lines (329 loc) · 11.8 KB
/
build.rs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use std::boxed::Box;
use std::env;
use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};
use cc::Build;
use cmake::Config;
mod external;
use external::llvm_sys;
extern crate cc;
#[macro_use]
extern crate lazy_static;
// Make sure one version of llvm features is used
#[cfg(all(
not(any(feature = "llvm11-0")),
not(any(feature = "llvm12-0")),
not(any(feature = "llvm13-0")),
not(any(feature = "llvm14-0")),
))]
compile_error!("One of the features `qirlib/llvm11-0`, `qirlib/llvm12-0`, `qirlib/llvm13-0`, and `qirlib/llvm14-0` must be used exclusive.");
// Make sure only one llvm option is used.
#[cfg(any(
all(
feature = "llvm11-0",
any(feature = "llvm12-0", feature = "llvm13-0", feature = "llvm14-0")
),
all(
feature = "llvm12-0",
any(feature = "llvm11-0", feature = "llvm13-0", feature = "llvm14-0")
),
all(
feature = "llvm13-0",
any(feature = "llvm11-0", feature = "llvm12-0", feature = "llvm14-0")
),
all(
feature = "llvm14-0",
any(feature = "llvm11-0", feature = "llvm12-0", feature = "llvm13-0")
),
))]
compile_error!("Features `qirlib/llvm11-0`, `qirlib/llvm12-0`, `qirlib/llvm13-0`, and `qirlib/llvm14-0` must be used exclusive.");
// Make sure one of the linking features is used
#[cfg(all(
not(any(feature = "qirlib-llvm-linking")),
not(any(feature = "external-llvm-linking")),
not(any(feature = "no-llvm-linking")),
))]
compile_error!("One of the features `qirlib/qirlib-llvm-linking`, `qirlib/external-llvm-linking`, and `qirlib/no-llvm-linking` must be used exclusive.");
// Make sure only one linking option is used.
#[cfg(any(
all(
feature = "qirlib-llvm-linking",
any(feature = "external-llvm-linking", feature = "no-llvm-linking")
),
all(
feature = "external-llvm-linking",
any(feature = "qirlib-llvm-linking", feature = "no-llvm-linking")
),
all(
feature = "no-llvm-linking",
any(feature = "qirlib-llvm-linking", feature = "external-llvm-linking")
),
))]
compile_error!("Features `qirlib/qirlib-llvm-linking`, `qirlib/external-llvm-linking`, and `qirlib/no-llvm-linking` are mutually exclusive.");
// if we are building or downloading, we cannot be externally linking
#[cfg(any(
all(
feature = "build-llvm",
any(feature = "download-llvm", feature = "external-llvm-linking")
),
all(
feature = "download-llvm",
any(feature = "build-llvm", feature = "external-llvm-linking")
),
))]
compile_error!("Features `qirlib/build-llvm` and `qirlib/download-llvm` are mutually exclusive.");
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=config.cmake");
println!("cargo:rerun-if-changed=CMakeLists.txt");
let install_dir = get_llvm_install_dir();
println!("cargo:rerun-if-changed={:?}", install_dir);
// llvm-sys components
println!("cargo:rerun-if-changed=external.rs");
println!("cargo:rerun-if-changed=target.c");
println!("cargo:rerun-if-changed=llvm-wrapper/LLVMWrapper.h");
println!("cargo:rerun-if-changed=llvm-wrapper/MetadataWrapper.cpp");
println!("cargo:rerun-if-changed=llvm-wrapper/ModuleWrapper.cpp");
// Download vars passed to cmake
println!("cargo:rerun-if-env-changed=QIRLIB_DOWNLOAD_LLVM");
println!("cargo:rerun-if-env-changed=QIRLIB_LLVM_BUILDS_URL");
println!("cargo:rerun-if-env-changed=QIRLIB_LLVM_PKG_NAME");
// Package vars used only in here
println!("cargo:rerun-if-env-changed=QIRLIB_PKG_DEST");
// Build vars passed to cmake
println!("cargo:rerun-if-env-changed=QIRLIB_LLVM_TAG");
// maps to CPACK_PACKAGE_FILE_NAME
println!("cargo:rerun-if-env-changed=QIRLIB_PACKAGE_FILE_NAME");
// maps to CMAKE_INSTALL_PREFIX passed to cmake in build and download
println!("cargo:rerun-if-env-changed=QIRLIB_CACHE_DIR");
if cfg!(feature = "download-llvm") {
println!("Downloading llvm");
download_llvm()?;
} else if cfg!(feature = "build-llvm") {
println!("Building llvm");
compile_llvm()?;
}
if cfg!(feature = "qirlib-llvm-linking") {
println!("Linking llvm");
link_llvm();
let build_dir = get_build_dir()?;
compile_target_wrappers(&build_dir)?;
} else if cfg!(feature = "external-llvm-linking") {
println!("LLVM_SYS_{{}}_PREFIX will provide the LLVM linking");
} else {
println!("No LLVM linking");
}
if !cfg!(feature = "no-llvm-linking") {
compile_llvm_wrapper()?;
}
Ok(())
}
fn download_llvm() -> Result<(), Box<dyn Error>> {
// If the download url isn't set, we need to immediately fail.
let url = env::var("QIRLIB_LLVM_BUILDS_URL")?;
let enable_download = env::var("QIRLIB_DOWNLOAD_LLVM").unwrap_or_else(|_| "true".to_owned());
let build_dir = get_build_dir()?;
let mut config = Config::new(build_dir);
config
.generator("Ninja")
.no_build_target(true)
.env("QIRLIB_LLVM_PKG_NAME", get_package_file_name()?)
.env("QIRLIB_LLVM_BUILDS_URL", url)
.env("QIRLIB_DOWNLOAD_LLVM", enable_download)
.define("CPACK_PACKAGE_FILE_NAME", get_package_name()?)
.define("CMAKE_INSTALL_PREFIX", get_llvm_install_dir())
.very_verbose(true);
let _ = config.build();
Ok(())
}
fn get_llvm_compile_target() -> String {
// We always install unless package is chosen.
// The user's choices for CMAKE_INSTALL_PREFIX will choose whether
// the installation goes into the target folder for linking or
// into another dir for potential reuse
if cfg!(feature = "package-llvm") {
"llvm-prefix/src/llvm-stamp/llvm-package".to_owned()
} else {
"llvm-prefix/src/llvm-stamp/llvm-install".to_owned()
}
}
fn compile_llvm() -> Result<(), Box<dyn Error>> {
let build_dir = get_build_dir()?;
let mut config = Config::new(build_dir);
config
.generator("Ninja")
.build_target(get_llvm_compile_target().as_str())
.env("QIRLIB_LLVM_TAG", get_llvm_tag())
.define("CPACK_PACKAGE_FILE_NAME", get_package_name()?)
.define("CMAKE_INSTALL_PREFIX", get_llvm_install_dir());
let _ = config.build();
if cfg!(feature = "package-llvm") {
package_llvm()?;
}
Ok(())
}
fn package_llvm() -> Result<(), Box<dyn Error>> {
let out_dir = env::var("OUT_DIR").expect("Could not get OUT_DIR environment variable");
let output = PathBuf::from(out_dir)
.join("build")
.join("llvm-prefix")
.join("src")
.join("llvm-build")
.join(get_package_file_name()?);
if let Ok(dest_dir) = env::var("QIRLIB_PKG_DEST") {
let dest = PathBuf::from(dest_dir).join(get_package_file_name()?);
println!(
"Moving {} to {}.",
output.as_path().display(),
dest.as_path().display()
);
fs::rename(output, dest)?;
} else {
println!("Not moving package output. QIRLIB_PKG_DEST not set.");
}
Ok(())
}
fn get_build_dir() -> Result<PathBuf, Box<dyn Error>> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
let build_dir = PathBuf::from(manifest_dir.as_str());
let normalized_build_dir = fs::canonicalize(build_dir)?;
println!(
"llvm build files dir: {}",
normalized_build_dir.to_str().unwrap()
);
Ok(normalized_build_dir)
}
fn link_llvm() {
let libdir = llvm_sys::llvm_config("--libdir");
// Export information to other crates
println!(
"cargo:config_path={}",
llvm_sys::LLVM_CONFIG_PATH.clone().unwrap().display()
); // will be DEP_QIRLIB_CONFIG_PATH
println!("cargo:libdir={}", libdir); // DEP_QIRLIB_LIBDIR
// Link LLVM libraries
println!("cargo:rustc-link-search=native={}", libdir);
for name in llvm_sys::get_link_libraries() {
println!("cargo:rustc-link-lib=static={}", name);
}
// Link system libraries
for name in llvm_sys::get_system_libraries() {
println!("cargo:rustc-link-lib=dylib={}", name);
}
}
fn compile_target_wrappers(build_dir: &Path) -> Result<(), Box<dyn Error>> {
let target_c = build_dir.join("target.c").canonicalize()?;
env::set_var("CFLAGS", llvm_sys::get_llvm_cflags());
Build::new().file(target_c).compile("targetwrappers");
Ok(())
}
fn compile_llvm_wrapper() -> Result<(), Box<dyn Error>> {
let mut cfg = cc::Build::new();
cfg.warnings(false);
let cxxflags = llvm_sys::get_llvm_cxxflags();
for flag in cxxflags.split_whitespace() {
if flag.starts_with("-flto") {
continue;
}
cfg.flag(flag);
}
cfg.cpp(true)
.cpp_link_stdlib(None)
.static_crt(true)
.file("llvm-wrapper/MetadataWrapper.cpp")
.file("llvm-wrapper/ModuleWrapper.cpp")
.compile("llvm-wrapper");
Ok(())
}
fn get_package_file_name() -> Result<String, Box<dyn Error>> {
let mut base_name = get_package_name()?;
if llvm_sys::target_os_is("windows") {
base_name.push_str(".zip");
} else {
base_name.push_str(".tar.gz");
}
Ok(base_name)
}
fn get_llvm_tag() -> String {
if let Ok(tag) = env::var("QIRLIB_LLVM_TAG") {
tag
} else if cfg!(feature = "llvm11-0") {
"llvmorg-11.1.0".to_owned() // 1fdec59bf
} else if cfg!(feature = "llvm12-0") {
"llvmorg-12.0.1".to_owned() // fed4134
} else if cfg!(feature = "llvm13-0") {
"llvmorg-13.0.1".to_owned() // 75e33f7
} else if cfg!(feature = "llvm14-0") {
"llvmorg-14.0.6".to_owned() // 28c006
} else {
panic!("Unsupported LLVM version. The LLVM feature flags or QIRLIB_LLVM_TAG must be set.")
}
}
fn get_package_name() -> Result<String, Box<dyn Error>> {
if let Ok(file_name) = env::var("QIRLIB_PACKAGE_FILE_NAME") {
Ok(file_name)
} else {
let tag = get_llvm_tag();
let triple = get_target_triple()?;
let package_name = format!("qirlib-llvm-{}-{}", triple, tag);
Ok(package_name)
}
}
fn get_target_triple() -> Result<String, Box<dyn Error>> {
let target = if llvm_sys::target_os_is("windows") {
// TODO: remove static linking and just return the TARGET
"x86_64-pc-windows-msvc-static".to_owned()
} else {
env::var("TARGET")?
};
Ok(target)
}
fn get_llvm_install_dir() -> PathBuf {
if let Ok(path) = env::var("QIRLIB_CACHE_DIR") {
PathBuf::from(path)
} else {
// if we install to OUT_DIR the llvm install task during the extraction
// of the archive will empty the target directory breaking the build.
// To avoid that, we put llvm binaries into the OUT_DIR/llvm folder.
let out_dir = env::var("OUT_DIR").expect("Could not get OUT_DIR environment variable");
PathBuf::from(out_dir).join("llvm")
}
}
fn locate_llvm_config() -> Option<PathBuf> {
let major = if cfg!(feature = "llvm11-0") {
"11"
} else if cfg!(feature = "llvm12-0") {
"12"
} else if cfg!(feature = "llvm13-0") {
"13"
} else if cfg!(feature = "llvm14-0") {
"14"
} else {
"unknown"
};
if let Ok(path) = env::var(format!("DEP_LLVM_{major}_CONFIG_PATH")) {
Some(PathBuf::from(path))
} else {
let dir = get_llvm_install_dir();
println!("Looking in {:?}", dir);
let prefix = dir.join("bin");
let binary_name = llvm_config_name();
let binary_path = prefix.join(binary_name);
if binary_path.as_path().exists() {
Some(binary_path)
} else {
None
}
}
}
pub fn llvm_config_name() -> String {
let mut base_name = "llvm-config".to_owned();
// On Windows, also search for llvm-config.exe
if llvm_sys::target_os_is("windows") {
base_name.push_str(".exe");
}
base_name
}