Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
extension.py243 linesDownload Raw Back to _distutils
1"""distutils.extension2 3Provides the Extension class, used to describe C/C++ extension4modules in setup scripts."""5 6import os7import warnings8 9# This class is really only used by the "build_ext" command, so it might10# make sense to put it in distutils.command.build_ext.  However, that11# module is already big enough, and I want to make this class a bit more12# complex to simplify some common cases ("foo" module in "foo.c") and do13# better error-checking ("foo.c" actually exists).14#15# Also, putting this in build_ext.py means every setup script would have to16# import that large-ish module (indirectly, through distutils.core) in17# order to do anything.18 19 20class Extension:21    """Just a collection of attributes that describes an extension22    module and everything needed to build it (hopefully in a portable23    way, but there are hooks that let you be as unportable as you need).24 25    Instance attributes:26      name : string27        the full name of the extension, including any packages -- ie.28        *not* a filename or pathname, but Python dotted name29      sources : [string]30        list of source filenames, relative to the distribution root31        (where the setup script lives), in Unix form (slash-separated)32        for portability.  Source files may be C, C++, SWIG (.i),33        platform-specific resource files, or whatever else is recognized34        by the "build_ext" command as source for a Python extension.35      include_dirs : [string]36        list of directories to search for C/C++ header files (in Unix37        form for portability)38      define_macros : [(name : string, value : string|None)]39        list of macros to define; each macro is defined using a 2-tuple,40        where 'value' is either the string to define it to or None to41        define it without a particular value (equivalent of "#define42        FOO" in source or -DFOO on Unix C compiler command line)43      undef_macros : [string]44        list of macros to undefine explicitly45      library_dirs : [string]46        list of directories to search for C/C++ libraries at link time47      libraries : [string]48        list of library names (not filenames or paths) to link against49      runtime_library_dirs : [string]50        list of directories to search for C/C++ libraries at run time51        (for shared extensions, this is when the extension is loaded)52      extra_objects : [string]53        list of extra files to link with (eg. object files not implied54        by 'sources', static library that must be explicitly specified,55        binary resource files, etc.)56      extra_compile_args : [string]57        any extra platform- and compiler-specific information to use58        when compiling the source files in 'sources'.  For platforms and59        compilers where "command line" makes sense, this is typically a60        list of command-line arguments, but for other platforms it could61        be anything.62      extra_link_args : [string]63        any extra platform- and compiler-specific information to use64        when linking object files together to create the extension (or65        to create a new static Python interpreter).  Similar66        interpretation as for 'extra_compile_args'.67      export_symbols : [string]68        list of symbols to be exported from a shared extension.  Not69        used on all platforms, and not generally necessary for Python70        extensions, which typically export exactly one symbol: "init" +71        extension_name.72      swig_opts : [string]73        any extra options to pass to SWIG if a source file has the .i74        extension.75      depends : [string]76        list of files that the extension depends on77      language : string78        extension language (i.e. "c", "c++", "objc"). Will be detected79        from the source extensions if not provided.80      optional : boolean81        specifies that a build failure in the extension should not abort the82        build process, but simply not install the failing extension.83    """84 85    # When adding arguments to this constructor, be sure to update86    # setup_keywords in core.py.87    def __init__(88        self,89        name,90        sources,91        include_dirs=None,92        define_macros=None,93        undef_macros=None,94        library_dirs=None,95        libraries=None,96        runtime_library_dirs=None,97        extra_objects=None,98        extra_compile_args=None,99        extra_link_args=None,100        export_symbols=None,101        swig_opts=None,102        depends=None,103        language=None,104        optional=None,105        **kw,  # To catch unknown keywords106    ):107        if not isinstance(name, str):108            raise AssertionError("'name' must be a string")109        if not (isinstance(sources, list) and all(isinstance(v, str) for v in sources)):110            raise AssertionError("'sources' must be a list of strings")111 112        self.name = name113        self.sources = sources114        self.include_dirs = include_dirs or []115        self.define_macros = define_macros or []116        self.undef_macros = undef_macros or []117        self.library_dirs = library_dirs or []118        self.libraries = libraries or []119        self.runtime_library_dirs = runtime_library_dirs or []120        self.extra_objects = extra_objects or []121        self.extra_compile_args = extra_compile_args or []122        self.extra_link_args = extra_link_args or []123        self.export_symbols = export_symbols or []124        self.swig_opts = swig_opts or []125        self.depends = depends or []126        self.language = language127        self.optional = optional128 129        # If there are unknown keyword options, warn about them130        if len(kw) > 0:131            options = [repr(option) for option in kw]132            options = ', '.join(sorted(options))133            msg = "Unknown Extension options: %s" % options134            warnings.warn(msg)135 136    def __repr__(self):137        return f'<{self.__class__.__module__}.{self.__class__.__qualname__}({self.name!r}) at {id(self):#x}>'138 139 140def read_setup_file(filename):  # noqa: C901141    """Reads a Setup file and returns Extension instances."""142    from distutils.sysconfig import _variable_rx, expand_makefile_vars, parse_makefile143    from distutils.text_file import TextFile144    from distutils.util import split_quoted145 146    # First pass over the file to gather "VAR = VALUE" assignments.147    vars = parse_makefile(filename)148 149    # Second pass to gobble up the real content: lines of the form150    #   <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]151    file = TextFile(152        filename,153        strip_comments=1,154        skip_blanks=1,155        join_lines=1,156        lstrip_ws=1,157        rstrip_ws=1,158    )159    try:160        extensions = []161 162        while True:163            line = file.readline()164            if line is None:  # eof165                break166            if _variable_rx.match(line):  # VAR=VALUE, handled in first pass167                continue168 169            if line[0] == line[-1] == "*":170                file.warn("'%s' lines not handled yet" % line)171                continue172 173            line = expand_makefile_vars(line, vars)174            words = split_quoted(line)175 176            # NB. this parses a slightly different syntax than the old177            # makesetup script: here, there must be exactly one extension per178            # line, and it must be the first word of the line.  I have no idea179            # why the old syntax supported multiple extensions per line, as180            # they all wind up being the same.181 182            module = words[0]183            ext = Extension(module, [])184            append_next_word = None185 186            for word in words[1:]:187                if append_next_word is not None:188                    append_next_word.append(word)189                    append_next_word = None190                    continue191 192                suffix = os.path.splitext(word)[1]193                switch = word[0:2]194                value = word[2:]195 196                if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):197                    # hmm, should we do something about C vs. C++ sources?198                    # or leave it up to the CCompiler implementation to199                    # worry about?200                    ext.sources.append(word)201                elif switch == "-I":202                    ext.include_dirs.append(value)203                elif switch == "-D":204                    equals = value.find("=")205                    if equals == -1:  # bare "-DFOO" -- no value206                        ext.define_macros.append((value, None))207                    else:  # "-DFOO=blah"208                        ext.define_macros.append((value[0:equals], value[equals + 2 :]))209                elif switch == "-U":210                    ext.undef_macros.append(value)211                elif switch == "-C":  # only here 'cause makesetup has it!212                    ext.extra_compile_args.append(word)213                elif switch == "-l":214                    ext.libraries.append(value)215                elif switch == "-L":216                    ext.library_dirs.append(value)217                elif switch == "-R":218                    ext.runtime_library_dirs.append(value)219                elif word == "-rpath":220                    append_next_word = ext.runtime_library_dirs221                elif word == "-Xlinker":222                    append_next_word = ext.extra_link_args223                elif word == "-Xcompiler":224                    append_next_word = ext.extra_compile_args225                elif switch == "-u":226                    ext.extra_link_args.append(word)227                    if not value:228                        append_next_word = ext.extra_link_args229                elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):230                    # NB. a really faithful emulation of makesetup would231                    # append a .o file to extra_objects only if it232                    # had a slash in it; otherwise, it would s/.o/.c/233                    # and append it to sources.  Hmmmm.234                    ext.extra_objects.append(word)235                else:236                    file.warn("unrecognized argument '%s'" % word)237 238            extensions.append(ext)239    finally:240        file.close()241 242    return extensions243 
codekingpro/portable-devtools · Team Ai