【问题标题】:Tidying Up the Cython Build-Flags used by gcc整理 gcc 使用的 Cython 构建标志
【发布时间】:2018-12-03 18:25:27
【问题描述】:

我目前使用“setuptools”在 Linux 上使用 gcc 自动 cythonize 和编译我的 Cython 模块。从现在开始,我需要更多地控制提供给 gcc 的构建标志。如果我在setup.py 中使用以下内容:

cythonize(
    [Extension("*", ["project/*.pyx"])
    nthreads=4
)

我得到了构建标志,看起来像:

gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -fPIC -I./fastmat/core -I/home/seb/.local/lib/python3.6/site-packages/numpy/core/include -Iproject/core -Ifastmat/inspect -Iutil -I/usr/include/python3.6m -c project/BlockDiag.c -o build/temp.linux-x86_64-3.6/project/BlockDiag.o

在这里,我完全惊讶于几个构建标志多次出现并且没有以任何(对我来说显而易见的)方式发出的事实。

如何清理这些构建标志,使它们看起来像建议的here?我希望在此过程中学习一些关于 setuptools 的知识,以便最终完全控制构建过程,而无需使用自维护的 makefile。

【问题讨论】:

标签: python cython setuptools compiler-flags cythonize


【解决方案1】:

GCC 获得的标志来自环境变量之一。输入

$ python -c "from distutils import sysconfig;\
print(sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',\
'BASECFLAGS', 'LDFLAGS', 'CCSHARED', 'LDSHARED', 'SO'))"

打印它们。这是 distutils 默认使用的扩展编译。现在检查哪个环境变量引入了哪个标志并相应地覆盖环境变量,例如

$ CC="gcc-7.3.0" CFLAGS="-Ofast" python setup.py build_ext

使用特定的编译器版本并开启O3优化。

此外,您似乎使用的是 numpy.distutils 而不是原版的 distutils,因此请注意额外的包含/链接标志 numpy 在后台添加。

【讨论】:

  • 谢谢!我使用CC="gcc" CXX="g++" OPT="" CFLAGS="" BASECFLAGS="" LDFLAGS="" CCSHARED="" LDSHARED="gcc -shared" PY_CORE_CFLAGS="" PY_CFLAGS="" SO="" python setup.py build_ext --inplace 将其清理到gcc -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I./fastmat/core [...] -o build/temp.linux-x86_64-3.6/fastmat/BlockDiag.o。现在我仍然想知道剩余的标志来自哪里,因为我搜索了由sysconfig.get_config_vars() 返回的字典,例如包含-O3 的任何键并将其设置为"",但它仍然继续弹出。
  • 为了澄清起见,我只是链接了一些 numpy 库,但我使用的是标准的 distutils 库。
  • 如果您找不到在其中一个环境变量中设置的标志(供参考,here's the spot in distutils source where the flags are read from env),那么它们可能已被供应商或发行版维护者烘焙到 gcc 中(你可以检查gcc -dumpspecs )。如果你需要一个香草gcc,从源代码构建一个并通过CC var 传递自定义可执行文件。
猜你喜欢
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多