blob: 1ad0d5ff58d4f8c69d53c6eee2eb46c984b1a077 [file] [log] [blame]
Greg Warde393ddb1999-08-14 23:57:49 +00001"""distutils.command.build_ext
2
3Implements the Distutils 'build_ext' command, for building extension
Jeremy Hyltona05e2932000-06-28 14:48:01 +00004modules (currently limited to C extensions, should accommodate C++
Greg Warde393ddb1999-08-14 23:57:49 +00005extensions ASAP)."""
6
Neal Norwitz9d72bb42007-04-17 08:48:32 +00007import sys, os, re
Greg Warde393ddb1999-08-14 23:57:49 +00008from distutils.core import Command
Tarek Ziadéedacea32010-01-29 11:41:03 +00009from distutils.errors import *
Tarek Ziadé36797272010-07-22 12:50:05 +000010from distutils.sysconfig import customize_compiler, get_python_version
Vinay Sajip8cffdd92012-07-16 18:30:03 +010011from distutils.sysconfig import get_config_h_filename
Greg Ward52134002000-05-26 01:31:53 +000012from distutils.dep_util import newer_group
Greg Ward5322f002000-05-31 01:09:52 +000013from distutils.extension import Extension
Tarek Ziadé36797272010-07-22 12:50:05 +000014from distutils.util import get_platform
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000015from distutils import log
Greg Warde393ddb1999-08-14 23:57:49 +000016
Tarek Ziadé38e3d512009-02-27 12:58:56 +000017# this keeps compatibility from 2.3 to 2.5
18if sys.version < "2.6":
19 USER_BASE = None
20 HAS_USER_SITE = False
21else:
22 from site import USER_BASE
23 HAS_USER_SITE = True
24
Christian Heimescbf3b5c2007-12-03 21:02:03 +000025if os.name == 'nt':
26 from distutils.msvccompiler import get_build_version
27 MSVC_VERSION = int(get_build_version())
28
Greg Warddbb96251999-09-21 18:27:12 +000029# An extension name is just a dot-separated list of Python NAMEs (ie.
30# the same as a fully-qualified module name).
31extension_name_re = re.compile \
32 (r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
Greg Warde393ddb1999-08-14 23:57:49 +000033
34
Greg Ward34593812000-06-24 01:23:37 +000035def show_compilers ():
36 from distutils.ccompiler import show_compilers
37 show_compilers()
38
39
Collin Winter5b7e9d72007-08-30 03:52:21 +000040class build_ext(Command):
Fred Drake21d45352001-12-06 21:01:19 +000041
Greg Ward37bc8152000-01-30 18:34:15 +000042 description = "build C/C++ extensions (compile/link to build directory)"
43
Greg Warde393ddb1999-08-14 23:57:49 +000044 # XXX thoughts on how to deal with complex command-line options like
45 # these, i.e. how to make it so fancy_getopt can suck them off the
46 # command line and make it look like setup.py defined the appropriate
47 # lists of tuples of what-have-you.
48 # - each command needs a callback to process its command-line options
49 # - Command.__init__() needs access to its share of the whole
50 # command line (must ultimately come from
51 # Distribution.parse_command_line())
52 # - it then calls the current command class' option-parsing
53 # callback to deal with weird options like -D, which have to
54 # parse the option text and churn out some custom data
55 # structure
56 # - that data structure (in this case, a list of 2-tuples)
57 # will then be present in the command object by the time
Greg Warde01149c2000-02-18 00:35:22 +000058 # we get to finalize_options() (i.e. the constructor
Greg Warde393ddb1999-08-14 23:57:49 +000059 # takes care of both command-line and client options
Greg Warde01149c2000-02-18 00:35:22 +000060 # in between initialize_options() and finalize_options())
Greg Warde393ddb1999-08-14 23:57:49 +000061
Greg Ward9ae7c3c2000-09-16 01:44:45 +000062 sep_by = " (separated by '%s')" % os.pathsep
Greg Wardbbeceea2000-02-18 00:25:39 +000063 user_options = [
Greg Warde51d69e2000-03-01 01:43:28 +000064 ('build-lib=', 'b',
Greg Wardbbeceea2000-02-18 00:25:39 +000065 "directory for compiled extension modules"),
Greg Warde51d69e2000-03-01 01:43:28 +000066 ('build-temp=', 't',
67 "directory for temporary files (build by-products)"),
Christian Heimes5e696852008-04-09 08:37:03 +000068 ('plat-name=', 'p',
69 "platform name to cross-compile for, if supported "
70 "(default: %s)" % get_platform()),
Greg Warde51d69e2000-03-01 01:43:28 +000071 ('inplace', 'i',
Greg Ward6a2035d2000-09-06 02:06:27 +000072 "ignore build-lib and put compiled extensions into the source " +
Greg Warde51d69e2000-03-01 01:43:28 +000073 "directory alongside your pure Python modules"),
Greg Wardbbeceea2000-02-18 00:25:39 +000074 ('include-dirs=', 'I',
Greg Ward9ae7c3c2000-09-16 01:44:45 +000075 "list of directories to search for header files" + sep_by),
Greg Wardbbeceea2000-02-18 00:25:39 +000076 ('define=', 'D',
77 "C preprocessor macros to define"),
78 ('undef=', 'U',
79 "C preprocessor macros to undefine"),
Greg Wardcdb20ba2000-03-26 21:45:14 +000080 ('libraries=', 'l',
Greg Wardbbeceea2000-02-18 00:25:39 +000081 "external C libraries to link with"),
82 ('library-dirs=', 'L',
Greg Ward9ae7c3c2000-09-16 01:44:45 +000083 "directories to search for external C libraries" + sep_by),
Greg Wardbbeceea2000-02-18 00:25:39 +000084 ('rpath=', 'R',
85 "directories to search for shared C libraries at runtime"),
86 ('link-objects=', 'O',
87 "extra explicit link objects to include in the link"),
88 ('debug', 'g',
89 "compile/link with debugging information"),
Greg Wardc41d6b32000-04-10 00:19:42 +000090 ('force', 'f',
Gregory P. Smith6cd0c432000-05-12 00:34:12 +000091 "forcibly build everything (ignore file timestamps)"),
92 ('compiler=', 'c',
93 "specify the compiler type"),
Greg Wardf3bd7472000-06-27 01:37:10 +000094 ('swig-cpp', None,
95 "make SWIG create C++ files (default is C)"),
Anthony Baxtera0240342004-10-14 10:02:08 +000096 ('swig-opts=', None,
97 "list of SWIG command line options"),
98 ('swig=', None,
99 "path to the SWIG executable"),
Greg Wardbbeceea2000-02-18 00:25:39 +0000100 ]
Greg Ward34593812000-06-24 01:23:37 +0000101
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000102 boolean_options = ['inplace', 'debug', 'force', 'swig-cpp']
103
104 if HAS_USER_SITE:
105 user_options.append(('user', None,
106 "add user include, library and rpath"))
107 boolean_options.append('user')
Greg Ward99b032e2000-09-25 01:41:15 +0000108
Greg Ward9d17a7a2000-06-07 03:00:06 +0000109 help_options = [
110 ('help-compiler', None,
Greg Ward34593812000-06-24 01:23:37 +0000111 "list available compilers", show_compilers),
Greg Ward0419a4f2000-08-01 23:54:29 +0000112 ]
Greg Warde393ddb1999-08-14 23:57:49 +0000113
Collin Winter5b7e9d72007-08-30 03:52:21 +0000114 def initialize_options(self):
Greg Ward71eb8641999-09-08 02:42:30 +0000115 self.extensions = None
Greg Warde51d69e2000-03-01 01:43:28 +0000116 self.build_lib = None
Christian Heimes5e696852008-04-09 08:37:03 +0000117 self.plat_name = None
Greg Warde51d69e2000-03-01 01:43:28 +0000118 self.build_temp = None
119 self.inplace = 0
Greg Ward609a5c81999-09-13 13:55:34 +0000120 self.package = None
121
Greg Warde393ddb1999-08-14 23:57:49 +0000122 self.include_dirs = None
123 self.define = None
124 self.undef = None
Greg Wardcdb20ba2000-03-26 21:45:14 +0000125 self.libraries = None
Greg Warde393ddb1999-08-14 23:57:49 +0000126 self.library_dirs = None
127 self.rpath = None
128 self.link_objects = None
Greg Warde8c6ce42000-02-09 02:20:14 +0000129 self.debug = None
Greg Wardc41d6b32000-04-10 00:19:42 +0000130 self.force = None
Gregory P. Smith6cd0c432000-05-12 00:34:12 +0000131 self.compiler = None
Anthony Baxtera0240342004-10-14 10:02:08 +0000132 self.swig = None
Greg Wardf3bd7472000-06-27 01:37:10 +0000133 self.swig_cpp = None
Anthony Baxtera0240342004-10-14 10:02:08 +0000134 self.swig_opts = None
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000135 self.user = None
Greg Warda6cb8ae1999-09-29 12:49:35 +0000136
Collin Winter5b7e9d72007-08-30 03:52:21 +0000137 def finalize_options(self):
Tarek Ziadé36797272010-07-22 12:50:05 +0000138 from distutils import sysconfig
139
Greg Wardcb1f4c42000-09-30 18:27:54 +0000140 self.set_undefined_options('build',
141 ('build_lib', 'build_lib'),
142 ('build_temp', 'build_temp'),
143 ('compiler', 'compiler'),
144 ('debug', 'debug'),
Christian Heimes5e696852008-04-09 08:37:03 +0000145 ('force', 'force'),
146 ('plat_name', 'plat_name'),
147 )
Greg Warde393ddb1999-08-14 23:57:49 +0000148
Greg Ward609a5c81999-09-13 13:55:34 +0000149 if self.package is None:
Greg Warddbb96251999-09-21 18:27:12 +0000150 self.package = self.distribution.ext_package
151
152 self.extensions = self.distribution.ext_modules
Fred Drake21d45352001-12-06 21:01:19 +0000153
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000154 # Make sure Python's include directories (for Python.h, pyconfig.h,
Greg Ward1561ae12000-04-14 00:50:49 +0000155 # etc.) are in the include search path.
Tarek Ziadé36797272010-07-22 12:50:05 +0000156 py_include = sysconfig.get_python_inc()
157 plat_py_include = sysconfig.get_python_inc(plat_specific=1)
Greg Warde393ddb1999-08-14 23:57:49 +0000158 if self.include_dirs is None:
Greg Warddbb96251999-09-21 18:27:12 +0000159 self.include_dirs = self.distribution.include_dirs or []
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000160 if isinstance(self.include_dirs, str):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000161 self.include_dirs = self.include_dirs.split(os.pathsep)
Greg Warda6cb8ae1999-09-29 12:49:35 +0000162
Vinay Sajipe87acc12012-10-23 20:26:14 +0100163 # If in a virtualenv, add its include directory
164 # Issue 16116
165 if sys.exec_prefix != sys.base_exec_prefix:
166 self.include_dirs.append(os.path.join(sys.exec_prefix, 'include'))
167
Greg Ward1d16a9f2000-03-29 04:13:49 +0000168 # Put the Python "system" include dir at the end, so that
169 # any local include dirs take precedence.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000170 self.include_dirs.append(py_include)
Greg Ward1561ae12000-04-14 00:50:49 +0000171 if plat_py_include != py_include:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000172 self.include_dirs.append(plat_py_include)
Greg Warde393ddb1999-08-14 23:57:49 +0000173
Éric Araujob2f5c0a2012-02-15 16:44:37 +0100174 self.ensure_string_list('libraries')
Greg Warddedd5b52000-03-18 15:21:03 +0000175
Greg Ward84229642000-03-31 03:50:23 +0000176 # Life is easier if we're not forever checking for None, so
177 # simplify these options to empty lists if unset
178 if self.libraries is None:
179 self.libraries = []
180 if self.library_dirs is None:
181 self.library_dirs = []
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000182 elif isinstance(self.library_dirs, str):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000183 self.library_dirs = self.library_dirs.split(os.pathsep)
Andrew M. Kuchling27eba5e2001-02-17 04:48:41 +0000184
Greg Ward84229642000-03-31 03:50:23 +0000185 if self.rpath is None:
186 self.rpath = []
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000187 elif isinstance(self.rpath, str):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000188 self.rpath = self.rpath.split(os.pathsep)
Greg Wardc1854672000-02-05 02:23:16 +0000189
Greg Ward84229642000-03-31 03:50:23 +0000190 # for extensions under windows use different directories
191 # for Release and Debug builds.
192 # also Python's library directory must be appended to library_dirs
193 if os.name == 'nt':
Christian Heimes5e696852008-04-09 08:37:03 +0000194 # the 'libs' directory is for binary installs - we assume that
195 # must be the *native* platform. But we don't really support
196 # cross-compiling via a binary install anyway, so we let it go.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000197 self.library_dirs.append(os.path.join(sys.exec_prefix, 'libs'))
Vinay Sajipe87acc12012-10-23 20:26:14 +0100198 if sys.base_exec_prefix != sys.prefix: # Issue 16116
199 self.library_dirs.append(os.path.join(sys.base_exec_prefix, 'libs'))
Greg Ward84229642000-03-31 03:50:23 +0000200 if self.debug:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000201 self.build_temp = os.path.join(self.build_temp, "Debug")
Greg Ward84229642000-03-31 03:50:23 +0000202 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000203 self.build_temp = os.path.join(self.build_temp, "Release")
Andrew M. Kuchling506f0b12001-01-24 15:43:09 +0000204
Thomas Heller378498d2002-04-25 17:26:37 +0000205 # Append the source distribution include and library directories,
206 # this allows distutils on windows to work in the source tree
Vinay Sajip8cffdd92012-07-16 18:30:03 +0100207 self.include_dirs.append(os.path.dirname(get_config_h_filename()))
208 _sys_home = getattr(sys, '_home', None)
209 if _sys_home:
210 self.library_dirs.append(_sys_home)
Brian Curtin401f9f32012-05-13 11:19:23 -0500211 if MSVC_VERSION >= 9:
Christian Heimes5e696852008-04-09 08:37:03 +0000212 # Use the .lib files for the correct architecture
213 if self.plat_name == 'win32':
214 suffix = ''
215 else:
216 # win-amd64 or win-ia64
217 suffix = self.plat_name[4:]
218 new_lib = os.path.join(sys.exec_prefix, 'PCbuild')
219 if suffix:
220 new_lib = os.path.join(new_lib, suffix)
221 self.library_dirs.append(new_lib)
222
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000223 elif MSVC_VERSION == 8:
224 self.library_dirs.append(os.path.join(sys.exec_prefix,
Éric Araujo60532bd2010-12-15 21:07:22 +0000225 'PC', 'VS8.0'))
Hirokazu Yamamoto95b1cf62008-08-14 07:35:13 +0000226 elif MSVC_VERSION == 7:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000227 self.library_dirs.append(os.path.join(sys.exec_prefix,
Christian Heimes292d3512008-02-03 16:51:08 +0000228 'PC', 'VS7.1'))
Hirokazu Yamamoto95b1cf62008-08-14 07:35:13 +0000229 else:
230 self.library_dirs.append(os.path.join(sys.exec_prefix,
231 'PC', 'VC6'))
Thomas Heller378498d2002-04-25 17:26:37 +0000232
Tim Peters182b5ac2004-07-18 06:16:08 +0000233 # OS/2 (EMX) doesn't support Debug vs Release builds, but has the
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000234 # import libraries in its "Config" subdirectory
235 if os.name == 'os2':
236 self.library_dirs.append(os.path.join(sys.exec_prefix, 'Config'))
237
Tarek Ziadé36797272010-07-22 12:50:05 +0000238 # for extensions under Cygwin and AtheOS Python's library directory must be
Andrew M. Kuchling506f0b12001-01-24 15:43:09 +0000239 # appended to library_dirs
Tarek Ziadé36797272010-07-22 12:50:05 +0000240 if sys.platform[:6] == 'cygwin' or sys.platform[:6] == 'atheos':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000241 if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")):
Andrew M. Kuchling506f0b12001-01-24 15:43:09 +0000242 # building third party extensions
Thomas Heller378498d2002-04-25 17:26:37 +0000243 self.library_dirs.append(os.path.join(sys.prefix, "lib",
Tarek Ziadé36797272010-07-22 12:50:05 +0000244 "python" + get_python_version(),
Thomas Heller378498d2002-04-25 17:26:37 +0000245 "config"))
Andrew M. Kuchling506f0b12001-01-24 15:43:09 +0000246 else:
247 # building python standard extensions
248 self.library_dirs.append('.')
249
Tarek Ziadé5874ef12009-02-05 22:56:14 +0000250 # for extensions under Linux or Solaris with a shared Python library,
Thomas Wouterscf297e42007-02-23 15:07:44 +0000251 # Python's library directory must be appended to library_dirs
Tarek Ziadé36797272010-07-22 12:50:05 +0000252 sysconfig.get_config_var('Py_ENABLE_SHARED')
Victor Stinnere6747472011-08-21 00:39:18 +0200253 if (sys.platform.startswith(('linux', 'gnu', 'sunos'))
Tarek Ziadé36797272010-07-22 12:50:05 +0000254 and sysconfig.get_config_var('Py_ENABLE_SHARED')):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000255 if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000256 # building third party extensions
Tarek Ziadé36797272010-07-22 12:50:05 +0000257 self.library_dirs.append(sysconfig.get_config_var('LIBDIR'))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000258 else:
259 # building python standard extensions
260 self.library_dirs.append('.')
261
Andrew M. Kuchling7620bbd2001-03-17 20:15:41 +0000262 # The argument parsing will result in self.define being a string, but
263 # it has to be a list of 2-tuples. All the preprocessor symbols
264 # specified by the 'define' option will be set to '1'. Multiple
265 # symbols can be separated with commas.
Fred Drake21d45352001-12-06 21:01:19 +0000266
Andrew M. Kuchling7620bbd2001-03-17 20:15:41 +0000267 if self.define:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000268 defines = self.define.split(',')
Amaury Forgeot d'Arc61cb0872008-07-26 20:09:45 +0000269 self.define = [(symbol, '1') for symbol in defines]
Andrew M. Kuchling7620bbd2001-03-17 20:15:41 +0000270
271 # The option for macros to undefine is also a string from the
272 # option parsing, but has to be a list. Multiple symbols can also
273 # be separated with commas here.
274 if self.undef:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000275 self.undef = self.undef.split(',')
Andrew M. Kuchling7620bbd2001-03-17 20:15:41 +0000276
Anthony Baxtera0240342004-10-14 10:02:08 +0000277 if self.swig_opts is None:
278 self.swig_opts = []
279 else:
280 self.swig_opts = self.swig_opts.split(' ')
281
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000282 # Finally add the user include and library directories if requested
283 if self.user:
284 user_include = os.path.join(USER_BASE, "include")
285 user_lib = os.path.join(USER_BASE, "lib")
286 if os.path.isdir(user_include):
287 self.include_dirs.append(user_include)
288 if os.path.isdir(user_lib):
289 self.library_dirs.append(user_lib)
290 self.rpath.append(user_lib)
291
Collin Winter5b7e9d72007-08-30 03:52:21 +0000292 def run(self):
Greg Ward37bc8152000-01-30 18:34:15 +0000293 from distutils.ccompiler import new_compiler
294
Greg Ward5322f002000-05-31 01:09:52 +0000295 # 'self.extensions', as supplied by setup.py, is a list of
296 # Extension instances. See the documentation for Extension (in
Greg Wardb081e182000-06-17 23:04:31 +0000297 # distutils.extension) for details.
Fred Drake21d45352001-12-06 21:01:19 +0000298 #
Greg Ward5322f002000-05-31 01:09:52 +0000299 # For backwards compatibility with Distutils 0.8.2 and earlier, we
300 # also allow the 'extensions' list to be a list of tuples:
Greg Warde393ddb1999-08-14 23:57:49 +0000301 # (ext_name, build_info)
Greg Ward5322f002000-05-31 01:09:52 +0000302 # where build_info is a dictionary containing everything that
303 # Extension instances do except the name, with a few things being
304 # differently named. We convert these 2-tuples to Extension
305 # instances as needed.
Greg Warde393ddb1999-08-14 23:57:49 +0000306
Greg Ward71eb8641999-09-08 02:42:30 +0000307 if not self.extensions:
308 return
Greg Warde393ddb1999-08-14 23:57:49 +0000309
Greg Wardcdb20ba2000-03-26 21:45:14 +0000310 # If we were asked to build any C/C++ libraries, make sure that the
311 # directory where we put them is in the library search path for
312 # linking extensions.
Greg Ward4d16e0a2000-03-29 02:13:09 +0000313 if self.distribution.has_c_libraries():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000314 build_clib = self.get_finalized_command('build_clib')
315 self.libraries.extend(build_clib.get_library_names() or [])
316 self.library_dirs.append(build_clib.build_clib)
Greg Wardcdb20ba2000-03-26 21:45:14 +0000317
Greg Warde393ddb1999-08-14 23:57:49 +0000318 # Setup the CCompiler object that we'll use to do all the
319 # compiling and linking
Tarek Ziadé36797272010-07-22 12:50:05 +0000320 self.compiler = new_compiler(compiler=self.compiler,
321 verbose=self.verbose,
322 dry_run=self.dry_run,
323 force=self.force)
324 customize_compiler(self.compiler)
Christian Heimes5e696852008-04-09 08:37:03 +0000325 # If we are cross-compiling, init the compiler now (if we are not
326 # cross-compiling, init would not hurt, but people may rely on
327 # late initialization of compiler even if they shouldn't...)
328 if os.name == 'nt' and self.plat_name != get_platform():
Tarek Ziadé36797272010-07-22 12:50:05 +0000329 self.compiler.initialize(self.plat_name)
Greg Wardcdb20ba2000-03-26 21:45:14 +0000330
331 # And make sure that any compile/link-related options (which might
332 # come from the command-line or from the setup script) are set in
333 # that CCompiler object -- that way, they automatically apply to
334 # all compiling and linking done here.
Greg Warde393ddb1999-08-14 23:57:49 +0000335 if self.include_dirs is not None:
Tarek Ziadé36797272010-07-22 12:50:05 +0000336 self.compiler.set_include_dirs(self.include_dirs)
Greg Warde393ddb1999-08-14 23:57:49 +0000337 if self.define is not None:
338 # 'define' option is a list of (name,value) tuples
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000339 for (name, value) in self.define:
Tarek Ziadé36797272010-07-22 12:50:05 +0000340 self.compiler.define_macro(name, value)
Greg Warde393ddb1999-08-14 23:57:49 +0000341 if self.undef is not None:
342 for macro in self.undef:
Tarek Ziadé36797272010-07-22 12:50:05 +0000343 self.compiler.undefine_macro(macro)
Greg Wardcdb20ba2000-03-26 21:45:14 +0000344 if self.libraries is not None:
Tarek Ziadé36797272010-07-22 12:50:05 +0000345 self.compiler.set_libraries(self.libraries)
Greg Warde393ddb1999-08-14 23:57:49 +0000346 if self.library_dirs is not None:
Tarek Ziadé36797272010-07-22 12:50:05 +0000347 self.compiler.set_library_dirs(self.library_dirs)
Greg Warde393ddb1999-08-14 23:57:49 +0000348 if self.rpath is not None:
Tarek Ziadé36797272010-07-22 12:50:05 +0000349 self.compiler.set_runtime_library_dirs(self.rpath)
Greg Warde393ddb1999-08-14 23:57:49 +0000350 if self.link_objects is not None:
Tarek Ziadé36797272010-07-22 12:50:05 +0000351 self.compiler.set_link_objects(self.link_objects)
Greg Ward60f64332000-03-02 01:32:21 +0000352
Greg Wardcdb20ba2000-03-26 21:45:14 +0000353 # Now actually compile and link everything.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000354 self.build_extensions()
Greg Warde393ddb1999-08-14 23:57:49 +0000355
Collin Winter5b7e9d72007-08-30 03:52:21 +0000356 def check_extensions_list(self, extensions):
Greg Wardc1854672000-02-05 02:23:16 +0000357 """Ensure that the list of extensions (presumably provided as a
Greg Ward5322f002000-05-31 01:09:52 +0000358 command option 'extensions') is valid, i.e. it is a list of
359 Extension objects. We also support the old-style list of 2-tuples,
360 where the tuples are (ext_name, build_info), which are converted to
361 Extension instances here.
Greg Warde393ddb1999-08-14 23:57:49 +0000362
Greg Ward5322f002000-05-31 01:09:52 +0000363 Raise DistutilsSetupError if the structure is invalid anywhere;
364 just returns otherwise.
365 """
Collin Winter5b7e9d72007-08-30 03:52:21 +0000366 if not isinstance(extensions, list):
367 raise DistutilsSetupError(
368 "'ext_modules' option must be a list of Extension instances")
Fred Drake21d45352001-12-06 21:01:19 +0000369
Collin Winter5b7e9d72007-08-30 03:52:21 +0000370 for i, ext in enumerate(extensions):
Greg Ward5322f002000-05-31 01:09:52 +0000371 if isinstance(ext, Extension):
372 continue # OK! (assume type-checking done
373 # by Extension constructor)
Greg Warde393ddb1999-08-14 23:57:49 +0000374
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000375 if not isinstance(ext, tuple) or len(ext) != 2:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000376 raise DistutilsSetupError(
377 "each element of 'ext_modules' option must be an "
Greg Ward5322f002000-05-31 01:09:52 +0000378 "Extension instance or 2-tuple")
Greg Warde393ddb1999-08-14 23:57:49 +0000379
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000380 ext_name, build_info = ext
381
382 log.warn(("old-style (ext_name, build_info) tuple found in "
383 "ext_modules for extension '%s'"
384 "-- please convert to Extension instance" % ext_name))
385
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000386 if not (isinstance(ext_name, str) and
Greg Ward5322f002000-05-31 01:09:52 +0000387 extension_name_re.match(ext_name)):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000388 raise DistutilsSetupError(
389 "first element of each tuple in 'ext_modules' "
Greg Ward5322f002000-05-31 01:09:52 +0000390 "must be the extension name (a string)")
Greg Warde393ddb1999-08-14 23:57:49 +0000391
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000392 if not isinstance(build_info, dict):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000393 raise DistutilsSetupError(
394 "second element of each tuple in 'ext_modules' "
Greg Ward5322f002000-05-31 01:09:52 +0000395 "must be a dictionary (build info)")
396
397 # OK, the (ext_name, build_info) dict is type-safe: convert it
398 # to an Extension instance.
399 ext = Extension(ext_name, build_info['sources'])
400
401 # Easy stuff: one-to-one mapping from dict elements to
402 # instance attributes.
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000403 for key in ('include_dirs', 'library_dirs', 'libraries',
404 'extra_objects', 'extra_compile_args',
Greg Ward5322f002000-05-31 01:09:52 +0000405 'extra_link_args'):
Greg Wardd8014e62000-08-26 02:21:55 +0000406 val = build_info.get(key)
407 if val is not None:
408 setattr(ext, key, val)
Greg Ward5322f002000-05-31 01:09:52 +0000409
410 # Medium-easy stuff: same syntax/semantics, different names.
411 ext.runtime_library_dirs = build_info.get('rpath')
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000412 if 'def_file' in build_info:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000413 log.warn("'def_file' element of build info dict "
414 "no longer supported")
Greg Ward5322f002000-05-31 01:09:52 +0000415
416 # Non-trivial stuff: 'macros' split into 'define_macros'
417 # and 'undef_macros'.
418 macros = build_info.get('macros')
419 if macros:
420 ext.define_macros = []
421 ext.undef_macros = []
422 for macro in macros:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000423 if not (isinstance(macro, tuple) and len(macro) in (1, 2)):
424 raise DistutilsSetupError(
425 "'macros' element of build info dict "
426 "must be 1- or 2-tuple")
Greg Ward5322f002000-05-31 01:09:52 +0000427 if len(macro) == 1:
428 ext.undef_macros.append(macro[0])
429 elif len(macro) == 2:
430 ext.define_macros.append(macro)
431
432 extensions[i] = ext
433
Collin Winter5b7e9d72007-08-30 03:52:21 +0000434 def get_source_files(self):
Andrew M. Kuchling23adc9f2000-07-14 13:35:07 +0000435 self.check_extensions_list(self.extensions)
Greg Warda6cb8ae1999-09-29 12:49:35 +0000436 filenames = []
437
438 # Wouldn't it be neat if we knew the names of header files too...
Greg Ward5322f002000-05-31 01:09:52 +0000439 for ext in self.extensions:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000440 filenames.extend(ext.sources)
Greg Warda6cb8ae1999-09-29 12:49:35 +0000441 return filenames
442
Collin Winter5b7e9d72007-08-30 03:52:21 +0000443 def get_outputs(self):
Greg Ward4d16e0a2000-03-29 02:13:09 +0000444 # Sanity check the 'extensions' list -- can't assume this is being
445 # done in the same run as a 'build_extensions()' call (in fact, we
446 # can probably assume that it *isn't*!).
Greg Wardcb1f4c42000-09-30 18:27:54 +0000447 self.check_extensions_list(self.extensions)
Greg Ward4d16e0a2000-03-29 02:13:09 +0000448
449 # And build the list of output (built) filenames. Note that this
450 # ignores the 'inplace' flag, and assumes everything goes in the
451 # "build" tree.
452 outputs = []
Greg Ward5322f002000-05-31 01:09:52 +0000453 for ext in self.extensions:
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000454 outputs.append(self.get_ext_fullpath(ext.name))
Greg Ward4d16e0a2000-03-29 02:13:09 +0000455 return outputs
456
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000457 def build_extensions(self):
Greg Ward4d16e0a2000-03-29 02:13:09 +0000458 # First, sanity-check the 'extensions' list
Greg Wardcb1f4c42000-09-30 18:27:54 +0000459 self.check_extensions_list(self.extensions)
Greg Ward4d16e0a2000-03-29 02:13:09 +0000460
Greg Ward5322f002000-05-31 01:09:52 +0000461 for ext in self.extensions:
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000462 try:
463 self.build_extension(ext)
Tarek Ziadé30911292009-03-31 22:50:54 +0000464 except (CCompilerError, DistutilsError, CompileError) as e:
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000465 if not ext.optional:
466 raise
467 self.warn('building extension "%s" failed: %s' %
468 (ext.name, e))
Greg Warda6cb8ae1999-09-29 12:49:35 +0000469
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000470 def build_extension(self, ext):
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000471 sources = ext.sources
Collin Winter5b7e9d72007-08-30 03:52:21 +0000472 if sources is None or not isinstance(sources, (list, tuple)):
473 raise DistutilsSetupError(
474 "in 'ext_modules' option (extension '%s'), "
475 "'sources' must be present and must be "
476 "a list of source filenames" % ext.name)
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000477 sources = list(sources)
Greg Ward52134002000-05-26 01:31:53 +0000478
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000479 ext_path = self.get_ext_fullpath(ext.name)
Jeremy Hylton09e532b2002-06-12 20:08:56 +0000480 depends = sources + ext.depends
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000481 if not (self.force or newer_group(depends, ext_path, 'newer')):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000482 log.debug("skipping '%s' extension (up-to-date)", ext.name)
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000483 return
484 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000485 log.info("building '%s' extension", ext.name)
Greg Ward5322f002000-05-31 01:09:52 +0000486
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000487 # First, scan the sources for SWIG definition files (.i), run
488 # SWIG on 'em to create .c files, and modify the sources list
489 # accordingly.
Anthony Baxtera0240342004-10-14 10:02:08 +0000490 sources = self.swig_sources(sources, ext)
Greg Ward5322f002000-05-31 01:09:52 +0000491
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000492 # Next, compile the source code to object files.
Greg Ward5322f002000-05-31 01:09:52 +0000493
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000494 # XXX not honouring 'define_macros' or 'undef_macros' -- the
495 # CCompiler API needs to change to accommodate this, and I
496 # want to do one thing at a time!
Greg Warddf9e6b82000-09-17 00:54:58 +0000497
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000498 # Two possible sources for extra compiler arguments:
499 # - 'extra_compile_args' in Extension object
500 # - CFLAGS environment variable (not particularly
501 # elegant, but people seem to expect it and I
502 # guess it's useful)
503 # The environment variable should take precedence, and
504 # any sensible compiler will give precedence to later
505 # command line args. Hence we combine them in order:
506 extra_args = ext.extra_compile_args or []
Greg Ward5322f002000-05-31 01:09:52 +0000507
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000508 macros = ext.define_macros[:]
509 for undef in ext.undef_macros:
510 macros.append((undef,))
Greg Warde393ddb1999-08-14 23:57:49 +0000511
Tarek Ziadé36797272010-07-22 12:50:05 +0000512 objects = self.compiler.compile(sources,
513 output_dir=self.build_temp,
514 macros=macros,
515 include_dirs=ext.include_dirs,
516 debug=self.debug,
517 extra_postargs=extra_args,
518 depends=ext.depends)
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000519
Fred Drake9028d0a2001-12-06 22:59:54 +0000520 # XXX -- this is a Vile HACK!
521 #
522 # The setup.py script for Python on Unix needs to be able to
523 # get this list so it can perform all the clean up needed to
524 # avoid keeping object files around when cleaning out a failed
525 # build of an extension module. Since Distutils does not
526 # track dependencies, we have to get rid of intermediates to
527 # ensure all the intermediates will be properly re-built.
528 #
529 self._built_objects = objects[:]
530
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000531 # Now link the object files together into a "shared object" --
532 # of course, first we have to figure out all the other things
533 # that go into the mix.
534 if ext.extra_objects:
535 objects.extend(ext.extra_objects)
536 extra_args = ext.extra_link_args or []
Greg Ward968d8832000-02-10 02:17:06 +0000537
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000538 # Detect target language, if not provided
Tarek Ziadé36797272010-07-22 12:50:05 +0000539 language = ext.language or self.compiler.detect_language(sources)
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000540
Tarek Ziadé36797272010-07-22 12:50:05 +0000541 self.compiler.link_shared_object(
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000542 objects, ext_path,
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000543 libraries=self.get_libraries(ext),
544 library_dirs=ext.library_dirs,
545 runtime_library_dirs=ext.runtime_library_dirs,
546 extra_postargs=extra_args,
Fred Drake21d45352001-12-06 21:01:19 +0000547 export_symbols=self.get_export_symbols(ext),
Marc-André Lemburg49c99422001-01-26 18:00:48 +0000548 debug=self.debug,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000549 build_temp=self.build_temp,
550 target_lang=language)
Greg Warde393ddb1999-08-14 23:57:49 +0000551
Collin Winter5b7e9d72007-08-30 03:52:21 +0000552 def swig_sources(self, sources, extension):
Greg Wardffcaf2d2000-06-24 00:19:35 +0000553 """Walk the list of source files in 'sources', looking for SWIG
554 interface (.i) files. Run SWIG on all that are found, and
555 return a modified 'sources' list with SWIG source files replaced
556 by the generated C (or C++) files.
557 """
Greg Wardffcaf2d2000-06-24 00:19:35 +0000558 new_sources = []
559 swig_sources = []
560 swig_targets = {}
561
Greg Wardf3bd7472000-06-27 01:37:10 +0000562 # XXX this drops generated C/C++ files into the source tree, which
Greg Wardffcaf2d2000-06-24 00:19:35 +0000563 # is fine for developers who want to distribute the generated
564 # source -- but there should be an option to put SWIG output in
565 # the temp dir.
566
Greg Wardf3bd7472000-06-27 01:37:10 +0000567 if self.swig_cpp:
Anthony Baxtera0240342004-10-14 10:02:08 +0000568 log.warn("--swig-cpp is deprecated - use --swig-opts=-c++")
569
Guido van Rossumd8faa362007-04-27 19:54:29 +0000570 if self.swig_cpp or ('-c++' in self.swig_opts) or \
571 ('-c++' in extension.swig_opts):
Greg Wardf3bd7472000-06-27 01:37:10 +0000572 target_ext = '.cpp'
573 else:
574 target_ext = '.c'
575
Greg Wardffcaf2d2000-06-24 00:19:35 +0000576 for source in sources:
577 (base, ext) = os.path.splitext(source)
Greg Ward5ca84b82000-06-25 02:10:46 +0000578 if ext == ".i": # SWIG interface file
Thomas Heller5cba76d2002-01-18 20:30:53 +0000579 new_sources.append(base + '_wrap' + target_ext)
Greg Ward5ca84b82000-06-25 02:10:46 +0000580 swig_sources.append(source)
Greg Wardffcaf2d2000-06-24 00:19:35 +0000581 swig_targets[source] = new_sources[-1]
582 else:
583 new_sources.append(source)
584
Greg Ward5ca84b82000-06-25 02:10:46 +0000585 if not swig_sources:
Greg Wardffcaf2d2000-06-24 00:19:35 +0000586 return new_sources
587
Anthony Baxtera0240342004-10-14 10:02:08 +0000588 swig = self.swig or self.find_swig()
Thomas Heller5cba76d2002-01-18 20:30:53 +0000589 swig_cmd = [swig, "-python"]
Anthony Baxtera0240342004-10-14 10:02:08 +0000590 swig_cmd.extend(self.swig_opts)
Greg Wardf3bd7472000-06-27 01:37:10 +0000591 if self.swig_cpp:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000592 swig_cmd.append("-c++")
Greg Wardffcaf2d2000-06-24 00:19:35 +0000593
Anthony Baxtera0240342004-10-14 10:02:08 +0000594 # Do not override commandline arguments
595 if not self.swig_opts:
596 for o in extension.swig_opts:
597 swig_cmd.append(o)
598
Greg Wardffcaf2d2000-06-24 00:19:35 +0000599 for source in swig_sources:
Greg Wardf3bd7472000-06-27 01:37:10 +0000600 target = swig_targets[source]
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000601 log.info("swigging %s to %s", source, target)
Greg Wardf3bd7472000-06-27 01:37:10 +0000602 self.spawn(swig_cmd + ["-o", target, source])
Greg Wardffcaf2d2000-06-24 00:19:35 +0000603
604 return new_sources
605
Collin Winter5b7e9d72007-08-30 03:52:21 +0000606 def find_swig(self):
Greg Wardffcaf2d2000-06-24 00:19:35 +0000607 """Return the name of the SWIG executable. On Unix, this is
608 just "swig" -- it should be in the PATH. Tries a bit harder on
609 Windows.
610 """
Greg Wardffcaf2d2000-06-24 00:19:35 +0000611 if os.name == "posix":
612 return "swig"
613 elif os.name == "nt":
Greg Wardffcaf2d2000-06-24 00:19:35 +0000614 # Look for SWIG in its standard installation directory on
615 # Windows (or so I presume!). If we find it there, great;
616 # if not, act like Unix and assume it's in the PATH.
617 for vers in ("1.3", "1.2", "1.1"):
618 fn = os.path.join("c:\\swig%s" % vers, "swig.exe")
Greg Wardcb1f4c42000-09-30 18:27:54 +0000619 if os.path.isfile(fn):
Greg Wardffcaf2d2000-06-24 00:19:35 +0000620 return fn
621 else:
622 return "swig.exe"
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000623 elif os.name == "os2":
624 # assume swig available in the PATH.
625 return "swig.exe"
Greg Wardffcaf2d2000-06-24 00:19:35 +0000626 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000627 raise DistutilsPlatformError(
628 "I don't know how to find (much less run) SWIG "
629 "on platform '%s'" % os.name)
Fred Drake21d45352001-12-06 21:01:19 +0000630
Greg Wardb081e182000-06-17 23:04:31 +0000631 # -- Name generators -----------------------------------------------
632 # (extension names, filenames, whatever)
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000633 def get_ext_fullpath(self, ext_name):
634 """Returns the path of the filename for a given extension.
635
636 The file is located in `build_lib` or directly in the package
637 (inplace option).
638 """
Tarek Ziadé822eb842009-05-19 16:22:57 +0000639 fullname = self.get_ext_fullname(ext_name)
Tarek Ziadé0156f912009-06-29 16:19:22 +0000640 modpath = fullname.split('.')
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000641 filename = self.get_ext_filename(modpath[-1])
642
Tarek Ziadé822eb842009-05-19 16:22:57 +0000643 if not self.inplace:
644 # no further work needed
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000645 # returning :
646 # build_dir/package/path/filename
647 filename = os.path.join(*modpath[:-1]+[filename])
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000648 return os.path.join(self.build_lib, filename)
Greg Wardb081e182000-06-17 23:04:31 +0000649
Tarek Ziadé822eb842009-05-19 16:22:57 +0000650 # the inplace option requires to find the package directory
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000651 # using the build_py command for that
652 package = '.'.join(modpath[0:-1])
Tarek Ziadé822eb842009-05-19 16:22:57 +0000653 build_py = self.get_finalized_command('build_py')
654 package_dir = os.path.abspath(build_py.get_package_dir(package))
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000655
656 # returning
657 # package_dir/filename
Tarek Ziadé822eb842009-05-19 16:22:57 +0000658 return os.path.join(package_dir, filename)
659
Collin Winter5b7e9d72007-08-30 03:52:21 +0000660 def get_ext_fullname(self, ext_name):
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000661 """Returns the fullname of a given extension name.
662
663 Adds the `package.` prefix"""
Greg Warde51d69e2000-03-01 01:43:28 +0000664 if self.package is None:
665 return ext_name
666 else:
667 return self.package + '.' + ext_name
668
Collin Winter5b7e9d72007-08-30 03:52:21 +0000669 def get_ext_filename(self, ext_name):
Ka-Ping Yee22fcae92001-03-10 09:33:14 +0000670 r"""Convert the name of an extension (eg. "foo.bar") into the name
Greg Wardb081e182000-06-17 23:04:31 +0000671 of the file from which it will be loaded (eg. "foo/bar.so", or
672 "foo\bar.pyd").
673 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000674 from distutils.sysconfig import get_config_var
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000675 ext_path = ext_name.split('.')
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000676 # OS/2 has an 8 character module (extension) limit :-(
677 if os.name == "os2":
678 ext_path[len(ext_path) - 1] = ext_path[len(ext_path) - 1][:8]
Greg Ward84229642000-03-31 03:50:23 +0000679 # extensions in debug_mode are named 'module_d.pyd' under windows
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700680 ext_suffix = get_config_var('EXT_SUFFIX')
Greg Ward84229642000-03-31 03:50:23 +0000681 if os.name == 'nt' and self.debug:
doko@ubuntu.comd5537d02013-03-21 13:21:49 -0700682 return os.path.join(*ext_path) + '_d' + ext_suffix
683 return os.path.join(*ext_path) + ext_suffix
Greg Warde393ddb1999-08-14 23:57:49 +0000684
Collin Winter5b7e9d72007-08-30 03:52:21 +0000685 def get_export_symbols(self, ext):
Greg Ward0419a4f2000-08-01 23:54:29 +0000686 """Return the list of symbols that a shared extension has to
687 export. This either uses 'ext.export_symbols' or, if it's not
Amaury Forgeot d'Arc3b4e4792008-06-11 17:46:10 +0000688 provided, "PyInit_" + module_name. Only relevant on Windows, where
Tarek Ziadé6504c662009-07-11 10:59:56 +0000689 the .pyd file (DLL) must export the module "PyInit_" function.
Greg Ward0419a4f2000-08-01 23:54:29 +0000690 """
Amaury Forgeot d'Arc3b4e4792008-06-11 17:46:10 +0000691 initfunc_name = "PyInit_" + ext.name.split('.')[-1]
Greg Warda35c9312000-08-13 00:42:35 +0000692 if initfunc_name not in ext.export_symbols:
693 ext.export_symbols.append(initfunc_name)
694 return ext.export_symbols
Greg Ward0419a4f2000-08-01 23:54:29 +0000695
Collin Winter5b7e9d72007-08-30 03:52:21 +0000696 def get_libraries(self, ext):
Greg Ward0419a4f2000-08-01 23:54:29 +0000697 """Return the list of libraries to link against when building a
698 shared extension. On most platforms, this is just 'ext.libraries';
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000699 on Windows and OS/2, we add the Python library (eg. python20.dll).
Greg Ward0419a4f2000-08-01 23:54:29 +0000700 """
701 # The python library is always needed on Windows. For MSVC, this
702 # is redundant, since the library is mentioned in a pragma in
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000703 # pyconfig.h that MSVC groks. The other Windows compilers all seem
Greg Ward0419a4f2000-08-01 23:54:29 +0000704 # to need it mentioned explicitly, though, so that's what we do.
Thomas Heller18b9b932000-09-28 19:28:35 +0000705 # Append '_d' to the python import library on debug builds.
Jeremy Hyltonc01b3502002-06-18 18:40:54 +0000706 if sys.platform == "win32":
707 from distutils.msvccompiler import MSVCCompiler
Tarek Ziadé36797272010-07-22 12:50:05 +0000708 if not isinstance(self.compiler, MSVCCompiler):
Jeremy Hyltonc01b3502002-06-18 18:40:54 +0000709 template = "python%d%d"
710 if self.debug:
711 template = template + '_d'
712 pythonlib = (template %
713 (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
714 # don't extend ext.libraries, it may be shared with other
715 # extensions, it is a reference to the original list
716 return ext.libraries + [pythonlib]
Thomas Heller05c93352002-10-31 14:26:37 +0000717 else:
718 return ext.libraries
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000719 elif sys.platform == "os2emx":
720 # EMX/GCC requires the python library explicitly, and I
721 # believe VACPP does as well (though not confirmed) - AIM Apr01
722 template = "python%d%d"
Tim Peters182b5ac2004-07-18 06:16:08 +0000723 # debug versions of the main DLL aren't supported, at least
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000724 # not at this time - AIM Apr01
725 #if self.debug:
726 # template = template + '_d'
727 pythonlib = (template %
728 (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
729 # don't extend ext.libraries, it may be shared with other
730 # extensions, it is a reference to the original list
731 return ext.libraries + [pythonlib]
Andrew M. Kuchling506f0b12001-01-24 15:43:09 +0000732 elif sys.platform[:6] == "cygwin":
733 template = "python%d.%d"
734 pythonlib = (template %
735 (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
736 # don't extend ext.libraries, it may be shared with other
737 # extensions, it is a reference to the original list
738 return ext.libraries + [pythonlib]
Tarek Ziadé36797272010-07-22 12:50:05 +0000739 elif sys.platform[:6] == "atheos":
740 from distutils import sysconfig
741
742 template = "python%d.%d"
743 pythonlib = (template %
744 (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
745 # Get SHLIBS from Makefile
746 extra = []
747 for lib in sysconfig.get_config_var('SHLIBS').split():
748 if lib.startswith('-l'):
749 extra.append(lib[2:])
750 else:
751 extra.append(lib)
752 # don't extend ext.libraries, it may be shared with other
753 # extensions, it is a reference to the original list
754 return ext.libraries + [pythonlib, "m"] + extra
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755 elif sys.platform == 'darwin':
756 # Don't use the default code below
757 return ext.libraries
Antoine Pitrou8e6b4072010-09-10 19:44:44 +0000758 elif sys.platform[:3] == 'aix':
759 # Don't use the default code below
760 return ext.libraries
Greg Ward0419a4f2000-08-01 23:54:29 +0000761 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000762 from distutils import sysconfig
763 if sysconfig.get_config_var('Py_ENABLE_SHARED'):
Barry Warsaw8cf4eae2010-10-16 01:04:07 +0000764 pythonlib = 'python{}.{}{}'.format(
765 sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff,
766 sys.abiflags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 return ext.libraries + [pythonlib]
768 else:
769 return ext.libraries