1+ #!/usr/bin/env python3
2+
13import subprocess
2- import os , sys
4+ import os
5+ import sys
6+ import json
7+
8+
9+ # TODO: dedup
10+ def is_cmdfile_arg (arg ):
11+ # type: (str) -> bool
12+ return arg .startswith ('@' )
13+
14+
15+ def cmdfile_path (arg ):
16+ # type: (str) -> str
17+ return arg [1 :]
18+
19+
20+ def read_from_command_file (arg ):
21+ # type: (str) -> list[str]
22+ with open (arg ) as afile :
23+ return afile .read ().splitlines ()
324
4- # Explicitly enable local imports
5- # Don't forget to add imported scripts to inputs of the calling command!
6- sys .path .append (os .path .dirname (os .path .abspath (__file__ )))
7- import process_command_files as pcf
25+
26+ def skip_markers (args ):
27+ # type: (list[str]) -> list[str]
28+ res = []
29+ for arg in args :
30+ if arg == '--ya-start-command-file' or arg == '--ya-end-command-file' :
31+ continue
32+ res .append (arg )
33+ return res
34+
35+
36+ def iter_args (
37+ args , # type: list[str]
38+ ):
39+ for arg in args :
40+ if not is_cmdfile_arg (arg ):
41+ if arg == '--ya-start-command-file' or arg == '--ya-end-command-file' :
42+ continue
43+ yield arg
44+ else :
45+ for cmdfile_arg in read_from_command_file (cmdfile_path (arg )):
46+ yield cmdfile_arg
47+
48+
49+ def get_args (args ):
50+ # type: (list[str]) -> list[str]
51+ return list (iter_args (args ))
52+ # end TODO
853
954
1055def run (* args ):
11- # print >>sys.stderr, args
1256 return subprocess .check_output (list (args ), shell = False ).strip ()
1357
1458
@@ -80,10 +124,20 @@ def rename_syms(where, ret, libs):
80124 return ret
81125
82126
127+ def find_lld (args ):
128+ for x in args :
129+ if 'lld-link' in x :
130+ return x
131+
132+ raise IndexError ()
133+
134+
83135def fix_py2 (cmd , have_comand_files = False , prefix = 'lib' , suffix = 'a' ):
84136 args = cmd
137+
85138 if have_comand_files :
86- args = pcf .get_args (cmd )
139+ args = get_args (cmd )
140+
87141 if 'protobuf_old' not in str (args ):
88142 return cmd
89143
@@ -99,7 +153,10 @@ def need_rename(x):
99153 old = []
100154 lib = []
101155
102- where = os .path .dirname (cmd [0 ]) + '/'
156+ try :
157+ where = os .path .dirname (cmd [cmd .index ('--objcopy-exe' ) + 1 ]) + '/'
158+ except ValueError :
159+ where = os .path .dirname (find_lld (cmd )) + '/'
103160
104161 for x in args :
105162 if need_rename (x ):
@@ -113,9 +170,9 @@ def need_rename(x):
113170 return old + [name ]
114171
115172 for file in cmd :
116- if pcf . is_cmdfile_arg (file ):
117- cmd_file_path = pcf . cmdfile_path (file )
118- args = pcf . read_from_command_file (cmd_file_path )
173+ if is_cmdfile_arg (file ):
174+ cmd_file_path = cmdfile_path (file )
175+ args = read_from_command_file (cmd_file_path )
119176 if not 'protobuf_old' in str (args ):
120177 continue
121178 with open (cmd_file_path , 'w' ) as afile :
@@ -125,3 +182,14 @@ def need_rename(x):
125182 afile .write (name )
126183
127184 return cmd
185+
186+
187+ if __name__ == '__main__' :
188+ args = sys .argv [1 :]
189+
190+ if 'lld-link' in str (args ):
191+ cmd = fix_py2 (args , have_comand_files = True , prefix = '' , suffix = 'lib' )
192+ else :
193+ cmd = fix_py2 (args )
194+
195+ sys .stdout .write (json .dumps (cmd ))
0 commit comments