| OLD | NEW |
| 1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python2 |
| 2 | 2 |
| 3 import argparse | 3 import argparse |
| 4 from collections import namedtuple |
| 4 import os | 5 import os |
| 5 import shutil | 6 import shutil |
| 6 import tempfile | 7 import tempfile |
| 7 from utils import shellcmd | 8 from utils import shellcmd |
| 8 from utils import FindBaseNaCl | 9 from utils import FindBaseNaCl |
| 9 | 10 |
| 10 def Translate(ll_files, extra_args, obj, verbose): | 11 def Translate(ll_files, extra_args, obj, verbose): |
| 11 """Translate a set of input bitcode files into a single object file. | 12 """Translate a set of input bitcode files into a single object file. |
| 12 | 13 |
| 13 Use pnacl-llc to translate textual bitcode input ll_files into object file | 14 Use pnacl-llc to translate textual bitcode input ll_files into object file |
| 14 obj, using extra_args as the architectural flags. | 15 obj, using extra_args as the architectural flags. |
| 15 """ | 16 """ |
| 16 shellcmd(['cat'] + ll_files + ['|', | 17 shellcmd(['cat'] + ll_files + ['|', |
| 17 'pnacl-llc', | 18 'pnacl-llc', |
| 18 '-externalize', | 19 '-externalize', |
| 19 '-function-sections', | 20 '-function-sections', |
| 20 '-O2', | 21 '-O2', |
| 21 '-filetype=obj', | 22 '-filetype=obj', |
| 22 '-bitcode-format=llvm', | 23 '-bitcode-format=llvm', |
| 23 '-o', obj | 24 '-o', obj |
| 24 ] + extra_args, echo=verbose) | 25 ] + extra_args, echo=verbose) |
| 25 shellcmd(['objcopy', | 26 shellcmd(['le32-nacl-objcopy', |
| 26 '--strip-symbol=nacl_tp_tdb_offset', | 27 '--strip-symbol=nacl_tp_tdb_offset', |
| 27 '--strip-symbol=nacl_tp_tls_offset', | 28 '--strip-symbol=nacl_tp_tls_offset', |
| 28 obj | 29 obj |
| 29 ], echo=verbose) | 30 ], echo=verbose) |
| 30 | 31 |
| 31 def PartialLink(obj_files, extra_args, lib, verbose): | 32 def PartialLink(obj_files, extra_args, lib, verbose): |
| 32 """Partially links a set of obj files into a final obj library.""" | 33 """Partially links a set of obj files into a final obj library.""" |
| 33 shellcmd(['ld', | 34 shellcmd(['le32-nacl-ld', |
| 34 '-o', lib, | 35 '-o', lib, |
| 35 '-r', | 36 '-r', |
| 36 ] + extra_args + obj_files, echo=verbose) | 37 ] + extra_args + obj_files, echo=verbose) |
| 37 | 38 |
| 39 |
| 40 TargetInfo = namedtuple('TargetInfo', |
| 41 ['target', 'triple', 'llc_flags', 'ld_emu']) |
| 42 |
| 43 |
| 44 def MakeRuntimesForTarget(target_info, ll_files, |
| 45 srcdir, tempdir, rtdir, verbose): |
| 46 def TmpFile(template): |
| 47 return template.format(dir=tempdir, target=target_info.target) |
| 48 def OutFile(template): |
| 49 return template.format(rtdir=rtdir, target=target_info.target) |
| 50 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to |
| 51 # szrt_native_{target}.tmp.o. |
| 52 Translate(ll_files, |
| 53 ['-mtriple=' + target_info.triple] + target_info.llc_flags, |
| 54 TmpFile('{dir}/szrt_native_{target}.tmp.o'), |
| 55 verbose) |
| 56 # Compile srcdir/szrt_profiler.c to tempdir/szrt_profiler_native_{target}.o |
| 57 shellcmd(['clang', |
| 58 '-O2', |
| 59 '-target=' + target_info.triple, |
| 60 '-c', |
| 61 '{srcdir}/szrt_profiler.c'.format(srcdir=srcdir), |
| 62 '-o', TmpFile('{dir}/szrt_profiler_native_{target}.o') |
| 63 ], echo=verbose) |
| 64 # Writing full szrt_native_{target}.o. |
| 65 PartialLink([TmpFile('{dir}/szrt_native_{target}.tmp.o'), |
| 66 TmpFile('{dir}/szrt_profiler_native_{target}.o')], |
| 67 ['-m {ld_emu}'.format(ld_emu=target_info.ld_emu)], |
| 68 OutFile('{rtdir}/szrt_native_{target}.o'), |
| 69 verbose) |
| 70 |
| 71 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to szrt_sb_{target}.o |
| 72 # The sandboxed library does not get the profiler helper function as the |
| 73 # binaries are linked with -nostdlib. |
| 74 Translate(ll_files, |
| 75 ['-mtriple=' + target_info.triple.replace('linux', 'nacl')] + |
| 76 target_info.llc_flags, |
| 77 OutFile('{rtdir}/szrt_sb_{target}.o'), |
| 78 verbose) |
| 79 |
| 80 |
| 38 def main(): | 81 def main(): |
| 39 """Build the Subzero runtime support library for all architectures. | 82 """Build the Subzero runtime support library for all architectures. |
| 40 """ | 83 """ |
| 41 argparser = argparse.ArgumentParser( | 84 argparser = argparse.ArgumentParser( |
| 42 description=' ' + main.__doc__, | 85 description=' ' + main.__doc__, |
| 43 formatter_class=argparse.RawTextHelpFormatter) | 86 formatter_class=argparse.RawTextHelpFormatter) |
| 44 argparser.add_argument('--verbose', '-v', dest='verbose', | 87 argparser.add_argument('--verbose', '-v', dest='verbose', |
| 45 action='store_true', | 88 action='store_true', |
| 46 help='Display some extra debugging output') | 89 help='Display some extra debugging output') |
| 47 argparser.add_argument('--pnacl-root', dest='pnacl_root', | 90 argparser.add_argument('--pnacl-root', dest='pnacl_root', |
| (...skipping 25 matching lines...) Expand all Loading... |
| 73 '-pnacl-abi-simplify-preopt', | 116 '-pnacl-abi-simplify-preopt', |
| 74 '-pnacl-abi-simplify-postopt', | 117 '-pnacl-abi-simplify-postopt', |
| 75 '-pnaclabi-allow-debug-metadata', | 118 '-pnaclabi-allow-debug-metadata', |
| 76 '{dir}/szrt.tmp.bc'.format(dir=tempdir), | 119 '{dir}/szrt.tmp.bc'.format(dir=tempdir), |
| 77 '-S', | 120 '-S', |
| 78 '-o', '{dir}/szrt.ll'.format(dir=tempdir) | 121 '-o', '{dir}/szrt.ll'.format(dir=tempdir) |
| 79 ], echo=args.verbose) | 122 ], echo=args.verbose) |
| 80 ll_files = ['{dir}/szrt.ll'.format(dir=tempdir), | 123 ll_files = ['{dir}/szrt.ll'.format(dir=tempdir), |
| 81 '{srcdir}/szrt_ll.ll'.format(srcdir=srcdir)] | 124 '{srcdir}/szrt_ll.ll'.format(srcdir=srcdir)] |
| 82 | 125 |
| 83 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to | 126 x8632_target = TargetInfo(target='x8632', |
| 84 # szrt_native_x8632.tmp.o. | 127 triple='i686-none-linux', |
| 85 Translate(ll_files, | 128 llc_flags=['-mcpu=pentium4m'], |
| 86 ['-mtriple=i686', '-mcpu=pentium4m'], | 129 ld_emu='elf_i386_nacl') |
| 87 '{dir}/szrt_native_x8632.tmp.o'.format(dir=tempdir), | 130 MakeRuntimesForTarget(x8632_target, ll_files, |
| 88 args.verbose) | 131 srcdir, tempdir, rtdir, args.verbose) |
| 89 # Compile srcdir/szrt_profiler.c to tempdir/szrt_profiler_native_i686.o | 132 arm32_target = TargetInfo(target='arm32', |
| 90 shellcmd(['clang', | 133 triple='armv7a-none-linux-gnueabihf', |
| 91 '-O2', | 134 llc_flags=['-mcpu=cortex-a9', |
| 92 '-target=i686', | 135 '-float-abi=hard', |
| 93 '-c', | 136 '-mattr=+neon'], |
| 94 '{srcdir}/szrt_profiler.c'.format(srcdir=srcdir), | 137 ld_emu='armelf_nacl') |
| 95 '-o', '{dir}/szrt_profiler_native_x8632.o'.format(dir=tempdir) | 138 MakeRuntimesForTarget(arm32_target, ll_files, |
| 96 ], echo=args.verbose) | 139 srcdir, tempdir, rtdir, args.verbose) |
| 97 # Writing full szrt_native_i686.o. | |
| 98 PartialLink(['{dir}/szrt_native_x8632.tmp.o'.format(dir=tempdir), | |
| 99 '{dir}/szrt_profiler_native_x8632.o'.format(dir=tempdir) | |
| 100 ], ['-m elf_i386'], | |
| 101 '{rtdir}/szrt_native_x8632.o'.format(rtdir=rtdir), args.verbose) | |
| 102 | 140 |
| 103 # Translate tempdir/szrt.ll and tempdir/szrt_ll.ll to szrt_sb_x8632.o | |
| 104 # The sandboxed library does not get the profiler helper function as the | |
| 105 # binaries are linked with -nostdlib. | |
| 106 Translate(ll_files, | |
| 107 ['-mtriple=i686-nacl', '-mcpu=pentium4m'], | |
| 108 '{rtdir}/szrt_sb_x8632.o'.format(rtdir=rtdir), | |
| 109 args.verbose) | |
| 110 finally: | 141 finally: |
| 111 try: | 142 try: |
| 112 shutil.rmtree(tempdir) | 143 shutil.rmtree(tempdir) |
| 113 except OSError as exc: | 144 except OSError as exc: |
| 114 if exc.errno != errno.ENOENT: # ENOENT - no such file or directory | 145 if exc.errno != errno.ENOENT: # ENOENT - no such file or directory |
| 115 raise # re-raise exception | 146 raise # re-raise exception |
| 116 | 147 |
| 117 if __name__ == '__main__': | 148 if __name__ == '__main__': |
| 118 main() | 149 main() |
| OLD | NEW |