Skip to content

Commit 857d675

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 857d675

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-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: 88 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,93 @@ 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!(all(
207+
not(feature = "cuda-dynamic-loading"),
208+
not(target_os = "macos")
209+
)) {
210+
if cfg!(target_os = "windows") {
211+
println!("cargo:rustc-link-lib=static=cublas");
212+
println!("cargo:rustc-link-lib=static=cublasLt");
213+
} else {
214+
println!("cargo:rustc-link-lib=static=cublas_static");
215+
println!("cargo:rustc-link-lib=static=cublasLt_static");
216+
println!("cargo:rustc-link-lib=static=culibos");
217+
}
218+
}
219+
if cfg!(feature = "accelerate") {
220+
println!("cargo:rustc-link-lib=framework=Accelerate");
221+
}
222+
if cfg!(feature = "ruy") {
223+
println!("cargo:rustc-link-lib=ruy_allocator");
224+
println!("cargo:rustc-link-lib=ruy_apply_multiplier");
225+
println!("cargo:rustc-link-lib=ruy_block_map");
226+
println!("cargo:rustc-link-lib=ruy_blocking_counter");
227+
println!("cargo:rustc-link-lib=ruy_context");
228+
println!("cargo:rustc-link-lib=ruy_context_get_ctx");
229+
println!("cargo:rustc-link-lib=ruy_cpuinfo");
230+
println!("cargo:rustc-link-lib=ruy_ctx");
231+
println!("cargo:rustc-link-lib=ruy_denormal");
232+
println!("cargo:rustc-link-lib=ruy_frontend");
233+
println!("cargo:rustc-link-lib=ruy_have_built_path_for_avx");
234+
println!("cargo:rustc-link-lib=ruy_have_built_path_for_avx2_fma");
235+
println!("cargo:rustc-link-lib=ruy_have_built_path_for_avx512");
236+
println!("cargo:rustc-link-lib=ruy_kernel_arm");
237+
println!("cargo:rustc-link-lib=ruy_kernel_avx");
238+
println!("cargo:rustc-link-lib=ruy_kernel_avx2_fma");
239+
println!("cargo:rustc-link-lib=ruy_kernel_avx512");
240+
println!("cargo:rustc-link-lib=ruy_pack_arm");
241+
println!("cargo:rustc-link-lib=ruy_pack_avx");
242+
println!("cargo:rustc-link-lib=ruy_pack_avx2_fma");
243+
println!("cargo:rustc-link-lib=ruy_pack_avx512");
244+
println!("cargo:rustc-link-lib=ruy_prepacked_cache");
245+
println!("cargo:rustc-link-lib=ruy_prepare_packed_matrices");
246+
println!("cargo:rustc-link-lib=ruy_profiler_instrumentation");
247+
println!("cargo:rustc-link-lib=ruy_system_aligned_alloc");
248+
println!("cargo:rustc-link-lib=ruy_thread_pool");
249+
println!("cargo:rustc-link-lib=ruy_trmul");
250+
println!("cargo:rustc-link-lib=ruy_tune");
251+
println!("cargo:rustc-link-lib=ruy_wait");
252+
println!("cargo:rustc-link-lib=cpuinfo");
253+
println!("cargo:rustc-link-lib=clog");
254+
}
255+
if cfg!(feature = "dnnl") {
256+
println!("cargo:rustc-link-lib=dnnl");
257+
}
258+
if cfg!(feature = "openmp-runtime-comp") {
259+
println!("cargo:rustc-link-lib=gomp");
260+
}
261+
if cfg!(feature = "openmp-runtime-intel") {
262+
println!("cargo:rustc-link-lib=iomp5");
263+
}
264+
}
265+
266+
fn main() {
267+
println!("cargo:rerun-if-changed=build.rs");
268+
println!("cargo:rerun-if-changed=src/sys");
269+
println!("cargo:rerun-if-changed=include");
270+
println!("cargo:rerun-if-changed=CTranslate2");
271+
add_search_paths("LIBRARY_PATH");
272+
println!("cargo:rerun-if-env-changed=CMAKE_INCLUDE_PATH");
273+
add_search_paths("CMAKE_LIBRARY_PATH");
274+
275+
if cfg!(feature = "system") {
276+
link_system_libraries();
277+
} else {
278+
build_ctranslate2()
279+
}
201280

202281
cxx_build::bridges([
203282
"src/sys/types.rs",

0 commit comments

Comments
 (0)