Skip to content

Commit 30ea430

Browse files
committed
feat: add system feature to use pre-installed CTranslate2 library
Introduced a new `system` feature to skip compiling CTranslate2 and instead link to an existing shared library. Updated build script, feature flags, and README with usage details.
1 parent 939d6d3 commit 30ea430

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ If you want to use platform-specific default features, use the `ct2rs-platform`
8383

8484
- `whisper`: Enables [Whisper](https://huggingface.co/docs/transformers/model_doc/whisper) model support
8585
- `hub`: Enables [HuggingFace Hub](https://huggingface.co/docs/hub) integration
86+
- `system`: Skip compiling CTranslate2 and use the system's pre-installed shared library instead (requires setting the
87+
appropriate environment variables to locate the CTranslate2 shared library)
8688

8789
### Platform Specific Features
8890

ct2rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ whisper = [
7575
"all-tokenizers",
7676
]
7777
hub = ["dep:hf-hub"]
78+
system = ["intel-onemkl-prebuild?/system", "openblas-src?/system"]
7879

7980
# Features to select backends.
8081
cuda = []

ct2rs/build.rs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ enum Os {
3131
Unknown,
3232
}
3333

34-
fn main() {
35-
println!("cargo:rerun-if-changed=build.rs");
36-
println!("cargo:rerun-if-changed=src/sys");
37-
println!("cargo:rerun-if-changed=include");
38-
println!("cargo:rerun-if-changed=CTranslate2");
39-
add_search_paths("LIBRARY_PATH");
40-
println!("cargo:rerun-if-env-changed=CMAKE_INCLUDE_PATH");
41-
add_search_paths("CMAKE_LIBRARY_PATH");
42-
34+
fn build_ctranslate2() {
4335
let mut cmake = Config::new("CTranslate2");
4436
match env::var("CMAKE_PARALLEL") {
4537
Ok(job_n) => {
@@ -198,6 +190,57 @@ fn main() {
198190

199191
let ctranslate2 = cmake.build();
200192
link_libraries(ctranslate2.join("build"));
193+
}
194+
195+
fn link_system_libraries() {
196+
println!("cargo:rustc-link-lib=ctranslate2");
197+
if cfg!(target_arch = "x86_64") {
198+
println!("cargo:rustc-link-lib=cpu_features");
199+
}
200+
if cfg!(feature = "cuda") {
201+
println!("cargo:rustc-link-lib=static=cudart_static");
202+
}
203+
if cfg!(feature = "cudnn") {
204+
println!("cargo:rustc-link-lib=cudnn");
205+
}
206+
if !cfg!(feature = "cuda-dynamic-loading") {
207+
if cfg!(target_os = "windows") {
208+
println!("cargo:rustc-link-lib=static=cublas");
209+
println!("cargo:rustc-link-lib=static=cublasLt");
210+
}else{
211+
println!("cargo:rustc-link-lib=static=cublas_static");
212+
println!("cargo:rustc-link-lib=static=cublasLt_static");
213+
println!("cargo:rustc-link-lib=static=culibos");
214+
}
215+
}
216+
if cfg!(feature = "accelerate") {
217+
println!("cargo:rustc-link-lib=framework=Accelerate");
218+
}
219+
if cfg!(feature = "dnnl") {
220+
println!("cargo:rustc-link-lib=dnnl");
221+
}
222+
if cfg!(feature = "openmp-runtime-comp") {
223+
println!("cargo:rustc-link-lib=gomp");
224+
}
225+
if cfg!(feature = "openmp-runtime-intel") {
226+
println!("cargo:rustc-link-lib=iomp5");
227+
}
228+
}
229+
230+
fn main() {
231+
println!("cargo:rerun-if-changed=build.rs");
232+
println!("cargo:rerun-if-changed=src/sys");
233+
println!("cargo:rerun-if-changed=include");
234+
println!("cargo:rerun-if-changed=CTranslate2");
235+
add_search_paths("LIBRARY_PATH");
236+
println!("cargo:rerun-if-env-changed=CMAKE_INCLUDE_PATH");
237+
add_search_paths("CMAKE_LIBRARY_PATH");
238+
239+
if cfg!(feature = "system") {
240+
link_system_libraries();
241+
} else {
242+
build_ctranslate2()
243+
}
201244

202245
cxx_build::bridges([
203246
"src/sys/types.rs",

0 commit comments

Comments
 (0)