test_unixccompiler.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """Tests for distutils.unixccompiler."""
  2. import os
  3. import sys
  4. import unittest
  5. from test.support import EnvironmentVarGuard, run_unittest
  6. from distutils import sysconfig
  7. from distutils.unixccompiler import UnixCCompiler
  8. class UnixCCompilerTestCase(unittest.TestCase):
  9. def setUp(self):
  10. self._backup_platform = sys.platform
  11. self._backup_get_config_var = sysconfig.get_config_var
  12. class CompilerWrapper(UnixCCompiler):
  13. def rpath_foo(self):
  14. return self.runtime_library_dir_option('/foo')
  15. self.cc = CompilerWrapper()
  16. def tearDown(self):
  17. sys.platform = self._backup_platform
  18. sysconfig.get_config_var = self._backup_get_config_var
  19. @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
  20. def test_runtime_libdir_option(self):
  21. # Issue#5900
  22. #
  23. # Ensure RUNPATH is added to extension modules with RPATH if
  24. # GNU ld is used
  25. # darwin
  26. sys.platform = 'darwin'
  27. self.assertEqual(self.cc.rpath_foo(), '-L/foo')
  28. # hp-ux
  29. sys.platform = 'hp-ux'
  30. old_gcv = sysconfig.get_config_var
  31. def gcv(v):
  32. return 'xxx'
  33. sysconfig.get_config_var = gcv
  34. self.assertEqual(self.cc.rpath_foo(), ['+s', '-L/foo'])
  35. def gcv(v):
  36. return 'gcc'
  37. sysconfig.get_config_var = gcv
  38. self.assertEqual(self.cc.rpath_foo(), ['-Wl,+s', '-L/foo'])
  39. def gcv(v):
  40. return 'g++'
  41. sysconfig.get_config_var = gcv
  42. self.assertEqual(self.cc.rpath_foo(), ['-Wl,+s', '-L/foo'])
  43. sysconfig.get_config_var = old_gcv
  44. # irix646
  45. sys.platform = 'irix646'
  46. self.assertEqual(self.cc.rpath_foo(), ['-rpath', '/foo'])
  47. # osf1V5
  48. sys.platform = 'osf1V5'
  49. self.assertEqual(self.cc.rpath_foo(), ['-rpath', '/foo'])
  50. # GCC GNULD
  51. sys.platform = 'bar'
  52. def gcv(v):
  53. if v == 'CC':
  54. return 'gcc'
  55. elif v == 'GNULD':
  56. return 'yes'
  57. sysconfig.get_config_var = gcv
  58. self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo')
  59. # GCC non-GNULD
  60. sys.platform = 'bar'
  61. def gcv(v):
  62. if v == 'CC':
  63. return 'gcc'
  64. elif v == 'GNULD':
  65. return 'no'
  66. sysconfig.get_config_var = gcv
  67. self.assertEqual(self.cc.rpath_foo(), '-Wl,-R/foo')
  68. # GCC GNULD with fully qualified configuration prefix
  69. # see #7617
  70. sys.platform = 'bar'
  71. def gcv(v):
  72. if v == 'CC':
  73. return 'x86_64-pc-linux-gnu-gcc-4.4.2'
  74. elif v == 'GNULD':
  75. return 'yes'
  76. sysconfig.get_config_var = gcv
  77. self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo')
  78. # non-GCC GNULD
  79. sys.platform = 'bar'
  80. def gcv(v):
  81. if v == 'CC':
  82. return 'cc'
  83. elif v == 'GNULD':
  84. return 'yes'
  85. sysconfig.get_config_var = gcv
  86. self.assertEqual(self.cc.rpath_foo(), '-R/foo')
  87. # non-GCC non-GNULD
  88. sys.platform = 'bar'
  89. def gcv(v):
  90. if v == 'CC':
  91. return 'cc'
  92. elif v == 'GNULD':
  93. return 'no'
  94. sysconfig.get_config_var = gcv
  95. self.assertEqual(self.cc.rpath_foo(), '-R/foo')
  96. @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X')
  97. def test_osx_cc_overrides_ldshared(self):
  98. # Issue #18080:
  99. # ensure that setting CC env variable also changes default linker
  100. def gcv(v):
  101. if v == 'LDSHARED':
  102. return 'gcc-4.2 -bundle -undefined dynamic_lookup '
  103. return 'gcc-4.2'
  104. sysconfig.get_config_var = gcv
  105. with EnvironmentVarGuard() as env:
  106. env['CC'] = 'my_cc'
  107. del env['LDSHARED']
  108. sysconfig.customize_compiler(self.cc)
  109. self.assertEqual(self.cc.linker_so[0], 'my_cc')
  110. @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X')
  111. def test_osx_explicit_ldshared(self):
  112. # Issue #18080:
  113. # ensure that setting CC env variable does not change
  114. # explicit LDSHARED setting for linker
  115. def gcv(v):
  116. if v == 'LDSHARED':
  117. return 'gcc-4.2 -bundle -undefined dynamic_lookup '
  118. return 'gcc-4.2'
  119. sysconfig.get_config_var = gcv
  120. with EnvironmentVarGuard() as env:
  121. env['CC'] = 'my_cc'
  122. env['LDSHARED'] = 'my_ld -bundle -dynamic'
  123. sysconfig.customize_compiler(self.cc)
  124. self.assertEqual(self.cc.linker_so[0], 'my_ld')
  125. def test_suite():
  126. return unittest.makeSuite(UnixCCompilerTestCase)
  127. if __name__ == "__main__":
  128. run_unittest(test_suite())