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