#!/usr/bin/env python from __future__ import print_function import fcntl import os import pickle import sys from subprocess import Popen from pipsgcc import getDirectory, Object, LinkerObject, _CCWorkspace, CCWorkspace def ofile(argv): """""" for opt in argv: if opt == '-o': index = argv.index(opt) return argv[index + 1] return '' def hascopt(argv): """ Create object """ for i in argv: if i == '-c': return True return False if __name__ == '__main__': CC = os.getenv('PIPS_CC', 'gcc') if ofile(sys.argv).find('.o') == -1: CC = os.getenv('PIPS_LD', CC) if len(sys.argv) == 1: sp = Popen(CC) exitcode = sp.wait() sys.exit(0) if sys.argv[1] == '-print-prog-name=ld': print(sys.argv[0]) sys.exit(0) if sys.argv[1] == '-do': wp = CCWorkspace('pips.ws') # todo: missing parameter if not os.path.exists('output'): os.mkdir('output') wp.compile(outdir='output') sys.exit(0) # Call GCC cmd = [CC] + sys.argv[1:] sp = Popen(cmd) exitcode = sp.wait() obj = None if ofile(sys.argv).find('.o') != -1 or hascopt(sys.argv): obj = Object(sys.argv[1:]) else: obj = LinkerObject(sys.argv[1:]) # Load workspace workspace = None wsfile = None try: wsfile = open(os.path.join(getDirectory(), 'workspace'), 'r+') fcntl.flock(wsfile, fcntl.LOCK_EX) except IOError: workspace = _CCWorkspace() if wsfile is not None: workspace = pickle.load(wsfile) else: wsfile = open(os.path.join(getDirectory(), 'workspace'), 'w') fcntl.flock(wsfile, fcntl.LOCK_EX) # Add the new object to the workspace workspace.addObject(obj) # Save the new workspace wsfile.seek(0) pickle.dump(workspace, wsfile) sys.exit(exitcode)