2009-04-07 5 views
26

たとえば、私はpython setup.py build --compiler=msvcまたはpython setup.py build --compiler=mingw32またはちょうどpython setup.py buildを使用することができます。その場合、デフォルトのコンパイラ(たとえば、bcpp)が使用されます。私のsetup.pyの中でどのようにコンパイラ名を得ることができますか?(例えば、msvcmingw32bcppPythonのdistutilsは、どのようにコンパイラを使用する予定ですか?

UPD:デフォルトのコンパイラは必要ありません。が実際にはになります。これは必ずしもデフォルトのコンパイラではありません。これまでのところ、sys.argvを解析して--compiler...という文字列があるかどうかを確認するよりも良い方法は見つかりませんでした。

+0

私は同じ問題があります。私はmsvcのための余分な 'stdint.h'ヘッダをインクルードしたいが、他のコンパイラはインクルードしたくない。 –

答えて

0

輸入distutils.ccompiler

compiler_name = distutils.ccompiler.get_default_compiler()

+3

これは必ずしもdistutilsが使用するコンパイラではありません! – Jon

7

あなたはdistutils.command.build_ext.build_extコマンドをサブクラス化することができます。 build_ext.finalize_options()メソッドが呼び出された

たら、コンパイラ型は文字列としてself.compiler.compiler_typeに保存されている(などbuild_extに渡されたものと同じの--compilerオプション、例えば 『MINGW32』、 『GCC』、...) 。

+0

コンパイラオブジェクトは 'self.compiler'です。タイプは' self.compiler.compiler_type'にあります –

+0

編集ありがとう! –

1
 
#This should work pretty good 
def compilerName(): 
    import re 
    import distutils.ccompiler 
    comp = distutils.ccompiler.get_default_compiler() 
    getnext = False 

    for a in sys.argv[2:]: 
    if getnext: 
     comp = a 
     getnext = False 
     continue 
    #separated by space 
    if a == '--compiler' or re.search('^-[a-z]*c$', a): 
     getnext = True 
     continue 
    #without space 
    m = re.search('^--compiler=(.+)', a) 
    if m == None: 
     m = re.search('^-[a-z]*c(.+)', a) 
    if m: 
     comp = m.group(1) 

    return comp 


print "Using compiler " + '"' + compilerName() + '"' 
23

これは、Windows上でmingwのとMSVCの両方を使用してコンパイルするOpenMPの拡張子を取得するために私のために働いたLuperルーシュの答えの拡張バージョンです。 build_extをサブクラス化した後、cmdclass argのsetup.pyに渡す必要があります。 finalize_optionsの代わりにbuild_extensionsをサブクラス化すると、実際のコンパイラオブジェクトを調べることができるので、より詳細なバージョン情報を得ることができます。あなた結局ごとのコンパイラ、あたり拡張ベースでコンパイラフラグを設定できます

from distutils.core import setup, Extension 
from distutils.command.build_ext import build_ext 
copt = {'msvc': ['/openmp', '/Ox', '/fp:fast','/favor:INTEL64','/Og'] , 
    'mingw32' : ['-fopenmp','-O3','-ffast-math','-march=native']  } 
lopt = {'mingw32' : ['-fopenmp'] } 

class build_ext_subclass(build_ext): 
    def build_extensions(self): 
     c = self.compiler.compiler_type 
     if copt.has_key(c): 
      for e in self.extensions: 
       e.extra_compile_args = copt[ c ] 
     if lopt.has_key(c): 
      for e in self.extensions: 
       e.extra_link_args = lopt[ c ] 
     build_ext.build_extensions(self) 

mod = Extension('_wripaca', 
      sources=['../wripaca_wrap.c', 
        '../../src/wripaca.c'], 
      include_dirs=['../../include'] 
      ) 

setup (name = 'wripaca', 
    ext_modules = [mod], 
    py_modules = ["wripaca"], 
    cmdclass = {'build_ext': build_ext_subclass }) 
+0

gcc/clangがlinux/posixの場合、loptのkeynameは 'unix'でなければならないことに注意してください。 – marscher

+0

ありがとう。情報は本当にそれよりもアクセスしやすいはずです。私たちはdistutilsにバグを報告するべきです –

0
import sys 
sys.argv.extend(['--compiler', 'msvc']) 
0
class BuildWithDLLs(build): 

    # On Windows, we install the git2.dll too. 
    def _get_dlls(self): 
     # return a list of of (FQ-in-name, relative-out-name) tuples. 
     ret = [] 
     bld_ext = self.distribution.get_command_obj('build_ext') 
     compiler_type = bld_ext.compiler.compiler_type 

あなたがにbuild_extインスタンスを取得するためにself.distribution.get_command_obj(「にbuild_ext」)を使用することができ、 とコンパイラタイプを取得

関連する問題