From: Felix Yan <felixonmars@archlinux.org>
Date: Tue, 29 Nov 2022 07:34:00 +0200
Subject: Allow building with system libstemmer

In distribution packaging, we generally prefer linking to system
libraries instead of bundling an unmodified copy. This PR adds support
for building with system libstemmer but still defaults to the previous
behavior to avoid breaking anything.

Origin: upstream, https://github.com/snowballstem/pystemmer/commit/2f52b4b2ff113fe6c33cebe14ed4fd4388bb1742
---
 setup.py | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/setup.py b/setup.py
index 018ecf3..5fcf1cc 100644
--- a/setup.py
+++ b/setup.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 from setuptools import setup, Command, Extension
-import os.path
+import os
 
 
 long_description = r"""
@@ -111,8 +111,22 @@ class LibrarySourceCode:
 
 
 LIBRARY_SOURCE_CODE = LibrarySourceCode()
-if not LIBRARY_SOURCE_CODE.is_present_on_disk():
-    LIBRARY_SOURCE_CODE.download()
+
+SYSTEM_LIBSTEMMER = os.environ.get('PYSTEMMER_SYSTEM_LIBSTEMMER', False)
+if SYSTEM_LIBSTEMMER:
+    C_EXTENSION = Extension(
+        'Stemmer',
+        ['src/Stemmer.pyx'],
+        libraries=['stemmer'],
+    )
+else:
+    if not LIBRARY_SOURCE_CODE.is_present_on_disk():
+        LIBRARY_SOURCE_CODE.download()
+    C_EXTENSION = Extension(
+        'Stemmer',
+        ['src/Stemmer.pyx'] + list(LIBRARY_SOURCE_CODE.source_code_paths()),
+        include_dirs=LIBRARY_SOURCE_CODE.include_directories
+    )
 
 
 class BootstrapCommand(Command):
@@ -194,11 +208,7 @@ setup(name='PyStemmer',
       ],
       setup_requires=['Cython>=0.28.5,<1.0', 'setuptools>=18.0'],
       ext_modules=[
-        Extension(
-            'Stemmer',
-            ['src/Stemmer.pyx'] + list(LIBRARY_SOURCE_CODE.source_code_paths()),
-            include_dirs=LIBRARY_SOURCE_CODE.include_directories
-        )
+          C_EXTENSION
       ],
       cmdclass={'bootstrap': BootstrapCommand}
       )
