Skip to content

Commit 6272fc7

Browse files
SquallATFMax Lv
authored andcommitted
Allowed build under msys2 or cygwin
1 parent eb32cb4 commit 6272fc7

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

core/src/main/rust/linker-wrapper.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,46 @@
66
import subprocess
77
import sys
88

9-
args = [os.environ['RUST_ANDROID_GRADLE_CC'], os.environ['RUST_ANDROID_GRADLE_CC_LINK_ARG']] + sys.argv[1:]
9+
rustcc = os.environ['RUST_ANDROID_GRADLE_CC']
10+
11+
if sys.platform == 'msys' or sys.platform == 'cygwin':
12+
import ctypes
13+
14+
cygdll = 'cygwin1.dll' if sys.platform == 'cygwin' else 'msys-2.0.dll'
15+
cygwin = ctypes.cdll.LoadLibrary(cygdll)
16+
17+
def win2posix(path):
18+
CCP_WIN_W_TO_POSIX = 3
19+
size = cygwin.cygwin_conv_path(CCP_WIN_W_TO_POSIX, path, 0, 0)
20+
retval = ctypes.create_string_buffer(size)
21+
cygwin.cygwin_conv_path(CCP_WIN_W_TO_POSIX, path, retval, size)
22+
return retval.value.decode()
23+
24+
rustcc = win2posix(rustcc)
25+
26+
args = [rustcc, os.environ['RUST_ANDROID_GRADLE_CC_LINK_ARG']] + sys.argv[1:]
27+
28+
linkargfileName = ''
29+
if (sys.platform == 'msys' or sys.platform == 'cygwin') and len(''.join(args)) > 8191:
30+
import codecs
31+
import tempfile
32+
33+
def posix2win(path):
34+
CCP_POSIX_TO_WIN_W = 1
35+
size = cygwin.cygwin_conv_path(CCP_POSIX_TO_WIN_W, str(path).encode(), 0, 0)
36+
retval = ctypes.create_unicode_buffer(size)
37+
cygwin.cygwin_conv_path(CCP_POSIX_TO_WIN_W, str(path).encode(), retval, size)
38+
return retval.value
39+
40+
# response file should be use UTF-16 with BOM
41+
linkargfile = tempfile.NamedTemporaryFile(delete=False)
42+
linkargfile.write(codecs.BOM_UTF16_LE)
43+
linkargfile.write('\n'.join(sys.argv[1:]).encode('utf-16-le'))
44+
linkargfile.close()
45+
linkargfileName = linkargfile.name
46+
linkargfileNameW = posix2win(linkargfileName)
47+
args = [rustcc, os.environ['RUST_ANDROID_GRADLE_CC_LINK_ARG'], '@' + linkargfileNameW]
48+
1049

1150
# This only appears when the subprocess call fails, but it's helpful then.
1251
printable_cmd = ' '.join(pipes.quote(arg) for arg in args)
@@ -21,4 +60,6 @@
2160
with open(linkargs[0][1:]) as f:
2261
sys_argv = f.read().splitlines()
2362
shutil.copyfile(sys_argv[sys_argv.index('-o') + 1], os.environ['RUST_ANDROID_GRADLE_TARGET'])
63+
if linkargfileName != '':
64+
os.unlink(linkargfileName)
2465
sys.exit(code)

0 commit comments

Comments
 (0)