OLD | NEW |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python2 |
2 | 2 |
3 import argparse | 3 import argparse |
4 import os | 4 import os |
5 import shutil | 5 import shutil |
6 import tempfile | 6 import tempfile |
7 | 7 |
8 import targets | 8 import targets |
9 from utils import FindBaseNaCl, GetObjcopyCmd, shellcmd | 9 from utils import FindBaseNaCl, GetObjcopyCmd, shellcmd |
10 | 10 |
11 | 11 |
12 def Translate(ll_files, extra_args, obj, verbose): | 12 def Translate(ll_files, extra_args, obj, verbose, target): |
13 """Translate a set of input bitcode files into a single object file. | 13 """Translate a set of input bitcode files into a single object file. |
14 | 14 |
15 Use pnacl-llc to translate textual bitcode input ll_files into object file | 15 Use pnacl-llc to translate textual bitcode input ll_files into object file |
16 obj, using extra_args as the architectural flags. | 16 obj, using extra_args as the architectural flags. |
17 """ | 17 """ |
18 externalize = [] | |
Jim Stichnoth
2016/09/23 05:29:11
externalize = [] if target == 'mips32' else ['-ext
obucinac
2016/09/23 13:22:03
Done.
| |
19 if target != 'mips32': | |
20 externalize = ['-externalize'] | |
18 shellcmd(['cat'] + ll_files + ['|', | 21 shellcmd(['cat'] + ll_files + ['|', |
19 'pnacl-llc', | 22 'pnacl-llc', |
20 '-externalize', | |
21 '-function-sections', | 23 '-function-sections', |
22 '-O2', | 24 '-O2', |
23 '-filetype=obj', | 25 '-filetype=obj', |
24 '-bitcode-format=llvm', | 26 '-bitcode-format=llvm', |
25 '-arm-enable-dwarf-eh=1', | |
26 '-o', obj | 27 '-o', obj |
27 ] + extra_args, echo=verbose) | 28 ] + extra_args + externalize, echo=verbose) |
28 shellcmd([GetObjcopyCmd(), | 29 strip_syms = [] |
Jim Stichnoth
2016/09/23 05:29:11
strip_syms = [] if target == 'mips32' else ['nacl_
obucinac
2016/09/23 13:22:02
Done.
| |
29 '--strip-symbol=nacl_tp_tdb_offset', | 30 if target != 'mips32': |
30 '--strip-symbol=nacl_tp_tls_offset', | 31 strip_syms += ['nacl_tp_tdb_offset', 'nacl_tp_tls_offset'] |
31 obj | 32 shellcmd([GetObjcopyCmd(target), obj] + |
32 ], echo=verbose) | 33 [('--strip-symbol=' + sym) for sym in strip_syms]) |
33 | 34 |
34 | 35 |
35 def PartialLink(obj_files, extra_args, lib, verbose): | 36 def PartialLink(obj_files, extra_args, lib, verbose): |
36 """Partially links a set of obj files into a final obj library.""" | 37 """Partially links a set of obj files into a final obj library.""" |
37 shellcmd(['le32-nacl-ld', | 38 shellcmd(['le32-nacl-ld', |
38 '-o', lib, | 39 '-o', lib, |
39 '-r', | 40 '-r', |
40 ] + extra_args + obj_files, echo=verbose) | 41 ] + extra_args + obj_files, echo=verbose) |
41 | 42 |
42 | 43 |
43 def MakeRuntimesForTarget(target_info, ll_files, | 44 def MakeRuntimesForTarget(target_info, ll_files, |
44 srcdir, tempdir, rtdir, verbose): | 45 srcdir, tempdir, rtdir, verbose, excluded_targets): |
45 """Builds native, sandboxed, and nonsfi runtimes for the given target.""" | 46 """Builds native, sandboxed, and nonsfi runtimes for the given target.""" |
47 if target_info.target in excluded_targets: | |
48 return | |
46 # File-mangling helper functions. | 49 # File-mangling helper functions. |
47 def TmpFile(template): | 50 def TmpFile(template): |
48 return template.format(dir=tempdir, target=target_info.target) | 51 return template.format(dir=tempdir, target=target_info.target) |
49 def OutFile(template): | 52 def OutFile(template): |
50 return template.format(rtdir=rtdir, target=target_info.target) | 53 return template.format(rtdir=rtdir, target=target_info.target) |
51 # Helper function for building the native unsandboxed runtime. | 54 # Helper function for building the native unsandboxed runtime. |
52 def MakeNativeRuntime(): | 55 def MakeNativeRuntime(): |
53 """Builds just the native runtime.""" | 56 """Builds just the native runtime.""" |
54 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to | 57 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to |
55 # szrt_native_{target}.tmp.o. | 58 # szrt_native_{target}.tmp.o. |
56 Translate(ll_files, | 59 Translate(ll_files, |
57 ['-mtriple=' + target_info.triple] + target_info.llc_flags, | 60 ['-mtriple=' + target_info.triple] + target_info.llc_flags, |
58 TmpFile('{dir}/szrt_native_{target}.tmp.o'), | 61 TmpFile('{dir}/szrt_native_{target}.tmp.o'), |
59 verbose) | 62 verbose, target_info.target) |
60 # Compile srcdir/szrt_profiler.c to | 63 # Compile srcdir/szrt_profiler.c to |
61 # tempdir/szrt_profiler_native_{target}.o. | 64 # tempdir/szrt_profiler_native_{target}.o. |
62 shellcmd(['clang', | 65 shellcmd(['clang', |
63 '-O2', | 66 '-O2', |
64 '-target=' + target_info.triple, | 67 '-target=' + target_info.triple, |
65 '-c', | 68 '-c', |
66 '{srcdir}/szrt_profiler.c'.format(srcdir=srcdir), | 69 '{srcdir}/szrt_profiler.c'.format(srcdir=srcdir), |
67 '-o', TmpFile('{dir}/szrt_native_profiler_{target}.o') | 70 '-o', TmpFile('{dir}/szrt_native_profiler_{target}.o') |
68 ], echo=verbose) | 71 ], echo=verbose) |
69 # Assemble srcdir/szrt_asm_{target}.s to tempdir/szrt_asm_{target}.o. | 72 # Assemble srcdir/szrt_asm_{target}.s to tempdir/szrt_asm_{target}.o. |
70 shellcmd(['llvm-mc', | 73 shellcmd(['llvm-mc', |
71 '-triple=' + target_info.triple, '--defsym NATIVE=1', | 74 '-triple=' + target_info.triple, '--defsym NATIVE=1', |
72 '-filetype=obj', | 75 '-filetype=obj', |
73 '-o', TmpFile('{dir}/szrt_native_asm_{target}.o'), | 76 '-o', TmpFile('{dir}/szrt_native_asm_{target}.o'), |
74 '{srcdir}/szrt_asm_{target}.s'.format( | 77 '{srcdir}/szrt_asm_{target}.s'.format( |
75 srcdir=srcdir, target=target_info.target) | 78 srcdir=srcdir, target=target_info.target) |
76 ], echo=verbose) | 79 ], echo=verbose) |
77 # Write full szrt_native_{target}.o. | 80 # Write full szrt_native_{target}.o. |
78 PartialLink([TmpFile('{dir}/szrt_native_{target}.tmp.o'), | 81 PartialLink([TmpFile('{dir}/szrt_native_{target}.tmp.o'), |
79 TmpFile('{dir}/szrt_native_asm_{target}.o'), | 82 TmpFile('{dir}/szrt_native_asm_{target}.o'), |
80 TmpFile('{dir}/szrt_native_profiler_{target}.o')], | 83 TmpFile('{dir}/szrt_native_profiler_{target}.o')], |
81 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], | 84 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], |
82 OutFile('{rtdir}/szrt_native_{target}.o'), | 85 OutFile('{rtdir}/szrt_native_{target}.o'), |
83 verbose) | 86 verbose) |
84 shellcmd([GetObjcopyCmd(), | 87 shellcmd([GetObjcopyCmd(target_info.target), |
85 '--strip-symbol=NATIVE', | 88 '--strip-symbol=NATIVE', |
86 OutFile('{rtdir}/szrt_native_{target}.o')]) | 89 OutFile('{rtdir}/szrt_native_{target}.o')]) |
87 # Compile srcdir/szrt_asan.c to szrt_asan_{target}.o | 90 # Compile srcdir/szrt_asan.c to szrt_asan_{target}.o |
88 shellcmd(['clang', | 91 shellcmd(['clang', |
89 '-O2', | 92 '-O2', |
90 '-target=' + target_info.triple, | 93 '-target=' + target_info.triple, |
91 '-c', | 94 '-c', |
92 '{srcdir}/szrt_asan.c'.format(srcdir=srcdir), | 95 '{srcdir}/szrt_asan.c'.format(srcdir=srcdir), |
93 '-o', OutFile('{rtdir}/szrt_asan_{target}.o') | 96 '-o', OutFile('{rtdir}/szrt_asan_{target}.o') |
94 ], echo=verbose) | 97 ], echo=verbose) |
95 | 98 |
96 # Helper function for building the sandboxed runtime. | 99 # Helper function for building the sandboxed runtime. |
97 def MakeSandboxedRuntime(): | 100 def MakeSandboxedRuntime(): |
98 """Builds just the sandboxed runtime.""" | 101 """Builds just the sandboxed runtime.""" |
99 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to szrt_sb_{target}.o. | 102 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to szrt_sb_{target}.o. |
100 # The sandboxed library does not get the profiler helper function as the | 103 # The sandboxed library does not get the profiler helper function as the |
101 # binaries are linked with -nostdlib. | 104 # binaries are linked with -nostdlib. |
102 Translate(ll_files, | 105 Translate(ll_files, |
103 ['-mtriple=' + targets.ConvertTripleToNaCl(target_info.triple)] + | 106 ['-mtriple=' + targets.ConvertTripleToNaCl(target_info.triple)] + |
104 target_info.llc_flags, | 107 target_info.llc_flags, |
105 TmpFile('{dir}/szrt_sb_{target}.tmp.o'), | 108 TmpFile('{dir}/szrt_sb_{target}.tmp.o'), |
106 verbose) | 109 verbose,target_info.target) |
107 # Assemble srcdir/szrt_asm_{target}.s to tempdir/szrt_asm_{target}.o. | 110 # Assemble srcdir/szrt_asm_{target}.s to tempdir/szrt_asm_{target}.o. |
108 shellcmd(['llvm-mc', | 111 shellcmd(['llvm-mc', |
109 '-triple=' + targets.ConvertTripleToNaCl(target_info.triple), | 112 '-triple=' + targets.ConvertTripleToNaCl(target_info.triple), |
110 '--defsym NACL=1', | 113 '--defsym NACL=1', |
111 '-filetype=obj', | 114 '-filetype=obj', |
112 '-o', TmpFile('{dir}/szrt_sb_asm_{target}.o'), | 115 '-o', TmpFile('{dir}/szrt_sb_asm_{target}.o'), |
113 '{srcdir}/szrt_asm_{target}.s'.format( | 116 '{srcdir}/szrt_asm_{target}.s'.format( |
114 srcdir=srcdir, target=target_info.target) | 117 srcdir=srcdir, target=target_info.target) |
115 ], echo=verbose) | 118 ], echo=verbose) |
116 PartialLink([TmpFile('{dir}/szrt_sb_{target}.tmp.o'), | 119 PartialLink([TmpFile('{dir}/szrt_sb_{target}.tmp.o'), |
117 TmpFile('{dir}/szrt_sb_asm_{target}.o')], | 120 TmpFile('{dir}/szrt_sb_asm_{target}.o')], |
118 ['-m {ld_emu}'.format(ld_emu=target_info.sb_emu)], | 121 ['-m {ld_emu}'.format(ld_emu=target_info.sb_emu)], |
119 OutFile('{rtdir}/szrt_sb_{target}.o'), | 122 OutFile('{rtdir}/szrt_sb_{target}.o'), |
120 verbose) | 123 verbose) |
121 shellcmd([GetObjcopyCmd(), | 124 shellcmd([GetObjcopyCmd(target_info.target), |
122 '--strip-symbol=NACL', | 125 '--strip-symbol=NACL', |
123 OutFile('{rtdir}/szrt_sb_{target}.o')]) | 126 OutFile('{rtdir}/szrt_sb_{target}.o')]) |
124 | 127 |
125 # Helper function for building the Non-SFI runtime. | 128 # Helper function for building the Non-SFI runtime. |
126 def MakeNonsfiRuntime(): | 129 def MakeNonsfiRuntime(): |
127 """Builds just the nonsfi runtime.""" | 130 """Builds just the nonsfi runtime.""" |
128 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to | 131 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to |
129 # szrt_nonsfi_{target}.tmp.o. | 132 # szrt_nonsfi_{target}.tmp.o. |
130 Translate(ll_files, | 133 Translate(ll_files, |
131 ['-mtriple=' + target_info.triple] + target_info.llc_flags + | 134 ['-mtriple=' + target_info.triple] + target_info.llc_flags + |
132 ['-relocation-model=pic', '-force-tls-non-pic', '-malign-double'], | 135 ['-relocation-model=pic', '-force-tls-non-pic', '-malign-double'], |
133 TmpFile('{dir}/szrt_nonsfi_{target}.tmp.o'), | 136 TmpFile('{dir}/szrt_nonsfi_{target}.tmp.o'), |
134 verbose) | 137 verbose, target_info.target) |
135 # Assemble srcdir/szrt_asm_{target}.s to tempdir/szrt_asm_{target}.o. | 138 # Assemble srcdir/szrt_asm_{target}.s to tempdir/szrt_asm_{target}.o. |
136 shellcmd(['llvm-mc', | 139 shellcmd(['llvm-mc', |
137 '-triple=' + target_info.triple, '--defsym NONSFI=1', | 140 '-triple=' + target_info.triple, '--defsym NONSFI=1', |
138 '-filetype=obj', | 141 '-filetype=obj', |
139 '-o', TmpFile('{dir}/szrt_nonsfi_asm_{target}.o'), | 142 '-o', TmpFile('{dir}/szrt_nonsfi_asm_{target}.o'), |
140 '{srcdir}/szrt_asm_{target}.s'.format( | 143 '{srcdir}/szrt_asm_{target}.s'.format( |
141 srcdir=srcdir, target=target_info.target) | 144 srcdir=srcdir, target=target_info.target) |
142 ], echo=verbose) | 145 ], echo=verbose) |
143 # Write full szrt_nonsfi_{target}.o. | 146 # Write full szrt_nonsfi_{target}.o. |
144 PartialLink([TmpFile('{dir}/szrt_nonsfi_{target}.tmp.o'), | 147 PartialLink([TmpFile('{dir}/szrt_nonsfi_{target}.tmp.o'), |
145 TmpFile('{dir}/szrt_nonsfi_asm_{target}.o')], | 148 TmpFile('{dir}/szrt_nonsfi_asm_{target}.o')], |
146 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], | 149 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], |
147 OutFile('{rtdir}/szrt_nonsfi_{target}.o'), | 150 OutFile('{rtdir}/szrt_nonsfi_{target}.o'), |
148 verbose) | 151 verbose) |
149 shellcmd([GetObjcopyCmd(), | 152 shellcmd([GetObjcopyCmd(target_info.target), |
150 '--strip-symbol=NONSFI', | 153 '--strip-symbol=NONSFI', |
151 OutFile('{rtdir}/szrt_nonsfi_{target}.o')]) | 154 OutFile('{rtdir}/szrt_nonsfi_{target}.o')]) |
152 | 155 |
153 | 156 |
154 # Run the helper functions. | 157 # Run the helper functions. |
155 MakeNativeRuntime() | 158 MakeNativeRuntime() |
156 MakeSandboxedRuntime() | 159 MakeSandboxedRuntime() |
157 MakeNonsfiRuntime() | 160 MakeNonsfiRuntime() |
158 | 161 |
159 | 162 |
160 def main(): | 163 def main(): |
161 """Build the Subzero runtime support library for all architectures. | 164 """Build the Subzero runtime support library for all architectures. |
162 """ | 165 """ |
163 nacl_root = FindBaseNaCl() | 166 nacl_root = FindBaseNaCl() |
164 argparser = argparse.ArgumentParser( | 167 argparser = argparse.ArgumentParser( |
165 description=' ' + main.__doc__, | 168 description=' ' + main.__doc__, |
166 formatter_class=argparse.RawTextHelpFormatter) | 169 formatter_class=argparse.RawTextHelpFormatter) |
167 argparser.add_argument('--verbose', '-v', dest='verbose', | 170 argparser.add_argument('--verbose', '-v', dest='verbose', |
168 action='store_true', | 171 action='store_true', |
169 help='Display some extra debugging output') | 172 help='Display some extra debugging output') |
170 argparser.add_argument('--pnacl-root', dest='pnacl_root', | 173 argparser.add_argument('--pnacl-root', dest='pnacl_root', |
171 default=( | 174 default=( |
172 '{root}/toolchain/linux_x86/pnacl_newlib_raw' | 175 '{root}/toolchain/linux_x86/pnacl_newlib_raw' |
173 ).format(root=nacl_root), | 176 ).format(root=nacl_root), |
174 help='Path to PNaCl toolchain binaries.') | 177 help='Path to PNaCl toolchain binaries.') |
178 argparser.add_argument('--exclude-target', dest='excluded_targets', | |
179 default=[], action='append', | |
180 help='Target whose runtime should not be built') | |
175 args = argparser.parse_args() | 181 args = argparser.parse_args() |
176 os.environ['PATH'] = ('{root}/bin{sep}{path}' | 182 os.environ['PATH'] = ('{root}/bin{sep}{path}' |
177 ).format(root=args.pnacl_root, sep=os.pathsep, path=os.environ['PATH']) | 183 ).format(root=args.pnacl_root, sep=os.pathsep, path=os.environ['PATH']) |
178 srcdir = ( | 184 srcdir = ( |
179 '{root}/toolchain_build/src/subzero/runtime' | 185 '{root}/toolchain_build/src/subzero/runtime' |
180 ).format(root=nacl_root) | 186 ).format(root=nacl_root) |
181 rtdir = ( | 187 rtdir = ( |
182 '{root}/toolchain_build/src/subzero/build/runtime' | 188 '{root}/toolchain_build/src/subzero/build/runtime' |
183 ).format(root=nacl_root) | 189 ).format(root=nacl_root) |
184 try: | 190 try: |
(...skipping 14 matching lines...) Expand all Loading... | |
199 '-pnacl-abi-simplify-postopt', | 205 '-pnacl-abi-simplify-postopt', |
200 '-pnaclabi-allow-debug-metadata', | 206 '-pnaclabi-allow-debug-metadata', |
201 '{dir}/szrt.tmp.bc'.format(dir=tempdir), | 207 '{dir}/szrt.tmp.bc'.format(dir=tempdir), |
202 '-S', | 208 '-S', |
203 '-o', '{dir}/szrt.ll'.format(dir=tempdir) | 209 '-o', '{dir}/szrt.ll'.format(dir=tempdir) |
204 ], echo=args.verbose) | 210 ], echo=args.verbose) |
205 ll_files = ['{dir}/szrt.ll'.format(dir=tempdir), | 211 ll_files = ['{dir}/szrt.ll'.format(dir=tempdir), |
206 '{srcdir}/szrt_ll.ll'.format(srcdir=srcdir)] | 212 '{srcdir}/szrt_ll.ll'.format(srcdir=srcdir)] |
207 | 213 |
208 MakeRuntimesForTarget(targets.X8632Target, ll_files, | 214 MakeRuntimesForTarget(targets.X8632Target, ll_files, |
209 srcdir, tempdir, rtdir, args.verbose) | 215 srcdir, tempdir, rtdir, args.verbose, |
216 args.excluded_targets) | |
210 MakeRuntimesForTarget(targets.X8664Target, ll_files, | 217 MakeRuntimesForTarget(targets.X8664Target, ll_files, |
211 srcdir, tempdir, rtdir, args.verbose) | 218 srcdir, tempdir, rtdir, args.verbose, |
219 args.excluded_targets) | |
212 MakeRuntimesForTarget(targets.ARM32Target, ll_files, | 220 MakeRuntimesForTarget(targets.ARM32Target, ll_files, |
213 srcdir, tempdir, rtdir, args.verbose) | 221 srcdir, tempdir, rtdir, args.verbose, |
222 args.excluded_targets) | |
223 MakeRuntimesForTarget(targets.MIPS32Target, ll_files, | |
224 srcdir, tempdir, rtdir, args.verbose, | |
225 args.excluded_targets) | |
214 | 226 |
215 finally: | 227 finally: |
216 try: | 228 try: |
217 shutil.rmtree(tempdir) | 229 shutil.rmtree(tempdir) |
218 except OSError as exc: | 230 except OSError as exc: |
219 if exc.errno != errno.ENOENT: # ENOENT - no such file or directory | 231 if exc.errno != errno.ENOENT: # ENOENT - no such file or directory |
220 raise # re-raise exception | 232 raise # re-raise exception |
221 | 233 |
222 if __name__ == '__main__': | 234 if __name__ == '__main__': |
223 main() | 235 main() |
OLD | NEW |