From eb60c041f9d2ae8554045b50080bbfc7b439af01 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Tue, 30 Apr 2019 18:52:44 +0100 Subject: ninja: Implement Windows-style command line quoting We avoided having to get this right previously, as we'd always use a response file if possible. But this is so insane, I can't imagine it's right. See also: subprocess.list2cmdline() internal method --- mesonbuild/backend/ninjabackend.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'mesonbuild/backend/ninjabackend.py') diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 5743d48..fb150a3 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -53,8 +53,17 @@ FORTRAN_SUBMOD_PAT = r"^\s*\bsubmodule\b\s*\((\w+:?\w+)\)\s*(\w+)" FORTRAN_USE_PAT = r"^\s*use,?\s*(?:non_intrinsic)?\s*(?:::)?\s*(\w+)" def cmd_quote(s): - # XXX: this needs to understand how to escape any existing double quotes(") - return '"{}"'.format(s) + # see: https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-commandlinetoargvw#remarks + + # backslash escape any existing double quotes + # any existing backslashes preceding a quote are doubled + s = re.sub(r'(\\*)"', lambda m: '\\' * (len(m.group(1)) * 2 + 1) + '"', s) + # any terminal backslashes likewise need doubling + s = re.sub(r'(\\*)$', lambda m: '\\' * (len(m.group(1)) * 2), s) + # and double quote + s = '"{}"'.format(s) + + return s # How ninja executes command lines differs between Unix and Windows # (see https://ninja-build.org/manual.html#ref_rule_command) @@ -340,8 +349,6 @@ class NinjaBuildElement: else: quoter = lambda x: ninja_quote(qf(x)) i = i.replace('\\', '\\\\') - if qf('') == '""': - i = i.replace('"', '\\"') newelems.append(quoter(i)) line += ' '.join(newelems) line += '\n' -- cgit v1.1