-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnu_make_lib.py
110 lines (88 loc) · 2.99 KB
/
gnu_make_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import glob
import os
import shlex
# Utility functions (not exposed as make functions)
# Whether a string matches some patterns, for the $(filter) and $(filter-out) functions
def _match_filter(s, patterns):
for pat in patterns:
# Deal with %, which is the only special character, and only permitted once
if '%' in pat:
[pre, _, suf] = pat.partition('%')
if s.startswith(pre) and s.endswith(suf):
return True
elif s == pat:
return True
return False
def _split_cmd(text):
if not isinstance(text, str):
return text
s = shlex.shlex(text, punctuation_chars=True, posix=True)
# Allow % in the middle of words, since here it's generally a glob pattern
s.wordchars += '%'
return list(s)
def _split_cmds(cmds):
new_cmds = []
for cmd in cmds:
if not isinstance(cmd, list):
cmd = _split_cmd(cmd)
if isinstance(cmd, list):
# Check for shell syntax that we can't handle
# XXX We should be more strict really. And when we find it, should
# pass to 'sh'
for token in ['&&', '||', '|', '<', '>']:
if token in cmd:
assert False, ('unsupported shell syntax in command: '
'%s in %s' % (token, cmd))
# Split sub-commands by semicolon
while ';' in cmd:
idx = cmd.index(';')
new_cmds.append(cmd[:idx])
cmd = cmd[idx+1:]
new_cmds.append(cmd)
return new_cmds
# GNU Make function library
def addprefix(prefix, names):
return ' '.join(prefix + x for x in names.split())
def addsuffix(suffix, names):
return ' '.join(x + suffix for x in names.split())
def and_(*args):
arg = ''
for arg in args:
arg = arg.strip()
if not arg:
return arg
return arg
def filter(pattern, text):
pattern = pattern.split()
return ' '.join(s for s in text.split() if _match_filter(s, pattern))
def filter_out(pattern, text):
pattern = pattern.split()
return ' '.join(s for s in text.split() if not _match_filter(s, pattern))
def findstring(pattern, text):
return pattern if pattern in text else ''
def notdir(arg):
return ' '.join(os.path.split(v)[1] for v in arg.split())
def or_(*args):
for arg in args:
arg = arg.strip()
if arg:
return arg
return ''
def patsubst(old, new, s):
[prefix, _, suffix] = old.partition('%')
parts = []
for part in s.split():
if part.startswith(prefix) and part.endswith(suffix):
part = new.replace('%', part[len(prefix):-len(suffix) or None], 1)
parts.append(part)
return ' '.join(parts)
def realpath(arg):
return os.path.realpath(arg)
def sort(arg):
return ' '.join(sorted(set(arg.split())))
def strip(arg):
return ' '.join(arg.split())
def subst(old, new, s):
return s.replace(old, new)
def wildcard(arg):
return ' '.join(sorted(glob.glob(arg)))