Exemple #1
0
 def __init__(self, name, sources,
              pypp_file, binding_file,
              include_dirs=None,
              define_macros=None,
              undef_macros=None,
              library_dirs=None,
              libraries=None,
              runtime_library_dirs=None,
              extra_objects=None,
              extra_compile_args=None,
              extra_link_args=None,
              export_symbols=None,
              swig_opts=None,
              depends=None,
              language=None,
              f2py_options=None,
              module_dirs=None,):
     old_extension.Extension.__init__(self, name, sources,
                                  include_dirs, define_macros, undef_macros,
                                  util.convert_ulist(library_dirs),
                                  util.convert_ulist(libraries),
                                  util.convert_ulist(runtime_library_dirs),
                                  extra_objects,
                                  extra_compile_args, extra_link_args,
                                  export_symbols, swig_opts, depends,
                                  language, f2py_options, module_dirs,)
     self.pyppdef = pypp_file
     self.binding_file = binding_file
     self.builder = ''
Exemple #2
0
 def __init__(self, testfile, extra_sources=[],
              include_dirs=None,
              define_macros=None,
              undef_macros=None,
              library_dirs=None,
              libraries=None,
              runtime_library_dirs=None,
              extra_objects=None,
              extra_compile_args=None,
              extra_link_args=None,
              export_symbols=None,
              swig_opts=None,
              depends=None,
              language=None,
              f2py_options=None,
              module_dirs=None,):
     name = os.path.splitext(testfile)[0]
     sources = [testfile] + extra_sources
     Executable.__init__(self, name, sources,
                         include_dirs, define_macros, undef_macros,
                         util.convert_ulist(library_dirs),
                         util.convert_ulist(libraries),
                         util.convert_ulist(runtime_library_dirs),
                         extra_objects,
                         extra_compile_args, extra_link_args,
                         export_symbols, swig_opts, depends,
                         language, f2py_options, module_dirs,)
Exemple #3
0
 def __init__(self, name, sources,
              include_dirs=None,
              define_macros=None,
              undef_macros=None,
              library_dirs=None,
              libraries=None,
              runtime_library_dirs=None,
              extra_objects=None,
              extra_compile_args=None,
              extra_link_args=None,
              export_symbols=None,
              swig_opts=None,
              depends=None,
              language=None,
              f2py_options=None,
              module_dirs=None,
              link_with_fcompiler=False,):
     self.config_fc = dict()
     self.source_languages = []
     self.link_with_fcompiler = link_with_fcompiler
     old_extension.Extension.__init__(self, name, sources,
                                  include_dirs, define_macros, undef_macros,
                                  util.convert_ulist(library_dirs),
                                  util.convert_ulist(libraries),
                                  util.convert_ulist(runtime_library_dirs),
                                  extra_objects,
                                  extra_compile_args, extra_link_args,
                                  export_symbols, swig_opts, depends,
                                  language, f2py_options, module_dirs,)
