build_ext.py 4.93 KB
Newer Older
1 2 3 4 5 6 7 8 9
'''
Created on 13 fevr. 2014

@author: coissac
'''

from distutils import log
import os

10
from distutils import sysconfig
11 12
from distutils.errors import DistutilsSetupError

13 14 15 16 17 18 19 20 21 22 23 24
def _customize_compiler(compiler):
    compilername = compiler.compiler[0]
    if ("gcc" in compilername or "g++" in compilername):
        cc_cmd  = ' '.join(compiler.compiler + ['-fopenmp'])
        ccshared= ' '.join(x for x in sysconfig.get_config_vars("ccshared") if x is not None)
            
        compiler.set_executables(
            compiler=cc_cmd,
            compiler_so=cc_cmd + ' ' + ccshared
            )


25 26 27 28
try:
    from Cython.Distutils import build_ext  as ori_build_ext  # @UnresolvedImport
    from Cython.Compiler import Options as cython_options  # @UnresolvedImport
    class build_ext(ori_build_ext):
29
    
30
        
31
        def modifyDocScripts(self):
32 33 34 35
            try:
                os.mkdir("doc/sphinx")
            except:
                pass
36 37 38 39 40 41 42 43
            build_dir_file=open("doc/sphinx/build_dir.txt","w")
            print(self.build_lib,file=build_dir_file)
            build_dir_file.close()
            
        def initialize_options(self):
            ori_build_ext.initialize_options(self)  # @UndefinedVariable
            self.littlebigman = None
            self.built_files = None
44 45
    
        
46
        def finalize_options(self):
47 48
            super(build_ext, self).finalize_options()
            
49 50 51
    
            self.set_undefined_options('littlebigman',
                                       ('littlebigman',  'littlebigman'))
52
            
53 54
            self.set_undefined_options('build_files',
                                       ('files',  'built_files'))
55
    
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            self.cython_c_in_temp = 1
                       
            if self.littlebigman =='-DLITTLE_END':
                if self.define is None:
                    self.define=[('LITTLE_END',None)]
                else:
                    self.define.append('LITTLE_END',None)
            
        def substitute_sources(self,exe_name,sources):
            """
            Substitutes source file name starting by an @ by the actual
            name of the built file (see --> build_files)
            """
            sources = list(sources)
            for i in range(len(sources)):
                message = "%s :-> %s" % (exe_name,sources[i])
                if sources[i][0]=='@':
                    try:
                        filename = self.built_files[sources[i][1:]]
                    except KeyError:
                        tmpfilename = os.path.join(self.build_temp,sources[i][1:])
                        if os.path.isfile       (tmpfilename):
                            filename = tmpfilename
                        else:
                            raise DistutilsSetupError(
                                 'The %s filename declared in the source '
                                 'files of the program %s have not been '
                                 'built by the installation process' % (sources[i],
                                                                         exe_name))
                    sources[i]=filename
                    log.info("%s changed to %s",message,filename)
                else:
                    log.info("%s ok",message)
    
            return sources
    
        def build_extensions(self):
            # First, sanity-check the 'extensions' list
            
            for ext in self.extensions:
                ext.sources = self.substitute_sources(ext.name,ext.sources)
                
            self.check_extensions_list(self.extensions)
99 100 101 102 103
            
            print("pouic")
            print(ext.sources)
            print("pouac")
            
104 105 106 107 108
            for ext in self.extensions:
                log.info("%s :-> %s",ext.name,ext.sources)
                ext.sources = self.cython_sources(ext.sources, ext)
                self.build_extension(ext)
    
109
        def build_extensions(self):  # TODO what?? double? is it supposed to be build_extension?
110 111 112 113 114 115 116
            if hasattr(self, 'compiler'):
                _customize_compiler(self.compiler)
            if hasattr(self, 'shlib_compiler'):
                _customize_compiler(self.shlib_compiler)
            
            ori_build_ext.build_extensions(self)

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            
        def run(self):
            self.modifyDocScripts()
    
            for cmd_name in self.get_sub_commands():
                self.run_command(cmd_name)
    
            cython_options.annotate = True
            ori_build_ext.run(self)  # @UndefinedVariable
            
    
        def has_files(self):
            return self.distribution.has_files()
    
        def has_executables(self):
            return self.distribution.has_executables()
133
        
134 135
        sub_commands = [('build_files',has_files),
                        ('build_cexe', has_executables)
136
                        ] + ori_build_ext.sub_commands 
137 138 139 140

except ImportError:
    from distutils.command import build_ext  # @UnusedImport
           
141 142 143