How To Remove -pthread Compiler Flag From Cython Setup File
Solution 1:
As @DavidW has pointed out, for many options there is also an option which negates/overrides it and the last option on the command line "wins". So for example adding extra compile option -Os
would overrule the default setting -O2
or -O3
, because -Os
would come after -O2
on the command line (-fwrapv
/-fno-wrapv
another example for such pairs).
However, -pthread
has no such "partner" and the only chance to disable it, is to prevent its appearance on the command line altogether. The ways to do it are somewhat hacky, but is this hackiness not the reason we all use python?
distutils
uses distutils.sysconfig
to find the right compile/link flags. One possibility would be modify its functionality, so that -pthread
is filtered out.
I choose get_config_vars
, but there are also other options of course. The plan is simple:
- wrap
distutils.sysconfig.get_config_vars
, so that-pthread
is filtered out - replace
distutils.sysconfig.get_config_vars
with the wrapper - otherwise,
setup.py
is unchanged
Which leads to the following setup.py
:
# manipulate get_config_vars:# 1. step: wrap functionality and filterfrom distutils.sysconfig import get_config_vars as default_get_config_vars
defremove_pthread(x):
iftype(x) isstr:
# x.replace(" -pthread ") would be probably enough...# but we want to make sure we make it right for every inputif 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
defmy_get_config_vars(*args):
result = default_get_config_vars(*args)
# sometimes result is a list and sometimes a dict:iftype(result) islist:
return [remove_pthread(x) for x in result]
eliftype(result) isdict:
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.pyfrom 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))
I'm not entirely sure, it is a great idea to build without -pthread
and to load the resulting module in a python-interpreter which is built with -pthread
- not sure it will not break in some subtle ways and work as intended.
Post a Comment for "How To Remove -pthread Compiler Flag From Cython Setup File"