Exemple #4
0
    def run(self):
        if not self.executables:
            return

        # Make sure that library sources are complete.
        languages = []
        for exe in self.executables:
            if not all_strings(exe.sources):
                self.run_command('build_src')
            l = exe.language
            if l and l not in languages: languages.append(l)

        from distutils.ccompiler import new_compiler
        self.compiler = new_compiler(compiler=self.compiler,
                                     dry_run=self.dry_run,
                                     force=self.force)
        self.compiler.customize(self.distribution,
                                need_cxx=self.have_cxx_sources())

        self.compiler.customize_cmd(self)
        self.compiler.show_customization()

        if have_numpy and self.have_f_sources():
            from numpy.distutils.fcompiler import new_fcompiler
            self.fcompiler = new_fcompiler(compiler=self.fcompiler,
                                           verbose=self.verbose,
                                           dry_run=self.dry_run,
                                           force=self.force,
                                           requiref90='f90' in languages,
                                           c_compiler=self.compiler)
            if self.fcompiler is not None:
                self.fcompiler.customize(self.distribution)

                self.fcompiler.customize_cmd(self)
                self.fcompiler.show_customization()



        exes = []
        for exe in self.executables:
            libraries = exe.libraries or []
            library_dirs = exe.library_dirs or []
            runtime_library_dirs = exe.runtime_library_dirs or []
            extra_preargs = exe.extra_compile_args or []
            extra_postargs = exe.extra_link_args or []

            ## include libraries built by build_shlib and/or build_clib
            library_dirs.append(self.build_temp)

            ## Conditional recompile
            build_directory = self.build_clib
            target_path = os.path.join(build_directory, exe.name)
            recompile = False
            if not os.path.exists(target_path) or self.force:
                recompile = True
            else:
                for src in exe.sources:
                    if os.path.getmtime(target_path) < os.path.getmtime(src):
                        recompile = True
                        break
            if not recompile:
                return

            ########################################
            ## Copied from numpy.distutils.command.build_clib

            # default compilers
            compiler = self.compiler
            fcompiler = self.fcompiler

            sources = exe.sources
            if sources is None or not is_sequence(sources):
                raise DistutilsSetupError, \
                    ("in 'libraries' option (library '%s'), " +
                     "'sources' must be present and must be " +
                     "a list of source filenames") % exe.name
            sources = list(sources)

            c_sources, cxx_sources, f_sources, fmodule_sources \
                = filter_sources(sources)
            if not exe.language:
                exe.language = 'c'
            requiref90 = not not fmodule_sources or exe.language =='f90'

            # save source type information so that build_ext can use it.
            source_languages = []
            if c_sources: source_languages.append('c')
            if cxx_sources: source_languages.append('c++')
            if requiref90: source_languages.append('f90')
            elif f_sources: source_languages.append('f77')
            exe.source_languages = source_languages

            lib_file = compiler.library_filename(exe.name,
                                                 output_dir=build_directory)
            depends = sources + (exe.depends or [])
            if not (self.force or newer_group(depends, lib_file, 'newer')):
                log.debug("skipping '%s' library (up-to-date)", exe.name)
                return
            else:
                log.info("building '%s' library", exe.name)

            if have_numpy:
                config_fc = exe.config_fc or {}
                if fcompiler is not None and config_fc:
                    log.info('using additional config_fc from setup script '\
                                 'for fortran compiler: %s' \
                                 % (config_fc,))
                    from numpy.distutils.fcompiler import new_fcompiler
                    fcompiler = new_fcompiler(compiler=fcompiler.compiler_type,
                                              verbose=self.verbose,
                                              dry_run=self.dry_run,
                                              force=self.force,
                                              requiref90=requiref90,
                                              c_compiler=self.compiler)
                    if fcompiler is not None:
                        dist = self.distribution
                        base_config_fc = dist.get_option_dict('config_fc').copy()
                        base_config_fc.update(config_fc)
                        fcompiler.customize(base_config_fc)

                # check availability of Fortran compilers
                if (f_sources or fmodule_sources) and fcompiler is None:
                    raise DistutilsError, "library %s has Fortran sources"\
                        " but no Fortran compiler found" % (exe.name)

            macros = exe.define_macros
            include_dirs = exe.include_dirs
            if include_dirs is None:
                include_dirs = []

            if have_numpy:
                include_dirs.extend(get_numpy_include_dirs())
            # where compiled F90 module files are:
            module_dirs = exe.module_dirs or []
            module_build_dir = os.path.dirname(lib_file)
            if requiref90: self.mkpath(module_build_dir)

            if compiler.compiler_type=='msvc':
                # this hack works around the msvc compiler attributes
                # problem, msvc uses its own convention :(
                c_sources += cxx_sources
                cxx_sources = []

            objects = []
            if c_sources:
                log.info("compiling C sources")
                objects = compiler.compile(c_sources,
                                           output_dir=self.build_temp,
                                           macros=macros,
                                           include_dirs=include_dirs,
                                           debug=self.debug,
                                           extra_postargs=extra_postargs)

            if cxx_sources:
                log.info("compiling C++ sources")
                cxx_compiler = compiler.cxx_compiler()
                cxx_objects = cxx_compiler.compile(cxx_sources,
                                                   output_dir=self.build_temp,
                                                   macros=macros,
                                                   include_dirs=include_dirs,
                                                   debug=self.debug,
                                                   extra_postargs=extra_postargs)
                objects.extend(cxx_objects)

            if f_sources or fmodule_sources:
                extra_postargs = []
                f_objects = []

                if requiref90:
                    if fcompiler.module_dir_switch is None:
                        existing_modules = glob('*.mod')
                    extra_postargs += fcompiler.module_options(\
                        module_dirs,module_build_dir)

                if fmodule_sources:
                    log.info("compiling Fortran 90 module sources")
                    f_objects += fcompiler.compile(fmodule_sources,
                                                   output_dir=self.build_temp,
                                                   macros=macros,
                                                   include_dirs=include_dirs,
                                                   debug=self.debug,
                                                   extra_postargs=extra_postargs)

                if requiref90 and self.fcompiler.module_dir_switch is None:
                    # move new compiled F90 module files to module_build_dir
                    for f in glob('*.mod'):
                        if f in existing_modules:
                            continue
                        t = os.path.join(module_build_dir, f)
                        if os.path.abspath(f)==os.path.abspath(t):
                            continue
                        if os.path.isfile(t):
                            os.remove(t)
                        try:
                            self.move_file(f, module_build_dir)
                        except DistutilsFileError:
                            log.warn('failed to move %r to %r' \
                                         % (f, module_build_dir))

                if f_sources:
                    log.info("compiling Fortran sources")
                    f_objects += fcompiler.compile(f_sources,
                                                   output_dir=self.build_temp,
                                                   macros=macros,
                                                   include_dirs=include_dirs,
                                                   debug=self.debug,
                                                   extra_postargs=extra_postargs)
            else:
                f_objects = []

            objects.extend(f_objects)

            # assume that default linker is suitable for
            # linking Fortran object files
            ########################################

            if exe.link_with_fcompiler: # if using PROGRAM
                link_compiler = fcompiler
            else:
                link_compiler = compiler
            if cxx_sources:
                link_compiler = cxx_compiler

            ## May be dependent on other libs we're builing
            shlib_libraries = []
            for libinfo in exe.libraries:
                if isinstance(libinfo, basestring):
                    shlib_libraries.append(convert_ulist([libinfo])[0])
                else:
                    shlib_libraries.append(libinfo[0])

            if not hasattr(link_compiler, 'linker_exe') or \
                    link_compiler.linker_exe is None:
                link_compiler.linker_exe = [link_compiler.linker_so[0]]

            linker_args = dict(
                target_desc          = link_compiler.EXECUTABLE,
                objects              = objects,
                output_filename      = exe.name,
                output_dir           = build_directory,
                libraries            = shlib_libraries,
                library_dirs         = library_dirs,
                debug                = self.debug,
                extra_preargs        = extra_preargs,
                extra_postargs       = extra_postargs,
                )
            if not exe.link_with_fcompiler:
                linker_args['runtime_library_dirs'] = runtime_library_dirs

            ## Alternate ending
            link_compiler.link(**linker_args)
