1#!/usr/bin/env python2 2 3import argparse 4import errno 5import os 6import shutil 7import tempfile 8from utils import shellcmd 9from utils import FindBaseNaCl 10 11if __name__ == '__main__': 12 argparser = argparse.ArgumentParser() 13 argparser.add_argument('cfile', nargs='+', type=str, 14 help='C file(s) to convert') 15 argparser.add_argument('--dir', nargs='?', type=str, default='.', 16 help='Output directory. Default "%(default)s".') 17 argparser.add_argument('--disable-verify', action='store_true') 18 args = argparser.parse_args() 19 20 nacl_root = FindBaseNaCl() 21 # Prepend bin to $PATH. 22 os.environ['PATH'] = ( 23 nacl_root + '/toolchain/linux_x86/pnacl_newlib_raw/bin' + os.pathsep + 24 os.pathsep + os.environ['PATH']) 25 26 try: 27 tempdir = tempfile.mkdtemp() 28 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) 34 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 48