Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 1 | """distutils.command.build_ext |
| 2 | |
| 3 | Implements the Distutils 'build_ext' command, for building extension |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 4 | modules (currently limited to C extensions, should accommodate C++ |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 5 | extensions ASAP).""" |
| 6 | |
| 7 | # created 1999/08/09, Greg Ward |
| 8 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 9 | __revision__ = "$Id$" |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 10 | |
| 11 | import sys, os, string, re |
| 12 | from types import * |
| 13 | from distutils.core import Command |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 14 | from distutils.errors import * |
Greg Ward | 5ca84b8 | 2000-06-25 02:10:46 +0000 | [diff] [blame] | 15 | from distutils.sysconfig import customize_compiler |
Greg Ward | 5213400 | 2000-05-26 01:31:53 +0000 | [diff] [blame] | 16 | from distutils.dep_util import newer_group |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 17 | from distutils.extension import Extension |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 18 | |
Greg Ward | dbb9625 | 1999-09-21 18:27:12 +0000 | [diff] [blame] | 19 | # An extension name is just a dot-separated list of Python NAMEs (ie. |
| 20 | # the same as a fully-qualified module name). |
| 21 | extension_name_re = re.compile \ |
| 22 | (r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$') |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 23 | |
| 24 | |
Greg Ward | 3459381 | 2000-06-24 01:23:37 +0000 | [diff] [blame] | 25 | def show_compilers (): |
| 26 | from distutils.ccompiler import show_compilers |
| 27 | show_compilers() |
| 28 | |
| 29 | |
Greg Ward | 1993f9a | 2000-02-18 00:13:53 +0000 | [diff] [blame] | 30 | class build_ext (Command): |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 31 | |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 32 | description = "build C/C++ extensions (compile/link to build directory)" |
| 33 | |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 34 | # XXX thoughts on how to deal with complex command-line options like |
| 35 | # these, i.e. how to make it so fancy_getopt can suck them off the |
| 36 | # command line and make it look like setup.py defined the appropriate |
| 37 | # lists of tuples of what-have-you. |
| 38 | # - each command needs a callback to process its command-line options |
| 39 | # - Command.__init__() needs access to its share of the whole |
| 40 | # command line (must ultimately come from |
| 41 | # Distribution.parse_command_line()) |
| 42 | # - it then calls the current command class' option-parsing |
| 43 | # callback to deal with weird options like -D, which have to |
| 44 | # parse the option text and churn out some custom data |
| 45 | # structure |
| 46 | # - that data structure (in this case, a list of 2-tuples) |
| 47 | # will then be present in the command object by the time |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 48 | # we get to finalize_options() (i.e. the constructor |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 49 | # takes care of both command-line and client options |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 50 | # in between initialize_options() and finalize_options()) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 51 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 52 | user_options = [ |
Greg Ward | e51d69e | 2000-03-01 01:43:28 +0000 | [diff] [blame] | 53 | ('build-lib=', 'b', |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 54 | "directory for compiled extension modules"), |
Greg Ward | e51d69e | 2000-03-01 01:43:28 +0000 | [diff] [blame] | 55 | ('build-temp=', 't', |
| 56 | "directory for temporary files (build by-products)"), |
| 57 | ('inplace', 'i', |
| 58 | "ignore build-lib and put compiled extensions into the source" + |
| 59 | "directory alongside your pure Python modules"), |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 60 | ('include-dirs=', 'I', |
| 61 | "list of directories to search for header files"), |
| 62 | ('define=', 'D', |
| 63 | "C preprocessor macros to define"), |
| 64 | ('undef=', 'U', |
| 65 | "C preprocessor macros to undefine"), |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 66 | ('libraries=', 'l', |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 67 | "external C libraries to link with"), |
| 68 | ('library-dirs=', 'L', |
| 69 | "directories to search for external C libraries"), |
| 70 | ('rpath=', 'R', |
| 71 | "directories to search for shared C libraries at runtime"), |
| 72 | ('link-objects=', 'O', |
| 73 | "extra explicit link objects to include in the link"), |
| 74 | ('debug', 'g', |
| 75 | "compile/link with debugging information"), |
Greg Ward | c41d6b3 | 2000-04-10 00:19:42 +0000 | [diff] [blame] | 76 | ('force', 'f', |
Gregory P. Smith | 6cd0c43 | 2000-05-12 00:34:12 +0000 | [diff] [blame] | 77 | "forcibly build everything (ignore file timestamps)"), |
| 78 | ('compiler=', 'c', |
| 79 | "specify the compiler type"), |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 80 | ('swig-cpp', None, |
| 81 | "make SWIG create C++ files (default is C)"), |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 82 | ] |
Greg Ward | 3459381 | 2000-06-24 01:23:37 +0000 | [diff] [blame] | 83 | |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 84 | help_options = [ |
| 85 | ('help-compiler', None, |
Greg Ward | 3459381 | 2000-06-24 01:23:37 +0000 | [diff] [blame] | 86 | "list available compilers", show_compilers), |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 87 | ] |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 88 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 89 | def initialize_options (self): |
Greg Ward | 71eb864 | 1999-09-08 02:42:30 +0000 | [diff] [blame] | 90 | self.extensions = None |
Greg Ward | e51d69e | 2000-03-01 01:43:28 +0000 | [diff] [blame] | 91 | self.build_lib = None |
| 92 | self.build_temp = None |
| 93 | self.inplace = 0 |
Greg Ward | 609a5c8 | 1999-09-13 13:55:34 +0000 | [diff] [blame] | 94 | self.package = None |
| 95 | |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 96 | self.include_dirs = None |
| 97 | self.define = None |
| 98 | self.undef = None |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 99 | self.libraries = None |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 100 | self.library_dirs = None |
| 101 | self.rpath = None |
| 102 | self.link_objects = None |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame] | 103 | self.debug = None |
Greg Ward | c41d6b3 | 2000-04-10 00:19:42 +0000 | [diff] [blame] | 104 | self.force = None |
Gregory P. Smith | 6cd0c43 | 2000-05-12 00:34:12 +0000 | [diff] [blame] | 105 | self.compiler = None |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 106 | self.swig_cpp = None |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 107 | |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 108 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 109 | def finalize_options (self): |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 110 | from distutils import sysconfig |
| 111 | |
Greg Ward | e8c6ce4 | 2000-02-09 02:20:14 +0000 | [diff] [blame] | 112 | self.set_undefined_options ('build', |
Greg Ward | e51d69e | 2000-03-01 01:43:28 +0000 | [diff] [blame] | 113 | ('build_lib', 'build_lib'), |
| 114 | ('build_temp', 'build_temp'), |
Gregory P. Smith | 6cd0c43 | 2000-05-12 00:34:12 +0000 | [diff] [blame] | 115 | ('compiler', 'compiler'), |
Greg Ward | c41d6b3 | 2000-04-10 00:19:42 +0000 | [diff] [blame] | 116 | ('debug', 'debug'), |
| 117 | ('force', 'force')) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 118 | |
Greg Ward | 609a5c8 | 1999-09-13 13:55:34 +0000 | [diff] [blame] | 119 | if self.package is None: |
Greg Ward | dbb9625 | 1999-09-21 18:27:12 +0000 | [diff] [blame] | 120 | self.package = self.distribution.ext_package |
| 121 | |
| 122 | self.extensions = self.distribution.ext_modules |
| 123 | |
Greg Ward | 609a5c8 | 1999-09-13 13:55:34 +0000 | [diff] [blame] | 124 | |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 125 | # Make sure Python's include directories (for Python.h, config.h, |
Greg Ward | 1561ae1 | 2000-04-14 00:50:49 +0000 | [diff] [blame] | 126 | # etc.) are in the include search path. |
| 127 | py_include = sysconfig.get_python_inc() |
| 128 | plat_py_include = sysconfig.get_python_inc(plat_specific=1) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 129 | if self.include_dirs is None: |
Greg Ward | dbb9625 | 1999-09-21 18:27:12 +0000 | [diff] [blame] | 130 | self.include_dirs = self.distribution.include_dirs or [] |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 131 | if type (self.include_dirs) is StringType: |
| 132 | self.include_dirs = string.split (self.include_dirs, |
| 133 | os.pathsep) |
| 134 | |
Greg Ward | 1d16a9f | 2000-03-29 04:13:49 +0000 | [diff] [blame] | 135 | # Put the Python "system" include dir at the end, so that |
| 136 | # any local include dirs take precedence. |
| 137 | self.include_dirs.append (py_include) |
Greg Ward | 1561ae1 | 2000-04-14 00:50:49 +0000 | [diff] [blame] | 138 | if plat_py_include != py_include: |
| 139 | self.include_dirs.append (plat_py_include) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 140 | |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 141 | if type (self.libraries) is StringType: |
| 142 | self.libraries = [self.libraries] |
Greg Ward | dedd5b5 | 2000-03-18 15:21:03 +0000 | [diff] [blame] | 143 | |
Greg Ward | 8422964 | 2000-03-31 03:50:23 +0000 | [diff] [blame] | 144 | # Life is easier if we're not forever checking for None, so |
| 145 | # simplify these options to empty lists if unset |
| 146 | if self.libraries is None: |
| 147 | self.libraries = [] |
| 148 | if self.library_dirs is None: |
| 149 | self.library_dirs = [] |
| 150 | if self.rpath is None: |
| 151 | self.rpath = [] |
Greg Ward | c185467 | 2000-02-05 02:23:16 +0000 | [diff] [blame] | 152 | |
Greg Ward | 8422964 | 2000-03-31 03:50:23 +0000 | [diff] [blame] | 153 | # for extensions under windows use different directories |
| 154 | # for Release and Debug builds. |
| 155 | # also Python's library directory must be appended to library_dirs |
| 156 | if os.name == 'nt': |
| 157 | self.library_dirs.append (os.path.join(sys.exec_prefix, 'libs')) |
Greg Ward | b593793 | 2000-06-27 01:43:24 +0000 | [diff] [blame] | 158 | self.implib_dir = self.build_temp |
Greg Ward | 8422964 | 2000-03-31 03:50:23 +0000 | [diff] [blame] | 159 | if self.debug: |
| 160 | self.build_temp = os.path.join (self.build_temp, "Debug") |
| 161 | else: |
| 162 | self.build_temp = os.path.join (self.build_temp, "Release") |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 163 | # finalize_options () |
Greg Ward | c185467 | 2000-02-05 02:23:16 +0000 | [diff] [blame] | 164 | |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 165 | |
| 166 | def run (self): |
| 167 | |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 168 | from distutils.ccompiler import new_compiler |
| 169 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 170 | # 'self.extensions', as supplied by setup.py, is a list of |
| 171 | # Extension instances. See the documentation for Extension (in |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 172 | # distutils.extension) for details. |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 173 | # |
| 174 | # For backwards compatibility with Distutils 0.8.2 and earlier, we |
| 175 | # also allow the 'extensions' list to be a list of tuples: |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 176 | # (ext_name, build_info) |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 177 | # where build_info is a dictionary containing everything that |
| 178 | # Extension instances do except the name, with a few things being |
| 179 | # differently named. We convert these 2-tuples to Extension |
| 180 | # instances as needed. |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 181 | |
Greg Ward | 71eb864 | 1999-09-08 02:42:30 +0000 | [diff] [blame] | 182 | if not self.extensions: |
| 183 | return |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 184 | |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 185 | # If we were asked to build any C/C++ libraries, make sure that the |
| 186 | # directory where we put them is in the library search path for |
| 187 | # linking extensions. |
Greg Ward | 4d16e0a | 2000-03-29 02:13:09 +0000 | [diff] [blame] | 188 | if self.distribution.has_c_libraries(): |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 189 | build_clib = self.get_finalized_command ('build_clib') |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 190 | self.libraries.extend (build_clib.get_library_names() or []) |
| 191 | self.library_dirs.append (build_clib.build_clib) |
| 192 | |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 193 | # Setup the CCompiler object that we'll use to do all the |
| 194 | # compiling and linking |
Greg Ward | e401e15 | 2000-06-25 02:30:15 +0000 | [diff] [blame] | 195 | self.compiler = new_compiler (compiler=self.compiler, |
Gregory P. Smith | 6cd0c43 | 2000-05-12 00:34:12 +0000 | [diff] [blame] | 196 | verbose=self.verbose, |
Greg Ward | 3c6204a | 1999-10-03 21:08:42 +0000 | [diff] [blame] | 197 | dry_run=self.dry_run, |
| 198 | force=self.force) |
Greg Ward | 5ca84b8 | 2000-06-25 02:10:46 +0000 | [diff] [blame] | 199 | customize_compiler(self.compiler) |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 200 | |
| 201 | # And make sure that any compile/link-related options (which might |
| 202 | # come from the command-line or from the setup script) are set in |
| 203 | # that CCompiler object -- that way, they automatically apply to |
| 204 | # all compiling and linking done here. |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 205 | if self.include_dirs is not None: |
| 206 | self.compiler.set_include_dirs (self.include_dirs) |
| 207 | if self.define is not None: |
| 208 | # 'define' option is a list of (name,value) tuples |
| 209 | for (name,value) in self.define: |
| 210 | self.compiler.define_macro (name, value) |
| 211 | if self.undef is not None: |
| 212 | for macro in self.undef: |
| 213 | self.compiler.undefine_macro (macro) |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 214 | if self.libraries is not None: |
| 215 | self.compiler.set_libraries (self.libraries) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 216 | if self.library_dirs is not None: |
| 217 | self.compiler.set_library_dirs (self.library_dirs) |
| 218 | if self.rpath is not None: |
| 219 | self.compiler.set_runtime_library_dirs (self.rpath) |
| 220 | if self.link_objects is not None: |
| 221 | self.compiler.set_link_objects (self.link_objects) |
Greg Ward | 60f6433 | 2000-03-02 01:32:21 +0000 | [diff] [blame] | 222 | |
Greg Ward | cdb20ba | 2000-03-26 21:45:14 +0000 | [diff] [blame] | 223 | # Now actually compile and link everything. |
Greg Ward | 4d16e0a | 2000-03-29 02:13:09 +0000 | [diff] [blame] | 224 | self.build_extensions () |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 225 | |
Greg Ward | 4d16e0a | 2000-03-29 02:13:09 +0000 | [diff] [blame] | 226 | # run () |
| 227 | |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 228 | |
| 229 | def check_extensions_list (self, extensions): |
Greg Ward | c185467 | 2000-02-05 02:23:16 +0000 | [diff] [blame] | 230 | """Ensure that the list of extensions (presumably provided as a |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 231 | command option 'extensions') is valid, i.e. it is a list of |
| 232 | Extension objects. We also support the old-style list of 2-tuples, |
| 233 | where the tuples are (ext_name, build_info), which are converted to |
| 234 | Extension instances here. |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 235 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 236 | Raise DistutilsSetupError if the structure is invalid anywhere; |
| 237 | just returns otherwise. |
| 238 | """ |
| 239 | if type(extensions) is not ListType: |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 240 | raise DistutilsSetupError, \ |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 241 | "'ext_modules' option must be a list of Extension instances" |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 242 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 243 | for i in range(len(extensions)): |
| 244 | ext = extensions[i] |
| 245 | if isinstance(ext, Extension): |
| 246 | continue # OK! (assume type-checking done |
| 247 | # by Extension constructor) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 248 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 249 | (ext_name, build_info) = ext |
| 250 | self.warn(("old-style (ext_name, build_info) tuple found in " |
| 251 | "ext_modules for extension '%s'" |
| 252 | "-- please convert to Extension instance" % ext_name)) |
| 253 | if type(ext) is not TupleType and len(ext) != 2: |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 254 | raise DistutilsSetupError, \ |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 255 | ("each element of 'ext_modules' option must be an " |
| 256 | "Extension instance or 2-tuple") |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 257 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 258 | if not (type(ext_name) is StringType and |
| 259 | extension_name_re.match(ext_name)): |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 260 | raise DistutilsSetupError, \ |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 261 | ("first element of each tuple in 'ext_modules' " |
| 262 | "must be the extension name (a string)") |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 263 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 264 | if type(build_info) is not DictionaryType: |
| 265 | raise DistutilsSetupError, \ |
| 266 | ("second element of each tuple in 'ext_modules' " |
| 267 | "must be a dictionary (build info)") |
| 268 | |
| 269 | # OK, the (ext_name, build_info) dict is type-safe: convert it |
| 270 | # to an Extension instance. |
| 271 | ext = Extension(ext_name, build_info['sources']) |
| 272 | |
| 273 | # Easy stuff: one-to-one mapping from dict elements to |
| 274 | # instance attributes. |
| 275 | for key in ('include_dirs', |
| 276 | 'library_dirs', |
| 277 | 'libraries', |
| 278 | 'extra_objects', |
| 279 | 'extra_compile_args', |
| 280 | 'extra_link_args'): |
| 281 | setattr(ext, key, build_info.get(key)) |
| 282 | |
| 283 | # Medium-easy stuff: same syntax/semantics, different names. |
| 284 | ext.runtime_library_dirs = build_info.get('rpath') |
| 285 | ext.export_symbol_file = build_info.get('def_file') |
| 286 | |
| 287 | # Non-trivial stuff: 'macros' split into 'define_macros' |
| 288 | # and 'undef_macros'. |
| 289 | macros = build_info.get('macros') |
| 290 | if macros: |
| 291 | ext.define_macros = [] |
| 292 | ext.undef_macros = [] |
| 293 | for macro in macros: |
| 294 | if not (type(macro) is TupleType and |
Greg Ward | 53c1bc3 | 2000-07-27 01:21:54 +0000 | [diff] [blame] | 295 | 1 <= len(macro) <= 2): |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 296 | raise DistutilsSetupError, \ |
| 297 | ("'macros' element of build info dict " |
| 298 | "must be 1- or 2-tuple") |
| 299 | if len(macro) == 1: |
| 300 | ext.undef_macros.append(macro[0]) |
| 301 | elif len(macro) == 2: |
| 302 | ext.define_macros.append(macro) |
| 303 | |
| 304 | extensions[i] = ext |
| 305 | |
| 306 | # for extensions |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 307 | |
| 308 | # check_extensions_list () |
| 309 | |
| 310 | |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 311 | def get_source_files (self): |
Andrew M. Kuchling | 23adc9f | 2000-07-14 13:35:07 +0000 | [diff] [blame] | 312 | self.check_extensions_list(self.extensions) |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 313 | filenames = [] |
| 314 | |
| 315 | # Wouldn't it be neat if we knew the names of header files too... |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 316 | for ext in self.extensions: |
| 317 | filenames.extend (ext.sources) |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 318 | |
| 319 | return filenames |
| 320 | |
| 321 | |
Greg Ward | 4d16e0a | 2000-03-29 02:13:09 +0000 | [diff] [blame] | 322 | def get_outputs (self): |
| 323 | |
| 324 | # Sanity check the 'extensions' list -- can't assume this is being |
| 325 | # done in the same run as a 'build_extensions()' call (in fact, we |
| 326 | # can probably assume that it *isn't*!). |
| 327 | self.check_extensions_list (self.extensions) |
| 328 | |
| 329 | # And build the list of output (built) filenames. Note that this |
| 330 | # ignores the 'inplace' flag, and assumes everything goes in the |
| 331 | # "build" tree. |
| 332 | outputs = [] |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 333 | for ext in self.extensions: |
| 334 | fullname = self.get_ext_fullname (ext.name) |
Greg Ward | 4d16e0a | 2000-03-29 02:13:09 +0000 | [diff] [blame] | 335 | outputs.append (os.path.join (self.build_lib, |
| 336 | self.get_ext_filename(fullname))) |
| 337 | return outputs |
| 338 | |
| 339 | # get_outputs () |
| 340 | |
| 341 | |
Greg Ward | 71d5583 | 2000-03-30 19:47:22 +0000 | [diff] [blame] | 342 | def build_extensions (self): |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 343 | |
Greg Ward | 4d16e0a | 2000-03-29 02:13:09 +0000 | [diff] [blame] | 344 | # First, sanity-check the 'extensions' list |
| 345 | self.check_extensions_list (self.extensions) |
| 346 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 347 | for ext in self.extensions: |
| 348 | sources = ext.sources |
Greg Ward | 48697d9 | 1999-12-12 17:01:01 +0000 | [diff] [blame] | 349 | if sources is None or type (sources) not in (ListType, TupleType): |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 350 | raise DistutilsSetupError, \ |
Greg Ward | c185467 | 2000-02-05 02:23:16 +0000 | [diff] [blame] | 351 | ("in 'ext_modules' option (extension '%s'), " + |
Greg Ward | 7f0fb0b | 2000-02-03 23:07:54 +0000 | [diff] [blame] | 352 | "'sources' must be present and must be " + |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 353 | "a list of source filenames") % ext.name |
Greg Ward | 48697d9 | 1999-12-12 17:01:01 +0000 | [diff] [blame] | 354 | sources = list (sources) |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 355 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 356 | fullname = self.get_ext_fullname (ext.name) |
Greg Ward | 5213400 | 2000-05-26 01:31:53 +0000 | [diff] [blame] | 357 | if self.inplace: |
| 358 | # ignore build-lib -- put the compiled extension into |
| 359 | # the source tree along with pure Python modules |
| 360 | |
| 361 | modpath = string.split (fullname, '.') |
| 362 | package = string.join (modpath[0:-1], '.') |
| 363 | base = modpath[-1] |
| 364 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 365 | build_py = self.get_finalized_command ('build_py') |
Greg Ward | 5213400 | 2000-05-26 01:31:53 +0000 | [diff] [blame] | 366 | package_dir = build_py.get_package_dir (package) |
| 367 | ext_filename = os.path.join (package_dir, |
| 368 | self.get_ext_filename(base)) |
| 369 | else: |
| 370 | ext_filename = os.path.join (self.build_lib, |
| 371 | self.get_ext_filename(fullname)) |
| 372 | |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 373 | if not (self.force or newer_group(sources, ext_filename, 'newer')): |
| 374 | self.announce ("skipping '%s' extension (up-to-date)" % |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 375 | ext.name) |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 376 | continue # 'for' loop over all extensions |
| 377 | else: |
| 378 | self.announce ("building '%s' extension" % ext.name) |
Greg Ward | 7f0fb0b | 2000-02-03 23:07:54 +0000 | [diff] [blame] | 379 | |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 380 | # First, scan the sources for SWIG definition files (.i), run |
| 381 | # SWIG on 'em to create .c files, and modify the sources list |
| 382 | # accordingly. |
| 383 | sources = self.swig_sources(sources) |
| 384 | |
| 385 | # Next, compile the source code to object files. |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 386 | |
| 387 | # XXX not honouring 'define_macros' or 'undef_macros' -- the |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 388 | # CCompiler API needs to change to accommodate this, and I |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 389 | # want to do one thing at a time! |
| 390 | |
| 391 | # Two possible sources for extra compiler arguments: |
| 392 | # - 'extra_compile_args' in Extension object |
| 393 | # - CFLAGS environment variable (not particularly |
| 394 | # elegant, but people seem to expect it and I |
| 395 | # guess it's useful) |
| 396 | # The environment variable should take precedence, and |
| 397 | # any sensible compiler will give precendence to later |
| 398 | # command line args. Hence we combine them in order: |
Greg Ward | e401e15 | 2000-06-25 02:30:15 +0000 | [diff] [blame] | 399 | extra_args = ext.extra_compile_args or [] |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 400 | |
| 401 | # XXX and if we support CFLAGS, why not CC (compiler |
| 402 | # executable), CPPFLAGS (pre-processor options), and LDFLAGS |
| 403 | # (linker options) too? |
| 404 | # XXX should we use shlex to properly parse CFLAGS? |
| 405 | |
Gregory P. Smith | b5ebe5d | 2000-05-13 01:52:14 +0000 | [diff] [blame] | 406 | if os.environ.has_key('CFLAGS'): |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 407 | extra_args.extend(string.split(os.environ['CFLAGS'])) |
Gregory P. Smith | b5ebe5d | 2000-05-13 01:52:14 +0000 | [diff] [blame] | 408 | |
Greg Ward | e51d69e | 2000-03-01 01:43:28 +0000 | [diff] [blame] | 409 | objects = self.compiler.compile (sources, |
| 410 | output_dir=self.build_temp, |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 411 | #macros=macros, |
| 412 | include_dirs=ext.include_dirs, |
Greg Ward | 18856b8 | 2000-05-06 13:12:59 +0000 | [diff] [blame] | 413 | debug=self.debug, |
| 414 | extra_postargs=extra_args) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 415 | |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 416 | # Now link the object files together into a "shared object" -- |
| 417 | # of course, first we have to figure out all the other things |
| 418 | # that go into the mix. |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 419 | if ext.extra_objects: |
| 420 | objects.extend (ext.extra_objects) |
Greg Ward | e401e15 | 2000-06-25 02:30:15 +0000 | [diff] [blame] | 421 | extra_args = ext.extra_link_args or [] |
Greg Ward | 968d883 | 2000-02-10 02:17:06 +0000 | [diff] [blame] | 422 | |
Greg Ward | f46a688 | 2000-06-25 02:23:11 +0000 | [diff] [blame] | 423 | # Bunch of fixing-up we have to do for Microsoft's linker. |
| 424 | if self.compiler.compiler_type == 'msvc': |
| 425 | self.msvc_prelink_hack(sources, ext, extra_args) |
Greg Ward | a6cb8ae | 1999-09-29 12:49:35 +0000 | [diff] [blame] | 426 | |
Greg Ward | 5322f00 | 2000-05-31 01:09:52 +0000 | [diff] [blame] | 427 | self.compiler.link_shared_object ( |
| 428 | objects, ext_filename, |
| 429 | libraries=ext.libraries, |
| 430 | library_dirs=ext.library_dirs, |
| 431 | runtime_library_dirs=ext.runtime_library_dirs, |
| 432 | extra_postargs=extra_args, |
Greg Ward | cc623a2 | 2000-06-28 01:29:37 +0000 | [diff] [blame] | 433 | debug=self.debug, |
| 434 | build_temp=self.build_temp) |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 435 | |
| 436 | # build_extensions () |
| 437 | |
| 438 | |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 439 | def swig_sources (self, sources): |
| 440 | |
| 441 | """Walk the list of source files in 'sources', looking for SWIG |
| 442 | interface (.i) files. Run SWIG on all that are found, and |
| 443 | return a modified 'sources' list with SWIG source files replaced |
| 444 | by the generated C (or C++) files. |
| 445 | """ |
| 446 | |
| 447 | new_sources = [] |
| 448 | swig_sources = [] |
| 449 | swig_targets = {} |
| 450 | |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 451 | # XXX this drops generated C/C++ files into the source tree, which |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 452 | # is fine for developers who want to distribute the generated |
| 453 | # source -- but there should be an option to put SWIG output in |
| 454 | # the temp dir. |
| 455 | |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 456 | if self.swig_cpp: |
| 457 | target_ext = '.cpp' |
| 458 | else: |
| 459 | target_ext = '.c' |
| 460 | |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 461 | for source in sources: |
| 462 | (base, ext) = os.path.splitext(source) |
Greg Ward | 5ca84b8 | 2000-06-25 02:10:46 +0000 | [diff] [blame] | 463 | if ext == ".i": # SWIG interface file |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 464 | new_sources.append(base + target_ext) |
Greg Ward | 5ca84b8 | 2000-06-25 02:10:46 +0000 | [diff] [blame] | 465 | swig_sources.append(source) |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 466 | swig_targets[source] = new_sources[-1] |
| 467 | else: |
| 468 | new_sources.append(source) |
| 469 | |
Greg Ward | 5ca84b8 | 2000-06-25 02:10:46 +0000 | [diff] [blame] | 470 | if not swig_sources: |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 471 | return new_sources |
| 472 | |
| 473 | swig = self.find_swig() |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 474 | swig_cmd = [swig, "-python", "-dnone", "-ISWIG"] |
| 475 | if self.swig_cpp: |
| 476 | swig_cmd.append ("-c++") |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 477 | |
| 478 | for source in swig_sources: |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 479 | target = swig_targets[source] |
| 480 | self.announce ("swigging %s to %s" % (source, target)) |
| 481 | self.spawn(swig_cmd + ["-o", target, source]) |
Greg Ward | ffcaf2d | 2000-06-24 00:19:35 +0000 | [diff] [blame] | 482 | |
| 483 | return new_sources |
| 484 | |
| 485 | # swig_sources () |
| 486 | |
| 487 | def find_swig (self): |
| 488 | """Return the name of the SWIG executable. On Unix, this is |
| 489 | just "swig" -- it should be in the PATH. Tries a bit harder on |
| 490 | Windows. |
| 491 | """ |
| 492 | |
| 493 | if os.name == "posix": |
| 494 | return "swig" |
| 495 | elif os.name == "nt": |
| 496 | |
| 497 | # Look for SWIG in its standard installation directory on |
| 498 | # Windows (or so I presume!). If we find it there, great; |
| 499 | # if not, act like Unix and assume it's in the PATH. |
| 500 | for vers in ("1.3", "1.2", "1.1"): |
| 501 | fn = os.path.join("c:\\swig%s" % vers, "swig.exe") |
| 502 | if os.path.isfile (fn): |
| 503 | return fn |
| 504 | else: |
| 505 | return "swig.exe" |
| 506 | |
| 507 | else: |
| 508 | raise DistutilsPlatformError, \ |
| 509 | ("I don't know how to find (much less run) SWIG " |
| 510 | "on platform '%s'") % os.name |
| 511 | |
| 512 | # find_swig () |
| 513 | |
| 514 | |
Greg Ward | f46a688 | 2000-06-25 02:23:11 +0000 | [diff] [blame] | 515 | # -- Hooks 'n hacks ------------------------------------------------ |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 516 | |
Greg Ward | f46a688 | 2000-06-25 02:23:11 +0000 | [diff] [blame] | 517 | def msvc_prelink_hack (self, sources, ext, extra_args): |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 518 | |
| 519 | # XXX this is a kludge! Knowledge of specific compilers or |
| 520 | # platforms really doesn't belong here; in an ideal world, the |
| 521 | # CCompiler interface would provide access to everything in a |
| 522 | # compiler/linker system needs to build Python extensions, and |
| 523 | # we would just do everything nicely and cleanly through that |
| 524 | # interface. However, this is a not an ideal world and the |
| 525 | # CCompiler interface doesn't handle absolutely everything. |
| 526 | # Thus, kludges like this slip in occasionally. (This is no |
| 527 | # excuse for committing more platform- and compiler-specific |
| 528 | # kludges; they are to be avoided if possible!) |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 529 | |
Greg Ward | f46a688 | 2000-06-25 02:23:11 +0000 | [diff] [blame] | 530 | def_file = ext.export_symbol_file |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 531 | |
Greg Ward | f46a688 | 2000-06-25 02:23:11 +0000 | [diff] [blame] | 532 | if def_file is not None: |
| 533 | extra_args.append ('/DEF:' + def_file) |
| 534 | else: |
| 535 | modname = string.split (ext.name, '.')[-1] |
| 536 | extra_args.append('/export:init%s' % modname) |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 537 | |
Greg Ward | f3bd747 | 2000-06-27 01:37:10 +0000 | [diff] [blame] | 538 | # The MSVC linker generates .lib and .exp files, which cannot be |
| 539 | # suppressed by any linker switches. The .lib files may even be |
| 540 | # needed! Make sure they are generated in the temporary build |
| 541 | # directory. Since they have different names for debug and release |
| 542 | # builds, they can go into the same directory. |
Greg Ward | f46a688 | 2000-06-25 02:23:11 +0000 | [diff] [blame] | 543 | implib_file = os.path.join ( |
Greg Ward | b593793 | 2000-06-27 01:43:24 +0000 | [diff] [blame] | 544 | self.implib_dir, |
Greg Ward | f46a688 | 2000-06-25 02:23:11 +0000 | [diff] [blame] | 545 | self.get_ext_libname (ext.name)) |
| 546 | extra_args.append ('/IMPLIB:' + implib_file) |
| 547 | self.mkpath (os.path.dirname (implib_file)) |
| 548 | |
| 549 | # msvc_prelink_hack () |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 550 | |
| 551 | |
| 552 | # -- Name generators ----------------------------------------------- |
| 553 | # (extension names, filenames, whatever) |
| 554 | |
Greg Ward | e51d69e | 2000-03-01 01:43:28 +0000 | [diff] [blame] | 555 | def get_ext_fullname (self, ext_name): |
| 556 | if self.package is None: |
| 557 | return ext_name |
| 558 | else: |
| 559 | return self.package + '.' + ext_name |
| 560 | |
| 561 | def get_ext_filename (self, ext_name): |
Greg Ward | b081e18 | 2000-06-17 23:04:31 +0000 | [diff] [blame] | 562 | """Convert the name of an extension (eg. "foo.bar") into the name |
| 563 | of the file from which it will be loaded (eg. "foo/bar.so", or |
| 564 | "foo\bar.pyd"). |
| 565 | """ |
| 566 | |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 567 | from distutils import sysconfig |
Greg Ward | dbb9625 | 1999-09-21 18:27:12 +0000 | [diff] [blame] | 568 | ext_path = string.split (ext_name, '.') |
Greg Ward | 8422964 | 2000-03-31 03:50:23 +0000 | [diff] [blame] | 569 | # extensions in debug_mode are named 'module_d.pyd' under windows |
| 570 | if os.name == 'nt' and self.debug: |
| 571 | return apply (os.path.join, ext_path) + '_d' + sysconfig.SO |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 572 | return apply (os.path.join, ext_path) + sysconfig.SO |
Greg Ward | e393ddb | 1999-08-14 23:57:49 +0000 | [diff] [blame] | 573 | |
Greg Ward | 8422964 | 2000-03-31 03:50:23 +0000 | [diff] [blame] | 574 | def get_ext_libname (self, ext_name): |
| 575 | # create a filename for the (unneeded) lib-file. |
| 576 | # extensions in debug_mode are named 'module_d.pyd' under windows |
| 577 | ext_path = string.split (ext_name, '.') |
| 578 | if os.name == 'nt' and self.debug: |
| 579 | return apply (os.path.join, ext_path) + '_d.lib' |
| 580 | return apply (os.path.join, ext_path) + '.lib' |
| 581 | |
Greg Ward | fcd974e | 2000-05-25 01:10:04 +0000 | [diff] [blame] | 582 | # class build_ext |