2009-04-07 10 views
26

उदाहरण के लिए, मैं python setup.py build --compiler=msvc या python setup.py build --compiler=mingw32 या केवल python setup.py build का उपयोग कर सकता हूं, इस स्थिति में डिफ़ॉल्ट कंपाइलर (कहें, bcpp) का उपयोग किया जाएगा। मैं अपने setup.py (ई। जी। msvc, mingw32 और bcpp क्रमशः क्रमशः संकलक नाम कैसे प्राप्त कर सकता हूं)?पायथन डिस्ट्यूट, एक कंपाइलर कैसे प्राप्त किया जा रहा है?

युपीडी .: मैं डिफ़ॉल्ट संकलक की जरूरत नहीं है, मैं एक है कि वास्तव में प्रयोग की जाने वाली है, जो जरूरी डिफ़ॉल्ट से एक नहीं है के लिए जा रहा जरूरत है। अब तक को पार्स करने के लिए मुझे --compiler... स्ट्रिंग देखने के लिए बेहतर तरीका नहीं मिला है।

+0

मुझे एक ही समस्या है। मैं msvc के लिए अतिरिक्त 'stdint.h' शीर्षलेख शामिल करना चाहता हूं, लेकिन अन्य कंपाइलर नहीं। –

उत्तर

0

आयात distutils.ccompiler

compiler_name = distutils.ccompiler.get_default_compiler()

+3

यह आवश्यक नहीं है कि संकलक का उपयोग करने जा रहा है! – Jon

7

आप distutils.command.build_ext.build_ext आदेश उपवर्ग कर सकते हैं।

एक बार build_ext.finalize_options() विधि बुलाया गया है, संकलक प्रकार एक स्ट्रिंग के रूप self.compiler.compiler_type में संग्रहित है (एक build_ext के लिए पारित रूप में एक ही है --compiler विकल्प, जैसे 'mingw32', 'जीसीसी', आदि ...) ।

+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

यह वह जगह है Luper रॉच के जवाब यह है कि मेरे लिए काम किया खिड़कियों पर दोनों MinGW और MSVC का उपयोग कर संकलित करने के लिए एक OpenMP विस्तार पाने के लिए की एक विस्तारित संस्करण। Build_ext subclassing के बाद आपको 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

यह ध्यान दिया जाना चाहिए कि लिनक्स/पॉज़िक्स के तहत जीसीसी/क्लैंग के लिए लूप के लिए मुख्य नाम 'यूनिक्स' होना चाहिए – marscher

+0

धन्यवाद। जानकारी वास्तव में उससे अधिक सुलभ होना चाहिए। हमें परेशानियों के लिए एक बग की रिपोर्ट करनी चाहिए –

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 

आप self.distribution.get_command_obj ('build_ext') का उपयोग कर सकते हैं, उदाहरण के build_ext पाने के लिए और फिर compiler_type

संबंधित मुद्दे