blob: 8eb59b4d2e8075b36fc1590a165f819c099238dd [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
Georg Brandlb533e262008-05-25 18:19:30 +00003from io import StringIO
Ronald Oussoren222e89a2011-05-15 16:46:11 +02004import textwrap
Georg Brandlb533e262008-05-25 18:19:30 +00005
Barry Warsaw8cf4eae2010-10-16 01:04:07 +00006from distutils.core import Distribution
Georg Brandlb533e262008-05-25 18:19:30 +00007from distutils.command.build_ext import build_ext
Tarek Ziadé36797272010-07-22 12:50:05 +00008from distutils import sysconfig
Éric Araujodef15da2011-08-20 06:27:18 +02009from distutils.tests.support import (TempdirManager, LoggingSilencer,
Éric Araujo6e3ad872011-08-21 17:02:07 +020010 copy_xxmodule_c, fixup_build_ext)
Tarek Ziadéb2e36f12009-03-31 22:37:55 +000011from distutils.extension import Extension
Barry Warsaw8cf4eae2010-10-16 01:04:07 +000012from distutils.errors import (
Ned Deilyd13007f2011-06-28 19:43:15 -070013 CompileError, DistutilsPlatformError, DistutilsSetupError,
14 UnknownFileError)
Georg Brandlb533e262008-05-25 18:19:30 +000015
16import unittest
17from test import support
18
Christian Heimes3e7e0692008-11-25 21:21:32 +000019# http://bugs.python.org/issue4373
20# Don't load the xx module more than once.
21ALREADY_TESTED = False
22
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000023
Tarek Ziadé36797272010-07-22 12:50:05 +000024class BuildExtTestCase(TempdirManager,
25 LoggingSilencer,
26 unittest.TestCase):
Georg Brandlb533e262008-05-25 18:19:30 +000027 def setUp(self):
28 # Create a simple test environment
29 # Note that we're making changes to sys.path
Tarek Ziadé38e3d512009-02-27 12:58:56 +000030 super(BuildExtTestCase, self).setUp()
Tarek Ziadéc1375d52009-02-14 14:35:51 +000031 self.tmp_dir = self.mkdtemp()
Tarek Ziadé36797272010-07-22 12:50:05 +000032 self.sys_path = sys.path, sys.path[:]
33 sys.path.append(self.tmp_dir)
Tarek Ziadé38e3d512009-02-27 12:58:56 +000034 if sys.version > "2.6":
35 import site
36 self.old_user_base = site.USER_BASE
37 site.USER_BASE = self.mkdtemp()
38 from distutils.command import build_ext
39 build_ext.USER_BASE = site.USER_BASE
Georg Brandlb533e262008-05-25 18:19:30 +000040
41 def test_build_ext(self):
Christian Heimes3e7e0692008-11-25 21:21:32 +000042 global ALREADY_TESTED
Éric Araujodef15da2011-08-20 06:27:18 +020043 copy_xxmodule_c(self.tmp_dir)
Georg Brandlb533e262008-05-25 18:19:30 +000044 xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
45 xx_ext = Extension('xx', [xx_c])
46 dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
47 dist.package_dir = self.tmp_dir
48 cmd = build_ext(dist)
Éric Araujo6e3ad872011-08-21 17:02:07 +020049 fixup_build_ext(cmd)
Thomas Heller84b7f0c2008-05-26 11:51:44 +000050 if os.name == "nt":
51 # On Windows, we must build a debug version iff running
52 # a debug build of Python
53 cmd.debug = sys.executable.endswith("_d.exe")
Georg Brandlb533e262008-05-25 18:19:30 +000054 cmd.build_lib = self.tmp_dir
55 cmd.build_temp = self.tmp_dir
56
57 old_stdout = sys.stdout
58 if not support.verbose:
59 # silence compiler output
60 sys.stdout = StringIO()
61 try:
62 cmd.ensure_finalized()
63 cmd.run()
64 finally:
65 sys.stdout = old_stdout
66
Christian Heimes3e7e0692008-11-25 21:21:32 +000067 if ALREADY_TESTED:
68 return
69 else:
70 ALREADY_TESTED = True
71
Georg Brandlb533e262008-05-25 18:19:30 +000072 import xx
73
74 for attr in ('error', 'foo', 'new', 'roj'):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000075 self.assertTrue(hasattr(xx, attr))
Georg Brandlb533e262008-05-25 18:19:30 +000076
Ezio Melottib3aedd42010-11-20 19:04:17 +000077 self.assertEqual(xx.foo(2, 5), 7)
78 self.assertEqual(xx.foo(13,15), 28)
79 self.assertEqual(xx.new().demo(), None)
Georg Brandlb533e262008-05-25 18:19:30 +000080 doc = 'This is a template module just for instruction.'
Ezio Melottib3aedd42010-11-20 19:04:17 +000081 self.assertEqual(xx.__doc__, doc)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000082 self.assertTrue(isinstance(xx.Null(), xx.Null))
83 self.assertTrue(isinstance(xx.Str(), xx.Str))
Georg Brandlb533e262008-05-25 18:19:30 +000084
Tarek Ziadé36797272010-07-22 12:50:05 +000085 def tearDown(self):
86 # Get everything back to normal
87 support.unload('xx')
88 sys.path = self.sys_path[0]
89 sys.path[:] = self.sys_path[1]
90 if sys.version > "2.6":
91 import site
92 site.USER_BASE = self.old_user_base
93 from distutils.command import build_ext
94 build_ext.USER_BASE = self.old_user_base
95 super(BuildExtTestCase, self).tearDown()
96
Tarek Ziadé5874ef12009-02-05 22:56:14 +000097 def test_solaris_enable_shared(self):
98 dist = Distribution({'name': 'xx'})
99 cmd = build_ext(dist)
100 old = sys.platform
101
102 sys.platform = 'sunos' # fooling finalize_options
Tarek Ziadé36797272010-07-22 12:50:05 +0000103 from distutils.sysconfig import _config_vars
104 old_var = _config_vars.get('Py_ENABLE_SHARED')
105 _config_vars['Py_ENABLE_SHARED'] = 1
Tarek Ziadé5874ef12009-02-05 22:56:14 +0000106 try:
107 cmd.ensure_finalized()
108 finally:
109 sys.platform = old
110 if old_var is None:
Tarek Ziadé36797272010-07-22 12:50:05 +0000111 del _config_vars['Py_ENABLE_SHARED']
Tarek Ziadé5874ef12009-02-05 22:56:14 +0000112 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000113 _config_vars['Py_ENABLE_SHARED'] = old_var
Tarek Ziadé5874ef12009-02-05 22:56:14 +0000114
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000115 # make sure we get some library dirs under solaris
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000116 self.assertTrue(len(cmd.library_dirs) > 0)
Tarek Ziadé5874ef12009-02-05 22:56:14 +0000117
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000118 def test_user_site(self):
119 # site.USER_SITE was introduced in 2.6
120 if sys.version < '2.6':
121 return
122
123 import site
124 dist = Distribution({'name': 'xx'})
125 cmd = build_ext(dist)
126
Tarek Ziadébe720e02009-05-09 11:55:12 +0000127 # making sure the user option is there
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000128 options = [name for name, short, lable in
129 cmd.user_options]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000130 self.assertTrue('user' in options)
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000131
132 # setting a value
133 cmd.user = 1
134
135 # setting user based lib and include
136 lib = os.path.join(site.USER_BASE, 'lib')
137 incl = os.path.join(site.USER_BASE, 'include')
138 os.mkdir(lib)
139 os.mkdir(incl)
140
141 # let's run finalize
142 cmd.ensure_finalized()
143
144 # see if include_dirs and library_dirs
145 # were set
Éric Araujo6e3ad872011-08-21 17:02:07 +0200146 self.assertIn(lib, cmd.library_dirs)
147 self.assertIn(lib, cmd.rpath)
148 self.assertIn(incl, cmd.include_dirs)
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000149
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000150 def test_optional_extension(self):
151
152 # this extension will fail, but let's ignore this failure
153 # with the optional argument.
154 modules = [Extension('foo', ['xxx'], optional=False)]
155 dist = Distribution({'name': 'xx', 'ext_modules': modules})
156 cmd = build_ext(dist)
157 cmd.ensure_finalized()
Tarek Ziadé30911292009-03-31 22:50:54 +0000158 self.assertRaises((UnknownFileError, CompileError),
159 cmd.run) # should raise an error
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000160
161 modules = [Extension('foo', ['xxx'], optional=True)]
162 dist = Distribution({'name': 'xx', 'ext_modules': modules})
163 cmd = build_ext(dist)
164 cmd.ensure_finalized()
165 cmd.run() # should pass
166
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000167 def test_finalize_options(self):
168 # Make sure Python's include directories (for Python.h, pyconfig.h,
169 # etc.) are in the include search path.
170 modules = [Extension('foo', ['xxx'], optional=False)]
171 dist = Distribution({'name': 'xx', 'ext_modules': modules})
172 cmd = build_ext(dist)
173 cmd.finalize_options()
174
Tarek Ziadé36797272010-07-22 12:50:05 +0000175 from distutils import sysconfig
176 py_include = sysconfig.get_python_inc()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000177 self.assertTrue(py_include in cmd.include_dirs)
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000178
Tarek Ziadé36797272010-07-22 12:50:05 +0000179 plat_py_include = sysconfig.get_python_inc(plat_specific=1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000180 self.assertTrue(plat_py_include in cmd.include_dirs)
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000181
182 # make sure cmd.libraries is turned into a list
183 # if it's a string
184 cmd = build_ext(dist)
185 cmd.libraries = 'my_lib'
186 cmd.finalize_options()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000187 self.assertEqual(cmd.libraries, ['my_lib'])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000188
189 # make sure cmd.library_dirs is turned into a list
190 # if it's a string
191 cmd = build_ext(dist)
192 cmd.library_dirs = 'my_lib_dir'
193 cmd.finalize_options()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000194 self.assertTrue('my_lib_dir' in cmd.library_dirs)
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000195
196 # make sure rpath is turned into a list
197 # if it's a list of os.pathsep's paths
198 cmd = build_ext(dist)
199 cmd.rpath = os.pathsep.join(['one', 'two'])
200 cmd.finalize_options()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000201 self.assertEqual(cmd.rpath, ['one', 'two'])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000202
203 # XXX more tests to perform for win32
204
205 # make sure define is turned into 2-tuples
206 # strings if they are ','-separated strings
207 cmd = build_ext(dist)
208 cmd.define = 'one,two'
209 cmd.finalize_options()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000210 self.assertEqual(cmd.define, [('one', '1'), ('two', '1')])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000211
212 # make sure undef is turned into a list of
213 # strings if they are ','-separated strings
214 cmd = build_ext(dist)
215 cmd.undef = 'one,two'
216 cmd.finalize_options()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000217 self.assertEqual(cmd.undef, ['one', 'two'])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000218
219 # make sure swig_opts is turned into a list
220 cmd = build_ext(dist)
221 cmd.swig_opts = None
222 cmd.finalize_options()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000223 self.assertEqual(cmd.swig_opts, [])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000224
225 cmd = build_ext(dist)
226 cmd.swig_opts = '1 2'
227 cmd.finalize_options()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000228 self.assertEqual(cmd.swig_opts, ['1', '2'])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000229
230 def test_check_extensions_list(self):
231 dist = Distribution()
232 cmd = build_ext(dist)
233 cmd.finalize_options()
234
235 #'extensions' option must be a list of Extension instances
Barry Warsaw8cf4eae2010-10-16 01:04:07 +0000236 self.assertRaises(DistutilsSetupError,
237 cmd.check_extensions_list, 'foo')
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000238
239 # each element of 'ext_modules' option must be an
240 # Extension instance or 2-tuple
241 exts = [('bar', 'foo', 'bar'), 'foo']
242 self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
243
244 # first element of each tuple in 'ext_modules'
245 # must be the extension name (a string) and match
246 # a python dotted-separated name
247 exts = [('foo-bar', '')]
248 self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
249
250 # second element of each tuple in 'ext_modules'
251 # must be a ary (build info)
252 exts = [('foo.bar', '')]
253 self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
254
255 # ok this one should pass
256 exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
257 'some': 'bar'})]
258 cmd.check_extensions_list(exts)
259 ext = exts[0]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000260 self.assertTrue(isinstance(ext, Extension))
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000261
262 # check_extensions_list adds in ext the values passed
263 # when they are in ('include_dirs', 'library_dirs', 'libraries'
264 # 'extra_objects', 'extra_compile_args', 'extra_link_args')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000265 self.assertEqual(ext.libraries, 'foo')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000266 self.assertTrue(not hasattr(ext, 'some'))
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000267
268 # 'macros' element of build info dict must be 1- or 2-tuple
269 exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
270 'some': 'bar', 'macros': [('1', '2', '3'), 'foo']})]
271 self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
272
273 exts[0][1]['macros'] = [('1', '2'), ('3',)]
274 cmd.check_extensions_list(exts)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000275 self.assertEqual(exts[0].undef_macros, ['3'])
276 self.assertEqual(exts[0].define_macros, [('1', '2')])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000277
278 def test_get_source_files(self):
279 modules = [Extension('foo', ['xxx'], optional=False)]
280 dist = Distribution({'name': 'xx', 'ext_modules': modules})
281 cmd = build_ext(dist)
282 cmd.ensure_finalized()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000283 self.assertEqual(cmd.get_source_files(), ['xxx'])
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000284
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000285 def test_compiler_option(self):
286 # cmd.compiler is an option and
287 # should not be overriden by a compiler instance
288 # when the command is run
289 dist = Distribution()
290 cmd = build_ext(dist)
291 cmd.compiler = 'unix'
292 cmd.ensure_finalized()
293 cmd.run()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000294 self.assertEqual(cmd.compiler, 'unix')
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000295
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000296 def test_get_outputs(self):
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000297 tmp_dir = self.mkdtemp()
298 c_file = os.path.join(tmp_dir, 'foo.c')
Victor Stinner3e2b7172010-11-09 09:32:19 +0000299 self.write_file(c_file, 'void PyInit_foo(void) {}\n')
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000300 ext = Extension('foo', [c_file], optional=False)
301 dist = Distribution({'name': 'xx',
302 'ext_modules': [ext]})
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000303 cmd = build_ext(dist)
Éric Araujo6e3ad872011-08-21 17:02:07 +0200304 fixup_build_ext(cmd)
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000305 cmd.ensure_finalized()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000306 self.assertEqual(len(cmd.get_outputs()), 1)
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000307
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000308 if os.name == "nt":
309 cmd.debug = sys.executable.endswith("_d.exe")
310
311 cmd.build_lib = os.path.join(self.tmp_dir, 'build')
312 cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
313
314 # issue #5977 : distutils build_ext.get_outputs
315 # returns wrong result with --inplace
Tarek Ziadé4210c6e2009-05-14 20:20:47 +0000316 other_tmp_dir = os.path.realpath(self.mkdtemp())
317 old_wd = os.getcwd()
318 os.chdir(other_tmp_dir)
319 try:
320 cmd.inplace = 1
321 cmd.run()
322 so_file = cmd.get_outputs()[0]
323 finally:
324 os.chdir(old_wd)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000325 self.assertTrue(os.path.exists(so_file))
Barry Warsaw35f3a2c2010-09-03 18:30:30 +0000326 so_ext = sysconfig.get_config_var('SO')
327 self.assertTrue(so_file.endswith(so_ext))
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000328 so_dir = os.path.dirname(so_file)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000329 self.assertEqual(so_dir, other_tmp_dir)
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000330
331 cmd.inplace = 0
Tarek Ziadé36797272010-07-22 12:50:05 +0000332 cmd.compiler = None
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000333 cmd.run()
334 so_file = cmd.get_outputs()[0]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000335 self.assertTrue(os.path.exists(so_file))
Barry Warsaw35f3a2c2010-09-03 18:30:30 +0000336 self.assertTrue(so_file.endswith(so_ext))
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000337 so_dir = os.path.dirname(so_file)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000338 self.assertEqual(so_dir, cmd.build_lib)
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000339
Tarek Ziadé822eb842009-05-19 16:22:57 +0000340 # inplace = 0, cmd.package = 'bar'
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000341 build_py = cmd.get_finalized_command('build_py')
342 build_py.package_dir = {'': 'bar'}
Tarek Ziadé822eb842009-05-19 16:22:57 +0000343 path = cmd.get_ext_fullpath('foo')
Tarek Ziadé0156f912009-06-29 16:19:22 +0000344 # checking that the last directory is the build_dir
Tarek Ziadé822eb842009-05-19 16:22:57 +0000345 path = os.path.split(path)[0]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000346 self.assertEqual(path, cmd.build_lib)
Tarek Ziadé822eb842009-05-19 16:22:57 +0000347
348 # inplace = 1, cmd.package = 'bar'
349 cmd.inplace = 1
350 other_tmp_dir = os.path.realpath(self.mkdtemp())
351 old_wd = os.getcwd()
352 os.chdir(other_tmp_dir)
353 try:
354 path = cmd.get_ext_fullpath('foo')
355 finally:
356 os.chdir(old_wd)
357 # checking that the last directory is bar
358 path = os.path.split(path)[0]
359 lastdir = os.path.split(path)[-1]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000360 self.assertEqual(lastdir, 'bar')
Tarek Ziadé822eb842009-05-19 16:22:57 +0000361
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000362 def test_ext_fullpath(self):
Tarek Ziadéb7815e32009-07-10 09:14:31 +0000363 ext = sysconfig.get_config_vars()['SO']
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000364 # building lxml.etree inplace
365 #etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
366 #etree_ext = Extension('lxml.etree', [etree_c])
367 #dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
368 dist = Distribution()
Tarek Ziadé0156f912009-06-29 16:19:22 +0000369 cmd = build_ext(dist)
370 cmd.inplace = 1
371 cmd.distribution.package_dir = {'': 'src'}
372 cmd.distribution.packages = ['lxml', 'lxml.html']
373 curdir = os.getcwd()
Tarek Ziadéb7815e32009-07-10 09:14:31 +0000374 wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
Tarek Ziadé0156f912009-06-29 16:19:22 +0000375 path = cmd.get_ext_fullpath('lxml.etree')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000376 self.assertEqual(wanted, path)
Tarek Ziadé0156f912009-06-29 16:19:22 +0000377
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000378 # building lxml.etree not inplace
379 cmd.inplace = 0
380 cmd.build_lib = os.path.join(curdir, 'tmpdir')
Tarek Ziadéb7815e32009-07-10 09:14:31 +0000381 wanted = os.path.join(curdir, 'tmpdir', 'lxml', 'etree' + ext)
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000382 path = cmd.get_ext_fullpath('lxml.etree')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000383 self.assertEqual(wanted, path)
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000384
385 # building twisted.runner.portmap not inplace
386 build_py = cmd.get_finalized_command('build_py')
387 build_py.package_dir = {}
388 cmd.distribution.packages = ['twisted', 'twisted.runner.portmap']
389 path = cmd.get_ext_fullpath('twisted.runner.portmap')
390 wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner',
Tarek Ziadéb7815e32009-07-10 09:14:31 +0000391 'portmap' + ext)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000392 self.assertEqual(wanted, path)
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000393
394 # building twisted.runner.portmap inplace
395 cmd.inplace = 1
396 path = cmd.get_ext_fullpath('twisted.runner.portmap')
Tarek Ziadéb7815e32009-07-10 09:14:31 +0000397 wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000398 self.assertEqual(wanted, path)
Tarek Ziadée10d6de2009-07-03 08:33:28 +0000399
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200400
401 @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX')
Ned Deilyd13007f2011-06-28 19:43:15 -0700402 def test_deployment_target_default(self):
403 # Issue 9516: Test that, in the absence of the environment variable,
404 # an extension module is compiled with the same deployment target as
405 # the interpreter.
406 self._try_compile_deployment_target('==', None)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200407
Ned Deilyd13007f2011-06-28 19:43:15 -0700408 @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX')
409 def test_deployment_target_too_low(self):
410 # Issue 9516: Test that an extension module is not allowed to be
411 # compiled with a deployment target less than that of the interpreter.
412 self.assertRaises(DistutilsPlatformError,
413 self._try_compile_deployment_target, '>', '10.1')
414
415 @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX')
416 def test_deployment_target_higher_ok(self):
417 # Issue 9516: Test that an extension module can be compiled with a
418 # deployment target higher than that of the interpreter: the ext
419 # module may depend on some newer OS feature.
420 deptarget = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
421 if deptarget:
422 # increment the minor version number (i.e. 10.6 -> 10.7)
423 deptarget = [int(x) for x in deptarget.split('.')]
424 deptarget[-1] += 1
425 deptarget = '.'.join(str(i) for i in deptarget)
426 self._try_compile_deployment_target('<', deptarget)
427
428 def _try_compile_deployment_target(self, operator, target):
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200429 orig_environ = os.environ
430 os.environ = orig_environ.copy()
431 self.addCleanup(setattr, os, 'environ', orig_environ)
432
Ned Deilyd13007f2011-06-28 19:43:15 -0700433 if target is None:
434 if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
435 del os.environ['MACOSX_DEPLOYMENT_TARGET']
436 else:
437 os.environ['MACOSX_DEPLOYMENT_TARGET'] = target
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200438
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200439 deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')
440
441 with open(deptarget_c, 'w') as fp:
442 fp.write(textwrap.dedent('''\
443 #include <AvailabilityMacros.h>
444
445 int dummy;
446
Ned Deilyd13007f2011-06-28 19:43:15 -0700447 #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED
448 #else
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200449 #error "Unexpected target"
450 #endif
451
Ned Deilyd13007f2011-06-28 19:43:15 -0700452 ''' % operator))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200453
Ned Deilyd13007f2011-06-28 19:43:15 -0700454 # get the deployment target that the interpreter was built with
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200455 target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
456 target = tuple(map(int, target.split('.')))
457 target = '%02d%01d0' % target
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200458 deptarget_ext = Extension(
459 'deptarget',
460 [deptarget_c],
461 extra_compile_args=['-DTARGET=%s'%(target,)],
462 )
463 dist = Distribution({
464 'name': 'deptarget',
465 'ext_modules': [deptarget_ext]
466 })
467 dist.package_dir = self.tmp_dir
468 cmd = build_ext(dist)
469 cmd.build_lib = self.tmp_dir
470 cmd.build_temp = self.tmp_dir
471
472 try:
473 old_stdout = sys.stdout
474 if not support.verbose:
475 # silence compiler output
476 sys.stdout = StringIO()
477 try:
478 cmd.ensure_finalized()
479 cmd.run()
480 finally:
481 sys.stdout = old_stdout
482
483 except CompileError:
484 self.fail("Wrong deployment target during compilation")
485
486
Georg Brandlb533e262008-05-25 18:19:30 +0000487def test_suite():
Éric Araujodef15da2011-08-20 06:27:18 +0200488 return unittest.makeSuite(BuildExtTestCase)
Georg Brandlb533e262008-05-25 18:19:30 +0000489
490if __name__ == '__main__':
491 support.run_unittest(test_suite())