Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Yiran Wang | a70724f | 2015-07-16 13:22:25 -0700 | [diff] [blame] | 2 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Yiran Wang | a70724f | 2015-07-16 13:22:25 -0700 | [diff] [blame] | 5 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 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 | } |
Yiran Wang | a70724f | 2015-07-16 13:22:25 -0700 | [diff] [blame] | 17 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 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 |
Yiran Wang | a70724f | 2015-07-16 13:22:25 -0700 | [diff] [blame] | 29 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 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] |
Yiran Wang | a70724f | 2015-07-16 13:22:25 -0700 | [diff] [blame] | 36 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 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 |
Yiran Wang | a70724f | 2015-07-16 13:22:25 -0700 | [diff] [blame] | 48 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 49 | def add_flags(self): |
| 50 | self.args = self.prepend_flags + self.args + self.append_flags |
Yiran Wang | a70724f | 2015-07-16 13:22:25 -0700 | [diff] [blame] | 51 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 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) |
Rong Xu | 90135de | 2014-09-05 14:09:07 -0700 | [diff] [blame] | 59 | |
| 60 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 61 | def main(argv): |
| 62 | cw = CompilerWrapper(argv[1:]) |
| 63 | cw.invoke_compiler() |
Rong Xu | 90135de | 2014-09-05 14:09:07 -0700 | [diff] [blame] | 64 | |
Caroline Tice | bf02f9b | 2016-10-10 09:17:19 -0700 | [diff] [blame] | 65 | if __name__ == "__main__": |
| 66 | main(sys.argv) |