Skip to content

Commit 0b5a7f2

Browse files
committed
Make cl-wrapper compatible with windows
1 parent 0e0ebbb commit 0b5a7f2

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ FetchContent_Declare(
88
URL_HASH MD5=73e7cc13e078c56c7798c3f6686fbea4
99
)
1010
FetchContent_MakeAvailable(msvc_compiler)
11-
set(CMAKE_CXX_COMPILER "${CMAKE_CURRENT_SOURCE_DIR}/scripts/source_code/wine-cl-wrapper.py")
12-
set(CMAKE_CXX_COMPILER_ARG1 "${msvc_compiler_SOURCE_DIR}")
11+
12+
find_package(Python3 REQUIRED)
13+
14+
set(CMAKE_CXX_COMPILER ${Python3_EXECUTABLE})
15+
set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CURRENT_SOURCE_DIR}/scripts/source_code/wine-cl-wrapper.py ${msvc_compiler_SOURCE_DIR}")
1316
set(CMAKE_CXX_COMPILER_WORKS ON)
1417

1518
# Set the llvm utils before project so the toolchain selection has the right paths
@@ -67,7 +70,6 @@ set(CMAKE_CXX_FLAGS_DEBUG "/Zi /TP /MT /GR")
6770
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/Zi /TP /MT /GR /O2")
6871
set(CMAKE_CXX_FLAGS_RELEASE "/TP /MT /GR /O2")
6972

70-
find_package(Python3 REQUIRED)
7173
find_program(LLVM_STRIP_EXECUTABLE NAMES llvm-strip REQUIRED)
7274
find_program(LLVM_AR_EXECUTABLE NAMES llvm-ar REQUIRED)
7375
find_program(LLVM_OBJCOPY_EXECUTABLE NAMES llvm-objcopy REQUIRED)
@@ -1121,4 +1123,3 @@ add_custom_command(
11211123
COMMAND ${CMAKE_COMMAND} -E md5sum "$<TARGET_FILE:runblack-reassembled>"
11221124
COMMENT "Computing the md5sum"
11231125
)
1124-

scripts/source_code/wine-cl-wrapper.py

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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

78
import 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

81113
if __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

Comments
 (0)