Closed anthrotype closed 1 year ago
The problem here was that ninja user-defined build variables (e.g. --target_font $target_font
in glue_together
rule) don't get shell-quoted automatically when they are expanded, only $in
and $out
are (hence the latter at least are fine when inputs/outputs contain spaces).
If the other variables happen to refer to paths with spaces or other special characters, ninja passes them through unchanged and the underlying shell will split the command line arguments incorrectly.
So we need to enquote them following the shell's own rules, which differ between POSIX and Windows, as usual.
Unfortunately the stdlib's shlex
module's quote() and split() commands don't work well with Windows, so I had to resort to a third-party module called https://github.com/smoofra/mslex to accomplish this (it's only installed if sys.platform=="win32").
Now, subprocess
also has a similar method called list2cmdline
which follows Windows MS C runtime rules, but.. it's private and undocumented, and I also need the reverse of that in order to split a str into shell-compatible list of arguments (we need to split inside nanoemoji.util.expand_ninja_response_files
to emulate the @
-prefixed so-called response files used by ninja to work around maximum-args limits).
https://github.com/smoofra/mslex looks like a tiny dead one-man project. I don't think this is a good dependency. Since Python can do things like run windows processes I'd have thought it must have this built-in but if not perhaps we should just write our own little function?
ok, I removed the dependency on mslex, and went back to using subprocess.list2cmdline for shell quoting on Windows. Whereas for splitting a command line string to list of args, I am now using ctypes to wrap a call into Windows' own CommandLineToArgvW
method (which works only on Windows of course, but that's ok). I copied the tests from cpyton's test_subprocess.py to confirm that both quoting and splitting back do roundtrip as intended on Windows.
I'm mildly amazed where this has taken us haha.
Part of fixing #435