sentencepiece
sentencepiece copied to clipboard
Fix static linking support for non-Windows platforms
Problem
When using RUSTFLAGS="-C target-feature=+crt-static" for static linking on non-Windows platforms (Linux/macOS), the build fails with linker errors. The current build.rs only handles static linking configuration for MSVC compiler but completely ignores GCC/Clang compilers.
Solution
Extend static linking support to GCC/Clang compilers by:
- Detecting
crt-staticfeature for all compiler types - Setting
SPM_ENABLE_SHARED=OFFandSPM_BUILD_SHARED_LIBS=OFFfor GCC/Clang when static linking is requested - This prevents CMake from building shared libraries when Rust expects static linking
Changes
Modified sentencepiece-sys/build.rs to:
- Extract
crt-staticdetection into a reusable variable - Add GCC/Clang static linking configuration mirroring the existing MSVC logic
- Maintain backward compatibility with existing behavior
Testing
Verified that static linking now works correctly on Linux platforms with RUSTFLAGS="-C target-feature=+crt-static".