1 #!/usr/bin/python 2 3 import os 4 import sys 5 6 class CompilerWrapper(): 7 def __init__(self, argv): 8 self.args = argv 9 self.execargs = [] 10 self.real_compiler = None 11 self.argv0 = None 12 self.append_flags = [] 13 self.prepend_flags = [] 14 self.custom_flags = { 15 '--gomacc-path': None 16 } 17 18 def set_real_compiler(self): 19 """Find the real compiler with the absolute path.""" 20 compiler_path = os.path.dirname(os.path.abspath(__file__)) 21 if os.path.islink(__file__): 22 compiler = os.path.basename(os.readlink(__file__)) 23 else: 24 compiler = os.path.basename(os.path.abspath(__file__)) 25 self.real_compiler = os.path.join( 26 compiler_path, 27 "real-" + compiler) 28 self.argv0 = self.real_compiler 29 30 def process_gomacc_command(self): 31 """Return the gomacc command if '--gomacc-path' is set.""" 32 gomacc = self.custom_flags['--gomacc-path'] 33 if gomacc and os.path.isfile(gomacc): 34 self.argv0 = gomacc 35 self.execargs += [gomacc] 36 37 def parse_custom_flags(self): 38 i = 0 39 args = [] 40 while i < len(self.args): 41 if self.args[i] in self.custom_flags: 42 self.custom_flags[self.args[i]] = self.args[i + 1] 43 i = i + 2 44 else: 45 args.append(self.args[i]) 46 i = i + 1 47 self.args = args 48 49 def add_flags(self): 50 self.args = self.prepend_flags + self.args + self.append_flags 51 52 def invoke_compiler(self): 53 self.set_real_compiler() 54 self.parse_custom_flags() 55 self.process_gomacc_command() 56 self.add_flags() 57 self.execargs += [self.real_compiler] + self.args 58 os.execv(self.argv0, self.execargs) 59 60 61 def main(argv): 62 cw = CompilerWrapper(argv[1:]) 63 cw.invoke_compiler() 64 65 if __name__ == "__main__": 66 main(sys.argv) 67