1"""Tests for distutils.ccompiler.""" 2import os 3import unittest 4from test.test_support import captured_stdout 5 6from distutils.ccompiler import (gen_lib_options, CCompiler, 7 get_default_compiler) 8from distutils.sysconfig import customize_compiler 9from distutils import debug 10from distutils.tests import support 11 12class FakeCompiler(object): 13 def library_dir_option(self, dir): 14 return "-L" + dir 15 16 def runtime_library_dir_option(self, dir): 17 return ["-cool", "-R" + dir] 18 19 def find_library_file(self, dirs, lib, debug=0): 20 return 'found' 21 22 def library_option(self, lib): 23 return "-l" + lib 24 25class CCompilerTestCase(support.EnvironGuard, unittest.TestCase): 26 27 def test_set_executables(self): 28 class MyCCompiler(CCompiler): 29 executables = {'compiler': '', 'compiler_cxx': '', 'linker': ''} 30 31 compiler = MyCCompiler() 32 33 # set executable as list 34 compiler.set_executables(compiler=['env', 'OMPI_MPICC=clang', 'mpicc']) 35 self.assertEqual(compiler.compiler, ['env', 36 'OMPI_MPICC=clang', 37 'mpicc']) 38 39 # set executable as string 40 compiler.set_executables(compiler_cxx='env OMPI_MPICXX=clang++ mpicxx') 41 self.assertEqual(compiler.compiler_cxx, ['env', 42 'OMPI_MPICXX=clang++', 43 'mpicxx']) 44 45 # set executable as unicode string 46 compiler.set_executables(linker=u'env OMPI_MPICXX=clang++ mpiCC') 47 self.assertEqual(compiler.linker, [u'env', 48 u'OMPI_MPICXX=clang++', 49 u'mpiCC']) 50 51 def test_gen_lib_options(self): 52 compiler = FakeCompiler() 53 libdirs = ['lib1', 'lib2'] 54 runlibdirs = ['runlib1'] 55 libs = [os.path.join('dir', 'name'), 'name2'] 56 57 opts = gen_lib_options(compiler, libdirs, runlibdirs, libs) 58 wanted = ['-Llib1', '-Llib2', '-cool', '-Rrunlib1', 'found', 59 '-lname2'] 60 self.assertEqual(opts, wanted) 61 62 def test_debug_print(self): 63 64 class MyCCompiler(CCompiler): 65 executables = {} 66 67 compiler = MyCCompiler() 68 with captured_stdout() as stdout: 69 compiler.debug_print('xxx') 70 stdout.seek(0) 71 self.assertEqual(stdout.read(), '') 72 73 debug.DEBUG = True 74 try: 75 with captured_stdout() as stdout: 76 compiler.debug_print('xxx') 77 stdout.seek(0) 78 self.assertEqual(stdout.read(), 'xxx\n') 79 finally: 80 debug.DEBUG = False 81 82 @unittest.skipUnless(get_default_compiler() == 'unix', 83 'not testing if default compiler is not unix') 84 def test_customize_compiler(self): 85 os.environ['AR'] = 'my_ar' 86 os.environ['ARFLAGS'] = '-arflags' 87 88 # make sure AR gets caught 89 class compiler: 90 compiler_type = 'unix' 91 92 def set_executables(self, **kw): 93 self.exes = kw 94 95 comp = compiler() 96 customize_compiler(comp) 97 self.assertEqual(comp.exes['archiver'], 'my_ar -arflags') 98 99def test_suite(): 100 return unittest.makeSuite(CCompilerTestCase) 101 102if __name__ == "__main__": 103 unittest.main(defaultTest="test_suite") 104