diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 658a19271229..5dcb7bc9a59e 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -123,13 +123,6 @@ def get_rsp_threshold() -> int: NINJA_QUOTE_VAR_PAT = re.compile(r"[$ \n]") def ninja_quote(text: str, is_build_line: bool = False) -> str: - if is_build_line: - quote_re = NINJA_QUOTE_BUILD_PAT - else: - quote_re = NINJA_QUOTE_VAR_PAT - # Fast path for when no quoting is necessary - if not quote_re.search(text): - return text if '\n' in text: errmsg = f'''Ninja does not support newlines in rules. The content was: @@ -137,7 +130,19 @@ def ninja_quote(text: str, is_build_line: bool = False) -> str: Please report this error with a test case to the Meson bug tracker.''' raise MesonException(errmsg) - return quote_re.sub(r'$\g<0>', text) + + if is_build_line: + if ':' in text: + return NINJA_QUOTE_BUILD_PAT.sub(r'$\g<0>', text) + quote_re = NINJA_QUOTE_BUILD_PAT + else: + quote_re = NINJA_QUOTE_VAR_PAT + + if ' ' in text: + return quote_re.sub(r'$\g<0>', text) + if '$' in text: + return quote_re.sub(r'$\g<0>', text) + return text @dataclass