blob: fe6009b6175272443deb28f3bc8bb92e52511c2a [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
Tarek Ziadéd7b5f662009-02-13 16:23:57 +00003import tempfile
Georg Brandlb533e262008-05-25 18:19:30 +00004import shutil
5from io import StringIO
6
7from distutils.core import Extension, Distribution
8from distutils.command.build_ext import build_ext
9from distutils import sysconfig
Tarek Ziadéc1375d52009-02-14 14:35:51 +000010from distutils.tests.support import TempdirManager
Tarek Ziadéb2e36f12009-03-31 22:37:55 +000011from distutils.tests.support import LoggingSilencer
12from distutils.extension import Extension
Tarek Ziadé06fbee12009-05-10 10:34:01 +000013from distutils.errors import (UnknownFileError, DistutilsSetupError,
14 CompileError)
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 +000023def _get_source_filename():
24 srcdir = sysconfig.get_config_var('srcdir')
25 return os.path.join(srcdir, 'Modules', 'xxmodule.c')
26
Tarek Ziadéb2e36f12009-03-31 22:37:55 +000027class BuildExtTestCase(TempdirManager,
28 LoggingSilencer,
29 unittest.TestCase):
Georg Brandlb533e262008-05-25 18:19:30 +000030 def setUp(self):
31 # Create a simple test environment
32 # Note that we're making changes to sys.path
Tarek Ziadé38e3d512009-02-27 12:58:56 +000033 super(BuildExtTestCase, self).setUp()
Tarek Ziadéc1375d52009-02-14 14:35:51 +000034 self.tmp_dir = self.mkdtemp()
Georg Brandlb533e262008-05-25 18:19:30 +000035 self.sys_path = sys.path[:]
36 sys.path.append(self.tmp_dir)
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000037 shutil.copy(_get_source_filename(), self.tmp_dir)
Tarek Ziadé38e3d512009-02-27 12:58:56 +000038 if sys.version > "2.6":
39 import site
40 self.old_user_base = site.USER_BASE
41 site.USER_BASE = self.mkdtemp()
42 from distutils.command import build_ext
43 build_ext.USER_BASE = site.USER_BASE
Georg Brandlb533e262008-05-25 18:19:30 +000044
45 def test_build_ext(self):
Christian Heimes3e7e0692008-11-25 21:21:32 +000046 global ALREADY_TESTED
Georg Brandlb533e262008-05-25 18:19:30 +000047 xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
48 xx_ext = Extension('xx', [xx_c])
49 dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
50 dist.package_dir = self.tmp_dir
51 cmd = build_ext(dist)
Thomas Heller84b7f0c2008-05-26 11:51:44 +000052 if os.name == "nt":
53 # On Windows, we must build a debug version iff running
54 # a debug build of Python
55 cmd.debug = sys.executable.endswith("_d.exe")
Georg Brandlb533e262008-05-25 18:19:30 +000056 cmd.build_lib = self.tmp_dir
57 cmd.build_temp = self.tmp_dir
58
59 old_stdout = sys.stdout
60 if not support.verbose:
61 # silence compiler output
62 sys.stdout = StringIO()
63 try:
64 cmd.ensure_finalized()
65 cmd.run()
66 finally:
67 sys.stdout = old_stdout
68
Christian Heimes3e7e0692008-11-25 21:21:32 +000069 if ALREADY_TESTED:
70 return
71 else:
72 ALREADY_TESTED = True
73
Georg Brandlb533e262008-05-25 18:19:30 +000074 import xx
75
76 for attr in ('error', 'foo', 'new', 'roj'):
77 self.assert_(hasattr(xx, attr))
78
79 self.assertEquals(xx.foo(2, 5), 7)
80 self.assertEquals(xx.foo(13,15), 28)
81 self.assertEquals(xx.new().demo(), None)
82 doc = 'This is a template module just for instruction.'
83 self.assertEquals(xx.__doc__, doc)
84 self.assert_(isinstance(xx.Null(), xx.Null))
85 self.assert_(isinstance(xx.Str(), xx.Str))
86
87 def tearDown(self):
88 # Get everything back to normal
89 support.unload('xx')
90 sys.path = self.sys_path
Tarek Ziadé38e3d512009-02-27 12:58:56 +000091 if sys.version > "2.6":
92 import site
93 site.USER_BASE = self.old_user_base
94 from distutils.command import build_ext
95 build_ext.USER_BASE = self.old_user_base
96 super(BuildExtTestCase, self).tearDown()
Georg Brandlb533e262008-05-25 18:19:30 +000097
Tarek Ziadé5874ef12009-02-05 22:56:14 +000098 def test_solaris_enable_shared(self):
99 dist = Distribution({'name': 'xx'})
100 cmd = build_ext(dist)
101 old = sys.platform
102
103 sys.platform = 'sunos' # fooling finalize_options
104 from distutils.sysconfig import _config_vars
105 old_var = _config_vars.get('Py_ENABLE_SHARED')
106 _config_vars['Py_ENABLE_SHARED'] = 1
107 try:
108 cmd.ensure_finalized()
109 finally:
110 sys.platform = old
111 if old_var is None:
112 del _config_vars['Py_ENABLE_SHARED']
113 else:
114 _config_vars['Py_ENABLE_SHARED'] = old_var
115
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000116 # make sure we get some library dirs under solaris
Tarek Ziadé5874ef12009-02-05 22:56:14 +0000117 self.assert_(len(cmd.library_dirs) > 0)
118
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000119 def test_user_site(self):
120 # site.USER_SITE was introduced in 2.6
121 if sys.version < '2.6':
122 return
123
124 import site
125 dist = Distribution({'name': 'xx'})
126 cmd = build_ext(dist)
127
Tarek Ziadébe720e02009-05-09 11:55:12 +0000128 # making sure the user option is there
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000129 options = [name for name, short, lable in
130 cmd.user_options]
131 self.assert_('user' in options)
132
133 # setting a value
134 cmd.user = 1
135
136 # setting user based lib and include
137 lib = os.path.join(site.USER_BASE, 'lib')
138 incl = os.path.join(site.USER_BASE, 'include')
139 os.mkdir(lib)
140 os.mkdir(incl)
141
142 # let's run finalize
143 cmd.ensure_finalized()
144
145 # see if include_dirs and library_dirs
146 # were set
147 self.assert_(lib in cmd.library_dirs)
Tarek Ziadébe720e02009-05-09 11:55:12 +0000148 self.assert_(lib in cmd.rpath)
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000149 self.assert_(incl in cmd.include_dirs)
150
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000151 def test_optional_extension(self):
152
153 # this extension will fail, but let's ignore this failure
154 # with the optional argument.
155 modules = [Extension('foo', ['xxx'], optional=False)]
156 dist = Distribution({'name': 'xx', 'ext_modules': modules})
157 cmd = build_ext(dist)
158 cmd.ensure_finalized()
Tarek Ziadé30911292009-03-31 22:50:54 +0000159 self.assertRaises((UnknownFileError, CompileError),
160 cmd.run) # should raise an error
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000161
162 modules = [Extension('foo', ['xxx'], optional=True)]
163 dist = Distribution({'name': 'xx', 'ext_modules': modules})
164 cmd = build_ext(dist)
165 cmd.ensure_finalized()
166 cmd.run() # should pass
167
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000168 def test_finalize_options(self):
169 # Make sure Python's include directories (for Python.h, pyconfig.h,
170 # etc.) are in the include search path.
171 modules = [Extension('foo', ['xxx'], optional=False)]
172 dist = Distribution({'name': 'xx', 'ext_modules': modules})
173 cmd = build_ext(dist)
174 cmd.finalize_options()
175
176 from distutils import sysconfig
177 py_include = sysconfig.get_python_inc()
178 self.assert_(py_include in cmd.include_dirs)
179
180 plat_py_include = sysconfig.get_python_inc(plat_specific=1)
181 self.assert_(plat_py_include in cmd.include_dirs)
182
183 # make sure cmd.libraries is turned into a list
184 # if it's a string
185 cmd = build_ext(dist)
186 cmd.libraries = 'my_lib'
187 cmd.finalize_options()
188 self.assertEquals(cmd.libraries, ['my_lib'])
189
190 # make sure cmd.library_dirs is turned into a list
191 # if it's a string
192 cmd = build_ext(dist)
193 cmd.library_dirs = 'my_lib_dir'
194 cmd.finalize_options()
Tarek Ziadéb2f073c2009-05-10 21:31:23 +0000195 self.assert_('my_lib_dir' in cmd.library_dirs)
Tarek Ziadé06fbee12009-05-10 10:34:01 +0000196
197 # make sure rpath is turned into a list
198 # if it's a list of os.pathsep's paths
199 cmd = build_ext(dist)
200 cmd.rpath = os.pathsep.join(['one', 'two'])
201 cmd.finalize_options()
202 self.assertEquals(cmd.rpath, ['one', 'two'])
203
204 # XXX more tests to perform for win32
205
206 # make sure define is turned into 2-tuples
207 # strings if they are ','-separated strings
208 cmd = build_ext(dist)
209 cmd.define = 'one,two'
210 cmd.finalize_options()
211 self.assertEquals(cmd.define, [('one', '1'), ('two', '1')])
212
213 # make sure undef is turned into a list of
214 # strings if they are ','-separated strings
215 cmd = build_ext(dist)
216 cmd.undef = 'one,two'
217 cmd.finalize_options()
218 self.assertEquals(cmd.undef, ['one', 'two'])
219
220 # make sure swig_opts is turned into a list
221 cmd = build_ext(dist)
222 cmd.swig_opts = None
223 cmd.finalize_options()
224 self.assertEquals(cmd.swig_opts, [])
225
226 cmd = build_ext(dist)
227 cmd.swig_opts = '1 2'
228 cmd.finalize_options()
229 self.assertEquals(cmd.swig_opts, ['1', '2'])
230
231 def test_check_extensions_list(self):
232 dist = Distribution()
233 cmd = build_ext(dist)
234 cmd.finalize_options()
235
236 #'extensions' option must be a list of Extension instances
237 self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, 'foo')
238
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]
260 self.assert_(isinstance(ext, Extension))
261
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')
265 self.assertEquals(ext.libraries, 'foo')
266 self.assert_(not hasattr(ext, 'some'))
267
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)
275 self.assertEquals(exts[0].undef_macros, ['3'])
276 self.assertEquals(exts[0].define_macros, [('1', '2')])
277
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()
283 self.assertEquals(cmd.get_source_files(), ['xxx'])
284
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()
294 self.assertEquals(cmd.compiler, 'unix')
295
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')
Tarek Ziadé4e3533e2009-05-13 22:20:49 +0000299 self.write_file(c_file, 'void initfoo(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)
304 cmd.ensure_finalized()
305 self.assertEquals(len(cmd.get_outputs()), 1)
306
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000307 if os.name == "nt":
308 cmd.debug = sys.executable.endswith("_d.exe")
309
310 cmd.build_lib = os.path.join(self.tmp_dir, 'build')
311 cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
312
313 # issue #5977 : distutils build_ext.get_outputs
314 # returns wrong result with --inplace
Tarek Ziadé4210c6e2009-05-14 20:20:47 +0000315 other_tmp_dir = os.path.realpath(self.mkdtemp())
316 old_wd = os.getcwd()
317 os.chdir(other_tmp_dir)
318 try:
319 cmd.inplace = 1
320 cmd.run()
321 so_file = cmd.get_outputs()[0]
322 finally:
323 os.chdir(old_wd)
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000324 self.assert_(os.path.exists(so_file))
Tarek Ziadéd18a84e2009-05-18 08:07:46 +0000325 self.assertEquals(os.path.splitext(so_file)[-1],
326 sysconfig.get_config_var('SO'))
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000327 so_dir = os.path.dirname(so_file)
Tarek Ziadé4210c6e2009-05-14 20:20:47 +0000328 self.assertEquals(so_dir, other_tmp_dir)
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000329
330 cmd.inplace = 0
331 cmd.run()
332 so_file = cmd.get_outputs()[0]
333 self.assert_(os.path.exists(so_file))
Tarek Ziadéd18a84e2009-05-18 08:07:46 +0000334 self.assertEquals(os.path.splitext(so_file)[-1],
335 sysconfig.get_config_var('SO'))
Tarek Ziadéff0e5002009-05-12 17:14:01 +0000336 so_dir = os.path.dirname(so_file)
337 self.assertEquals(so_dir, cmd.build_lib)
338
Tarek Ziadé822eb842009-05-19 16:22:57 +0000339 # inplace = 0, cmd.package = 'bar'
340 cmd.package = 'bar'
341 path = cmd.get_ext_fullpath('foo')
Tarek Ziadé0156f912009-06-29 16:19:22 +0000342 # checking that the last directory is the build_dir
Tarek Ziadé822eb842009-05-19 16:22:57 +0000343 path = os.path.split(path)[0]
Tarek Ziadé0156f912009-06-29 16:19:22 +0000344 self.assertEquals(path, cmd.build_lib)
Tarek Ziadé822eb842009-05-19 16:22:57 +0000345
346 # inplace = 1, cmd.package = 'bar'
347 cmd.inplace = 1
348 other_tmp_dir = os.path.realpath(self.mkdtemp())
349 old_wd = os.getcwd()
350 os.chdir(other_tmp_dir)
351 try:
352 path = cmd.get_ext_fullpath('foo')
353 finally:
354 os.chdir(old_wd)
355 # checking that the last directory is bar
356 path = os.path.split(path)[0]
357 lastdir = os.path.split(path)[-1]
358 self.assertEquals(lastdir, cmd.package)
359
Tarek Ziadé0156f912009-06-29 16:19:22 +0000360 def test_build_ext_inplace(self):
361 etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
362 etree_ext = Extension('lxml.etree', [etree_c])
363 dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
364 cmd = build_ext(dist)
365 cmd.inplace = 1
366 cmd.distribution.package_dir = {'': 'src'}
367 cmd.distribution.packages = ['lxml', 'lxml.html']
368 curdir = os.getcwd()
369 wanted = os.path.join(curdir, 'src', 'lxml', 'etree.so')
370 path = cmd.get_ext_fullpath('lxml.etree')
371 self.assertEquals(wanted, path)
372
Georg Brandlb533e262008-05-25 18:19:30 +0000373def test_suite():
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000374 src = _get_source_filename()
375 if not os.path.exists(src):
Georg Brandlb533e262008-05-25 18:19:30 +0000376 if support.verbose:
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000377 print('test_build_ext: Cannot find source code (test'
378 ' must run in python build dir)')
Georg Brandlb533e262008-05-25 18:19:30 +0000379 return unittest.TestSuite()
380 else: return unittest.makeSuite(BuildExtTestCase)
381
382if __name__ == '__main__':
383 support.run_unittest(test_suite())