summaryrefslogtreecommitdiffstats
path: root/misc/ninja_syntax.py
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2014-11-18 15:48:26 (GMT)
committerEvan Martin <martine@danga.com>2014-11-18 16:15:36 (GMT)
commit76a95e45bba61b617dfac14e83cb6229a4e4e897 (patch)
tree3a985efc70044e122011aa8c938fab502ea8c271 /misc/ninja_syntax.py
parentf323ff900a23526686af266b0ffabf711f48f19e (diff)
downloadNinja-76a95e45bba61b617dfac14e83cb6229a4e4e897.zip
Ninja-76a95e45bba61b617dfac14e83cb6229a4e4e897.tar.gz
Ninja-76a95e45bba61b617dfac14e83cb6229a4e4e897.tar.bz2
add an "expand" function to ninja_syntax
Implements basic variable expansion for use in configure.py.
Diffstat (limited to 'misc/ninja_syntax.py')
-rw-r--r--misc/ninja_syntax.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/misc/ninja_syntax.py b/misc/ninja_syntax.py
index 14b932f..e200514 100644
--- a/misc/ninja_syntax.py
+++ b/misc/ninja_syntax.py
@@ -7,6 +7,7 @@ just a helpful utility for build-file-generation systems that already
use Python.
"""
+import re
import textwrap
def escape_path(word):
@@ -154,3 +155,17 @@ def escape(string):
assert '\n' not in string, 'Ninja syntax does not allow newlines'
# We only have one special metacharacter: '$'.
return string.replace('$', '$$')
+
+
+def expand(string, vars, local_vars={}):
+ """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.
+ """
+ def exp(m):
+ var = m.group(1)
+ if var == '$':
+ return '$'
+ return local_vars.get(var, vars.get(var, ''))
+ return re.sub(r'\$(\$|\w*)', exp, string)