-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.rs
295 lines (249 loc) · 9.23 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
//! This build script emits the openblas linking directive if requested
#![allow(unused_variables)]
#[derive(PartialEq, Eq)]
enum Library {
Static,
Dynamic,
}
pub const SEQUENTIAL: bool = false;
pub const THREADED: bool = !SEQUENTIAL;
pub const LD_DIR: &str = if cfg!(windows) {
"PATH"
} else if cfg!(target_os = "linux") {
"LD_LIBRARY_PATH"
} else if cfg!(target_os = "macos") {
"DYLD_LIBRARY_PATH"
} else {
""
};
pub const DEFAULT_ONEAPI_ROOT: &str = if cfg!(windows) {
"C:/Program Files (x86)/Intel/oneAPI/"
} else {
"/opt/intel/oneapi/"
};
pub const MKL_CORE: &str = "mkl_core";
pub const MKL_THREAD: &str = if SEQUENTIAL {
"mkl_sequential"
} else {
"mkl_intel_thread"
};
pub const THREADING_LIB: &str = if cfg!(windows) { "libiomp5md" } else { "iomp5" };
pub const MKL_INTERFACE: &str = if cfg!(target_pointer_width = "32") {
"mkl_intel_ilp64"
} else {
"mkl_intel_lp64"
};
#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
pub const UNSUPPORTED_OS_ERROR: _ = "Target OS is not supported. Please contact me";
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
pub const LINK_DIRS: &[&str] = &[
"compiler/latest/windows/compiler/lib/ia32_win",
"mkl/latest/lib/ia32",
];
#[cfg(all(target_os = "windows", target_pointer_width = "64"))]
pub const LINK_DIRS: &[&str] = &[
"compiler/latest/windows/compiler/lib/intel64_win",
"mkl/latest/lib/intel64",
];
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
pub const SHARED_LIB_DIRS: &[&str] = &[
"compiler/latest/windows/redist/ia32_win/compiler",
"mkl/latest/redist/ia32",
];
#[cfg(all(target_os = "windows", target_pointer_width = "64"))]
pub const SHARED_LIB_DIRS: &[&str] = &[
"compiler/latest/windows/redist/intel64_win/compiler",
"mkl/latest/redist/intel64",
];
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
pub const LINK_DIRS: &[&str] = &[
"compiler/latest/linux/compiler/lib/ia32_lin",
"mkl/latest/lib/ia32",
];
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub const LINK_DIRS: &[&str] = &[
"compiler/latest/linux/compiler/lib/intel64_lin",
"mkl/latest/lib/intel64",
];
#[cfg(target_os = "linux")]
pub const SHARED_LIB_DIRS: &[&str] = LINK_DIRS;
#[cfg(target_os = "macos")]
const MACOS_COMPILER_PATH: &str = "compiler/latest/mac/compiler/lib";
#[cfg(target_os = "macos")]
pub const LINK_DIRS: &[&str] = &[MACOS_COMPILER_PATH, "mkl/latest/lib"];
#[cfg(target_os = "macos")]
pub const SHARED_LIB_DIRS: &[&str] = &["mkl/latest/lib"];
#[derive(Debug)]
pub enum BuildError {
OneAPINotFound(std::path::PathBuf),
OneAPINotADir(std::path::PathBuf),
PathNotFound(std::env::VarError),
AddSharedLibDirToPath(String),
}
#[cfg(feature = "intel-mkl")]
fn suggest_setvars_cmd(root: &str) -> String {
if cfg!(windows) {
format!("{root}/setvars.bat")
} else {
format!("source {root}/setvars.sh")
}
}
#[cfg(feature = "cuda")]
mod cuda {
pub fn build_ptx() {
let out_dir = std::env::var("OUT_DIR").unwrap();
let kernel_paths: Vec<std::path::PathBuf> = glob::glob("src/**/*.cu")
.unwrap()
.map(|p| p.unwrap())
.collect();
let mut include_directories: Vec<std::path::PathBuf> = glob::glob("src/**/*.cuh")
.unwrap()
.map(|p| p.unwrap())
.collect();
for path in &kernel_paths {
println!("cargo:rerun-if-changed={}", path.display());
}
for path in &mut include_directories {
println!("cargo:rerun-if-changed={}", path.display());
// remove the filename from the path so it's just the directory
path.pop();
}
include_directories.sort();
include_directories.dedup();
#[allow(unused)]
let include_options: Vec<String> = include_directories
.into_iter()
.map(|s| "-I".to_string() + &s.into_os_string().into_string().unwrap())
.collect::<Vec<_>>();
#[cfg(feature = "ci-check")]
for mut kernel_path in kernel_paths.into_iter() {
kernel_path.set_extension("ptx");
let mut ptx_path: std::path::PathBuf = out_dir.clone().into();
ptx_path.push(kernel_path.as_path().file_name().unwrap());
std::fs::File::create(ptx_path).unwrap();
}
#[cfg(not(feature = "ci-check"))]
{
let start = std::time::Instant::now();
kernel_paths
.iter()
.for_each(|p| println!("cargo:rerun-if-changed={}", p.display()));
let children = kernel_paths
.iter()
.map(|p| {
std::process::Command::new("nvcc")
.arg(format!("--gpu-architecture=native"))
.arg("--ptx")
.args(["--default-stream", "per-thread"])
.args(["--output-directory", &out_dir])
.args(&include_options)
.arg(p)
.spawn()
.expect("nvcc not found")
})
.collect::<Vec<_>>();
for (kernel_path, child) in kernel_paths.iter().zip(children.into_iter()) {
let output = child.wait_with_output().unwrap();
assert!(
output.status.success(),
"nvcc error while compiling {kernel_path:?}: {output:?}",
);
}
println!("cargo:warning=Compiled {:?}", kernel_paths,);
println!(
"cargo:warning=Compiled {:?} cuda kernels in {:?}",
kernel_paths.len(),
start.elapsed()
);
}
}
}
fn main() -> Result<(), BuildError> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=STATIC");
let library = if std::env::var("STATIC").unwrap_or_else(|_| "0".to_string()) == "1" {
Library::Static
} else {
Library::Dynamic
};
let link_type: &str = if Library::Static == library {
"static"
} else {
"dylib"
};
#[cfg(feature = "cblas")]
println!("cargo:rustc-link-lib={link_type}=cblas");
#[cfg(feature = "intel-mkl")]
{
let root = std::env::var("ONEAPI_ROOT").unwrap_or_else(|_| DEFAULT_ONEAPI_ROOT.to_string());
println!("Using '{root}' as ONEAPI_ROOT");
let path = match std::env::var(LD_DIR) {
Ok(path) => path,
Err(e) => {
// On macOS it's unusual to set $DYLD_LIBRARY_PATH, so we want to provide a helpful message
println!(
"Library path env variable '{LD_DIR}' was not found. Run `{}`",
suggest_setvars_cmd(&root)
);
return Err(BuildError::PathNotFound(e));
}
};
if library == Library::Dynamic {
// check to make sure that things in `SHARED_LIB_DIRS` are in `$LD_DIR`.
let path = path.replace('\\', "/");
for shared_lib_dir in SHARED_LIB_DIRS {
println!("cargo:rerun-if-env-changed=MKL_VERSION");
let mkl_version =
std::env::var("MKL_VERSION").unwrap_or_else(|_| "2023.0.0".to_string());
let versioned_dir = shared_lib_dir.replace("latest", &mkl_version);
println!("Checking that '{shared_lib_dir}' or '{versioned_dir}' is in {LD_DIR}");
if !path.contains(shared_lib_dir) && !path.contains(&versioned_dir) {
println!(
"'{shared_lib_dir}' not found in library path. Run `{}`",
suggest_setvars_cmd(&root)
);
return Err(BuildError::AddSharedLibDirToPath(
shared_lib_dir.to_string(),
));
}
}
}
let root: std::path::PathBuf = root.into();
if !root.exists() {
return Err(BuildError::OneAPINotFound(root));
}
if !root.is_dir() {
return Err(BuildError::OneAPINotADir(root));
}
for rel_lib_dir in LINK_DIRS {
let lib_dir = root.join(rel_lib_dir);
println!("cargo:rustc-link-search={}", lib_dir.display());
}
let lib_postfix: &str = if cfg!(windows) && library == Library::Static {
"_dll"
} else {
""
};
println!("cargo:rustc-link-lib={link_type}={MKL_INTERFACE}{lib_postfix}");
println!("cargo:rustc-link-lib={link_type}={MKL_THREAD}{lib_postfix}");
println!("cargo:rustc-link-lib={link_type}={MKL_CORE}{lib_postfix}");
if THREADED {
println!("cargo:rustc-link-lib=dylib={THREADING_LIB}");
}
if !cfg!(windows) {
println!("cargo:rustc-link-lib=pthread");
println!("cargo:rustc-link-lib=m");
println!("cargo:rustc-link-lib=dl");
}
#[cfg(target_os = "macos")]
{
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{}/{MACOS_COMPILER_PATH}",
root.display(),
);
}
}
#[cfg(feature = "cuda")]
cuda::build_ptx();
Ok(())
}