Skip to content

Commit

Permalink
fix: support msvs_quote_cmd in ninja generator (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Aug 18, 2021
1 parent 3462d4c commit 46486ac
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
27 changes: 14 additions & 13 deletions pylib/gyp/generator/ninja.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,18 +654,18 @@ def WriteActions(
description = self.GenerateDescription(
"ACTION", action.get("message", None), name
)
is_cygwin = (
self.msvs_settings.IsRuleRunUnderCygwin(action)
win_shell_flags = (
self.msvs_settings.GetRuleShellFlags(action)
if self.flavor == "win"
else False
else None
)
args = action["action"]
depfile = action.get("depfile", None)
if depfile:
depfile = self.ExpandSpecial(depfile, self.base_to_build)
pool = "console" if int(action.get("ninja_use_console", 0)) else None
rule_name, _ = self.WriteNewNinjaRule(
name, args, description, is_cygwin, env, pool, depfile=depfile
name, args, description, win_shell_flags, env, pool, depfile=depfile
)

inputs = [self.GypPathToNinja(i, env) for i in action["inputs"]]
Expand Down Expand Up @@ -707,14 +707,14 @@ def WriteRules(
rule.get("message", None),
("%s " + generator_default_variables["RULE_INPUT_PATH"]) % name,
)
is_cygwin = (
self.msvs_settings.IsRuleRunUnderCygwin(rule)
win_shell_flags = (
self.msvs_settings.GetRuleShellFlags(rule)
if self.flavor == "win"
else False
else None
)
pool = "console" if int(rule.get("ninja_use_console", 0)) else None
rule_name, args = self.WriteNewNinjaRule(
name, args, description, is_cygwin, env, pool
name, args, description, win_shell_flags, env, pool
)

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

def cygwin_munge(path):
# pylint: disable=cell-var-from-loop
if is_cygwin:
if win_shell_flags and win_shell_flags.cygwin:
return path.replace("\\", "/")
return path

Expand Down Expand Up @@ -1899,7 +1899,7 @@ def WriteVariableList(self, ninja_file, var, values):
ninja_file.variable(var, " ".join(values))

def WriteNewNinjaRule(
self, name, args, description, is_cygwin, env, pool, depfile=None
self, name, args, description, win_shell_flags, env, pool, depfile=None
):
"""Write out a new ninja "rule" statement for a given command.
Expand Down Expand Up @@ -1946,13 +1946,14 @@ def WriteNewNinjaRule(
if self.flavor == "win":
rspfile = rule_name + ".$unique_name.rsp"
# The cygwin case handles this inside the bash sub-shell.
run_in = "" if is_cygwin else " " + self.build_to_base
if is_cygwin:
run_in = "" if win_shell_flags.cygwin else " " + self.build_to_base
if win_shell_flags.cygwin:
rspfile_content = self.msvs_settings.BuildCygwinBashCommandLine(
args, self.build_to_base
)
else:
rspfile_content = gyp.msvs_emulation.EncodeRspFileList(args)
rspfile_content = gyp.msvs_emulation.EncodeRspFileList(
args, win_shell_flags.quote)
command = (
"%s gyp-win-tool action-wrapper $arch " % sys.executable
+ rspfile
Expand Down
45 changes: 31 additions & 14 deletions pylib/gyp/msvs_emulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
build systems, primarily ninja.
"""

import collections
import os
import re
import subprocess
Expand All @@ -19,7 +20,7 @@
windows_quoter_regex = re.compile(r'(\\*)"')


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

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

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

return arg

def EncodeRspFileList(args):

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


def _GenericRetrieve(root, default, path):
Expand Down Expand Up @@ -933,13 +941,22 @@ def BuildCygwinBashCommandLine(self, args, path_to_base):
)
return cmd

def IsRuleRunUnderCygwin(self, rule):
"""Determine if an action should be run under cygwin. If the variable is
unset, or set to 1 we use cygwin."""
return (
int(rule.get("msvs_cygwin_shell", self.spec.get("msvs_cygwin_shell", 1)))
!= 0
)
RuleShellFlags = collections.namedtuple("RuleShellFlags", ["cygwin", "quote"])

def GetRuleShellFlags(self, rule):
"""Return RuleShellFlags about how the given rule should be run. This
includes whether it should run under cygwin (msvs_cygwin_shell), and
whether the commands should be quoted (msvs_quote_cmd)."""
# If the variable is unset, or set to 1 we use cygwin
cygwin = int(rule.get("msvs_cygwin_shell",
self.spec.get("msvs_cygwin_shell", 1))) != 0
# Default to quoting. There's only a few special instances where the
# target command uses non-standard command line parsing and handle quotes
# and quote escaping differently.
quote_cmd = int(rule.get("msvs_quote_cmd", 1))
assert quote_cmd != 0 or cygwin != 1, \
"msvs_quote_cmd=0 only applicable for msvs_cygwin_shell=0"
return MsvsSettings.RuleShellFlags(cygwin, quote_cmd)

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

0 comments on commit 46486ac

Please sign in to comment.