blob: 54944a2ccbea692fa58d912e74b2514dab7a80f5 [file] [log] [blame]
Caroline Ticebf02f9b2016-10-10 09:17:19 -07001#!/usr/bin/python
Yiran Wanga70724f2015-07-16 13:22:25 -07002
Caroline Ticebf02f9b2016-10-10 09:17:19 -07003import os
4import sys
Yiran Wanga70724f2015-07-16 13:22:25 -07005
Caroline Ticebf02f9b2016-10-10 09:17:19 -07006class 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 Wanga70724f2015-07-16 13:22:25 -070017
Caroline Ticebf02f9b2016-10-10 09:17:19 -070018 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,
Rashed Abdel-Tawabeb576572019-09-08 19:46:16 -070027 "real-" + compiler)
Caroline Ticebf02f9b2016-10-10 09:17:19 -070028 self.argv0 = self.real_compiler
Yiran Wanga70724f2015-07-16 13:22:25 -070029
Caroline Ticebf02f9b2016-10-10 09:17:19 -070030 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 Wanga70724f2015-07-16 13:22:25 -070036
Caroline Ticebf02f9b2016-10-10 09:17:19 -070037 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 Wanga70724f2015-07-16 13:22:25 -070048
Caroline Ticebf02f9b2016-10-10 09:17:19 -070049 def add_flags(self):
50 self.args = self.prepend_flags + self.args + self.append_flags
Yiran Wanga70724f2015-07-16 13:22:25 -070051
Caroline Ticebf02f9b2016-10-10 09:17:19 -070052 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 Xu90135de2014-09-05 14:09:07 -070059
60
Caroline Ticebf02f9b2016-10-10 09:17:19 -070061def main(argv):
62 cw = CompilerWrapper(argv[1:])
63 cw.invoke_compiler()
Rong Xu90135de2014-09-05 14:09:07 -070064
Caroline Ticebf02f9b2016-10-10 09:17:19 -070065if __name__ == "__main__":
66 main(sys.argv)