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