|
21 | 21 | import shutil |
22 | 22 | import subprocess |
23 | 23 | import sys |
| 24 | +import sysconfig |
24 | 25 | from setuptools import Extension, setup |
25 | 26 | from setuptools.command.build_ext import build_ext as _build_ext |
26 | 27 |
|
@@ -60,6 +61,10 @@ def is_sentencepiece_installed(): |
60 | 61 | return False |
61 | 62 |
|
62 | 63 |
|
| 64 | +def is_gil_disabled(): |
| 65 | + return sysconfig.get_config_var('Py_GIL_DISABLED') |
| 66 | + |
| 67 | + |
63 | 68 | def get_cflags_and_libs(root): |
64 | 69 | cflags = ['-std=c++17', '-I' + os.path.join(root, 'include')] |
65 | 70 | libs = [] |
@@ -103,6 +108,8 @@ def build_extension(self, ext): |
103 | 108 | libs.append('-Wl,-strip-all') |
104 | 109 | if sys.platform == 'linux': |
105 | 110 | libs.append('-Wl,-Bsymbolic') |
| 111 | + if is_gil_disabled(): |
| 112 | + cflags.append('-DPy_GIL_DISABLED') |
106 | 113 | print('## cflags={}'.format(' '.join(cflags))) |
107 | 114 | print('## libs={}'.format(' '.join(libs))) |
108 | 115 | ext.extra_compile_args = cflags |
@@ -198,11 +205,17 @@ def get_win_arch(): |
198 | 205 | '8', |
199 | 206 | ]) |
200 | 207 | cflags = ['/std:c++17', '/I.\\build\\root\\include'] |
| 208 | + |
201 | 209 | libs = [ |
202 | 210 | '.\\build\\root\\lib\\sentencepiece.lib', |
203 | 211 | '.\\build\\root\\lib\\sentencepiece_train.lib', |
204 | 212 | ] |
205 | 213 |
|
| 214 | + # on Windows, GIL flag is not set automatically. |
| 215 | + # https://docs.python.org/3/howto/free-threading-python.html |
| 216 | + if is_gil_disabled(): |
| 217 | + cflags.append('/DPy_GIL_DISABLED') |
| 218 | + |
206 | 219 | SENTENCEPIECE_EXT = Extension( |
207 | 220 | 'sentencepiece._sentencepiece', |
208 | 221 | sources=['src/sentencepiece/sentencepiece_wrap.cxx'], |
|
0 commit comments