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