test_unixccompiler.py 4.5 KB

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