test_sysconfig.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. """Tests for distutils.sysconfig."""
  2. import contextlib
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. import textwrap
  8. import unittest
  9. from distutils import sysconfig
  10. from distutils.ccompiler import get_default_compiler
  11. from distutils.tests import support
  12. from test.support import run_unittest, swap_item, requires_subprocess, is_wasi
  13. from test.support.os_helper import TESTFN
  14. from test.support.warnings_helper import check_warnings
  15. class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
  16. def setUp(self):
  17. super(SysconfigTestCase, self).setUp()
  18. self.makefile = None
  19. def tearDown(self):
  20. if self.makefile is not None:
  21. os.unlink(self.makefile)
  22. self.cleanup_testfn()
  23. super(SysconfigTestCase, self).tearDown()
  24. def cleanup_testfn(self):
  25. if os.path.isfile(TESTFN):
  26. os.remove(TESTFN)
  27. elif os.path.isdir(TESTFN):
  28. shutil.rmtree(TESTFN)
  29. @unittest.skipIf(is_wasi, "Incompatible with WASI mapdir and OOT builds")
  30. def test_get_config_h_filename(self):
  31. config_h = sysconfig.get_config_h_filename()
  32. self.assertTrue(os.path.isfile(config_h), config_h)
  33. def test_get_python_lib(self):
  34. # XXX doesn't work on Linux when Python was never installed before
  35. #self.assertTrue(os.path.isdir(lib_dir), lib_dir)
  36. # test for pythonxx.lib?
  37. self.assertNotEqual(sysconfig.get_python_lib(),
  38. sysconfig.get_python_lib(prefix=TESTFN))
  39. def test_get_config_vars(self):
  40. cvars = sysconfig.get_config_vars()
  41. self.assertIsInstance(cvars, dict)
  42. self.assertTrue(cvars)
  43. @unittest.skipIf(is_wasi, "Incompatible with WASI mapdir and OOT builds")
  44. def test_srcdir(self):
  45. # See Issues #15322, #15364.
  46. srcdir = sysconfig.get_config_var('srcdir')
  47. self.assertTrue(os.path.isabs(srcdir), srcdir)
  48. self.assertTrue(os.path.isdir(srcdir), srcdir)
  49. if sysconfig.python_build:
  50. # The python executable has not been installed so srcdir
  51. # should be a full source checkout.
  52. Python_h = os.path.join(srcdir, 'Include', 'Python.h')
  53. self.assertTrue(os.path.exists(Python_h), Python_h)
  54. # <srcdir>/PC/pyconfig.h always exists even if unused on POSIX.
  55. pyconfig_h = os.path.join(srcdir, 'PC', 'pyconfig.h')
  56. self.assertTrue(os.path.exists(pyconfig_h), pyconfig_h)
  57. pyconfig_h_in = os.path.join(srcdir, 'pyconfig.h.in')
  58. self.assertTrue(os.path.exists(pyconfig_h_in), pyconfig_h_in)
  59. elif os.name == 'posix':
  60. self.assertEqual(
  61. os.path.dirname(sysconfig.get_makefile_filename()),
  62. srcdir)
  63. def test_srcdir_independent_of_cwd(self):
  64. # srcdir should be independent of the current working directory
  65. # See Issues #15322, #15364.
  66. srcdir = sysconfig.get_config_var('srcdir')
  67. cwd = os.getcwd()
  68. try:
  69. os.chdir('..')
  70. srcdir2 = sysconfig.get_config_var('srcdir')
  71. finally:
  72. os.chdir(cwd)
  73. self.assertEqual(srcdir, srcdir2)
  74. def customize_compiler(self):
  75. # make sure AR gets caught
  76. class compiler:
  77. compiler_type = 'unix'
  78. def set_executables(self, **kw):
  79. self.exes = kw
  80. sysconfig_vars = {
  81. 'AR': 'sc_ar',
  82. 'CC': 'sc_cc',
  83. 'CXX': 'sc_cxx',
  84. 'ARFLAGS': '--sc-arflags',
  85. 'CFLAGS': '--sc-cflags',
  86. 'CCSHARED': '--sc-ccshared',
  87. 'LDSHARED': 'sc_ldshared',
  88. 'SHLIB_SUFFIX': 'sc_shutil_suffix',
  89. # On macOS, disable _osx_support.customize_compiler()
  90. 'CUSTOMIZED_OSX_COMPILER': 'True',
  91. }
  92. comp = compiler()
  93. with contextlib.ExitStack() as cm:
  94. for key, value in sysconfig_vars.items():
  95. cm.enter_context(swap_item(sysconfig._config_vars, key, value))
  96. sysconfig.customize_compiler(comp)
  97. return comp
  98. @unittest.skipUnless(get_default_compiler() == 'unix',
  99. 'not testing if default compiler is not unix')
  100. def test_customize_compiler(self):
  101. # Make sure that sysconfig._config_vars is initialized
  102. sysconfig.get_config_vars()
  103. os.environ['AR'] = 'env_ar'
  104. os.environ['CC'] = 'env_cc'
  105. os.environ['CPP'] = 'env_cpp'
  106. os.environ['CXX'] = 'env_cxx --env-cxx-flags'
  107. os.environ['LDSHARED'] = 'env_ldshared'
  108. os.environ['LDFLAGS'] = '--env-ldflags'
  109. os.environ['ARFLAGS'] = '--env-arflags'
  110. os.environ['CFLAGS'] = '--env-cflags'
  111. os.environ['CPPFLAGS'] = '--env-cppflags'
  112. comp = self.customize_compiler()
  113. self.assertEqual(comp.exes['archiver'],
  114. 'env_ar --env-arflags')
  115. self.assertEqual(comp.exes['preprocessor'],
  116. 'env_cpp --env-cppflags')
  117. self.assertEqual(comp.exes['compiler'],
  118. 'env_cc --sc-cflags --env-cflags --env-cppflags')
  119. self.assertEqual(comp.exes['compiler_so'],
  120. ('env_cc --sc-cflags '
  121. '--env-cflags ''--env-cppflags --sc-ccshared'))
  122. self.assertEqual(comp.exes['compiler_cxx'],
  123. 'env_cxx --env-cxx-flags')
  124. self.assertEqual(comp.exes['linker_exe'],
  125. 'env_cc')
  126. self.assertEqual(comp.exes['linker_so'],
  127. ('env_ldshared --env-ldflags --env-cflags'
  128. ' --env-cppflags'))
  129. self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
  130. del os.environ['AR']
  131. del os.environ['CC']
  132. del os.environ['CPP']
  133. del os.environ['CXX']
  134. del os.environ['LDSHARED']
  135. del os.environ['LDFLAGS']
  136. del os.environ['ARFLAGS']
  137. del os.environ['CFLAGS']
  138. del os.environ['CPPFLAGS']
  139. comp = self.customize_compiler()
  140. self.assertEqual(comp.exes['archiver'],
  141. 'sc_ar --sc-arflags')
  142. self.assertEqual(comp.exes['preprocessor'],
  143. 'sc_cc -E')
  144. self.assertEqual(comp.exes['compiler'],
  145. 'sc_cc --sc-cflags')
  146. self.assertEqual(comp.exes['compiler_so'],
  147. 'sc_cc --sc-cflags --sc-ccshared')
  148. self.assertEqual(comp.exes['compiler_cxx'],
  149. 'sc_cxx')
  150. self.assertEqual(comp.exes['linker_exe'],
  151. 'sc_cc')
  152. self.assertEqual(comp.exes['linker_so'],
  153. 'sc_ldshared')
  154. self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
  155. def test_parse_makefile_base(self):
  156. self.makefile = TESTFN
  157. fd = open(self.makefile, 'w')
  158. try:
  159. fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n')
  160. fd.write('VAR=$OTHER\nOTHER=foo')
  161. finally:
  162. fd.close()
  163. d = sysconfig.parse_makefile(self.makefile)
  164. self.assertEqual(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'",
  165. 'OTHER': 'foo'})
  166. def test_parse_makefile_literal_dollar(self):
  167. self.makefile = TESTFN
  168. fd = open(self.makefile, 'w')
  169. try:
  170. fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
  171. fd.write('VAR=$OTHER\nOTHER=foo')
  172. finally:
  173. fd.close()
  174. d = sysconfig.parse_makefile(self.makefile)
  175. self.assertEqual(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'",
  176. 'OTHER': 'foo'})
  177. def test_sysconfig_module(self):
  178. import sysconfig as global_sysconfig
  179. self.assertEqual(global_sysconfig.get_config_var('CFLAGS'),
  180. sysconfig.get_config_var('CFLAGS'))
  181. self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'),
  182. sysconfig.get_config_var('LDFLAGS'))
  183. @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'),
  184. 'compiler flags customized')
  185. def test_sysconfig_compiler_vars(self):
  186. # On OS X, binary installers support extension module building on
  187. # various levels of the operating system with differing Xcode
  188. # configurations. This requires customization of some of the
  189. # compiler configuration directives to suit the environment on
  190. # the installed machine. Some of these customizations may require
  191. # running external programs and, so, are deferred until needed by
  192. # the first extension module build. With Python 3.3, only
  193. # the Distutils version of sysconfig is used for extension module
  194. # builds, which happens earlier in the Distutils tests. This may
  195. # cause the following tests to fail since no tests have caused
  196. # the global version of sysconfig to call the customization yet.
  197. # The solution for now is to simply skip this test in this case.
  198. # The longer-term solution is to only have one version of sysconfig.
  199. import sysconfig as global_sysconfig
  200. if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'):
  201. self.skipTest('compiler flags customized')
  202. self.assertEqual(global_sysconfig.get_config_var('LDSHARED'),
  203. sysconfig.get_config_var('LDSHARED'))
  204. self.assertEqual(global_sysconfig.get_config_var('CC'),
  205. sysconfig.get_config_var('CC'))
  206. @requires_subprocess()
  207. def test_customize_compiler_before_get_config_vars(self):
  208. # Issue #21923: test that a Distribution compiler
  209. # instance can be called without an explicit call to
  210. # get_config_vars().
  211. with open(TESTFN, 'w') as f:
  212. f.writelines(textwrap.dedent('''\
  213. from distutils.core import Distribution
  214. config = Distribution().get_command_obj('config')
  215. # try_compile may pass or it may fail if no compiler
  216. # is found but it should not raise an exception.
  217. rc = config.try_compile('int x;')
  218. '''))
  219. p = subprocess.Popen([str(sys.executable), TESTFN],
  220. stdout=subprocess.PIPE,
  221. stderr=subprocess.STDOUT,
  222. universal_newlines=True)
  223. outs, errs = p.communicate()
  224. self.assertEqual(0, p.returncode, "Subprocess failed: " + outs)
  225. def test_suite():
  226. suite = unittest.TestSuite()
  227. suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SysconfigTestCase))
  228. return suite
  229. if __name__ == '__main__':
  230. run_unittest(test_suite())