2022-10-02 05:13:41 +00:00
|
|
|
# Copyright 2011 Google Inc. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""Python module for generating .ninja files.
|
|
|
|
|
|
|
|
Note that this is emphatically not a required piece of Ninja; it's
|
|
|
|
just a helpful utility for build-file-generation systems that already
|
|
|
|
use Python.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
import textwrap
|
2024-06-24 04:34:51 +00:00
|
|
|
import os
|
|
|
|
from io import StringIO
|
|
|
|
from pathlib import Path
|
2024-10-30 05:37:41 +00:00
|
|
|
from typing import Dict, Iterable, List, Match, Optional, Tuple, Union
|
2024-06-24 04:34:51 +00:00
|
|
|
|
|
|
|
NinjaPath = Union[str, Path]
|
2024-10-30 05:37:41 +00:00
|
|
|
NinjaPaths = Iterable[Optional[NinjaPath]]
|
2024-06-24 04:34:51 +00:00
|
|
|
NinjaPathOrPaths = Union[NinjaPath, NinjaPaths]
|
|
|
|
|
|
|
|
|
|
|
|
def escape_path(word: str) -> str:
|
2023-10-08 11:41:43 +00:00
|
|
|
return word.replace("$ ", "$$ ").replace(" ", "$ ").replace(":", "$:")
|
|
|
|
|
2022-10-02 05:13:41 +00:00
|
|
|
|
|
|
|
class Writer(object):
|
2024-06-24 04:34:51 +00:00
|
|
|
def __init__(self, output: StringIO, width: int = 78) -> None:
|
2022-10-02 05:13:41 +00:00
|
|
|
self.output = output
|
|
|
|
self.width = width
|
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def newline(self) -> None:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.output.write("\n")
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def comment(self, text: str) -> None:
|
2023-10-08 11:41:43 +00:00
|
|
|
for line in textwrap.wrap(
|
|
|
|
text, self.width - 2, break_long_words=False, break_on_hyphens=False
|
|
|
|
):
|
|
|
|
self.output.write("# " + line + "\n")
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def variable(
|
|
|
|
self,
|
|
|
|
key: str,
|
|
|
|
value: Optional[NinjaPathOrPaths],
|
|
|
|
indent: int = 0,
|
|
|
|
) -> None:
|
|
|
|
value = " ".join(serialize_paths(value))
|
2023-10-08 11:41:43 +00:00
|
|
|
self._line("%s = %s" % (key, value), indent)
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def pool(self, name: str, depth: int) -> None:
|
2023-10-08 11:41:43 +00:00
|
|
|
self._line("pool %s" % name)
|
2024-06-24 04:34:51 +00:00
|
|
|
self.variable("depth", str(depth), indent=1)
|
2023-10-08 11:41:43 +00:00
|
|
|
|
|
|
|
def rule(
|
|
|
|
self,
|
2024-06-24 04:34:51 +00:00
|
|
|
name: str,
|
|
|
|
command: str,
|
|
|
|
description: Optional[str] = None,
|
|
|
|
depfile: Optional[NinjaPath] = None,
|
|
|
|
generator: bool = False,
|
|
|
|
pool: Optional[str] = None,
|
|
|
|
restat: bool = False,
|
|
|
|
rspfile: Optional[NinjaPath] = None,
|
|
|
|
rspfile_content: Optional[NinjaPath] = None,
|
|
|
|
deps: Optional[NinjaPathOrPaths] = None,
|
|
|
|
) -> None:
|
2023-10-08 11:41:43 +00:00
|
|
|
self._line("rule %s" % name)
|
|
|
|
self.variable("command", command, indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if description:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("description", description, indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if depfile:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("depfile", depfile, indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if generator:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("generator", "1", indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if pool:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("pool", pool, indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if restat:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("restat", "1", indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if rspfile:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("rspfile", rspfile, indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if rspfile_content:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("rspfile_content", rspfile_content, indent=1)
|
2022-10-02 05:13:41 +00:00
|
|
|
if deps:
|
2023-10-08 11:41:43 +00:00
|
|
|
self.variable("deps", deps, indent=1)
|
|
|
|
|
|
|
|
def build(
|
|
|
|
self,
|
2024-06-24 04:34:51 +00:00
|
|
|
outputs: NinjaPathOrPaths,
|
|
|
|
rule: str,
|
|
|
|
inputs: Optional[NinjaPathOrPaths] = None,
|
|
|
|
implicit: Optional[NinjaPathOrPaths] = None,
|
|
|
|
order_only: Optional[NinjaPathOrPaths] = None,
|
|
|
|
variables: Optional[
|
|
|
|
Union[
|
|
|
|
List[Tuple[str, Optional[NinjaPathOrPaths]]],
|
|
|
|
Dict[str, Optional[NinjaPathOrPaths]],
|
|
|
|
]
|
|
|
|
] = None,
|
|
|
|
implicit_outputs: Optional[NinjaPathOrPaths] = None,
|
|
|
|
pool: Optional[str] = None,
|
|
|
|
dyndep: Optional[NinjaPath] = None,
|
|
|
|
) -> List[str]:
|
2024-10-30 05:37:41 +00:00
|
|
|
str_outputs = serialize_paths(outputs)
|
|
|
|
out_outputs = [escape_path(x) for x in str_outputs]
|
2024-06-24 04:34:51 +00:00
|
|
|
all_inputs = [escape_path(x) for x in serialize_paths(inputs)]
|
2022-10-02 05:13:41 +00:00
|
|
|
|
|
|
|
if implicit:
|
2024-06-24 04:34:51 +00:00
|
|
|
implicit = [escape_path(x) for x in serialize_paths(implicit)]
|
2023-10-08 11:41:43 +00:00
|
|
|
all_inputs.append("|")
|
2024-06-24 04:34:51 +00:00
|
|
|
all_inputs.extend(map(str, implicit))
|
2022-10-02 05:13:41 +00:00
|
|
|
if order_only:
|
2024-06-24 04:34:51 +00:00
|
|
|
order_only = [escape_path(x) for x in serialize_paths(order_only)]
|
2023-10-08 11:41:43 +00:00
|
|
|
all_inputs.append("||")
|
2024-06-24 04:34:51 +00:00
|
|
|
all_inputs.extend(map(str, order_only))
|
2022-10-02 05:13:41 +00:00
|
|
|
if implicit_outputs:
|
2024-06-24 04:34:51 +00:00
|
|
|
implicit_outputs = [
|
|
|
|
escape_path(x) for x in serialize_paths(implicit_outputs)
|
|
|
|
]
|
2023-10-08 11:41:43 +00:00
|
|
|
out_outputs.append("|")
|
2024-06-24 04:34:51 +00:00
|
|
|
out_outputs.extend(map(str, implicit_outputs))
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2023-10-08 11:41:43 +00:00
|
|
|
self._line(
|
|
|
|
"build %s: %s" % (" ".join(out_outputs), " ".join([rule] + all_inputs))
|
|
|
|
)
|
2022-10-02 05:13:41 +00:00
|
|
|
if pool is not None:
|
2023-10-08 11:41:43 +00:00
|
|
|
self._line(" pool = %s" % pool)
|
2022-10-02 05:13:41 +00:00
|
|
|
if dyndep is not None:
|
2024-06-24 04:34:51 +00:00
|
|
|
self._line(" dyndep = %s" % serialize_path(dyndep))
|
2022-10-02 05:13:41 +00:00
|
|
|
|
|
|
|
if variables:
|
|
|
|
if isinstance(variables, dict):
|
|
|
|
iterator = iter(variables.items())
|
|
|
|
else:
|
|
|
|
iterator = iter(variables)
|
|
|
|
|
|
|
|
for key, val in iterator:
|
|
|
|
self.variable(key, val, indent=1)
|
|
|
|
|
2024-10-30 05:37:41 +00:00
|
|
|
return str_outputs
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def include(self, path: str) -> None:
|
2023-10-08 11:41:43 +00:00
|
|
|
self._line("include %s" % path)
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def subninja(self, path: str) -> None:
|
2023-10-08 11:41:43 +00:00
|
|
|
self._line("subninja %s" % path)
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def default(self, paths: NinjaPathOrPaths) -> None:
|
|
|
|
self._line("default %s" % " ".join(serialize_paths(paths)))
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def _count_dollars_before_index(self, s: str, i: int) -> int:
|
2022-10-02 05:13:41 +00:00
|
|
|
"""Returns the number of '$' characters right in front of s[i]."""
|
|
|
|
dollar_count = 0
|
|
|
|
dollar_index = i - 1
|
2023-10-08 11:41:43 +00:00
|
|
|
while dollar_index > 0 and s[dollar_index] == "$":
|
2022-10-02 05:13:41 +00:00
|
|
|
dollar_count += 1
|
|
|
|
dollar_index -= 1
|
|
|
|
return dollar_count
|
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def _line(self, text: str, indent: int = 0) -> None:
|
2022-10-02 05:13:41 +00:00
|
|
|
"""Write 'text' word-wrapped at self.width characters."""
|
2023-10-08 11:41:43 +00:00
|
|
|
leading_space = " " * indent
|
2022-10-02 05:13:41 +00:00
|
|
|
while len(leading_space) + len(text) > self.width:
|
|
|
|
# The text is too wide; wrap if possible.
|
|
|
|
|
|
|
|
# Find the rightmost space that would obey our width constraint and
|
|
|
|
# that's not an escaped space.
|
2023-10-08 11:41:43 +00:00
|
|
|
available_space = self.width - len(leading_space) - len(" $")
|
2022-10-02 05:13:41 +00:00
|
|
|
space = available_space
|
|
|
|
while True:
|
2023-10-08 11:41:43 +00:00
|
|
|
space = text.rfind(" ", 0, space)
|
|
|
|
if space < 0 or self._count_dollars_before_index(text, space) % 2 == 0:
|
2022-10-02 05:13:41 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if space < 0:
|
|
|
|
# No such space; just use the first unescaped space we can find.
|
|
|
|
space = available_space - 1
|
|
|
|
while True:
|
2023-10-08 11:41:43 +00:00
|
|
|
space = text.find(" ", space + 1)
|
|
|
|
if (
|
|
|
|
space < 0
|
|
|
|
or self._count_dollars_before_index(text, space) % 2 == 0
|
|
|
|
):
|
2022-10-02 05:13:41 +00:00
|
|
|
break
|
|
|
|
if space < 0:
|
|
|
|
# Give up on breaking.
|
|
|
|
break
|
|
|
|
|
2023-10-08 11:41:43 +00:00
|
|
|
self.output.write(leading_space + text[0:space] + " $\n")
|
|
|
|
text = text[space + 1 :]
|
2022-10-02 05:13:41 +00:00
|
|
|
|
|
|
|
# Subsequent lines are continuations, so indent them.
|
2023-10-08 11:41:43 +00:00
|
|
|
leading_space = " " * (indent + 2)
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2023-10-08 11:41:43 +00:00
|
|
|
self.output.write(leading_space + text + "\n")
|
2022-10-02 05:13:41 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def close(self) -> None:
|
2022-10-02 05:13:41 +00:00
|
|
|
self.output.close()
|
|
|
|
|
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def serialize_path(input: Optional[NinjaPath]) -> str:
|
|
|
|
if not input:
|
|
|
|
return ""
|
|
|
|
if isinstance(input, Path):
|
|
|
|
return str(input).replace("/", os.sep)
|
|
|
|
else:
|
|
|
|
return str(input)
|
|
|
|
|
|
|
|
|
|
|
|
def serialize_paths(input: Optional[NinjaPathOrPaths]) -> List[str]:
|
2024-10-30 05:37:41 +00:00
|
|
|
if isinstance(input, str) or isinstance(input, Path):
|
|
|
|
return [serialize_path(input)] if input else []
|
|
|
|
elif input is not None:
|
2024-06-24 04:34:51 +00:00
|
|
|
return [serialize_path(path) for path in input if path]
|
2024-10-30 05:37:41 +00:00
|
|
|
return []
|
2022-10-02 05:13:41 +00:00
|
|
|
|
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def escape(string: str) -> str:
|
2022-10-02 05:13:41 +00:00
|
|
|
"""Escape a string such that it can be embedded into a Ninja file without
|
|
|
|
further interpretation."""
|
2023-10-08 11:41:43 +00:00
|
|
|
assert "\n" not in string, "Ninja syntax does not allow newlines"
|
2022-10-02 05:13:41 +00:00
|
|
|
# We only have one special metacharacter: '$'.
|
2023-10-08 11:41:43 +00:00
|
|
|
return string.replace("$", "$$")
|
2022-10-02 05:13:41 +00:00
|
|
|
|
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def expand(string: str, vars: Dict[str, str], local_vars: Dict[str, str] = {}) -> str:
|
2022-10-02 05:13:41 +00:00
|
|
|
"""Expand a string containing $vars as Ninja would.
|
|
|
|
|
|
|
|
Note: doesn't handle the full Ninja variable syntax, but it's enough
|
|
|
|
to make configure.py's use of it work.
|
|
|
|
"""
|
2023-10-08 11:41:43 +00:00
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
def exp(m: Match[str]) -> str:
|
2022-10-02 05:13:41 +00:00
|
|
|
var = m.group(1)
|
2023-10-08 11:41:43 +00:00
|
|
|
if var == "$":
|
|
|
|
return "$"
|
|
|
|
return local_vars.get(var, vars.get(var, ""))
|
|
|
|
|
2024-06-24 04:34:51 +00:00
|
|
|
return re.sub(r"\$(\$|\w*)", exp, string)
|