如何从cython安装文件中删除-pthread编译器标志 [英] How to remove -pthread compiler flag from cython setup file

查看:139
本文介绍了如何从cython安装文件中删除-pthread编译器标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在linux环境中,当我为cython运行安装脚本时,会得到

In linux environment, when I run the setup script for cython, I get

gcc -pthread -B /apps/.../compiler_compat -Wl,-sysroot=/ -Wsign-compare 
-DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/ap......  

对于我的情况,我想删除pthread选项。如何通过cython设置文件来做到这一点?我看到有一些选项可以添加编译器标志,而没有一个可以删除。我的设置文件:

for my case, I want to remove the pthread option. How do I do that thru the cython setup file? I see there are options to add compiler flags but none to remove. My setup file:

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所指出的那样,许多选项中还有一个否定/覆盖它的选项,命令行上的最后一个选项 wins。因此,例如,添加额外的编译选项 -Os 会否决默认设置 -O2 - O3 ,因为在命令行中 -Os 会在 -O2 之后c $ c> -fwrapv / -fno-wrapv 这样的货币对的另一个示例)。

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).

但是, -pthread 没有这样的伙伴,并且禁用它的唯一机会是完全防止其出现在命令行中。做到这一点的方法有些怪异,但这不是我们所有人都使用python的原因吗?

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 使用 distutils.sysconfig 查找正确的编译/链接标志。一种可能性是修改其功能,以便过滤掉 -pthread

distutils uses distutils.sysconfig to find the right compile/link flags. One possibility would be modify its functionality, so that -pthread is filtered out.

我选择 get_config_vars ,但是当然还有其他选择。计划很简单:

I choose get_config_vars, but there are also other options of course. The plan is simple:


  1. 包装 distutils.sysconfig.get_config_vars ,这样 -pthread 被过滤掉

  2. 用包装器替换 distutils.sysconfig.get_config_vars

  3. 否则, setup.py 不变

  1. wrap distutils.sysconfig.get_config_vars, so that -pthread is filtered out
  2. replace distutils.sysconfig.get_config_vars with the wrapper
  3. otherwise, setup.py is unchanged

这会导致以下 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 的情况下进行构建并将结果模块加载到使用<$构建的python解释器中是一个好主意。 c $ c> -pthread -不确定它不会以某些微妙的方式破裂并按预期工作。


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.

这篇关于如何从cython安装文件中删除-pthread编译器标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