| 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 shellcmd | 9 from utils import shellcmd |
| 10 from utils import FindBaseNaCl | 10 from utils import FindBaseNaCl |
| 11 | 11 |
| 12 | 12 |
| 13 def Translate(ll_files, extra_args, obj, verbose): | 13 def Translate(ll_files, extra_args, obj, verbose): |
| 14 """Translate a set of input bitcode files into a single object file. | 14 """Translate a set of input bitcode files into a single object file. |
| 15 | 15 |
| 16 Use pnacl-llc to translate textual bitcode input ll_files into object file | 16 Use pnacl-llc to translate textual bitcode input ll_files into object file |
| 17 obj, using extra_args as the architectural flags. | 17 obj, using extra_args as the architectural flags. |
| 18 """ | 18 """ |
| 19 shellcmd(['cat'] + ll_files + ['|', | 19 shellcmd(['cat'] + ll_files + ['|', |
| 20 'pnacl-llc', | 20 'pnacl-llc', |
| 21 '-externalize', | 21 '-externalize', |
| 22 '-function-sections', | 22 '-function-sections', |
| 23 '-O2', | 23 '-O2', |
| 24 '-filetype=obj', | 24 '-filetype=obj', |
| 25 '-bitcode-format=llvm', | 25 '-bitcode-format=llvm', |
| 26 '-o', obj | 26 '-o', obj |
| 27 ] + extra_args, echo=verbose) | 27 ] + extra_args, echo=verbose) |
| 28 shellcmd(['le32-nacl-objcopy', | 28 shellcmd(['le32-nacl-objcopy', |
| 29 '--strip-symbol=nacl_tp_tdb_offset', | 29 '--strip-symbol=nacl_tp_tdb_offset', |
| 30 '--strip-symbol=nacl_tp_tls_offset', | 30 '--strip-symbol=nacl_tp_tls_offset', |
| 31 obj | 31 obj |
| 32 ], echo=verbose) | 32 ], echo=verbose) |
| 33 | 33 |
| 34 | 34 |
| 35 def PartialLink(obj_files, extra_args, lib, verbose): | 35 def PartialLink(obj_files, extra_args, lib, verbose): |
| 36 """Partially links a set of obj files into a final obj library.""" | 36 """Partially links a set of obj files into a final obj library.""" |
| 37 shellcmd(['le32-nacl-ld', | 37 shellcmd(['le32-nacl-ld', |
| 38 '-o', lib, | 38 '-o', lib, |
| 39 '-r', | 39 '-r', |
| 40 ] + extra_args + obj_files, echo=verbose) | 40 ] + extra_args + obj_files, echo=verbose) |
| 41 | 41 |
| 42 | 42 |
| 43 def MakeRuntimesForTarget(target_info, ll_files, | 43 def MakeRuntimesForTarget(target_info, ll_files, |
| 44 srcdir, tempdir, rtdir, verbose): | 44 srcdir, tempdir, rtdir, verbose): |
| 45 def TmpFile(template): | 45 """Builds native, sandboxed, and nonsfi runtimes for the given target.""" |
| 46 return template.format(dir=tempdir, target=target_info.target) | 46 # File-mangling helper functions. |
| 47 def OutFile(template): | 47 def TmpFile(template): |
| 48 return template.format(rtdir=rtdir, target=target_info.target) | 48 return template.format(dir=tempdir, target=target_info.target) |
| 49 def OutFile(template): |
| 50 return template.format(rtdir=rtdir, target=target_info.target) |
| 51 # Helper function for building the native unsandboxed runtime. |
| 52 def MakeNativeRuntime(): |
| 53 """Builds just the native runtime.""" |
| 49 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to | 54 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to |
| 50 # szrt_native_{target}.tmp.o. | 55 # szrt_native_{target}.tmp.o. |
| 51 Translate(ll_files, | 56 Translate(ll_files, |
| 52 ['-mtriple=' + target_info.triple] + target_info.llc_flags, | 57 ['-mtriple=' + target_info.triple] + target_info.llc_flags, |
| 53 TmpFile('{dir}/szrt_native_{target}.tmp.o'), | 58 TmpFile('{dir}/szrt_native_{target}.tmp.o'), |
| 54 verbose) | 59 verbose) |
| 55 # Compile srcdir/szrt_profiler.c to tempdir/szrt_profiler_native_{target}.o | 60 # Compile srcdir/szrt_profiler.c to |
| 61 # tempdir/szrt_profiler_native_{target}.o. |
| 56 shellcmd(['clang', | 62 shellcmd(['clang', |
| 57 '-O2', | 63 '-O2', |
| 58 '-target=' + target_info.triple, | 64 '-target=' + target_info.triple, |
| 59 '-c', | 65 '-c', |
| 60 '{srcdir}/szrt_profiler.c'.format(srcdir=srcdir), | 66 '{srcdir}/szrt_profiler.c'.format(srcdir=srcdir), |
| 61 '-o', TmpFile('{dir}/szrt_profiler_native_{target}.o') | 67 '-o', TmpFile('{dir}/szrt_profiler_native_{target}.o') |
| 62 ], echo=verbose) | 68 ], echo=verbose) |
| 63 # Writing full szrt_native_{target}.o. | 69 # Write full szrt_native_{target}.o. |
| 64 PartialLink([TmpFile('{dir}/szrt_native_{target}.tmp.o'), | 70 PartialLink([TmpFile('{dir}/szrt_native_{target}.tmp.o'), |
| 65 TmpFile('{dir}/szrt_profiler_native_{target}.o')], | 71 TmpFile('{dir}/szrt_profiler_native_{target}.o')], |
| 66 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], | 72 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], |
| 67 OutFile('{rtdir}/szrt_native_{target}.o'), | 73 OutFile('{rtdir}/szrt_native_{target}.o'), |
| 68 verbose) | 74 verbose) |
| 69 | 75 # Helper function for building the sandboxed runtime. |
| 70 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to szrt_sb_{target}.o | 76 def MakeSandboxedRuntime(): |
| 77 """Builds just the sandboxed runtime.""" |
| 78 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to szrt_sb_{target}.o. |
| 71 # The sandboxed library does not get the profiler helper function as the | 79 # The sandboxed library does not get the profiler helper function as the |
| 72 # binaries are linked with -nostdlib. | 80 # binaries are linked with -nostdlib. |
| 73 Translate(ll_files, | 81 Translate(ll_files, |
| 74 ['-mtriple=' + targets.ConvertTripleToNaCl(target_info.triple)] + | 82 ['-mtriple=' + targets.ConvertTripleToNaCl(target_info.triple)] + |
| 75 target_info.llc_flags, | 83 target_info.llc_flags, |
| 76 OutFile('{rtdir}/szrt_sb_{target}.o'), | 84 OutFile('{rtdir}/szrt_sb_{target}.o'), |
| 77 verbose) | 85 verbose) |
| 86 # Helper function for building the Non-SFI runtime. |
| 87 def MakeNonsfiRuntime(): |
| 88 """Builds just the nonsfi runtime.""" |
| 89 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to |
| 90 # szrt_nonsfi_{target}.tmp.o. |
| 91 Translate(ll_files, |
| 92 ['-mtriple=' + target_info.triple] + target_info.llc_flags + |
| 93 ['-relocation-model=pic', '-force-tls-non-pic', '-malign-double'], |
| 94 TmpFile('{dir}/szrt_nonsfi_{target}.tmp.o'), |
| 95 verbose) |
| 96 # Assemble srcdir/szrt_asm_{target}.s to tempdir/szrt_asm_{target}.o. |
| 97 shellcmd(['llvm-mc', |
| 98 '-triple=' + target_info.triple, |
| 99 '-filetype=obj', |
| 100 '-o', TmpFile('{dir}/szrt_asm_{target}.o'), |
| 101 '{srcdir}/szrt_asm_{target}.s'.format( |
| 102 srcdir=srcdir, target=target_info.target) |
| 103 ], echo=verbose) |
| 104 # Write full szrt_nonsfi_{target}.o. |
| 105 PartialLink([TmpFile('{dir}/szrt_nonsfi_{target}.tmp.o'), |
| 106 TmpFile('{dir}/szrt_asm_{target}.o')], |
| 107 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], |
| 108 OutFile('{rtdir}/szrt_nonsfi_{target}.o'), |
| 109 verbose) |
| 110 |
| 111 # Run the helper functions. |
| 112 MakeNativeRuntime() |
| 113 MakeSandboxedRuntime() |
| 114 MakeNonsfiRuntime() |
| 78 | 115 |
| 79 | 116 |
| 80 def main(): | 117 def main(): |
| 81 """Build the Subzero runtime support library for all architectures. | 118 """Build the Subzero runtime support library for all architectures. |
| 82 """ | 119 """ |
| 120 nacl_root = FindBaseNaCl() |
| 83 argparser = argparse.ArgumentParser( | 121 argparser = argparse.ArgumentParser( |
| 84 description=' ' + main.__doc__, | 122 description=' ' + main.__doc__, |
| 85 formatter_class=argparse.RawTextHelpFormatter) | 123 formatter_class=argparse.RawTextHelpFormatter) |
| 86 argparser.add_argument('--verbose', '-v', dest='verbose', | 124 argparser.add_argument('--verbose', '-v', dest='verbose', |
| 87 action='store_true', | 125 action='store_true', |
| 88 help='Display some extra debugging output') | 126 help='Display some extra debugging output') |
| 89 argparser.add_argument('--pnacl-root', dest='pnacl_root', | 127 argparser.add_argument('--pnacl-root', dest='pnacl_root', |
| 128 default=( |
| 129 '{root}/toolchain/linux_x86/pnacl_newlib_raw' |
| 130 ).format(root=nacl_root), |
| 90 help='Path to PNaCl toolchain binaries.') | 131 help='Path to PNaCl toolchain binaries.') |
| 91 args = argparser.parse_args() | 132 args = argparser.parse_args() |
| 92 nacl_root = FindBaseNaCl() | |
| 93 os.environ['PATH'] = ('{root}/bin{sep}{path}' | 133 os.environ['PATH'] = ('{root}/bin{sep}{path}' |
| 94 ).format(root=args.pnacl_root, sep=os.pathsep, path=os.environ['PATH']) | 134 ).format(root=args.pnacl_root, sep=os.pathsep, path=os.environ['PATH']) |
| 95 srcdir = ( | 135 srcdir = ( |
| 96 '{root}/toolchain_build/src/subzero/runtime' | 136 '{root}/toolchain_build/src/subzero/runtime' |
| 97 ).format(root=nacl_root) | 137 ).format(root=nacl_root) |
| 98 rtdir = ( | 138 rtdir = ( |
| 99 '{root}/toolchain_build/src/subzero/build/runtime' | 139 '{root}/toolchain_build/src/subzero/build/runtime' |
| 100 ).format(root=nacl_root) | 140 ).format(root=nacl_root) |
| 101 try: | 141 try: |
| 102 tempdir = tempfile.mkdtemp() | 142 tempdir = tempfile.mkdtemp() |
| (...skipping 28 matching lines...) Expand all Loading... |
| 131 | 171 |
| 132 finally: | 172 finally: |
| 133 try: | 173 try: |
| 134 shutil.rmtree(tempdir) | 174 shutil.rmtree(tempdir) |
| 135 except OSError as exc: | 175 except OSError as exc: |
| 136 if exc.errno != errno.ENOENT: # ENOENT - no such file or directory | 176 if exc.errno != errno.ENOENT: # ENOENT - no such file or directory |
| 137 raise # re-raise exception | 177 raise # re-raise exception |
| 138 | 178 |
| 139 if __name__ == '__main__': | 179 if __name__ == '__main__': |
| 140 main() | 180 main() |
| OLD | NEW |