Skip to content

Commit 46486ac

Browse files
authored
fix: support msvs_quote_cmd in ninja generator (#117)
1 parent 3462d4c commit 46486ac

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

pylib/gyp/generator/ninja.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -654,18 +654,18 @@ def WriteActions(
654654
description = self.GenerateDescription(
655655
"ACTION", action.get("message", None), name
656656
)
657-
is_cygwin = (
658-
self.msvs_settings.IsRuleRunUnderCygwin(action)
657+
win_shell_flags = (
658+
self.msvs_settings.GetRuleShellFlags(action)
659659
if self.flavor == "win"
660-
else False
660+
else None
661661
)
662662
args = action["action"]
663663
depfile = action.get("depfile", None)
664664
if depfile:
665665
depfile = self.ExpandSpecial(depfile, self.base_to_build)
666666
pool = "console" if int(action.get("ninja_use_console", 0)) else None
667667
rule_name, _ = self.WriteNewNinjaRule(
668-
name, args, description, is_cygwin, env, pool, depfile=depfile
668+
name, args, description, win_shell_flags, env, pool, depfile=depfile
669669
)
670670

671671
inputs = [self.GypPathToNinja(i, env) for i in action["inputs"]]
@@ -707,14 +707,14 @@ def WriteRules(
707707
rule.get("message", None),
708708
("%s " + generator_default_variables["RULE_INPUT_PATH"]) % name,
709709
)
710-
is_cygwin = (
711-
self.msvs_settings.IsRuleRunUnderCygwin(rule)
710+
win_shell_flags = (
711+
self.msvs_settings.GetRuleShellFlags(rule)
712712
if self.flavor == "win"
713-
else False
713+
else None
714714
)
715715
pool = "console" if int(rule.get("ninja_use_console", 0)) else None
716716
rule_name, args = self.WriteNewNinjaRule(
717-
name, args, description, is_cygwin, env, pool
717+
name, args, description, win_shell_flags, env, pool
718718
)
719719

720720
# TODO: if the command references the outputs directly, we should
@@ -733,7 +733,7 @@ def WriteRules(
733733

734734
def cygwin_munge(path):
735735
# pylint: disable=cell-var-from-loop
736-
if is_cygwin:
736+
if win_shell_flags and win_shell_flags.cygwin:
737737
return path.replace("\\", "/")
738738
return path
739739

@@ -1899,7 +1899,7 @@ def WriteVariableList(self, ninja_file, var, values):
18991899
ninja_file.variable(var, " ".join(values))
19001900

19011901
def WriteNewNinjaRule(
1902-
self, name, args, description, is_cygwin, env, pool, depfile=None
1902+
self, name, args, description, win_shell_flags, env, pool, depfile=None
19031903
):
19041904
"""Write out a new ninja "rule" statement for a given command.
19051905
@@ -1946,13 +1946,14 @@ def WriteNewNinjaRule(
19461946
if self.flavor == "win":
19471947
rspfile = rule_name + ".$unique_name.rsp"
19481948
# The cygwin case handles this inside the bash sub-shell.
1949-
run_in = "" if is_cygwin else " " + self.build_to_base
1950-
if is_cygwin:
1949+
run_in = "" if win_shell_flags.cygwin else " " + self.build_to_base
1950+
if win_shell_flags.cygwin:
19511951
rspfile_content = self.msvs_settings.BuildCygwinBashCommandLine(
19521952
args, self.build_to_base
19531953
)
19541954
else:
1955-
rspfile_content = gyp.msvs_emulation.EncodeRspFileList(args)
1955+
rspfile_content = gyp.msvs_emulation.EncodeRspFileList(
1956+
args, win_shell_flags.quote)
19561957
command = (
19571958
"%s gyp-win-tool action-wrapper $arch " % sys.executable
19581959
+ rspfile

pylib/gyp/msvs_emulation.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
build systems, primarily ninja.
88
"""
99

10+
import collections
1011
import os
1112
import re
1213
import subprocess
@@ -19,7 +20,7 @@
1920
windows_quoter_regex = re.compile(r'(\\*)"')
2021

2122

22-
def QuoteForRspFile(arg):
23+
def QuoteForRspFile(arg, quote_cmd=True):
2324
"""Quote a command line argument so that it appears as one argument when
2425
processed via cmd.exe and parsed by CommandLineToArgvW (as is typical for
2526
Windows programs)."""
@@ -36,7 +37,8 @@ def QuoteForRspFile(arg):
3637
# For a literal quote, CommandLineToArgvW requires 2n+1 backslashes
3738
# preceding it, and results in n backslashes + the quote. So we substitute
3839
# in 2* what we match, +1 more, plus the quote.
39-
arg = windows_quoter_regex.sub(lambda mo: 2 * mo.group(1) + '\\"', arg)
40+
if quote_cmd:
41+
arg = windows_quoter_regex.sub(lambda mo: 2 * mo.group(1) + '\\"', arg)
4042

4143
# %'s also need to be doubled otherwise they're interpreted as batch
4244
# positional arguments. Also make sure to escape the % so that they're
@@ -48,12 +50,17 @@ def QuoteForRspFile(arg):
4850
# These commands are used in rsp files, so no escaping for the shell (via ^)
4951
# is necessary.
5052

51-
# Finally, wrap the whole thing in quotes so that the above quote rule
52-
# applies and whitespace isn't a word break.
53-
return '"' + arg + '"'
53+
# As a workaround for programs that don't use CommandLineToArgvW, gyp
54+
# supports msvs_quote_cmd=0, which simply disables all quoting.
55+
if quote_cmd:
56+
# Finally, wrap the whole thing in quotes so that the above quote rule
57+
# applies and whitespace isn't a word break.
58+
return f'"{arg}"'
5459

60+
return arg
5561

56-
def EncodeRspFileList(args):
62+
63+
def EncodeRspFileList(args, quote_cmd):
5764
"""Process a list of arguments using QuoteCmdExeArgument."""
5865
# Note that the first argument is assumed to be the command. Don't add
5966
# quotes around it because then built-ins like 'echo', etc. won't work.
@@ -67,7 +74,8 @@ def EncodeRspFileList(args):
6774
program = call + " " + os.path.normpath(program)
6875
else:
6976
program = os.path.normpath(args[0])
70-
return program + " " + " ".join(QuoteForRspFile(arg) for arg in args[1:])
77+
return (program + " " +
78+
" ".join(QuoteForRspFile(arg, quote_cmd) for arg in args[1:]))
7179

7280

7381
def _GenericRetrieve(root, default, path):
@@ -933,13 +941,22 @@ def BuildCygwinBashCommandLine(self, args, path_to_base):
933941
)
934942
return cmd
935943

936-
def IsRuleRunUnderCygwin(self, rule):
937-
"""Determine if an action should be run under cygwin. If the variable is
938-
unset, or set to 1 we use cygwin."""
939-
return (
940-
int(rule.get("msvs_cygwin_shell", self.spec.get("msvs_cygwin_shell", 1)))
941-
!= 0
942-
)
944+
RuleShellFlags = collections.namedtuple("RuleShellFlags", ["cygwin", "quote"])
945+
946+
def GetRuleShellFlags(self, rule):
947+
"""Return RuleShellFlags about how the given rule should be run. This
948+
includes whether it should run under cygwin (msvs_cygwin_shell), and
949+
whether the commands should be quoted (msvs_quote_cmd)."""
950+
# If the variable is unset, or set to 1 we use cygwin
951+
cygwin = int(rule.get("msvs_cygwin_shell",
952+
self.spec.get("msvs_cygwin_shell", 1))) != 0
953+
# Default to quoting. There's only a few special instances where the
954+
# target command uses non-standard command line parsing and handle quotes
955+
# and quote escaping differently.
956+
quote_cmd = int(rule.get("msvs_quote_cmd", 1))
957+
assert quote_cmd != 0 or cygwin != 1, \
958+
"msvs_quote_cmd=0 only applicable for msvs_cygwin_shell=0"
959+
return MsvsSettings.RuleShellFlags(cygwin, quote_cmd)
943960

944961
def _HasExplicitRuleForExtension(self, spec, extension):
945962
"""Determine if there's an explicit rule for a particular extension."""

0 commit comments

Comments
 (0)