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