11#!/usr/bin/env python3
22
33"""
4- Provide a single file that can act as a compiler on linux while forwarding the calls through wine and fixing up the arguments
4+ Provide a single file that can act as a compiler on linux while forwarding the calls through wine and fixing up the arguments.
5+ On windows, prevent cmake from thinking this is a msvc compiler and forward build commands.
56"""
67
78import os
@@ -16,7 +17,53 @@ def winepath(path: pathlib.Path) -> str:
1617 return path .as_posix ().replace ("/" , "\\ " )
1718
1819
19- def main ():
20+ def clang_to_msvc_commands (args : list [str ], wine_path : bool ) -> list [str ]:
21+ skip_next = False
22+ commands = []
23+ for i , c in enumerate (args ):
24+ if skip_next :
25+ skip_next = False
26+ continue
27+ elif c == '-c' :
28+ commands .append ('/c' )
29+ elif c .startswith ("-I" ):
30+ commands .append ('/I' + c [2 :])
31+ elif c == '-o' :
32+ commands .append (f'/Fo{ args [i + 1 ]} ' )
33+ skip_next = True
34+ elif c in ('-target' , 'i686-pc-windows-gnu' , '-m32' , '-masm=intel' , '-march=i386' ):
35+ continue
36+ elif wine_path and pathlib .Path (c ).exists ():
37+ commands .append (f'Z:{ c } ' )
38+ else :
39+ commands .append (c )
40+ return commands
41+
42+
43+ def cl_call_passthrough ():
44+ # Prevent CMake from identifying the compiler
45+ if "CMakeCXXCompilerId.cpp" in sys .argv :
46+ return 1
47+ """Prevents cmake from complaining about mixing cl and clang on windows"""
48+ msvc_base = pathlib .Path (sys .argv [1 ])
49+ cl_path = msvc_base / "BIN/CL.EXE"
50+
51+ env = os .environ .copy ()
52+ env ["PATH" ] = str (msvc_base / "BIN" )
53+ env ["INCLUDE" ] = str (msvc_base / "INCLUDE" )
54+
55+ commands = [cl_path .as_posix (), "/nologo" ] + clang_to_msvc_commands (sys .argv [2 :], False )
56+
57+ # print(' '.join(sys.argv) + ' => ' + ' '.join(commands))
58+ result = subprocess .run (commands , capture_output = True , env = env )
59+
60+ sys .stdout .write (result .stdout .decode ())
61+ sys .stderr .write (result .stderr .decode ())
62+
63+ return result .returncode
64+
65+
66+ def wrap_cl_call_with_wine ():
2067 msvc_base = pathlib .Path (sys .argv [1 ])
2168 cl_path = msvc_base / "BIN/CL.EXE"
2269
@@ -35,26 +82,11 @@ def main():
3582 args .append (a )
3683
3784 subs = {}
38- skip_next = False
3985 for i , c in enumerate (args ):
40- if skip_next :
41- skip_next = False
42- continue
43- elif c == '-c' :
44- commands .append ('/c' )
45- elif c .startswith ("-I" ):
46- commands .append ('/I' + c [2 :])
47- elif c == '-o' :
48- commands .append (f'/Fo{ args [i + 1 ]} ' )
49- skip_next = True
50- elif c in ('-target' , 'i686-pc-windows-gnu' , '-m32' , '-masm=intel' , '-march=i386' ):
51- continue
52- elif pathlib .Path (c ).exists ():
53- commands .append (f'Z:{ c } ' )
54- else :
55- if c .startswith ("Z:/" ):
56- subs [os .path .basename (c [2 :]) + '\r \n ' ] = ''
57- commands .append (c )
86+ if c .startswith ("Z:/" ):
87+ subs [os .path .basename (c [2 :]) + '\r \n ' ] = ''
88+
89+ commands += clang_to_msvc_commands (args , True )
5890
5991 # print(' '.join(sys.argv) + ' => ' + ' '.join(commands))
6092 project_root = pathlib .Path (__file__ ).parent .parent .parent
@@ -79,4 +111,6 @@ def fix_paths(string):
79111 return result .returncode
80112
81113if __name__ == "__main__" :
82- exit (main ())
114+ if os .name == 'nt' :
115+ exit (cl_call_passthrough ())
116+ exit (wrap_cl_call_with_wine ())
0 commit comments