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. |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 76 | """ |
| 77 | |
| 78 | def __init__ (self, name, sources, |
| 79 | include_dirs=None, |
| 80 | define_macros=None, |
| 81 | undef_macros=None, |
| 82 | library_dirs=None, |
| 83 | libraries=None, |
| 84 | runtime_library_dirs=None, |
| 85 | extra_objects=None, |
| 86 | extra_compile_args=None, |
| 87 | extra_link_args=None, |
| 88 | export_symbols=None, |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 89 | ): |
| 90 | |
| 91 | assert type(name) is StringType, "'name' must be a string" |
| 92 | assert (type(sources) is ListType and |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 93 | map(type, sources) == [StringType]*len(sources)), \ |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 94 | "'sources' must be a list of strings" |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 95 | |
| 96 | self.name = name |
| 97 | self.sources = sources |
| 98 | self.include_dirs = include_dirs or [] |
| 99 | self.define_macros = define_macros or [] |
| 100 | self.undef_macros = undef_macros or [] |
| 101 | self.library_dirs = library_dirs or [] |
| 102 | self.libraries = libraries or [] |
| 103 | self.runtime_library_dirs = runtime_library_dirs or [] |
| 104 | self.extra_objects = extra_objects or [] |
| 105 | self.extra_compile_args = extra_compile_args or [] |
| 106 | self.extra_link_args = extra_link_args or [] |
Greg Ward | 1f6a0d4 | 2000-08-13 00:41:40 +0000 | [diff] [blame] | 107 | self.export_symbols = export_symbols or [] |
Greg Ward | 3ca54bc | 2000-05-31 01:05:35 +0000 | [diff] [blame] | 108 | |
| 109 | # class Extension |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | def read_setup_file (filename): |
| 113 | from distutils.sysconfig import \ |
| 114 | parse_makefile, expand_makefile_vars, _variable_rx |
| 115 | from distutils.text_file import TextFile |
| 116 | from distutils.util import split_quoted |
| 117 | |
| 118 | # First pass over the file to gather "VAR = VALUE" assignments. |
| 119 | vars = parse_makefile(filename) |
| 120 | |
| 121 | # Second pass to gobble up the real content: lines of the form |
| 122 | # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...] |
| 123 | file = TextFile(filename, |
| 124 | strip_comments=1, skip_blanks=1, join_lines=1, |
| 125 | lstrip_ws=1, rstrip_ws=1) |
| 126 | extensions = [] |
| 127 | |
| 128 | while 1: |
| 129 | line = file.readline() |
| 130 | if line is None: # eof |
| 131 | break |
| 132 | if _variable_rx.match(line): # VAR=VALUE, handled in first pass |
| 133 | continue |
| 134 | |
| 135 | if line[0] == line[-1] == "*": |
| 136 | file.warn("'%s' lines not handled yet" % line) |
| 137 | continue |
| 138 | |
| 139 | #print "original line: " + line |
| 140 | line = expand_makefile_vars(line, vars) |
| 141 | words = split_quoted(line) |
| 142 | #print "expanded line: " + line |
| 143 | |
| 144 | # NB. this parses a slightly different syntax than the old |
| 145 | # makesetup script: here, there must be exactly one extension per |
| 146 | # line, and it must be the first word of the line. I have no idea |
| 147 | # why the old syntax supported multiple extensions per line, as |
| 148 | # they all wind up being the same. |
| 149 | |
| 150 | module = words[0] |
| 151 | ext = Extension(module, []) |
| 152 | append_next_word = None |
| 153 | |
| 154 | for word in words[1:]: |
| 155 | if append_next_word is not None: |
| 156 | append_next_word.append(word) |
| 157 | append_next_word = None |
| 158 | continue |
| 159 | |
| 160 | suffix = os.path.splitext(word)[1] |
| 161 | switch = word[0:2] ; value = word[2:] |
| 162 | |
Andrew M. Kuchling | 3d2d980 | 2001-12-21 15:34:17 +0000 | [diff] [blame] | 163 | if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"): |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 164 | # hmm, should we do something about C vs. C++ sources? |
| 165 | # or leave it up to the CCompiler implementation to |
| 166 | # worry about? |
| 167 | ext.sources.append(word) |
| 168 | elif switch == "-I": |
| 169 | ext.include_dirs.append(value) |
| 170 | elif switch == "-D": |
Andrew M. Kuchling | ac20f77 | 2001-03-22 03:48:31 +0000 | [diff] [blame] | 171 | equals = string.find(value, "=") |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 172 | if equals == -1: # bare "-DFOO" -- no value |
| 173 | ext.define_macros.append((value, None)) |
| 174 | else: # "-DFOO=blah" |
| 175 | ext.define_macros.append((value[0:equals], |
| 176 | value[equals+2:])) |
| 177 | elif switch == "-U": |
| 178 | ext.undef_macros.append(value) |
| 179 | elif switch == "-C": # only here 'cause makesetup has it! |
| 180 | ext.extra_compile_args.append(word) |
| 181 | elif switch == "-l": |
| 182 | ext.libraries.append(value) |
| 183 | elif switch == "-L": |
| 184 | ext.library_dirs.append(value) |
| 185 | elif switch == "-R": |
| 186 | ext.runtime_library_dirs.append(value) |
| 187 | elif word == "-rpath": |
| 188 | append_next_word = ext.runtime_library_dirs |
| 189 | elif word == "-Xlinker": |
| 190 | append_next_word = ext.extra_link_args |
| 191 | elif switch == "-u": |
| 192 | ext.extra_link_args.append(word) |
| 193 | if not value: |
| 194 | append_next_word = ext.extra_link_args |
| 195 | elif suffix in (".a", ".so", ".sl", ".o"): |
| 196 | # NB. a really faithful emulation of makesetup would |
| 197 | # append a .o file to extra_objects only if it |
| 198 | # had a slash in it; otherwise, it would s/.o/.c/ |
| 199 | # and append it to sources. Hmmmm. |
| 200 | ext.extra_objects.append(word) |
| 201 | else: |
| 202 | file.warn("unrecognized argument '%s'" % word) |
| 203 | |
| 204 | extensions.append(ext) |
| 205 | |
| 206 | #print "module:", module |
| 207 | #print "source files:", source_files |
| 208 | #print "cpp args:", cpp_args |
| 209 | #print "lib args:", library_args |
| 210 | |
| 211 | #extensions[module] = { 'sources': source_files, |
| 212 | # 'cpp_args': cpp_args, |
| 213 | # 'lib_args': library_args } |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 214 | |
Greg Ward | 41ed12f | 2000-09-17 00:45:18 +0000 | [diff] [blame] | 215 | return extensions |
| 216 | |
| 217 | # read_setup_file () |