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
|
extern crate cmake;
extern crate convert_case;
use convert_case::{Case, Casing, StateConverter};
use std::{
env, io,
path::PathBuf,
process::{Command, Stdio},
};
use cmake::Config;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let llvm_components = ["core", "analysis", "bit_reader", "bit_writer", "i_r_reader"]
.iter()
.map(|comp| comp.from_case(Case::Snake));
let msvc = is_msvc();
let (llvm_dir, additonal_cmake_file) = get_llvm_dir();
let (cmake_profile, out_dir) =
build_cmake_targets(llvm_components.clone(), llvm_dir, additonal_cmake_file);
emit_compile_and_linking_information(llvm_components, cmake_profile, out_dir, msvc)
}
fn is_msvc() -> bool {
env::var("CARGO_CFG_TARGET_ENV").unwrap() == "msvc"
}
fn get_llvm_dir() -> (PathBuf, PathBuf) {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let mut additional_cmake_file = manifest_dir.clone();
additional_cmake_file.push("build.cmake");
let mut llvm_dir = manifest_dir.parent().unwrap().to_path_buf();
llvm_dir.push("llvm-project");
llvm_dir.push("llvm");
println!("cargo:rerun-if-changed={}", llvm_dir.display());
println!("cargo:rerun-if-changed={}", additional_cmake_file.display());
(llvm_dir, additional_cmake_file)
}
fn build_cmake_targets<'a>(
components: impl Iterator<Item = StateConverter<'a, &'static str>>,
llvm_dir: PathBuf,
additional_cmake_file: PathBuf,
) -> (String, PathBuf) {
let mut cmake = Config::new(llvm_dir);
use_ninja(&mut cmake);
cmake
.always_configure(true)
// Should be detected automatically, but we have reports of
// LLVM fiding ZLIB on Windows and then failing to link it.
// Out of caution we explicitly disable all autodetectable components
.define("LLVM_ENABLE_LIBXML2", "OFF")
.define("LLVM_ENABLE_ZLIB", "OFF")
.define("LLVM_ENABLE_ZSTD", "OFF")
.define("LLVM_ENABLE_CURL", "OFF")
.define("LLVM_ENABLE_HTTPLIB", "OFF")
.define("LLVM_ENABLE_LIBEDIT", "OFF")
.define("LLVM_ENABLE_TERMINFO", "OFF")
.define("LLVM_BUILD_TOOLS", "OFF")
.define("LLVM_TARGETS_TO_BUILD", "")
.define("LLVM_ENABLE_PROJECTS", "")
.define("CMAKE_PROJECT_INCLUDE_BEFORE", additional_cmake_file);
// Unfortunately CMake crate does not support building mutliple targets at once
for component in components {
cmake
.build_target(&format!("LLVM{}", component.to_case(Case::Pascal)))
.build();
}
(
cmake.get_profile().to_string(),
cmake.build_target("llvm-config").build(),
)
}
fn use_ninja(cmake: &mut Config) {
if let Ok(exit_status) = Command::new("ninja")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg("--version")
.status()
{
if !exit_status.success() {
return;
}
cmake.generator("Ninja");
}
}
fn emit_compile_and_linking_information<'a>(
llvm_components: impl Iterator<Item = StateConverter<'a, &'static str>> + Clone,
cmake_profile: String,
out_dir: PathBuf,
is_msvc: bool,
) {
// MSBuild uses didfferent output path from ninja or Makefile.
// Not sure how to query CMake about it, so we just try once with
// ninja/Makefile path and then once with MSBuild path
let llvm_config_output = execute_llvm_config(
&out_dir,
&["build", "bin", "llvm-config"],
llvm_components.clone(),
)
.or_else(|_| {
execute_llvm_config(
&out_dir,
&["build", &*cmake_profile, "bin", "llvm-config"],
llvm_components,
)
})
.unwrap();
if !llvm_config_output.status.success() {
panic!()
}
let output = String::from_utf8_lossy(&llvm_config_output.stdout);
let lines = (&output).lines().collect::<Vec<_>>();
let (cxxflags, ldflags, libdir, libnames, system_libs) = match &*lines {
[cxxflags, ldflags, libdir, libnames, system_libs, ..] => {
(cxxflags, ldflags, libdir, libnames, system_libs)
}
_ => panic!(),
};
println!("cargo:cxxflags={}", cxxflags);
println!("cargo:rustc-link-arg={}", ldflags);
println!("cargo:rustc-link-search={}", libdir);
for lib in libnames.split_ascii_whitespace() {
let lib = if is_msvc {
// For some reason rustc appends .lib twice on msvc
&lib[..lib.len() - 4]
} else {
// On Linux, we get "libLLVMIRReader.a", so we cut "lib" and ".a"
&lib[3..lib.len() - 2]
};
println!("cargo:rustc-link-lib=static={}", lib);
}
for lib in system_libs.split_ascii_whitespace() {
let lib = if is_msvc {
&lib[..lib.len() - 4]
} else {
// On Linux, we get it as "-lxml2", so we cut "-l" part
&lib[2..]
};
println!("cargo:rustc-link-lib={}", lib);
}
if !is_msvc {
println!("cargo:rustc-link-lib=stdc++");
}
}
fn execute_llvm_config<'a>(
out_dir: &PathBuf,
llvm_config_exe_relative: &[&str],
llvm_components: impl Iterator<Item = StateConverter<'a, &'static str>>,
) -> io::Result<std::process::Output> {
let mut llvm_config_path = out_dir.clone();
llvm_config_path.extend(llvm_config_exe_relative);
let mut llvm_config_cmd = Command::new(&llvm_config_path);
llvm_config_cmd.args([
"--cxxflags",
"--ldflags",
"--libdir",
"--libnames",
"--system-libs",
"--link-static",
]);
for component in llvm_components {
llvm_config_cmd.arg(&component.to_case(Case::Flat));
}
llvm_config_cmd
.stdin(Stdio::null())
.stderr(Stdio::null())
.output()
}
|