| 1 | n/a | """Build extension modules.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import re |
|---|
| 5 | n/a | import sys |
|---|
| 6 | n/a | import site |
|---|
| 7 | n/a | import sysconfig |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | from packaging.util import get_platform |
|---|
| 10 | n/a | from packaging.command.cmd import Command |
|---|
| 11 | n/a | from packaging.errors import (CCompilerError, CompileError, PackagingError, |
|---|
| 12 | n/a | PackagingPlatformError, PackagingSetupError) |
|---|
| 13 | n/a | from packaging.compiler import customize_compiler, show_compilers |
|---|
| 14 | n/a | from packaging.util import newer_group |
|---|
| 15 | n/a | from packaging.compiler.extension import Extension |
|---|
| 16 | n/a | from packaging import logger |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | if os.name == 'nt': |
|---|
| 19 | n/a | from packaging.compiler.msvccompiler import get_build_version |
|---|
| 20 | n/a | MSVC_VERSION = int(get_build_version()) |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | # An extension name is just a dot-separated list of Python NAMEs (ie. |
|---|
| 23 | n/a | # the same as a fully-qualified module name). |
|---|
| 24 | n/a | extension_name_re = re.compile \ |
|---|
| 25 | n/a | (r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$') |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | class build_ext(Command): |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | description = "build C/C++ extension modules (compile/link to build directory)" |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | # XXX thoughts on how to deal with complex command-line options like |
|---|
| 33 | n/a | # these, i.e. how to make it so fancy_getopt can suck them off the |
|---|
| 34 | n/a | # command line and turn them into the appropriate |
|---|
| 35 | n/a | # lists of tuples of what-have-you. |
|---|
| 36 | n/a | # - each command needs a callback to process its command-line options |
|---|
| 37 | n/a | # - Command.__init__() needs access to its share of the whole |
|---|
| 38 | n/a | # command line (must ultimately come from |
|---|
| 39 | n/a | # Distribution.parse_command_line()) |
|---|
| 40 | n/a | # - it then calls the current command class' option-parsing |
|---|
| 41 | n/a | # callback to deal with weird options like -D, which have to |
|---|
| 42 | n/a | # parse the option text and churn out some custom data |
|---|
| 43 | n/a | # structure |
|---|
| 44 | n/a | # - that data structure (in this case, a list of 2-tuples) |
|---|
| 45 | n/a | # will then be present in the command object by the time |
|---|
| 46 | n/a | # we get to finalize_options() (i.e. the constructor |
|---|
| 47 | n/a | # takes care of both command-line and client options |
|---|
| 48 | n/a | # in between initialize_options() and finalize_options()) |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | sep_by = " (separated by '%s')" % os.pathsep |
|---|
| 51 | n/a | user_options = [ |
|---|
| 52 | n/a | ('build-lib=', 'b', |
|---|
| 53 | n/a | "directory for compiled extension modules"), |
|---|
| 54 | n/a | ('build-temp=', 't', |
|---|
| 55 | n/a | "directory for temporary files (build by-products)"), |
|---|
| 56 | n/a | ('plat-name=', 'p', |
|---|
| 57 | n/a | "platform name to cross-compile for, if supported " |
|---|
| 58 | n/a | "(default: %s)" % get_platform()), |
|---|
| 59 | n/a | ('inplace', 'i', |
|---|
| 60 | n/a | "ignore build-lib and put compiled extensions into the source " + |
|---|
| 61 | n/a | "directory alongside your pure Python modules"), |
|---|
| 62 | n/a | ('user', None, |
|---|
| 63 | n/a | "add user include, library and rpath"), |
|---|
| 64 | n/a | ('include-dirs=', 'I', |
|---|
| 65 | n/a | "list of directories to search for header files" + sep_by), |
|---|
| 66 | n/a | ('define=', 'D', |
|---|
| 67 | n/a | "C preprocessor macros to define"), |
|---|
| 68 | n/a | ('undef=', 'U', |
|---|
| 69 | n/a | "C preprocessor macros to undefine"), |
|---|
| 70 | n/a | ('libraries=', 'l', |
|---|
| 71 | n/a | "external C libraries to link with"), |
|---|
| 72 | n/a | ('library-dirs=', 'L', |
|---|
| 73 | n/a | "directories to search for external C libraries" + sep_by), |
|---|
| 74 | n/a | ('rpath=', 'R', |
|---|
| 75 | n/a | "directories to search for shared C libraries at runtime"), |
|---|
| 76 | n/a | ('link-objects=', 'O', |
|---|
| 77 | n/a | "extra explicit link objects to include in the link"), |
|---|
| 78 | n/a | ('debug', 'g', |
|---|
| 79 | n/a | "compile/link with debugging information"), |
|---|
| 80 | n/a | ('force', 'f', |
|---|
| 81 | n/a | "forcibly build everything (ignore file timestamps)"), |
|---|
| 82 | n/a | ('compiler=', 'c', |
|---|
| 83 | n/a | "specify the compiler type"), |
|---|
| 84 | n/a | ('swig-opts=', None, |
|---|
| 85 | n/a | "list of SWIG command-line options"), |
|---|
| 86 | n/a | ('swig=', None, |
|---|
| 87 | n/a | "path to the SWIG executable"), |
|---|
| 88 | n/a | ] |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | boolean_options = ['inplace', 'debug', 'force', 'user'] |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | help_options = [ |
|---|
| 94 | n/a | ('help-compiler', None, |
|---|
| 95 | n/a | "list available compilers", show_compilers), |
|---|
| 96 | n/a | ] |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def initialize_options(self): |
|---|
| 99 | n/a | self.extensions = None |
|---|
| 100 | n/a | self.build_lib = None |
|---|
| 101 | n/a | self.plat_name = None |
|---|
| 102 | n/a | self.build_temp = None |
|---|
| 103 | n/a | self.inplace = False |
|---|
| 104 | n/a | self.package = None |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | self.include_dirs = None |
|---|
| 107 | n/a | self.define = None |
|---|
| 108 | n/a | self.undef = None |
|---|
| 109 | n/a | self.libraries = None |
|---|
| 110 | n/a | self.library_dirs = None |
|---|
| 111 | n/a | self.rpath = None |
|---|
| 112 | n/a | self.link_objects = None |
|---|
| 113 | n/a | self.debug = None |
|---|
| 114 | n/a | self.force = None |
|---|
| 115 | n/a | self.compiler = None |
|---|
| 116 | n/a | self.swig = None |
|---|
| 117 | n/a | self.swig_opts = None |
|---|
| 118 | n/a | self.user = None |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def finalize_options(self): |
|---|
| 121 | n/a | self.set_undefined_options('build', |
|---|
| 122 | n/a | 'build_lib', 'build_temp', 'compiler', |
|---|
| 123 | n/a | 'debug', 'force', 'plat_name') |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | if self.package is None: |
|---|
| 126 | n/a | self.package = self.distribution.ext_package |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | # Ensure that the list of extensions is valid, i.e. it is a list of |
|---|
| 129 | n/a | # Extension objects. |
|---|
| 130 | n/a | self.extensions = self.distribution.ext_modules |
|---|
| 131 | n/a | if self.extensions: |
|---|
| 132 | n/a | if not isinstance(self.extensions, (list, tuple)): |
|---|
| 133 | n/a | type_name = (self.extensions is None and 'None' |
|---|
| 134 | n/a | or type(self.extensions).__name__) |
|---|
| 135 | n/a | raise PackagingSetupError( |
|---|
| 136 | n/a | "'ext_modules' must be a sequence of Extension instances," |
|---|
| 137 | n/a | " not %s" % (type_name,)) |
|---|
| 138 | n/a | for i, ext in enumerate(self.extensions): |
|---|
| 139 | n/a | if isinstance(ext, Extension): |
|---|
| 140 | n/a | continue # OK! (assume type-checking done |
|---|
| 141 | n/a | # by Extension constructor) |
|---|
| 142 | n/a | type_name = (ext is None and 'None' or type(ext).__name__) |
|---|
| 143 | n/a | raise PackagingSetupError( |
|---|
| 144 | n/a | "'ext_modules' item %d must be an Extension instance," |
|---|
| 145 | n/a | " not %s" % (i, type_name)) |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | # Make sure Python's include directories (for Python.h, pyconfig.h, |
|---|
| 148 | n/a | # etc.) are in the include search path. |
|---|
| 149 | n/a | py_include = sysconfig.get_path('include') |
|---|
| 150 | n/a | plat_py_include = sysconfig.get_path('platinclude') |
|---|
| 151 | n/a | if self.include_dirs is None: |
|---|
| 152 | n/a | self.include_dirs = self.distribution.include_dirs or [] |
|---|
| 153 | n/a | if isinstance(self.include_dirs, str): |
|---|
| 154 | n/a | self.include_dirs = self.include_dirs.split(os.pathsep) |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | # Put the Python "system" include dir at the end, so that |
|---|
| 157 | n/a | # any local include dirs take precedence. |
|---|
| 158 | n/a | self.include_dirs.append(py_include) |
|---|
| 159 | n/a | if plat_py_include != py_include: |
|---|
| 160 | n/a | self.include_dirs.append(plat_py_include) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | self.ensure_string_list('libraries') |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | # Life is easier if we're not forever checking for None, so |
|---|
| 165 | n/a | # simplify these options to empty lists if unset |
|---|
| 166 | n/a | if self.libraries is None: |
|---|
| 167 | n/a | self.libraries = [] |
|---|
| 168 | n/a | if self.library_dirs is None: |
|---|
| 169 | n/a | self.library_dirs = [] |
|---|
| 170 | n/a | elif isinstance(self.library_dirs, str): |
|---|
| 171 | n/a | self.library_dirs = self.library_dirs.split(os.pathsep) |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | if self.rpath is None: |
|---|
| 174 | n/a | self.rpath = [] |
|---|
| 175 | n/a | elif isinstance(self.rpath, str): |
|---|
| 176 | n/a | self.rpath = self.rpath.split(os.pathsep) |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | # for extensions under windows use different directories |
|---|
| 179 | n/a | # for Release and Debug builds. |
|---|
| 180 | n/a | # also Python's library directory must be appended to library_dirs |
|---|
| 181 | n/a | if os.name == 'nt': |
|---|
| 182 | n/a | # the 'libs' directory is for binary installs - we assume that |
|---|
| 183 | n/a | # must be the *native* platform. But we don't really support |
|---|
| 184 | n/a | # cross-compiling via a binary install anyway, so we let it go. |
|---|
| 185 | n/a | # Note that we must use sys.base_exec_prefix here rather than |
|---|
| 186 | n/a | # exec_prefix, since the Python libs are not copied to a virtual |
|---|
| 187 | n/a | # environment. |
|---|
| 188 | n/a | self.library_dirs.append(os.path.join(sys.base_exec_prefix, 'libs')) |
|---|
| 189 | n/a | if self.debug: |
|---|
| 190 | n/a | self.build_temp = os.path.join(self.build_temp, "Debug") |
|---|
| 191 | n/a | else: |
|---|
| 192 | n/a | self.build_temp = os.path.join(self.build_temp, "Release") |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | # Append the source distribution include and library directories, |
|---|
| 195 | n/a | # this allows distutils on windows to work in the source tree |
|---|
| 196 | n/a | self.include_dirs.append(os.path.join(sys.exec_prefix, 'PC')) |
|---|
| 197 | n/a | if MSVC_VERSION >= 9: |
|---|
| 198 | n/a | # Use the .lib files for the correct architecture |
|---|
| 199 | n/a | if self.plat_name == 'win32': |
|---|
| 200 | n/a | suffix = '' |
|---|
| 201 | n/a | else: |
|---|
| 202 | n/a | # win-amd64 or win-ia64 |
|---|
| 203 | n/a | suffix = self.plat_name[4:] |
|---|
| 204 | n/a | new_lib = os.path.join(sys.exec_prefix, 'PCbuild') |
|---|
| 205 | n/a | if suffix: |
|---|
| 206 | n/a | new_lib = os.path.join(new_lib, suffix) |
|---|
| 207 | n/a | self.library_dirs.append(new_lib) |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | elif MSVC_VERSION == 8: |
|---|
| 210 | n/a | self.library_dirs.append(os.path.join(sys.exec_prefix, |
|---|
| 211 | n/a | 'PC', 'VS8.0')) |
|---|
| 212 | n/a | elif MSVC_VERSION == 7: |
|---|
| 213 | n/a | self.library_dirs.append(os.path.join(sys.exec_prefix, |
|---|
| 214 | n/a | 'PC', 'VS7.1')) |
|---|
| 215 | n/a | else: |
|---|
| 216 | n/a | self.library_dirs.append(os.path.join(sys.exec_prefix, |
|---|
| 217 | n/a | 'PC', 'VC6')) |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | # OS/2 (EMX) doesn't support Debug vs Release builds, but has the |
|---|
| 220 | n/a | # import libraries in its "Config" subdirectory |
|---|
| 221 | n/a | if os.name == 'os2': |
|---|
| 222 | n/a | self.library_dirs.append(os.path.join(sys.exec_prefix, 'Config')) |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | # for extensions under Cygwin and AtheOS Python's library directory must be |
|---|
| 225 | n/a | # appended to library_dirs |
|---|
| 226 | n/a | if sys.platform[:6] == 'cygwin' or sys.platform[:6] == 'atheos': |
|---|
| 227 | n/a | if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")): |
|---|
| 228 | n/a | # building third party extensions |
|---|
| 229 | n/a | self.library_dirs.append(os.path.join(sys.prefix, "lib", |
|---|
| 230 | n/a | "python" + sysconfig.get_python_version(), |
|---|
| 231 | n/a | "config")) |
|---|
| 232 | n/a | else: |
|---|
| 233 | n/a | # building python standard extensions |
|---|
| 234 | n/a | self.library_dirs.append(os.curdir) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | # for extensions under Linux or Solaris with a shared Python library, |
|---|
| 237 | n/a | # Python's library directory must be appended to library_dirs |
|---|
| 238 | n/a | sysconfig.get_config_var('Py_ENABLE_SHARED') |
|---|
| 239 | n/a | if (sys.platform.startswith(('linux', 'gnu', 'sunos')) |
|---|
| 240 | n/a | and sysconfig.get_config_var('Py_ENABLE_SHARED')): |
|---|
| 241 | n/a | if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")): |
|---|
| 242 | n/a | # building third party extensions |
|---|
| 243 | n/a | self.library_dirs.append(sysconfig.get_config_var('LIBDIR')) |
|---|
| 244 | n/a | else: |
|---|
| 245 | n/a | # building python standard extensions |
|---|
| 246 | n/a | self.library_dirs.append(os.curdir) |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | # The argument parsing will result in self.define being a string, but |
|---|
| 249 | n/a | # it has to be a list of 2-tuples. All the preprocessor symbols |
|---|
| 250 | n/a | # specified by the 'define' option will be set to '1'. Multiple |
|---|
| 251 | n/a | # symbols can be separated with commas. |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | if self.define: |
|---|
| 254 | n/a | defines = self.define.split(',') |
|---|
| 255 | n/a | self.define = [(symbol, '1') for symbol in defines] |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | # The option for macros to undefine is also a string from the |
|---|
| 258 | n/a | # option parsing, but has to be a list. Multiple symbols can also |
|---|
| 259 | n/a | # be separated with commas here. |
|---|
| 260 | n/a | if self.undef: |
|---|
| 261 | n/a | self.undef = self.undef.split(',') |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | if self.swig_opts is None: |
|---|
| 264 | n/a | self.swig_opts = [] |
|---|
| 265 | n/a | else: |
|---|
| 266 | n/a | self.swig_opts = self.swig_opts.split(' ') |
|---|
| 267 | n/a | |
|---|
| 268 | n/a | # Finally add the user include and library directories if requested |
|---|
| 269 | n/a | if self.user: |
|---|
| 270 | n/a | user_include = os.path.join(site.USER_BASE, "include") |
|---|
| 271 | n/a | user_lib = os.path.join(site.USER_BASE, "lib") |
|---|
| 272 | n/a | if os.path.isdir(user_include): |
|---|
| 273 | n/a | self.include_dirs.append(user_include) |
|---|
| 274 | n/a | if os.path.isdir(user_lib): |
|---|
| 275 | n/a | self.library_dirs.append(user_lib) |
|---|
| 276 | n/a | self.rpath.append(user_lib) |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | def run(self): |
|---|
| 279 | n/a | from packaging.compiler import new_compiler |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | if not self.extensions: |
|---|
| 282 | n/a | return |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | # If we were asked to build any C/C++ libraries, make sure that the |
|---|
| 285 | n/a | # directory where we put them is in the library search path for |
|---|
| 286 | n/a | # linking extensions. |
|---|
| 287 | n/a | if self.distribution.has_c_libraries(): |
|---|
| 288 | n/a | build_clib = self.get_finalized_command('build_clib') |
|---|
| 289 | n/a | self.libraries.extend(build_clib.get_library_names() or []) |
|---|
| 290 | n/a | self.library_dirs.append(build_clib.build_clib) |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | # Setup the CCompiler object that we'll use to do all the |
|---|
| 293 | n/a | # compiling and linking |
|---|
| 294 | n/a | self.compiler_obj = new_compiler(compiler=self.compiler, |
|---|
| 295 | n/a | dry_run=self.dry_run, |
|---|
| 296 | n/a | force=self.force) |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | customize_compiler(self.compiler_obj) |
|---|
| 299 | n/a | # If we are cross-compiling, init the compiler now (if we are not |
|---|
| 300 | n/a | # cross-compiling, init would not hurt, but people may rely on |
|---|
| 301 | n/a | # late initialization of compiler even if they shouldn't...) |
|---|
| 302 | n/a | if os.name == 'nt' and self.plat_name != get_platform(): |
|---|
| 303 | n/a | self.compiler_obj.initialize(self.plat_name) |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | # And make sure that any compile/link-related options (which might |
|---|
| 306 | n/a | # come from the command line or from the setup script) are set in |
|---|
| 307 | n/a | # that CCompiler object -- that way, they automatically apply to |
|---|
| 308 | n/a | # all compiling and linking done here. |
|---|
| 309 | n/a | if self.include_dirs is not None: |
|---|
| 310 | n/a | self.compiler_obj.set_include_dirs(self.include_dirs) |
|---|
| 311 | n/a | if self.define is not None: |
|---|
| 312 | n/a | # 'define' option is a list of (name,value) tuples |
|---|
| 313 | n/a | for name, value in self.define: |
|---|
| 314 | n/a | self.compiler_obj.define_macro(name, value) |
|---|
| 315 | n/a | if self.undef is not None: |
|---|
| 316 | n/a | for macro in self.undef: |
|---|
| 317 | n/a | self.compiler_obj.undefine_macro(macro) |
|---|
| 318 | n/a | if self.libraries is not None: |
|---|
| 319 | n/a | self.compiler_obj.set_libraries(self.libraries) |
|---|
| 320 | n/a | if self.library_dirs is not None: |
|---|
| 321 | n/a | self.compiler_obj.set_library_dirs(self.library_dirs) |
|---|
| 322 | n/a | if self.rpath is not None: |
|---|
| 323 | n/a | self.compiler_obj.set_runtime_library_dirs(self.rpath) |
|---|
| 324 | n/a | if self.link_objects is not None: |
|---|
| 325 | n/a | self.compiler_obj.set_link_objects(self.link_objects) |
|---|
| 326 | n/a | |
|---|
| 327 | n/a | # Now actually compile and link everything. |
|---|
| 328 | n/a | self.build_extensions() |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | def get_source_files(self): |
|---|
| 331 | n/a | filenames = [] |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | # Wouldn't it be neat if we knew the names of header files too... |
|---|
| 334 | n/a | for ext in self.extensions: |
|---|
| 335 | n/a | filenames.extend(ext.sources) |
|---|
| 336 | n/a | |
|---|
| 337 | n/a | return filenames |
|---|
| 338 | n/a | |
|---|
| 339 | n/a | def get_outputs(self): |
|---|
| 340 | n/a | # And build the list of output (built) filenames. Note that this |
|---|
| 341 | n/a | # ignores the 'inplace' flag, and assumes everything goes in the |
|---|
| 342 | n/a | # "build" tree. |
|---|
| 343 | n/a | outputs = [] |
|---|
| 344 | n/a | for ext in self.extensions: |
|---|
| 345 | n/a | outputs.append(self.get_ext_fullpath(ext.name)) |
|---|
| 346 | n/a | return outputs |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | def build_extensions(self): |
|---|
| 349 | n/a | for ext in self.extensions: |
|---|
| 350 | n/a | try: |
|---|
| 351 | n/a | self.build_extension(ext) |
|---|
| 352 | n/a | except (CCompilerError, PackagingError, CompileError) as e: |
|---|
| 353 | n/a | if not ext.optional: |
|---|
| 354 | n/a | raise |
|---|
| 355 | n/a | logger.warning('%s: building extension %r failed: %s', |
|---|
| 356 | n/a | self.get_command_name(), ext.name, e) |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | def build_extension(self, ext): |
|---|
| 359 | n/a | sources = ext.sources |
|---|
| 360 | n/a | if sources is None or not isinstance(sources, (list, tuple)): |
|---|
| 361 | n/a | raise PackagingSetupError(("in 'ext_modules' option (extension '%s'), " + |
|---|
| 362 | n/a | "'sources' must be present and must be " + |
|---|
| 363 | n/a | "a list of source filenames") % ext.name) |
|---|
| 364 | n/a | sources = list(sources) |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | ext_path = self.get_ext_fullpath(ext.name) |
|---|
| 367 | n/a | depends = sources + ext.depends |
|---|
| 368 | n/a | if not (self.force or newer_group(depends, ext_path, 'newer')): |
|---|
| 369 | n/a | logger.debug("skipping '%s' extension (up-to-date)", ext.name) |
|---|
| 370 | n/a | return |
|---|
| 371 | n/a | else: |
|---|
| 372 | n/a | logger.info("building '%s' extension", ext.name) |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | # First, scan the sources for SWIG definition files (.i), run |
|---|
| 375 | n/a | # SWIG on 'em to create .c files, and modify the sources list |
|---|
| 376 | n/a | # accordingly. |
|---|
| 377 | n/a | sources = self.swig_sources(sources, ext) |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | # Next, compile the source code to object files. |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | # XXX not honouring 'define_macros' or 'undef_macros' -- the |
|---|
| 382 | n/a | # CCompiler API needs to change to accommodate this, and I |
|---|
| 383 | n/a | # want to do one thing at a time! |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | # Two possible sources for extra compiler arguments: |
|---|
| 386 | n/a | # - 'extra_compile_args' in Extension object |
|---|
| 387 | n/a | # - CFLAGS environment variable (not particularly |
|---|
| 388 | n/a | # elegant, but people seem to expect it and I |
|---|
| 389 | n/a | # guess it's useful) |
|---|
| 390 | n/a | # The environment variable should take precedence, and |
|---|
| 391 | n/a | # any sensible compiler will give precedence to later |
|---|
| 392 | n/a | # command-line args. Hence we combine them in order: |
|---|
| 393 | n/a | extra_args = ext.extra_compile_args or [] |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | macros = ext.define_macros[:] |
|---|
| 396 | n/a | for undef in ext.undef_macros: |
|---|
| 397 | n/a | macros.append((undef,)) |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | objects = self.compiler_obj.compile(sources, |
|---|
| 400 | n/a | output_dir=self.build_temp, |
|---|
| 401 | n/a | macros=macros, |
|---|
| 402 | n/a | include_dirs=ext.include_dirs, |
|---|
| 403 | n/a | debug=self.debug, |
|---|
| 404 | n/a | extra_postargs=extra_args, |
|---|
| 405 | n/a | depends=ext.depends) |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | # XXX -- this is a Vile HACK! |
|---|
| 408 | n/a | # |
|---|
| 409 | n/a | # The setup.py script for Python on Unix needs to be able to |
|---|
| 410 | n/a | # get this list so it can perform all the clean up needed to |
|---|
| 411 | n/a | # avoid keeping object files around when cleaning out a failed |
|---|
| 412 | n/a | # build of an extension module. Since Packaging does not |
|---|
| 413 | n/a | # track dependencies, we have to get rid of intermediates to |
|---|
| 414 | n/a | # ensure all the intermediates will be properly re-built. |
|---|
| 415 | n/a | # |
|---|
| 416 | n/a | self._built_objects = objects[:] |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | # Now link the object files together into a "shared object" -- |
|---|
| 419 | n/a | # of course, first we have to figure out all the other things |
|---|
| 420 | n/a | # that go into the mix. |
|---|
| 421 | n/a | if ext.extra_objects: |
|---|
| 422 | n/a | objects.extend(ext.extra_objects) |
|---|
| 423 | n/a | extra_args = ext.extra_link_args or [] |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | # Detect target language, if not provided |
|---|
| 426 | n/a | language = ext.language or self.compiler_obj.detect_language(sources) |
|---|
| 427 | n/a | |
|---|
| 428 | n/a | self.compiler_obj.link_shared_object( |
|---|
| 429 | n/a | objects, ext_path, |
|---|
| 430 | n/a | libraries=self.get_libraries(ext), |
|---|
| 431 | n/a | library_dirs=ext.library_dirs, |
|---|
| 432 | n/a | runtime_library_dirs=ext.runtime_library_dirs, |
|---|
| 433 | n/a | extra_postargs=extra_args, |
|---|
| 434 | n/a | export_symbols=self.get_export_symbols(ext), |
|---|
| 435 | n/a | debug=self.debug, |
|---|
| 436 | n/a | build_temp=self.build_temp, |
|---|
| 437 | n/a | target_lang=language) |
|---|
| 438 | n/a | |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | def swig_sources(self, sources, extension): |
|---|
| 441 | n/a | """Walk the list of source files in 'sources', looking for SWIG |
|---|
| 442 | n/a | interface (.i) files. Run SWIG on all that are found, and |
|---|
| 443 | n/a | return a modified 'sources' list with SWIG source files replaced |
|---|
| 444 | n/a | by the generated C (or C++) files. |
|---|
| 445 | n/a | """ |
|---|
| 446 | n/a | new_sources = [] |
|---|
| 447 | n/a | swig_sources = [] |
|---|
| 448 | n/a | swig_targets = {} |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | # XXX this drops generated C/C++ files into the source tree, which |
|---|
| 451 | n/a | # is fine for developers who want to distribute the generated |
|---|
| 452 | n/a | # source -- but there should be an option to put SWIG output in |
|---|
| 453 | n/a | # the temp dir. |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | if ('-c++' in self.swig_opts or '-c++' in extension.swig_opts): |
|---|
| 456 | n/a | target_ext = '.cpp' |
|---|
| 457 | n/a | else: |
|---|
| 458 | n/a | target_ext = '.c' |
|---|
| 459 | n/a | |
|---|
| 460 | n/a | for source in sources: |
|---|
| 461 | n/a | base, ext = os.path.splitext(source) |
|---|
| 462 | n/a | if ext == ".i": # SWIG interface file |
|---|
| 463 | n/a | new_sources.append(base + '_wrap' + target_ext) |
|---|
| 464 | n/a | swig_sources.append(source) |
|---|
| 465 | n/a | swig_targets[source] = new_sources[-1] |
|---|
| 466 | n/a | else: |
|---|
| 467 | n/a | new_sources.append(source) |
|---|
| 468 | n/a | |
|---|
| 469 | n/a | if not swig_sources: |
|---|
| 470 | n/a | return new_sources |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | swig = self.swig or self.find_swig() |
|---|
| 473 | n/a | swig_cmd = [swig, "-python"] |
|---|
| 474 | n/a | swig_cmd.extend(self.swig_opts) |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | # Do not override commandline arguments |
|---|
| 477 | n/a | if not self.swig_opts: |
|---|
| 478 | n/a | for o in extension.swig_opts: |
|---|
| 479 | n/a | swig_cmd.append(o) |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | for source in swig_sources: |
|---|
| 482 | n/a | target = swig_targets[source] |
|---|
| 483 | n/a | logger.info("swigging %s to %s", source, target) |
|---|
| 484 | n/a | self.spawn(swig_cmd + ["-o", target, source]) |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | return new_sources |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | def find_swig(self): |
|---|
| 489 | n/a | """Return the name of the SWIG executable. On Unix, this is |
|---|
| 490 | n/a | just "swig" -- it should be in the PATH. Tries a bit harder on |
|---|
| 491 | n/a | Windows. |
|---|
| 492 | n/a | """ |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | if os.name == "posix": |
|---|
| 495 | n/a | return "swig" |
|---|
| 496 | n/a | elif os.name == "nt": |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | # Look for SWIG in its standard installation directory on |
|---|
| 499 | n/a | # Windows (or so I presume!). If we find it there, great; |
|---|
| 500 | n/a | # if not, act like Unix and assume it's in the PATH. |
|---|
| 501 | n/a | for vers in ("1.3", "1.2", "1.1"): |
|---|
| 502 | n/a | fn = os.path.join("c:\\swig%s" % vers, "swig.exe") |
|---|
| 503 | n/a | if os.path.isfile(fn): |
|---|
| 504 | n/a | return fn |
|---|
| 505 | n/a | else: |
|---|
| 506 | n/a | return "swig.exe" |
|---|
| 507 | n/a | |
|---|
| 508 | n/a | elif os.name == "os2": |
|---|
| 509 | n/a | # assume swig available in the PATH. |
|---|
| 510 | n/a | return "swig.exe" |
|---|
| 511 | n/a | |
|---|
| 512 | n/a | else: |
|---|
| 513 | n/a | raise PackagingPlatformError(("I don't know how to find (much less run) SWIG " |
|---|
| 514 | n/a | "on platform '%s'") % os.name) |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | # -- Name generators ----------------------------------------------- |
|---|
| 517 | n/a | # (extension names, filenames, whatever) |
|---|
| 518 | n/a | def get_ext_fullpath(self, ext_name): |
|---|
| 519 | n/a | """Returns the path of the filename for a given extension. |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | The file is located in `build_lib` or directly in the package |
|---|
| 522 | n/a | (inplace option). |
|---|
| 523 | n/a | """ |
|---|
| 524 | n/a | fullname = self.get_ext_fullname(ext_name) |
|---|
| 525 | n/a | modpath = fullname.split('.') |
|---|
| 526 | n/a | filename = self.get_ext_filename(modpath[-1]) |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | if not self.inplace: |
|---|
| 529 | n/a | # no further work needed |
|---|
| 530 | n/a | # returning : |
|---|
| 531 | n/a | # build_dir/package/path/filename |
|---|
| 532 | n/a | filename = os.path.join(*modpath[:-1]+[filename]) |
|---|
| 533 | n/a | return os.path.join(self.build_lib, filename) |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | # the inplace option requires to find the package directory |
|---|
| 536 | n/a | # using the build_py command for that |
|---|
| 537 | n/a | package = '.'.join(modpath[0:-1]) |
|---|
| 538 | n/a | build_py = self.get_finalized_command('build_py') |
|---|
| 539 | n/a | package_dir = os.path.abspath(build_py.get_package_dir(package)) |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | # returning |
|---|
| 542 | n/a | # package_dir/filename |
|---|
| 543 | n/a | return os.path.join(package_dir, filename) |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | def get_ext_fullname(self, ext_name): |
|---|
| 546 | n/a | """Returns the fullname of a given extension name. |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | Adds the `package.` prefix""" |
|---|
| 549 | n/a | if self.package is None: |
|---|
| 550 | n/a | return ext_name |
|---|
| 551 | n/a | else: |
|---|
| 552 | n/a | return self.package + '.' + ext_name |
|---|
| 553 | n/a | |
|---|
| 554 | n/a | def get_ext_filename(self, ext_name): |
|---|
| 555 | n/a | r"""Convert the name of an extension (eg. "foo.bar") into the name |
|---|
| 556 | n/a | of the file from which it will be loaded (eg. "foo/bar.so", or |
|---|
| 557 | n/a | "foo\bar.pyd"). |
|---|
| 558 | n/a | """ |
|---|
| 559 | n/a | ext_path = ext_name.split('.') |
|---|
| 560 | n/a | # OS/2 has an 8 character module (extension) limit :-( |
|---|
| 561 | n/a | if os.name == "os2": |
|---|
| 562 | n/a | ext_path[len(ext_path) - 1] = ext_path[len(ext_path) - 1][:8] |
|---|
| 563 | n/a | # extensions in debug_mode are named 'module_d.pyd' under windows |
|---|
| 564 | n/a | so_ext = sysconfig.get_config_var('SO') |
|---|
| 565 | n/a | if os.name == 'nt' and self.debug: |
|---|
| 566 | n/a | return os.path.join(*ext_path) + '_d' + so_ext |
|---|
| 567 | n/a | return os.path.join(*ext_path) + so_ext |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | def get_export_symbols(self, ext): |
|---|
| 570 | n/a | """Return the list of symbols that a shared extension has to |
|---|
| 571 | n/a | export. This either uses 'ext.export_symbols' or, if it's not |
|---|
| 572 | n/a | provided, "init" + module_name. Only relevant on Windows, where |
|---|
| 573 | n/a | the .pyd file (DLL) must export the module "init" function. |
|---|
| 574 | n/a | """ |
|---|
| 575 | n/a | initfunc_name = "PyInit_" + ext.name.split('.')[-1] |
|---|
| 576 | n/a | if initfunc_name not in ext.export_symbols: |
|---|
| 577 | n/a | ext.export_symbols.append(initfunc_name) |
|---|
| 578 | n/a | return ext.export_symbols |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | def get_libraries(self, ext): |
|---|
| 581 | n/a | """Return the list of libraries to link against when building a |
|---|
| 582 | n/a | shared extension. On most platforms, this is just 'ext.libraries'; |
|---|
| 583 | n/a | on Windows and OS/2, we add the Python library (eg. python20.dll). |
|---|
| 584 | n/a | """ |
|---|
| 585 | n/a | # The python library is always needed on Windows. For MSVC, this |
|---|
| 586 | n/a | # is redundant, since the library is mentioned in a pragma in |
|---|
| 587 | n/a | # pyconfig.h that MSVC groks. The other Windows compilers all seem |
|---|
| 588 | n/a | # to need it mentioned explicitly, though, so that's what we do. |
|---|
| 589 | n/a | # Append '_d' to the python import library on debug builds. |
|---|
| 590 | n/a | if sys.platform == "win32": |
|---|
| 591 | n/a | from packaging.compiler.msvccompiler import MSVCCompiler |
|---|
| 592 | n/a | if not isinstance(self.compiler_obj, MSVCCompiler): |
|---|
| 593 | n/a | template = "python%d%d" |
|---|
| 594 | n/a | if self.debug: |
|---|
| 595 | n/a | template = template + '_d' |
|---|
| 596 | n/a | pythonlib = template % sys.version_info[:2] |
|---|
| 597 | n/a | # don't extend ext.libraries, it may be shared with other |
|---|
| 598 | n/a | # extensions, it is a reference to the original list |
|---|
| 599 | n/a | return ext.libraries + [pythonlib] |
|---|
| 600 | n/a | else: |
|---|
| 601 | n/a | return ext.libraries |
|---|
| 602 | n/a | elif sys.platform == "os2emx": |
|---|
| 603 | n/a | # EMX/GCC requires the python library explicitly, and I |
|---|
| 604 | n/a | # believe VACPP does as well (though not confirmed) - AIM Apr01 |
|---|
| 605 | n/a | template = "python%d%d" |
|---|
| 606 | n/a | # debug versions of the main DLL aren't supported, at least |
|---|
| 607 | n/a | # not at this time - AIM Apr01 |
|---|
| 608 | n/a | #if self.debug: |
|---|
| 609 | n/a | # template = template + '_d' |
|---|
| 610 | n/a | pythonlib = template % sys.version_info[:2] |
|---|
| 611 | n/a | # don't extend ext.libraries, it may be shared with other |
|---|
| 612 | n/a | # extensions, it is a reference to the original list |
|---|
| 613 | n/a | return ext.libraries + [pythonlib] |
|---|
| 614 | n/a | elif sys.platform[:6] == "cygwin": |
|---|
| 615 | n/a | template = "python%d.%d" |
|---|
| 616 | n/a | pythonlib = template % sys.version_info[:2] |
|---|
| 617 | n/a | # don't extend ext.libraries, it may be shared with other |
|---|
| 618 | n/a | # extensions, it is a reference to the original list |
|---|
| 619 | n/a | return ext.libraries + [pythonlib] |
|---|
| 620 | n/a | elif sys.platform[:6] == "atheos": |
|---|
| 621 | n/a | template = "python%d.%d" |
|---|
| 622 | n/a | pythonlib = template % sys.version_info[:2] |
|---|
| 623 | n/a | # Get SHLIBS from Makefile |
|---|
| 624 | n/a | extra = [] |
|---|
| 625 | n/a | for lib in sysconfig.get_config_var('SHLIBS').split(): |
|---|
| 626 | n/a | if lib.startswith('-l'): |
|---|
| 627 | n/a | extra.append(lib[2:]) |
|---|
| 628 | n/a | else: |
|---|
| 629 | n/a | extra.append(lib) |
|---|
| 630 | n/a | # don't extend ext.libraries, it may be shared with other |
|---|
| 631 | n/a | # extensions, it is a reference to the original list |
|---|
| 632 | n/a | return ext.libraries + [pythonlib, "m"] + extra |
|---|
| 633 | n/a | |
|---|
| 634 | n/a | elif sys.platform == 'darwin': |
|---|
| 635 | n/a | # Don't use the default code below |
|---|
| 636 | n/a | return ext.libraries |
|---|
| 637 | n/a | |
|---|
| 638 | n/a | else: |
|---|
| 639 | n/a | if sysconfig.get_config_var('Py_ENABLE_SHARED'): |
|---|
| 640 | n/a | template = 'python%d.%d' + sys.abiflags |
|---|
| 641 | n/a | pythonlib = template % sys.version_info[:2] |
|---|
| 642 | n/a | return ext.libraries + [pythonlib] |
|---|
| 643 | n/a | else: |
|---|
| 644 | n/a | return ext.libraries |
|---|