Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 1 | """distutils.extension |
| 2 | |
| 3 | Provides the Extension class, used to describe C/C++ extension |
| 4 | modules in setup scripts.""" |
| 5 | |
Tarek Ziadé | 6840721 | 2009-06-03 11:17:15 +0000 | [diff] [blame] | 6 | import os |
Tarek Ziadé | 6840721 | 2009-06-03 11:17:15 +0000 | [diff] [blame] | 7 | import warnings |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 8 | |
| 9 | # This class is really only used by the "build_ext" command, so it might |
| 10 | # make sense to put it in distutils.command.build_ext. However, that |
| 11 | # module is already big enough, and I want to make this class a bit more |
| 12 | # complex to simplify some common cases ("foo" module in "foo.c") and do |
| 13 | # better error-checking ("foo.c" actually exists). |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 14 | # |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 15 | # Also, putting this in build_ext.py means every setup script would have to |
| 16 | # import that large-ish module (indirectly, through distutils.core) in |
| 17 | # order to do anything. |
| 18 | |
| 19 | class Extension: |
| 20 | """Just a collection of attributes that describes an extension |
| 21 | module and everything needed to build it (hopefully in a portable |
Greg Ward | 45b87bc | 2000-08-13 00:38:58 +0000 | [diff] [blame] | 22 | way, but there are hooks that let you be as unportable as you need). |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 23 | |
| 24 | Instance attributes: |
| 25 | name : string |
| 26 | the full name of the extension, including any packages -- ie. |
| 27 | *not* a filename or pathname, but Python dotted name |
| 28 | sources : [string] |
Greg Ward | cb18557 | 2000-06-24 00:18:24 +0000 | [diff] [blame] | 29 | list of source filenames, relative to the distribution root |
| 30 | (where the setup script lives), in Unix form (slash-separated) |
| 31 | for portability. Source files may be C, C++, SWIG (.i), |
| 32 | platform-specific resource files, or whatever else is recognized |
| 33 | by the "build_ext" command as source for a Python extension. |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 34 | include_dirs : [string] |
| 35 | list of directories to search for C/C++ header files (in Unix |
| 36 | form for portability) |
| 37 | define_macros : [(name : string, value : string|None)] |
| 38 | list of macros to define; each macro is defined using a 2-tuple, |
| 39 | where 'value' is either the string to define it to or None to |
| 40 | define it without a particular value (equivalent of "#define |
| 41 | FOO" in source or -DFOO on Unix C compiler command line) |
| 42 | undef_macros : [string] |
| 43 | list of macros to undefine explicitly |
| 44 | library_dirs : [string] |
| 45 | list of directories to search for C/C++ libraries at link time |
| 46 | libraries : [string] |
| 47 | list of library names (not filenames or paths) to link against |
| 48 | runtime_library_dirs : [string] |
| 49 | list of directories to search for C/C++ libraries at run time |
| 50 | (for shared extensions, this is when the extension is loaded) |
| 51 | extra_objects : [string] |
| 52 | list of extra files to link with (eg. object files not implied |
| 53 | by 'sources', static library that must be explicitly specified, |
| 54 | binary resource files, etc.) |
| 55 | extra_compile_args : [string] |
| 56 | any extra platform- and compiler-specific information to use |
| 57 | when compiling the source files in 'sources'. For platforms and |
| 58 | compilers where "command line" makes sense, this is typically a |
| 59 | list of command-line arguments, but for other platforms it could |
| 60 | be anything. |
| 61 | extra_link_args : [string] |
| 62 | any extra platform- and compiler-specific information to use |
| 63 | when linking object files together to create the extension (or |
| 64 | to create a new static Python interpreter). Similar |
| 65 | interpretation as for 'extra_compile_args'. |
| 66 | export_symbols : [string] |
| 67 | list of symbols to be exported from a shared extension. Not |
| 68 | used on all platforms, and not generally necessary for Python |
| 69 | extensions, which typically export exactly one symbol: "init" + |
| 70 | extension_name. |
Anthony Baxter | a024034 | 2004-10-14 10:02:08 +0000 | [diff] [blame] | 71 | swig_opts : [string] |
| 72 | any extra options to pass to SWIG if a source file has the .i |
| 73 | extension. |
Jeremy Hylton | 09e532b | 2002-06-12 20:08:56 +0000 | [diff] [blame] | 74 | depends : [string] |
| 75 | list of files that the extension depends on |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 76 | language : string |
| 77 | extension language (i.e. "c", "c++", "objc"). Will be detected |
| 78 | from the source extensions if not provided. |
Tarek Ziadé | b2e36f1 | 2009-03-31 22:37:55 +0000 | [diff] [blame] | 79 | optional : boolean |
| 80 | specifies that a build failure in the extension should not abort the |
| 81 | build process, but simply not install the failing extension. |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 82 | """ |
| 83 | |
Andrew M. Kuchling | 6ffdaab | 2003-01-27 16:30:36 +0000 | [diff] [blame] | 84 | # When adding arguments to this constructor, be sure to update |
| 85 | # setup_keywords in core.py. |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 86 | def __init__(self, name, sources, |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 87 | include_dirs=None, |
| 88 | define_macros=None, |
| 89 | undef_macros=None, |
| 90 | library_dirs=None, |
| 91 | libraries=None, |
| 92 | runtime_library_dirs=None, |
| 93 | extra_objects=None, |
| 94 | extra_compile_args=None, |
| 95 | extra_link_args=None, |
| 96 | export_symbols=None, |
Anthony Baxter | a024034 | 2004-10-14 10:02:08 +0000 | [diff] [blame] | 97 | swig_opts = None, |
Jeremy Hylton | 09e532b | 2002-06-12 20:08:56 +0000 | [diff] [blame] | 98 | depends=None, |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 99 | language=None, |
Tarek Ziadé | b2e36f1 | 2009-03-31 22:37:55 +0000 | [diff] [blame] | 100 | optional=None, |
Andrew M. Kuchling | 3f1c9a9 | 2002-11-13 20:54:21 +0000 | [diff] [blame] | 101 | **kw # To catch unknown keywords |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 102 | ): |
Tarek Ziadé | 2f19bb1 | 2009-07-22 08:57:28 +0000 | [diff] [blame] | 103 | if not isinstance(name, str): |
| 104 | raise AssertionError("'name' must be a string") |
| 105 | if not (isinstance(sources, list) and |
| 106 | all(isinstance(v, str) for v in sources)): |
| 107 | raise AssertionError("'sources' must be a list of strings") |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 108 | |
| 109 | self.name = name |
| 110 | self.sources = sources |
| 111 | self.include_dirs = include_dirs or [] |
| 112 | self.define_macros = define_macros or [] |
| 113 | self.undef_macros = undef_macros or [] |
| 114 | self.library_dirs = library_dirs or [] |
| 115 | self.libraries = libraries or [] |
| 116 | self.runtime_library_dirs = runtime_library_dirs or [] |
| 117 | self.extra_objects = extra_objects or [] |
| 118 | self.extra_compile_args = extra_compile_args or [] |
| 119 | self.extra_link_args = extra_link_args or [] |
Greg Ward | 1f6a0d4 | 2000-08-13 00:41:40 +0000 | [diff] [blame] | 120 | self.export_symbols = export_symbols or [] |
Anthony Baxter | a024034 | 2004-10-14 10:02:08 +0000 | [diff] [blame] | 121 | self.swig_opts = swig_opts or [] |
Jeremy Hylton | 09e532b | 2002-06-12 20:08:56 +0000 | [diff] [blame] | 122 | self.depends = depends or [] |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 123 | self.language = language |
Tarek Ziadé | b2e36f1 | 2009-03-31 22:37:55 +0000 | [diff] [blame] | 124 | self.optional = optional |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 125 | |
Andrew M. Kuchling | 3f1c9a9 | 2002-11-13 20:54:21 +0000 | [diff] [blame] | 126 | # If there are unknown keyword options, warn about them |
Tarek Ziadé | 6840721 | 2009-06-03 11:17:15 +0000 | [diff] [blame] | 127 | if len(kw) > 0: |
| 128 | options = [repr(option) for option in kw] |
| 129 | options = ', '.join(sorted(options)) |
| 130 | msg = "Unknown Extension options: %s" % options |
| 131 | warnings.warn(msg) |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 132 | |
Giampaolo Rodola' | 4a692ce | 2014-03-27 14:14:16 +0100 | [diff] [blame] | 133 | def __repr__(self): |
| 134 | return '<%s.%s(%r) at %#x>' % ( |
| 135 | self.__class__.__module__, |
Serhiy Storchaka | 521e586 | 2014-07-22 15:00:37 +0300 | [diff] [blame] | 136 | self.__class__.__qualname__, |
Giampaolo Rodola' | 4a692ce | 2014-03-27 14:14:16 +0100 | [diff] [blame] | 137 | self.name, |
| 138 | id(self)) |
| 139 | |
| 140 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 141 | def read_setup_file(filename): |
Tarek Ziadé | e6ed2f9 | 2009-06-03 10:31:15 +0000 | [diff] [blame] | 142 | """Reads a Setup file and returns Extension instances.""" |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 143 | from distutils.sysconfig import (parse_makefile, expand_makefile_vars, |
Tarek Ziadé | e6ed2f9 | 2009-06-03 10:31:15 +0000 | [diff] [blame] | 144 | _variable_rx) |
| 145 | |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 146 | from distutils.text_file import TextFile |
| 147 | from distutils.util import split_quoted |
| 148 | |
| 149 | # First pass over the file to gather "VAR = VALUE" assignments. |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 150 | vars = parse_makefile(filename) |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 151 | |
| 152 | # Second pass to gobble up the real content: lines of the form |
| 153 | # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...] |
| 154 | file = TextFile(filename, |
| 155 | strip_comments=1, skip_blanks=1, join_lines=1, |
| 156 | lstrip_ws=1, rstrip_ws=1) |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 157 | try: |
| 158 | extensions = [] |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 159 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 160 | while True: |
| 161 | line = file.readline() |
| 162 | if line is None: # eof |
| 163 | break |
| 164 | if _variable_rx.match(line): # VAR=VALUE, handled in first pass |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 165 | continue |
| 166 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 167 | if line[0] == line[-1] == "*": |
| 168 | file.warn("'%s' lines not handled yet" % line) |
| 169 | continue |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 170 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 171 | line = expand_makefile_vars(line, vars) |
| 172 | words = split_quoted(line) |
| 173 | |
| 174 | # NB. this parses a slightly different syntax than the old |
| 175 | # makesetup script: here, there must be exactly one extension per |
| 176 | # line, and it must be the first word of the line. I have no idea |
| 177 | # why the old syntax supported multiple extensions per line, as |
| 178 | # they all wind up being the same. |
| 179 | |
| 180 | module = words[0] |
| 181 | ext = Extension(module, []) |
| 182 | append_next_word = None |
| 183 | |
| 184 | for word in words[1:]: |
| 185 | if append_next_word is not None: |
| 186 | append_next_word.append(word) |
| 187 | append_next_word = None |
| 188 | continue |
| 189 | |
| 190 | suffix = os.path.splitext(word)[1] |
| 191 | switch = word[0:2] ; value = word[2:] |
| 192 | |
| 193 | if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"): |
| 194 | # hmm, should we do something about C vs. C++ sources? |
| 195 | # or leave it up to the CCompiler implementation to |
| 196 | # worry about? |
| 197 | ext.sources.append(word) |
| 198 | elif switch == "-I": |
| 199 | ext.include_dirs.append(value) |
| 200 | elif switch == "-D": |
| 201 | equals = value.find("=") |
| 202 | if equals == -1: # bare "-DFOO" -- no value |
| 203 | ext.define_macros.append((value, None)) |
| 204 | else: # "-DFOO=blah" |
| 205 | ext.define_macros.append((value[0:equals], |
| 206 | value[equals+2:])) |
| 207 | elif switch == "-U": |
| 208 | ext.undef_macros.append(value) |
| 209 | elif switch == "-C": # only here 'cause makesetup has it! |
| 210 | ext.extra_compile_args.append(word) |
| 211 | elif switch == "-l": |
| 212 | ext.libraries.append(value) |
| 213 | elif switch == "-L": |
| 214 | ext.library_dirs.append(value) |
| 215 | elif switch == "-R": |
| 216 | ext.runtime_library_dirs.append(value) |
| 217 | elif word == "-rpath": |
| 218 | append_next_word = ext.runtime_library_dirs |
| 219 | elif word == "-Xlinker": |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 220 | append_next_word = ext.extra_link_args |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 221 | elif word == "-Xcompiler": |
| 222 | append_next_word = ext.extra_compile_args |
| 223 | elif switch == "-u": |
| 224 | ext.extra_link_args.append(word) |
| 225 | if not value: |
| 226 | append_next_word = ext.extra_link_args |
| 227 | elif suffix in (".a", ".so", ".sl", ".o", ".dylib"): |
| 228 | # NB. a really faithful emulation of makesetup would |
| 229 | # append a .o file to extra_objects only if it |
| 230 | # had a slash in it; otherwise, it would s/.o/.c/ |
| 231 | # and append it to sources. Hmmmm. |
| 232 | ext.extra_objects.append(word) |
| 233 | else: |
| 234 | file.warn("unrecognized argument '%s'" % word) |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 235 | |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 236 | extensions.append(ext) |
| 237 | finally: |
| 238 | file.close() |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 239 | |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 240 | return extensions |