-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
205 lines (187 loc) · 6.63 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
// build.rs
//
// Copyright (c) 2023-2024 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
use std::env;
use std::path::{Path, PathBuf};
use cmake::Config;
use walkdir::WalkDir;
#[cfg(not(target_os = "windows"))]
const PATH_SEPARATOR: char = ':';
#[cfg(target_os = "windows")]
const PATH_SEPARATOR: char = ';';
fn add_search_paths(key: &str) {
println!("cargo:rerun-if-env-changed={}", key);
if let Ok(library_path) = env::var(key) {
library_path
.split(PATH_SEPARATOR)
.filter(|v| !v.is_empty())
.for_each(|v| {
println!("cargo:rustc-link-search={}", v);
});
}
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/sys");
println!("cargo:rerun-if-changed=include");
println!("cargo:rerun-if-changed=CTranslate2");
add_search_paths("LIBRARY_PATH");
add_search_paths("CMAKE_LIBRARY_PATH");
let mut cmake = Config::new("CTranslate2");
cmake
.define("BUILD_CLI", "OFF")
.define("BUILD_SHARED_LIBS", "OFF")
.define("WITH_MKL", "OFF")
.define("OPENMP_RUNTIME", "NONE");
if cfg!(target_os = "windows") {
let rustflags = env::var("CARGO_ENCODED_RUSTFLAGS").unwrap_or_default();
if !rustflags.contains("target-feature=+crt-static") {
println!("cargo:warning=For Windows compilation, set `RUSTFLAGS=-C target-feature=+crt-static`.");
}
println!("cargo::rustc-link-arg=/FORCE:MULTIPLE");
cmake.profile("Release").cxxflag("/EHsc").static_crt(true);
}
if cfg!(feature = "cuda") {
let cuda = cuda_root().expect("CUDA_TOOLKIT_ROOT_DIR is not specified");
cmake.define("WITH_CUDA", "ON");
cmake.define("CUDA_TOOLKIT_ROOT_DIR", &cuda);
println!("cargo:rustc-link-search={}", cuda.join("lib").display());
println!("cargo:rustc-link-search={}", cuda.join("lib64").display());
println!("cargo:rustc-link-search={}", cuda.join("lib/x64").display());
println!("cargo:rustc-link-lib=static=cudart_static");
if cfg!(feature = "cudnn") {
cmake.define("WITH_CUDNN", "ON");
println!("cargo:rustc-link-lib=cudnn");
}
if cfg!(feature = "cuda-dynamic-loading") {
cmake.define("CUDA_DYNAMIC_LOADING", "ON");
} else {
if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=static=cublas");
println!("cargo:rustc-link-lib=static=cublasLt");
} else {
println!("cargo:rustc-link-lib=static=cublas_static");
println!("cargo:rustc-link-lib=static=cublasLt_static");
println!("cargo:rustc-link-lib=static=culibos");
}
}
}
if cfg!(feature = "mkl") {
cmake.define("WITH_MKL", "ON");
}
if cfg!(feature = "openblas") {
println!("cargo:rustc-link-lib=static=openblas");
cmake.define("WITH_OPENBLAS", "ON");
}
if cfg!(feature = "ruy") {
cmake.define("WITH_RUY", "ON");
}
if cfg!(feature = "accelerate") {
println!("cargo:rustc-link-lib=framework=Accelerate");
cmake.define("WITH_ACCELERATE", "ON");
}
if cfg!(feature = "tensor-parallel") {
cmake.define("WITH_TENSOR_PARALLEL", "ON");
}
if cfg!(feature = "flash-attention") {
cmake.define("WITH_FLASH_ATTN", "ON");
}
let ctranslate2 = cmake.build();
link_libraries(ctranslate2.join("build"));
cxx_build::bridges([
"src/sys/types.rs",
"src/sys/config.rs",
"src/sys/translator.rs",
"src/sys/generator.rs",
"src/sys/storage_view.rs",
"src/sys/whisper.rs",
])
.file("src/sys/translator.cpp")
.file("src/sys/generator.cpp")
.file("src/sys/whisper.cpp")
.include("CTranslate2/include")
.std("c++17")
.static_crt(cfg!(target_os = "windows"))
.flag_if_supported("/EHsc")
.compile("ct2rs");
}
#[cfg(not(target_os = "windows"))]
fn is_library(name: &&str) -> bool {
name.starts_with("lib") && name.ends_with(".a")
}
#[cfg(not(target_os = "windows"))]
fn library_name(name: &str) -> &str {
&name[3..name.len() - 2]
}
#[cfg(target_os = "windows")]
fn is_library(name: &&str) -> bool {
name.ends_with(".lib")
}
#[cfg(target_os = "windows")]
fn library_name(name: &str) -> &str {
&name[0..name.len() - 4]
}
fn link_libraries<T: AsRef<Path>>(root: T) {
let mut current_dir = None;
for entry in WalkDir::new(root).into_iter().filter_map(Result::ok) {
let path = entry.path();
if path.is_file() {
path.file_name()
.and_then(|name| name.to_str())
.filter(is_library)
.iter()
.for_each(|name| {
let parent = path.parent();
if parent != current_dir.as_deref() {
let dir = parent.unwrap();
println!("cargo:rustc-link-search={}", dir.display());
current_dir = Some(dir.to_path_buf())
}
println!("cargo:rustc-link-lib=static={}", library_name(name));
});
}
}
}
// The function below was derived and modified from the `cudarc` crate.
// Original source: https://github.com/coreylowman/cudarc/blob/main/build.rs
//
// Copyright (c) 2024 Corey Lowman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
fn cuda_root() -> Option<PathBuf> {
let env_vars = [
"CUDA_PATH",
"CUDA_ROOT",
"CUDA_TOOLKIT_ROOT_DIR",
"CUDNN_LIB",
];
let env_vars = env_vars
.into_iter()
.map(std::env::var)
.filter_map(Result::ok);
let roots = [
"/usr",
"/usr/local/cuda",
"/opt/cuda",
"/usr/lib/cuda",
"C:/Program Files/NVIDIA GPU Computing Toolkit",
"C:/CUDA",
];
let roots = roots.into_iter().map(Into::into);
env_vars
.chain(roots)
.map(Into::<PathBuf>::into)
.find(|path| path.join("include").join("cuda.h").is_file())
}