如何从 cython 安装文件中删除 -pthread 编译器标志
How to remove -pthread compiler flag from cython setup file
在 linux 环境中,当我 运行 cython 的设置脚本时,我得到
gcc -pthread -B /apps/.../compiler_compat -Wl,-sysroot=/ -Wsign-compare
-DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/ap......
对于我的情况,我想删除 pthread 选项。我如何通过 cython 安装文件做到这一点?我看到有选项可以添加编译器标志,但 none 可以删除。我的设置文件:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
extensions = [Extension("foo",sources = ["foo.pyx"])]
setup(ext_modules = cythonize(extensions))
正如@DavidW 指出的那样,对于许多选项,还有一个选项 negates/overrides 它和命令行上的最后一个选项 "wins"。因此,例如添加额外的编译选项 -Os
将否决默认设置 -O2
或 -O3
,因为 -Os
会在命令行上的 -O2
之后出现(-fwrapv
/-fno-wrapv
此类对的另一个示例)。
然而,-pthread
没有这样的 "partner" 并且禁用它的唯一机会是完全阻止它出现在命令行中。这样做的方法有点 hacky,但这种 hackiness 不是我们都使用 python 的原因吗?
distutils
使用 distutils.sysconfig
找到正确的 compile/link 标志。一种可能性是修改其功能,以便过滤掉 -pthread
。
我选择get_config_vars
,当然也有其他的选择。计划很简单:
- 包裹
distutils.sysconfig.get_config_vars
,这样-pthread
就被过滤掉了
- 用包装器替换
distutils.sysconfig.get_config_vars
- 否则
setup.py
不变
这导致以下 setup.py
:
# manipulate get_config_vars:
# 1. step: wrap functionality and filter
from distutils.sysconfig import get_config_vars as default_get_config_vars
def remove_pthread(x):
if type(x) is str:
# x.replace(" -pthread ") would be probably enough...
# but we want to make sure we make it right for every input
if x=="-pthread":
return ""
if x.startswith("-pthread "):
return remove_pthread(x[len("-pthread "):])
if x.endswith(" -pthread"):
return remove_pthread(x[:-len(" -pthread")])
return x.replace(" -pthread ", " ")
return x
def my_get_config_vars(*args):
result = default_get_config_vars(*args)
# sometimes result is a list and sometimes a dict:
if type(result) is list:
return [remove_pthread(x) for x in result]
elif type(result) is dict:
return {k : remove_pthread(x) for k,x in result.items()}
else:
raise Exception("cannot handle type"+type(result))
# 2.step: replace
import distutils.sysconfig as dsc
dsc.get_config_vars = my_get_config_vars
# 3.step: normal setup.py
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
extensions = [Extension("foo",sources = ["foo.pyx"])]
setup(ext_modules = cythonize(extensions))
我不完全确定,在没有 -pthread
的情况下构建并将生成的模块加载到使用 -pthread
构建的 python-interpreter 中是一个好主意 - 不确定它不会以某些微妙的方式中断并按预期工作。
在 linux 环境中,当我 运行 cython 的设置脚本时,我得到
gcc -pthread -B /apps/.../compiler_compat -Wl,-sysroot=/ -Wsign-compare
-DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/ap......
对于我的情况,我想删除 pthread 选项。我如何通过 cython 安装文件做到这一点?我看到有选项可以添加编译器标志,但 none 可以删除。我的设置文件:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
extensions = [Extension("foo",sources = ["foo.pyx"])]
setup(ext_modules = cythonize(extensions))
正如@DavidW 指出的那样,对于许多选项,还有一个选项 negates/overrides 它和命令行上的最后一个选项 "wins"。因此,例如添加额外的编译选项 -Os
将否决默认设置 -O2
或 -O3
,因为 -Os
会在命令行上的 -O2
之后出现(-fwrapv
/-fno-wrapv
此类对的另一个示例)。
然而,-pthread
没有这样的 "partner" 并且禁用它的唯一机会是完全阻止它出现在命令行中。这样做的方法有点 hacky,但这种 hackiness 不是我们都使用 python 的原因吗?
distutils
使用 distutils.sysconfig
找到正确的 compile/link 标志。一种可能性是修改其功能,以便过滤掉 -pthread
。
我选择get_config_vars
,当然也有其他的选择。计划很简单:
- 包裹
distutils.sysconfig.get_config_vars
,这样-pthread
就被过滤掉了 - 用包装器替换
distutils.sysconfig.get_config_vars
- 否则
setup.py
不变
这导致以下 setup.py
:
# manipulate get_config_vars:
# 1. step: wrap functionality and filter
from distutils.sysconfig import get_config_vars as default_get_config_vars
def remove_pthread(x):
if type(x) is str:
# x.replace(" -pthread ") would be probably enough...
# but we want to make sure we make it right for every input
if x=="-pthread":
return ""
if x.startswith("-pthread "):
return remove_pthread(x[len("-pthread "):])
if x.endswith(" -pthread"):
return remove_pthread(x[:-len(" -pthread")])
return x.replace(" -pthread ", " ")
return x
def my_get_config_vars(*args):
result = default_get_config_vars(*args)
# sometimes result is a list and sometimes a dict:
if type(result) is list:
return [remove_pthread(x) for x in result]
elif type(result) is dict:
return {k : remove_pthread(x) for k,x in result.items()}
else:
raise Exception("cannot handle type"+type(result))
# 2.step: replace
import distutils.sysconfig as dsc
dsc.get_config_vars = my_get_config_vars
# 3.step: normal setup.py
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
extensions = [Extension("foo",sources = ["foo.pyx"])]
setup(ext_modules = cythonize(extensions))
我不完全确定,在没有 -pthread
的情况下构建并将生成的模块加载到使用 -pthread
构建的 python-interpreter 中是一个好主意 - 不确定它不会以某些微妙的方式中断并按预期工作。