syl*_*sm_ 10 python distutils setuptools
我有一个Python C++扩展,在OSX上使用clang编译时需要以下编译标志:
CPPFLAGS='-std=c++11 -stdlib=libc++ -mmacosx-version-min=10.8'
LDFLAGS='-lc++'
Run Code Online (Sandbox Code Playgroud)
在我的setup.py中检测OSX很容易.我可以做这个:
if sys.prefix == 'darwin':
compile_args.append(['-mmacosx-version-min=10.8', '-stdlib=libc++'])
link_args.append('-lc++')
Run Code Online (Sandbox Code Playgroud)
(有关完整上下文,请参阅https://github.com/honnibal/spaCy/blob/ba1d3ddd7f527d2e6e41b86da0f2887cc4dec83a/setup.py#L70)
但是,在GCC上,此编译标志无效.因此,如果有人试图在OSX上使用GCC,如果我以这种方式编写setup.py,则编译将失败.
GCC和clang支持不同的编译器标志.所以,我需要知道将调用哪个编译器,所以我可以发送不同的标志.在setup.py中检测编译器的正确方法是什么?
编辑1:
请注意,编译错误不会引发Python异常:
$ python setup.py build_ext --inplace
running build_ext
building 'spacy.strings' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -c spacy/strings.cpp -o build/temp.linux-x86_64-2.7/spacy/strings.o -O3 -mmacosx-version-min=10.8 -stdlib=libc++
gcc: error: unrecognized command line option ‘-mmacosx-version-min=10.8’
gcc: error: unrecognized command line option ‘-stdlib=libc++’
error: command 'gcc' failed with exit status 1
$
Run Code Online (Sandbox Code Playgroud)
我偶然发现了你的问题,因为我需要同样类型的开关。此外,就我而言,sys.prefix这并不是很好,因为clang无论平台如何,这些标志都是适用的。
我不确定它是否完美,但这是最适合我的。因此,我检查CC变量是否已设置;如果没有,我会检查我猜想的地方distutils。
欢迎任何更好的解决方案!
import os
import distutils
try:
if os.environ['CC'] == "clang":
clang = True
except KeyError:
clang = False
if clang or distutils.sysconfig_get_config_vars()['CC'] == 'clang':
try:
_ = os.environ['CFLAGS']
except KeyError:
os.environ['CFLAGS'] = ""
os.environ['CFLAGS'] += " -Wno-unused-function"
os.environ['CFLAGS'] += " -Wno-int-conversion"
os.environ['CFLAGS'] += " -Wno-incompatible-pointer-types
Run Code Online (Sandbox Code Playgroud)
脾气暴躁的人请注意:我很想使用该extra_compile_args选项,但它将标志放在clang编译命令中的错误位置。
| 归档时间: |
|
| 查看次数: |
878 次 |
| 最近记录: |