sentencepiece icon indicating copy to clipboard operation
sentencepiece copied to clipboard

Fix static linking support for non-Windows platforms

Open Ryaningli opened this issue 2 months ago • 0 comments

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-static feature for all compiler types
  • Setting SPM_ENABLE_SHARED=OFF and SPM_BUILD_SHARED_LIBS=OFF for 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-static detection 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".

Ryaningli avatar Oct 23 '25 06:10 Ryaningli