Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
261 lines (233 loc) · 8.89 KB
/
Copy pathbuild.rs
File metadata and controls
261 lines (233 loc) · 8.89 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
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
use std::env;
use std::ffi::OsString;
use std::path::{Component, Path, PathBuf};
use std::process::Command;
fn main() {
println!("cargo:rerun-if-env-changed=ZMEU_MLIR_DIR");
println!("cargo:rerun-if-env-changed=MLIR_DIR");
println!("cargo:rerun-if-env-changed=ZMEU_LLVM_DIR");
println!("cargo:rerun-if-env-changed=LLVM_DIR");
println!("cargo:rerun-if-env-changed=ZMEU_DIST_RPATH");
println!("cargo:rerun-if-env-changed=CARGO_TARGET_DIR");
println!("cargo:rerun-if-env-changed=CC");
println!("cargo:rerun-if-env-changed=AR");
println!("cargo:rerun-if-changed=mlir/CMakeLists.txt");
println!("cargo:rerun-if-changed=mlir/v1_abi.schema.toml");
println!("cargo:rerun-if-changed=mlir/include");
println!("cargo:rerun-if-changed=mlir/include/Dialect/Zmeu/IR/ZmeuDialect.td");
println!("cargo:rerun-if-changed=mlir/include/Dialect/Zmeu/IR/ZmeuOps.td");
println!("cargo:rerun-if-changed=mlir/lib");
println!("cargo:rerun-if-changed=runtime/zmeu_runtime.c");
println!("cargo:rerun-if-changed=runtime/zmeu_runtime_parts");
println!("cargo:rerun-if-changed=src/mlir/abi_asserts.rs");
println!("cargo:rerun-if-changed=tools/gen_mlir_abi.py");
if env::var_os("CARGO_FEATURE_MLIR_BACKEND").is_none() {
return;
}
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
check_mlir_abi(&manifest_dir);
let build_dir = mlir_build_dir(&manifest_dir);
let source_dir = manifest_dir.join("mlir");
let mlir_dir = discover_mlir_dir();
let llvm_dir = discover_llvm_dir(&mlir_dir);
let llvm_lib_dir = llvm_dir
.parent()
.and_then(Path::parent)
.map(Path::to_path_buf)
.expect("LLVM CMake config path should be <prefix>/lib/cmake/llvm");
let mut configure = Command::new("cmake");
configure
.arg("-S")
.arg(&source_dir)
.arg("-B")
.arg(&build_dir)
.arg("-DCMAKE_BUILD_TYPE=RelWithDebInfo")
.arg(format!("-DMLIR_DIR={}", mlir_dir.display()))
.arg(format!("-DLLVM_DIR={}", llvm_dir.display()));
let configure_status = configure.status().unwrap_or_else(|err| {
panic!("failed to run cmake for the MLIR backend: {err}");
});
if !configure_status.success() {
panic!("cmake configuration for the MLIR backend failed");
}
let build_status = Command::new("cmake")
.arg("--build")
.arg(&build_dir)
.status()
.unwrap_or_else(|err| panic!("failed to build the MLIR backend: {err}"));
if !build_status.success() {
panic!("cmake build for the MLIR backend failed");
}
let runtime_archive = build_runtime_archive(&manifest_dir, &build_dir);
println!("cargo:rustc-link-search=native={}", build_dir.display());
println!("cargo:rustc-link-search=native={}", llvm_lib_dir.display());
println!("cargo:rustc-link-lib=dylib=ZmeuMLIR");
println!(
"cargo:rustc-env=ZMEU_BUILT_RUNTIME_ARCHIVE={}",
runtime_archive.display()
);
if let Some(llvm_bin_dir) = llvm_bin_dir_from_cmake_dir(&llvm_dir) {
println!(
"cargo:rustc-env=ZMEU_BUILD_LLVM_BINDIR={}",
llvm_bin_dir.display()
);
}
if use_origin_rpath_for_mlir_bridge() {
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
} else {
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", build_dir.display());
}
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", llvm_lib_dir.display());
}
fn mlir_build_dir(manifest_dir: &Path) -> PathBuf {
let target_dir = env::var_os("CARGO_TARGET_DIR")
.map(PathBuf::from)
.map(|path| {
if path.is_absolute() {
path
} else {
manifest_dir.join(path)
}
})
.unwrap_or_else(|| manifest_dir.join("target"));
target_dir.join("zmeu-mlir-build")
}
fn use_origin_rpath_for_mlir_bridge() -> bool {
env::var_os("ZMEU_DIST_RPATH").is_some()
|| env::var("PROFILE").as_deref() == Ok("dist")
|| env::var_os("OUT_DIR")
.map(PathBuf::from)
.is_some_and(|path| {
path.components()
.any(|component| matches!(component, Component::Normal(part) if part == "dist"))
})
}
fn check_mlir_abi(manifest_dir: &Path) {
let status = Command::new("python3")
.arg(manifest_dir.join("tools").join("gen_mlir_abi.py"))
.arg("--check")
.current_dir(manifest_dir)
.status()
.unwrap_or_else(|err| panic!("failed to run MLIR ABI schema check: {err}"));
if !status.success() {
panic!("MLIR ABI schema check failed; run python3 tools/gen_mlir_abi.py");
}
}
fn discover_mlir_dir() -> PathBuf {
if let Some(path) = env_dir("ZMEU_MLIR_DIR").or_else(|| env_dir("MLIR_DIR")) {
require_config(&path, "MLIRConfig.cmake", "MLIR");
return path;
}
for version in [22, 21, 20, 19, 18] {
let path = PathBuf::from(format!("/usr/lib/llvm-{version}/lib/cmake/mlir"));
if path.join("MLIRConfig.cmake").is_file() {
return path;
}
}
panic!(
"mlir-backend feature is enabled, but no MLIR CMake config was found. \
Set ZMEU_MLIR_DIR to a directory containing MLIRConfig.cmake, or install libmlir-22-dev \
(also accepts apt-style /usr/lib/llvm-21, /usr/lib/llvm-20, /usr/lib/llvm-19, or /usr/lib/llvm-18 installs)."
);
}
fn discover_llvm_dir(mlir_dir: &Path) -> PathBuf {
if let Some(path) = env_dir("ZMEU_LLVM_DIR").or_else(|| env_dir("LLVM_DIR")) {
require_config(&path, "LLVMConfig.cmake", "LLVM");
return path;
}
if let Some(path) = llvm_dir_next_to_mlir(mlir_dir)
&& path.join("LLVMConfig.cmake").is_file()
{
return path;
}
let versions = llvm_versions_from_path(mlir_dir)
.into_iter()
.chain([22, 21, 20, 19, 18])
.collect::<Vec<_>>();
for version in versions {
let path = PathBuf::from(format!("/usr/lib/llvm-{version}/lib/cmake/llvm"));
if path.join("LLVMConfig.cmake").is_file() {
return path;
}
}
panic!(
"mlir-backend feature is enabled, but no LLVM CMake config was found. \
Set ZMEU_LLVM_DIR to a directory containing LLVMConfig.cmake, or install the matching LLVM dev package."
);
}
fn env_dir(name: &str) -> Option<PathBuf> {
env::var_os(name)
.map(PathBuf::from)
.filter(|path| !path.as_os_str().is_empty())
}
fn require_config(path: &Path, file_name: &str, package: &str) {
if !path.join(file_name).is_file() {
panic!(
"{package} CMake config was not found at {}. Expected {}.",
path.display(),
path.join(file_name).display()
);
}
}
fn llvm_dir_next_to_mlir(mlir_dir: &Path) -> Option<PathBuf> {
let cmake_dir = mlir_dir.parent()?;
if cmake_dir.file_name()? != "cmake" {
return None;
}
Some(cmake_dir.join("llvm"))
}
fn llvm_versions_from_path(path: &Path) -> Vec<u32> {
path.components()
.filter_map(|component| component.as_os_str().to_str())
.filter_map(|part| part.strip_prefix("llvm-"))
.filter_map(|version| version.parse::<u32>().ok())
.collect()
}
fn llvm_bin_dir_from_cmake_dir(llvm_dir: &Path) -> Option<PathBuf> {
let prefix = llvm_dir.parent()?.parent()?.parent()?;
Some(prefix.join("bin"))
}
fn build_runtime_archive(manifest_dir: &Path, build_dir: &Path) -> PathBuf {
let runtime_source = manifest_dir.join("runtime").join("zmeu_runtime.c");
let runtime_object = build_dir.join("zmeu_runtime.o");
let runtime_archive = build_dir.join("libzmeu_runtime.a");
let cc = env::var_os("CC").unwrap_or_else(|| OsString::from("cc"));
let mut compile = Command::new(&cc);
compile.arg("-std=c11").arg("-O2");
if cfg!(target_os = "linux") {
// Runtime C functions need frontend stack-clash probing at compile
// time; the native link step only sees the already-built archive.
compile.arg("-fstack-clash-protection");
}
let compile_status = compile
.arg("-c")
.arg(&runtime_source)
.arg("-o")
.arg(&runtime_object)
.status()
.unwrap_or_else(|err| {
panic!(
"failed to compile Zmeu runtime with `{}`: {err}",
PathBuf::from(&cc).display()
)
});
if !compile_status.success() {
panic!("Zmeu runtime C compilation failed");
}
let ar = env::var_os("AR").unwrap_or_else(|| OsString::from("ar"));
let archive_status = Command::new(&ar)
.arg("crus")
.arg(&runtime_archive)
.arg(&runtime_object)
.status()
.unwrap_or_else(|err| {
panic!(
"failed to archive Zmeu runtime with `{}`: {err}",
PathBuf::from(&ar).display()
)
});
if !archive_status.success() {
panic!("Zmeu runtime archive creation failed");
}
runtime_archive
}