bpo-36235: Enhance distutils test_customize_compiler() (GH-12403) (GH… · python/cpython@dd1cfef (original) (raw)
1
1
`"""Tests for distutils.sysconfig."""
`
``
2
`+
import contextlib
`
2
3
`import os
`
3
4
`import shutil
`
4
5
`import subprocess
`
`@@ -74,29 +75,94 @@ def test_srcdir_independent_of_cwd(self):
`
74
75
`os.chdir(cwd)
`
75
76
`self.assertEqual(srcdir, srcdir2)
`
76
77
``
77
``
`-
@unittest.skipUnless(get_default_compiler() == 'unix',
`
78
``
`-
'not testing if default compiler is not unix')
`
79
``
`-
def test_customize_compiler(self):
`
80
``
`-
os.environ['AR'] = 'my_ar'
`
81
``
`-
os.environ['CC'] = 'my_cc'
`
82
``
`-
os.environ['ARFLAGS'] = '--myarflags'
`
83
``
`-
os.environ['CFLAGS'] = '--mycflags'
`
84
``
-
``
78
`+
def customize_compiler(self):
`
85
79
`# make sure AR gets caught
`
86
80
`class compiler:
`
87
81
`compiler_type = 'unix'
`
88
82
``
89
83
`def set_executables(self, **kw):
`
90
84
`self.exes = kw
`
91
85
``
92
``
`-
Make sure that sysconfig._config_vars is initialized
`
93
``
`-
sysconfig.get_config_vars()
`
``
86
`+
sysconfig_vars = {
`
``
87
`+
'AR': 'sc_ar',
`
``
88
`+
'CC': 'sc_cc',
`
``
89
`+
'CXX': 'sc_cxx',
`
``
90
`+
'ARFLAGS': '--sc-arflags',
`
``
91
`+
'CFLAGS': '--sc-cflags',
`
``
92
`+
'CCSHARED': '--sc-ccshared',
`
``
93
`+
'LDSHARED': 'sc_ldshared',
`
``
94
`+
'SHLIB_SUFFIX': 'sc_shutil_suffix',
`
``
95
`+
}
`
94
96
``
95
97
`comp = compiler()
`
96
``
`-
with swap_item(sysconfig._config_vars, 'CFLAGS', '--sysconfig-cflags'):
`
``
98
`+
with contextlib.ExitStack() as cm:
`
``
99
`+
for key, value in sysconfig_vars.items():
`
``
100
`+
cm.enter_context(swap_item(sysconfig._config_vars, key, value))
`
97
101
`sysconfig.customize_compiler(comp)
`
98
``
`-
self.assertEqual(comp.exes['archiver'], 'my_ar --myarflags')
`
99
``
`-
self.assertEqual(comp.exes['compiler'], 'my_cc --sysconfig-cflags --mycflags')
`
``
102
+
``
103
`+
return comp
`
``
104
+
``
105
`+
@unittest.skipUnless(get_default_compiler() == 'unix',
`
``
106
`+
'not testing if default compiler is not unix')
`
``
107
`+
def test_customize_compiler(self):
`
``
108
`+
Make sure that sysconfig._config_vars is initialized
`
``
109
`+
sysconfig.get_config_vars()
`
``
110
+
``
111
`+
os.environ['AR'] = 'env_ar'
`
``
112
`+
os.environ['CC'] = 'env_cc'
`
``
113
`+
os.environ['CPP'] = 'env_cpp'
`
``
114
`+
os.environ['CXX'] = 'env_cxx --env-cxx-flags'
`
``
115
`+
os.environ['LDSHARED'] = 'env_ldshared'
`
``
116
`+
os.environ['LDFLAGS'] = '--env-ldflags'
`
``
117
`+
os.environ['ARFLAGS'] = '--env-arflags'
`
``
118
`+
os.environ['CFLAGS'] = '--env-cflags'
`
``
119
`+
os.environ['CPPFLAGS'] = '--env-cppflags'
`
``
120
+
``
121
`+
comp = self.customize_compiler()
`
``
122
`+
self.assertEqual(comp.exes['archiver'],
`
``
123
`+
'env_ar --env-arflags')
`
``
124
`+
self.assertEqual(comp.exes['preprocessor'],
`
``
125
`+
'env_cpp --env-cppflags')
`
``
126
`+
self.assertEqual(comp.exes['compiler'],
`
``
127
`+
'env_cc --sc-cflags --env-cflags --env-cppflags')
`
``
128
`+
self.assertEqual(comp.exes['compiler_so'],
`
``
129
`+
('env_cc --sc-cflags '
`
``
130
`+
'--env-cflags ''--env-cppflags --sc-ccshared'))
`
``
131
`+
self.assertEqual(comp.exes['compiler_cxx'],
`
``
132
`+
'env_cxx --env-cxx-flags')
`
``
133
`+
self.assertEqual(comp.exes['linker_exe'],
`
``
134
`+
'env_cc')
`
``
135
`+
self.assertEqual(comp.exes['linker_so'],
`
``
136
`+
('env_ldshared --env-ldflags --env-cflags'
`
``
137
`+
' --env-cppflags'))
`
``
138
`+
self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
`
``
139
+
``
140
`+
del os.environ['AR']
`
``
141
`+
del os.environ['CC']
`
``
142
`+
del os.environ['CPP']
`
``
143
`+
del os.environ['CXX']
`
``
144
`+
del os.environ['LDSHARED']
`
``
145
`+
del os.environ['LDFLAGS']
`
``
146
`+
del os.environ['ARFLAGS']
`
``
147
`+
del os.environ['CFLAGS']
`
``
148
`+
del os.environ['CPPFLAGS']
`
``
149
+
``
150
`+
comp = self.customize_compiler()
`
``
151
`+
self.assertEqual(comp.exes['archiver'],
`
``
152
`+
'sc_ar --sc-arflags')
`
``
153
`+
self.assertEqual(comp.exes['preprocessor'],
`
``
154
`+
'sc_cc -E')
`
``
155
`+
self.assertEqual(comp.exes['compiler'],
`
``
156
`+
'sc_cc --sc-cflags')
`
``
157
`+
self.assertEqual(comp.exes['compiler_so'],
`
``
158
`+
'sc_cc --sc-cflags --sc-ccshared')
`
``
159
`+
self.assertEqual(comp.exes['compiler_cxx'],
`
``
160
`+
'sc_cxx')
`
``
161
`+
self.assertEqual(comp.exes['linker_exe'],
`
``
162
`+
'sc_cc')
`
``
163
`+
self.assertEqual(comp.exes['linker_so'],
`
``
164
`+
'sc_ldshared')
`
``
165
`+
self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
`
100
166
``
101
167
`def test_parse_makefile_base(self):
`
102
168
`self.makefile = TESTFN
`