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