Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | |
| 3 | import argparse |
Jim Stichnoth | e8b404b | 2014-09-16 10:22:26 -0700 | [diff] [blame] | 4 | import errno |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 5 | import os |
Jim Stichnoth | e8b404b | 2014-09-16 10:22:26 -0700 | [diff] [blame] | 6 | import shutil |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 7 | import tempfile |
| 8 | from utils import shellcmd |
Jim Stichnoth | 16178a1 | 2014-09-02 14:11:57 -0700 | [diff] [blame] | 9 | from utils import FindBaseNaCl |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 10 | |
| 11 | if __name__ == '__main__': |
| 12 | argparser = argparse.ArgumentParser() |
| 13 | argparser.add_argument('cfile', nargs='+', type=str, |
| 14 | help='C file(s) to convert') |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 15 | argparser.add_argument('--dir', nargs='?', type=str, default='.', |
Jim Stichnoth | 65d8d53 | 2014-09-11 09:47:59 -0700 | [diff] [blame] | 16 | help='Output directory. Default "%(default)s".') |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 17 | argparser.add_argument('--disable-verify', action='store_true') |
| 18 | args = argparser.parse_args() |
| 19 | |
Jim Stichnoth | 16178a1 | 2014-09-02 14:11:57 -0700 | [diff] [blame] | 20 | nacl_root = FindBaseNaCl() |
| 21 | # Prepend bin to $PATH. |
| 22 | os.environ['PATH'] = ( |
Jim Stichnoth | bb9d11a | 2015-06-03 00:18:14 -0700 | [diff] [blame] | 23 | nacl_root + '/toolchain/linux_x86/pnacl_newlib_raw/bin' + os.pathsep + |
Jim Stichnoth | 16178a1 | 2014-09-02 14:11:57 -0700 | [diff] [blame] | 24 | os.pathsep + os.environ['PATH']) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 25 | |
Jim Stichnoth | e8b404b | 2014-09-16 10:22:26 -0700 | [diff] [blame] | 26 | try: |
| 27 | tempdir = tempfile.mkdtemp() |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 28 | |
Jim Stichnoth | e8b404b | 2014-09-16 10:22:26 -0700 | [diff] [blame] | 29 | for cname in args.cfile: |
| 30 | basename = os.path.splitext(cname)[0] |
| 31 | llname = os.path.join(tempdir, basename + '.ll') |
| 32 | pnaclname = basename + '.pnacl.ll' |
| 33 | pnaclname = os.path.join(args.dir, pnaclname) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 34 | |
Jim Stichnoth | e8b404b | 2014-09-16 10:22:26 -0700 | [diff] [blame] | 35 | shellcmd('pnacl-clang -O2 -c {0} -o {1}'.format(cname, llname)) |
| 36 | shellcmd('pnacl-opt ' + |
| 37 | '-pnacl-abi-simplify-preopt -pnacl-abi-simplify-postopt' + |
| 38 | ('' if args.disable_verify else |
| 39 | ' -verify-pnaclabi-module -verify-pnaclabi-functions') + |
| 40 | ' -pnaclabi-allow-debug-metadata' |
| 41 | ' {0} -S -o {1}'.format(llname, pnaclname)) |
| 42 | finally: |
| 43 | try: |
| 44 | shutil.rmtree(tempdir) |
| 45 | except OSError as exc: |
| 46 | if exc.errno != errno.ENOENT: # ENOENT - no such file or directory |
| 47 | raise # re-raise exception |