Skip to content

Commit f9ec116

Browse files
authored
Merge pull request #108 from jkawamoto/system
Add `system` feature for external CTranslate2 library usage
2 parents 939d6d3 + 19adde3 commit f9ec116

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-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-platform/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ features = ["openmp-runtime-comp", "openblas", "ruy"]
4444
default = ["all-tokenizers", "cuda-small-binary"]
4545
whisper = ["ct2rs/whisper"]
4646
hub = ["ct2rs/hub"]
47+
system = ["ct2rs/system"]
4748

4849
# Features to select backends.
4950
mkl = ["ct2rs/mkl"]

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: 96 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,101 @@ 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=cpuinfo");
224+
println!("cargo:rustc-link-lib=clog");
225+
println!("cargo:rustc-link-lib=ruy_allocator");
226+
println!("cargo:rustc-link-lib=ruy_apply_multiplier");
227+
println!("cargo:rustc-link-lib=ruy_block_map");
228+
println!("cargo:rustc-link-lib=ruy_blocking_counter");
229+
println!("cargo:rustc-link-lib=ruy_context");
230+
println!("cargo:rustc-link-lib=ruy_context_get_ctx");
231+
println!("cargo:rustc-link-lib=ruy_cpuinfo");
232+
println!("cargo:rustc-link-lib=ruy_ctx");
233+
println!("cargo:rustc-link-lib=ruy_denormal");
234+
println!("cargo:rustc-link-lib=ruy_frontend");
235+
println!("cargo:rustc-link-lib=ruy_prepacked_cache");
236+
println!("cargo:rustc-link-lib=ruy_prepare_packed_matrices");
237+
println!("cargo:rustc-link-lib=ruy_profiler_instrumentation");
238+
println!("cargo:rustc-link-lib=ruy_system_aligned_alloc");
239+
println!("cargo:rustc-link-lib=ruy_thread_pool");
240+
println!("cargo:rustc-link-lib=ruy_trmul");
241+
println!("cargo:rustc-link-lib=ruy_tune");
242+
println!("cargo:rustc-link-lib=ruy_wait");
243+
if cfg!(any(target_arch = "arm", target_arch = "aarch64")) {
244+
println!("cargo:rustc-link-lib=ruy_kernel_arm");
245+
println!("cargo:rustc-link-lib=ruy_pack_arm");
246+
}
247+
if cfg!(target_feature = "avx") {
248+
println!("cargo:rustc-link-lib=ruy_have_built_path_for_avx");
249+
println!("cargo:rustc-link-lib=ruy_kernel_avx");
250+
println!("cargo:rustc-link-lib=ruy_pack_avx");
251+
}
252+
if cfg!(target_feature = "avx2") {
253+
println!("cargo:rustc-link-lib=ruy_have_built_path_for_avx2_fma");
254+
println!("cargo:rustc-link-lib=ruy_kernel_avx2_fma");
255+
println!("cargo:rustc-link-lib=ruy_pack_avx2_fma");
256+
}
257+
if cfg!(target_feature = "avx512f") {
258+
println!("cargo:rustc-link-lib=ruy_have_built_path_for_avx512");
259+
println!("cargo:rustc-link-lib=ruy_kernel_avx512");
260+
println!("cargo:rustc-link-lib=ruy_pack_avx512");
261+
}
262+
}
263+
if cfg!(feature = "dnnl") {
264+
println!("cargo:rustc-link-lib=dnnl");
265+
}
266+
if cfg!(feature = "openmp-runtime-comp") {
267+
println!("cargo:rustc-link-lib=gomp");
268+
}
269+
if cfg!(feature = "openmp-runtime-intel") {
270+
println!("cargo:rustc-link-lib=iomp5");
271+
}
272+
}
273+
274+
fn main() {
275+
println!("cargo:rerun-if-changed=build.rs");
276+
println!("cargo:rerun-if-changed=src/sys");
277+
println!("cargo:rerun-if-changed=include");
278+
println!("cargo:rerun-if-changed=CTranslate2");
279+
add_search_paths("LIBRARY_PATH");
280+
println!("cargo:rerun-if-env-changed=CMAKE_INCLUDE_PATH");
281+
add_search_paths("CMAKE_LIBRARY_PATH");
282+
283+
if cfg!(feature = "system") {
284+
link_system_libraries();
285+
} else {
286+
build_ctranslate2()
287+
}
201288

202289
cxx_build::bridges([
203290
"src/sys/types.rs",

0 commit comments

Comments
 (0)