From 0a8a791fad15ec0ab98c353917087078d35b964e Mon Sep 17 00:00:00 2001 From: Jocelyn Stericker Date: Sun, 28 May 2023 09:22:26 -0400 Subject: [PATCH] Use position independent code (-fPIC) This allows whipser-rs-sys to work when building a cdylib on x86 Linux in certain conditions. Without this change, linking can fail with: ``` = note: /usr/bin/ld: /.../target/debug/deps/libwhisper_rs_sys-d9be91f496c91a32.rlib(whisper.cpp.o): warning: relocation against `_ZTVNSt7__cxx1115basic_stringbufIcSt11char_traitsIcESaIcEEE@@GLIBCXX_3.4.21' in read-only section `.text.unlikely' /usr/bin/ld: /.../target/debug/deps/libwhisper_rs_sys-d9be91f496c91a32.rlib(whisper.cpp.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: bad value collect2: error: ld returned 1 exit status ``` Note that this is one solution. I believe another would be to use the [cmake crate](https://github.com/rust-lang/cmake-rs), which builds with `-fPIC` by default on Linux. --- sys/build.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sys/build.rs b/sys/build.rs index 98b0512..93d33ef 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -104,6 +104,8 @@ fn main() { #[cfg(feature = "opencl")] cmd.arg("-DWHISPER_CLBLAST=ON"); + cmd.arg("-DCMAKE_POSITION_INDEPENDENT_CODE=ON"); + let code = cmd .status() .expect("Failed to run `cmake` (is CMake installed?)");