Exemple #5
0
    def build_a_library(self, build_info, lib_name, libraries):
        libraries = convert_ulist(build_info.get("libraries") or [])
        library_dirs = convert_ulist(build_info.get("library_dirs") or [])
        runtime_library_dirs = convert_ulist(build_info.get("runtime_library_dirs"))
        extra_preargs = build_info.get("extra_compiler_args") or []
        extra_postargs = build_info.get("extra_link_args") or []

        ## Conditional recompile
        target_library = self.compiler.library_filename(lib_name, lib_type="shared", output_dir="")
        target_path = os.path.join(self.build_clib, target_library)
        recompile = False
        if not os.path.exists(target_path) or self.force:
            recompile = True
        else:
            for src in build_info.get("sources"):
                if os.path.getmtime(target_path) < os.path.getmtime(src):
                    recompile = True
                    break
        if not recompile:
            return

        library_dirs += [self.build_clib]

        ########################################
        ## Copied verbatim from numpy.distutils.command.build_clib

        # default compilers
        compiler = self.compiler
        fcompiler = getattr(self, "_f_compiler", self.fcompiler)

        sources = build_info.get("sources")
        if sources is None or not is_sequence(sources):
            raise DistutilsSetupError, (
                "in 'libraries' option (library '%s'), "
                + "'sources' must be present and must be "
                + "a list of source filenames"
            ) % lib_name
        sources = list(sources)

        c_sources, cxx_sources, f_sources, fmodule_sources = filter_sources(sources)
        requiref90 = not not fmodule_sources or build_info.get("language", "c") == "f90"

        # save source type information so that build_ext can use it.
        source_languages = []
        if c_sources:
            source_languages.append("c")
        if cxx_sources:
            source_languages.append("c++")
        if requiref90:
            source_languages.append("f90")
        elif f_sources:
            source_languages.append("f77")

        specified = build_info.get("language", None)
        if specified:
            source_languages.append(specified)
            if specified == "c++":  ## force c++ compiler
                cxx_sources += c_sources
                c_sources = []
        build_info["source_languages"] = source_languages

        lib_file = compiler.library_filename(lib_name, lib_type="shared", output_dir=self.build_clib)
        depends = sources + build_info.get("depends", [])
        if not (self.force or newer_group(depends, lib_file, "newer")):
            log.debug("skipping '%s' library (up-to-date)", lib_name)
            return
        else:
            log.info("building '%s' library", lib_name)

        if have_numpy:
            config_fc = build_info.get("config_fc", {})
            if fcompiler is not None and config_fc:
                log.info("using additional config_fc from setup script " "for fortran compiler: %s" % (config_fc,))
                from numpy.distutils.fcompiler import new_fcompiler

                fcompiler = new_fcompiler(
                    compiler=fcompiler.compiler_type,
                    verbose=self.verbose,
                    dry_run=self.dry_run,
                    force=self.force,
                    requiref90=requiref90,
                    c_compiler=self.compiler,
                )
                if fcompiler is not None:
                    dist = self.distribution
                    base_config_fc = dist.get_option_dict("config_fc").copy()
                    base_config_fc.update(config_fc)
                    fcompiler.customize(base_config_fc)

            # check availability of Fortran compilers
            if (f_sources or fmodule_sources) and fcompiler is None:
                ver = "77"
                if requiref90:
                    ver = "90"
                raise DistutilsError, "library %s has Fortran%s sources" " but no Fortran compiler found" % (
                    lib_name,
                    ver,
                )
            """
            if fcompiler is not None:
                fcompiler.extra_f77_compile_args = build_info.get('extra_f77_compile_args') or []
                fcompiler.extra_f90_compile_args = build_info.get('extra_f90_compile_args') or []
                """

        macros = build_info.get("macros")
        include_dirs = build_info.get("include_dirs")
        if include_dirs is None:
            include_dirs = []
        extra_postargs = build_info.get("extra_compiler_args") or []

        if have_numpy:
            include_dirs.extend(get_numpy_include_dirs())
        # where compiled F90 module files are:
        module_dirs = build_info.get("module_dirs") or []
        module_build_dir = os.path.dirname(lib_file)
        if requiref90:
            self.mkpath(module_build_dir)

        if compiler.compiler_type == "msvc":
            # this hack works around the msvc compiler attributes
            # problem, msvc uses its own convention :(
            c_sources += cxx_sources
            cxx_sources = []

        objects = []
        if c_sources:
            log.info("compiling C sources")
            objects = compiler.compile(
                c_sources,
                output_dir=self.build_temp,
                macros=macros,
                include_dirs=include_dirs,
                debug=self.debug,
                extra_postargs=extra_postargs,
            )

        if cxx_sources:
            log.info("compiling C++ sources")
            cxx_compiler = compiler.cxx_compiler()
            cxx_objects = cxx_compiler.compile(
                cxx_sources,
                output_dir=self.build_temp,
                macros=macros,
                include_dirs=include_dirs,
                debug=self.debug,
                extra_postargs=extra_postargs,
            )
            objects.extend(cxx_objects)

        if f_sources or fmodule_sources:
            if not have_numpy:
                raise Exception("Fortran sources, but no NumPy to compile them.")
            extra_postargs = []
            f_objects = []

            if requiref90:
                if fcompiler.module_dir_switch is None:
                    existing_modules = glob("*.mod")
                extra_postargs += fcompiler.module_options(module_dirs, module_build_dir)

            if fmodule_sources:
                log.info("compiling Fortran 90 module sources")
                f_objects += fcompiler.compile(
                    fmodule_sources,
                    output_dir=self.build_temp,
                    macros=macros,
                    include_dirs=include_dirs,
                    debug=self.debug,
                    extra_postargs=extra_postargs,
                )

            if requiref90 and fcompiler.module_dir_switch is None:
                # move new compiled F90 module files to module_build_dir
                for f in glob("*.mod"):
                    if f in existing_modules:
                        continue
                    t = os.path.join(module_build_dir, f)
                    if os.path.abspath(f) == os.path.abspath(t):
                        continue
                    if os.path.isfile(t):
                        os.remove(t)
                    try:
                        self.move_file(f, module_build_dir)
                    except DistutilsFileError:
                        log.warn("failed to move %r to %r" % (f, module_build_dir))

            if f_sources:
                log.info("compiling Fortran sources")
                f_objects += fcompiler.compile(
                    f_sources,
                    output_dir=self.build_temp,
                    macros=macros,
                    include_dirs=include_dirs,
                    debug=self.debug,
                    extra_postargs=extra_postargs,
                )
        else:
            f_objects = []

        objects.extend(f_objects)

        # assume that default linker is suitable for
        # linking Fortran object files
        ########################################

        link_compiler = compiler
        if cxx_sources:
            link_compiler = cxx_compiler
        extra_postargs = build_info.get("extra_link_args") or []

        ## May be dependent on other libs we're builing
        shlib_libraries = []
        for libinfo in build_info.get("libraries", []):
            if isinstance(libinfo, basestring):
                shlib_libraries.append(convert_ulist([libinfo])[0])
            else:
                shlib_libraries.append(libinfo[0])

        ## Alternate ending
        link_compiler.link(
            target_desc=link_compiler.SHARED_LIBRARY,
            objects=objects,
            output_filename=target_library,
            output_dir=self.build_clib,
            libraries=shlib_libraries,
            library_dirs=library_dirs,
            runtime_library_dirs=runtime_library_dirs,
            debug=self.debug,
            extra_preargs=extra_preargs,
            extra_postargs=extra_postargs,
        )