-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
executable file
·159 lines (130 loc) · 4.18 KB
/
util.lua
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
local lib, print, print_status = ursa.gen.lib, ursa.gen.print, ursa.gen.print_status
-- turns out this is a surprisingly subtle function, though this really, really shouldn't be surprising anyone
function ursa.util.token_deferred(chunk)
assert(chunk[1])
local ab = ursa.absolute_from{"#" .. chunk[1]}
assert(type(ab) == "string")
assert(ab:sub(1, 2) == "#@")
local cdep = {}
for k, v in pairs(chunk) do
cdep[k] = v
end
cdep[1] = ab
local func = function ()
return ursa.token(cdep)
end
if chunk.default then
return func
else
return {ab, func}
end
end
function ursa.util.clean()
for k in ursa.FRAGILE.list{} do
if k:sub(1, 1) == '#' then
print_status("clearing " .. k)
ursa.FRAGILE.token_clear{k:sub(2)}
else
print_status("removing " .. k)
os.remove(k)
end
end
end
function ursa.util.system_template(st)
local str = unpack(st)
local tokz = {}
for tok in str:gmatch("(#[%w_]+)") do
table.insert(tokz, tok)
end
if #tokz == 0 then tokz = nil end
return {run = function(dests, deps)
if str:find("$TARGET") and not str:find("$TARGETS") then assert(#dests == 1, "Found string $TARGET, but rule has multiple targets") end
if str:find("$SOURCE") and not str:find("$SOURCES") then assert(#deps == 1, "Found string $SOURCE, but rule has multiple sources") end
local outdeps = {}
for _, v in pairs(deps) do
if v:sub(1, 1) ~= "#" then
table.insert(outdeps, v)
end
end
local stpass = str
--print(str)
str = str:gsub("$TARGETS", ursa.FRAGILE.parenthesize(dests))
str = str:gsub("$TARGET", ursa.FRAGILE.parenthesize(dests and dests[1]))
str = str:gsub("$SOURCES", ursa.FRAGILE.parenthesize(outdeps))
str = str:gsub("$SOURCE", ursa.FRAGILE.parenthesize(deps and deps[1]))
--print(str)
str = str:gsub("#([%w_]+)", function (param) return ursa.token{param} end)
return ursa.system{str}
end, depends = {"!" .. str, tokz}}
end
function ursa.util.copy()
return function(dests, deps)
assert(#dests == 1)
assert(#deps == 1)
print("Copying", deps[1], dests[1])
local i = io.open(deps[1], "rb")
local o = io.open(dests[1], "wb")
assert(i, "Couldn't open input file " .. deps[1])
assert(o, "Couldn't open output file " .. dests[1])
local size = 2^15
while true do
local block = i:read(size)
if not block then break end
o:write(block)
end
i:close()
o:close()
lib.chmod_set(dests[1], lib.chmod_get(deps[1]))
end
--return ursa.system_template{"cp $SOURCE $TARGET"} -- this may be reimplemented later
end
local param_map = {
j = "jobs",
}
function ursa.util.cli_parameter_parse(params)
local rv = {}
for _, v in ipairs(params) do
if v:sub(1, 1) == "-" then
local param, option
if v:sub(2, 2) == "-" then
-- long format
param, option = v:match("^%-%-(.*)=(.*)$")
if not param then
param = v:match("^%-%-(.*)$")
end
else
-- short format
param, option = v:match("^%-(.)(.*)$")
if option == "" then option = nil end
print("short format", v, param, option)
param = param_map[param]
end
if param == "paranoid" then
if option == "true" or option == "yes" or option == nil then option = true end
if option == "false" or option == "no" then option = false end
elseif param == "jobs" then
option = tonumber(option)
end
print("CS", param, option)
ursa.config_set{param, option}
else
table.insert(rv, v)
end
end
return rv
end
local after_count = 0
function ursa.util.after(params)
after_count = after_count + 1
return ursa.token.rule{"(ursa.util.after invocation " .. after_count .. ")", nil, function () ursa.build{params} return "" end, always_rebuild = true}
end
local params = {
system = {1},
token_deferred = {1, default = true},
system_template = {1},
clean = {0}, -- I'm actually not sure how this even works
copy = {0},
cli_parameter_parse = {1e10}, -- technically infinite
after = {1e10}, -- technically infinite
}
ursa.gen.wrap_funcs(ursa.util, params)