Debian Codegraph @

rpython/translator/test/test_driver.py

raw symbols SCIP
1 import pytest
2 import os
3 from rpython.translator.driver import TranslationDriver, shutil_copy
4 from rpython.tool.udir import udir
5 from rpython.translator.platform import windows, darwin, linux
6
7 def test_ctr():
8 td = TranslationDriver()
9 expected = ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source',
10 'compile', 'pyjitpl']
11 assert set(td.exposed) == set(expected)
12
13 assert td.backend_select_goals(['compile_c']) == ['compile_c']
14 assert td.backend_select_goals(['compile']) == ['compile_c']
15 assert td.backend_select_goals(['rtype']) == ['rtype_lltype']
16 assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']
17 assert td.backend_select_goals(['backendopt']) == ['backendopt_lltype']
18 assert td.backend_select_goals(['backendopt_lltype']) == [
19 'backendopt_lltype']
20
21 td = TranslationDriver({'backend': None, 'type_system': None})
22
23 assert td.backend_select_goals(['compile_c']) == ['compile_c']
24 pytest.raises(Exception, td.backend_select_goals, ['compile'])
25 pytest.raises(Exception, td.backend_select_goals, ['rtype'])
26 assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']
27 pytest.raises(Exception, td.backend_select_goals, ['backendopt'])
28 assert td.backend_select_goals(['backendopt_lltype']) == [
29 'backendopt_lltype']
30
31 expected = ['annotate', 'backendopt_lltype', 'llinterpret_lltype',
32 'rtype_lltype', 'source_c', 'compile_c', 'pyjitpl_lltype', ]
33 assert set(td.exposed) == set(expected)
34
35 td = TranslationDriver({'backend': None, 'type_system': 'lltype'})
36
37 assert td.backend_select_goals(['compile_c']) == ['compile_c']
38 pytest.raises(Exception, td.backend_select_goals, ['compile'])
39 assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']
40 assert td.backend_select_goals(['rtype']) == ['rtype_lltype']
41 assert td.backend_select_goals(['backendopt']) == ['backendopt_lltype']
42 assert td.backend_select_goals(['backendopt_lltype']) == [
43 'backendopt_lltype']
44
45 expected = ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source_c',
46 'compile_c', 'pyjitpl']
47
48 assert set(td.exposed) == set(expected)
49
50
51 @pytest.mark.parametrize("host, suffix", (
52 (windows.MsvcPlatform, '.exe'),
53 (darwin.Darwin, ''),
54 (linux.Linux, '')),
55 ids=('windows', 'macOS', 'linux'))
56 def test_compile_c(host, suffix):
57
58 exe_name = 'pypy-%(backend)s'
59 # Created by the fake "compile" function
60 # Create the dst directory to be tested
61 dst_name = udir.join('dst/pypy-c' + suffix)
62 dst_name.ensure()
63
64 class CBuilder(object):
65 def compile(self, exe_name):
66 from rpython.translator.tool.cbuild import ExternalCompilationInfo
67
68 # CBuilder.gen_makefile is called via CBuilder.generate_source
69 # in driver.task_source_c. We are faking parts of it here
70 targetdir = udir.join('src')
71 exe_name = targetdir.join(exe_name)
72 platform = host(cc='not_really_going_to_compile')
73 mk = platform.gen_makefile([], ExternalCompilationInfo(),
74 exe_name=exe_name,
75 path=targetdir,
76 shared=True,
77 )
78 # "compile" the needed outputs
79 src_name = udir.join('src/pypy-c' + suffix)
80 src_name.ensure()
81 src_name.write('exe')
82 dll_name = udir.join('src/pypy-c.dll')
83 dll_name.ensure()
84 dll_name.write('dll')
85 self.shared_library_name = dll_name
86
87 # mock the additional windows artifacts as well
88 wsrc_name = udir.join('src/pypy-cw.exe')
89 wsrc_name.ensure()
90 wsrc_name.write('wexe')
91 self.executable_name_w = wsrc_name
92 lib_name = udir.join('src/pypy-c.lib')
93 lib_name.ensure()
94 lib_name.write('lib')
95 pdb_name = udir.join('src/pypy-c.pdb')
96 pdb_name.ensure()
97 pdb_name.write('pdb')
98 self.executable_name = mk.exe_name
99
100 td = TranslationDriver(exe_name=str(exe_name))
101 # Normally done in the database_c task
102 td.cbuilder = CBuilder()
103 # Normally done when creating the driver via from_targetspec
104 td.standalone = True
105
106 cwd = os.getcwd()
107
108 # This calls compile(), sets td.c_entryp to CBuilder.executable_name,
109 # and calls create_exe(). We must cd into the target directory since
110 # create_exe() copies back to the current directory
111
112 try:
113 os.chdir(dst_name.dirname)
114 td.task_compile_c()
115 finally:
116 os.chdir(cwd)
117
118 assert dst_name.read() == 'exe'
119 assert dst_name.new(ext='dll').read() == 'dll'
120 if host is windows.MsvcPlatform:
121 assert dst_name.new(ext='lib').read() == 'lib'
122 assert dst_name.new(purebasename=dst_name.purebasename + 'w').read() == 'wexe'
123
124 def test_shutil_copy():
125 if os.name == 'nt':
126 pytest.skip('Windows cannot copy or rename to an in-use file')
127 a = udir.join('file_a')
128 b = udir.join('file_a')
129 a.write('hello')
130 shutil_copy(str(a), str(b))
131 assert b.read() == 'hello'