2016-06-12 18:59:43 +00:00
|
|
|
local function objdir(e)
|
|
|
|
return concatpath("$(OBJDIR)", cwd(), e.name)
|
|
|
|
end
|
|
|
|
|
2016-06-07 02:13:56 +00:00
|
|
|
definerule("normalrule",
|
|
|
|
{
|
|
|
|
ins = { type="targets" },
|
|
|
|
outleaves = { type="strings" },
|
|
|
|
label = { type="string", optional=true },
|
2016-06-12 18:59:43 +00:00
|
|
|
objdir = { type="string", optional=true },
|
2016-06-07 02:13:56 +00:00
|
|
|
commands = { type="strings" },
|
2016-06-07 03:00:26 +00:00
|
|
|
vars = { type="table", default={} },
|
2016-06-07 02:13:56 +00:00
|
|
|
},
|
|
|
|
function (e)
|
2016-06-12 18:59:43 +00:00
|
|
|
local dir = e.objdir or objdir(e)
|
2016-06-07 02:13:56 +00:00
|
|
|
local realouts = {}
|
|
|
|
for k, v in pairs(e.outleaves) do
|
2016-06-12 18:59:43 +00:00
|
|
|
realouts[k] = concatpath(dir, v)
|
2016-06-07 02:13:56 +00:00
|
|
|
end
|
|
|
|
|
2016-06-09 04:55:44 +00:00
|
|
|
local vars = inherit(e.vars, {
|
2016-06-12 18:59:43 +00:00
|
|
|
dir = dir
|
2016-06-09 04:55:44 +00:00
|
|
|
})
|
|
|
|
|
2016-06-07 03:00:26 +00:00
|
|
|
local result = simplerule {
|
2016-06-07 02:13:56 +00:00
|
|
|
name = e.name,
|
|
|
|
ins = e.ins,
|
|
|
|
outs = realouts,
|
|
|
|
label = e.label,
|
|
|
|
commands = e.commands,
|
2016-06-09 04:55:44 +00:00
|
|
|
vars = vars,
|
2016-06-07 02:13:56 +00:00
|
|
|
}
|
2016-06-12 18:59:43 +00:00
|
|
|
result.dir = dir
|
2016-06-07 03:00:26 +00:00
|
|
|
return result
|
2016-06-07 02:13:56 +00:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2016-06-07 03:00:26 +00:00
|
|
|
definerule("cfile",
|
|
|
|
{
|
|
|
|
srcs = { type="targets" },
|
2016-06-09 04:55:44 +00:00
|
|
|
deps = { type="targets", default={} },
|
2016-06-12 18:59:43 +00:00
|
|
|
cflags = { type="strings", default={} },
|
2016-06-07 03:00:26 +00:00
|
|
|
commands = {
|
|
|
|
type="strings",
|
|
|
|
default={
|
2016-06-12 18:59:43 +00:00
|
|
|
"$(CC) -c -o %{outs[1]} %{ins[1]} %{hdrpaths} %{cflags}"
|
2016-06-07 03:00:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function (e)
|
2016-06-08 01:21:53 +00:00
|
|
|
local csrcs = filenamesof(e.srcs, "%.c$")
|
|
|
|
if (#csrcs ~= 1) then
|
2016-06-07 03:00:26 +00:00
|
|
|
error("you must have exactly one .c file")
|
|
|
|
end
|
|
|
|
|
2016-06-09 04:55:44 +00:00
|
|
|
local hsrcs = filenamesof(e.srcs, "%.h$")
|
|
|
|
local hdeps = selectof(e.deps, "%.h$")
|
2016-06-08 01:21:53 +00:00
|
|
|
local hdrpaths = {}
|
2016-06-09 04:55:44 +00:00
|
|
|
for _, t in pairs(hdeps) do
|
2016-06-07 03:00:26 +00:00
|
|
|
hdrpaths[#hdrpaths+1] = "-I"..t.dir
|
|
|
|
end
|
2016-06-08 01:21:53 +00:00
|
|
|
hdrpaths = uniquify(hdrpaths)
|
2016-06-07 02:13:56 +00:00
|
|
|
|
2016-06-09 04:55:44 +00:00
|
|
|
for _, f in pairs(filenamesof(hdeps)) do
|
|
|
|
hsrcs[#hsrcs+1] = f
|
|
|
|
end
|
|
|
|
|
2016-06-08 01:21:53 +00:00
|
|
|
local outleaf = basename(csrcs[1]):gsub("%.c$", ".o")
|
2016-06-07 03:00:26 +00:00
|
|
|
|
|
|
|
return normalrule {
|
|
|
|
name = e.name,
|
2016-06-09 04:55:44 +00:00
|
|
|
ins = {csrcs[1], unpack(hsrcs)},
|
2016-06-07 03:00:26 +00:00
|
|
|
outleaves = {outleaf},
|
|
|
|
label = e.label,
|
|
|
|
commands = e.commands,
|
|
|
|
vars = {
|
2016-06-12 18:59:43 +00:00
|
|
|
hdrpaths = hdrpaths,
|
|
|
|
cflags = e.cflags,
|
2016-06-07 03:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
)
|
2016-06-06 22:10:22 +00:00
|
|
|
|
2016-06-09 04:55:44 +00:00
|
|
|
definerule("bundle",
|
|
|
|
{
|
|
|
|
srcs = { type="targets" },
|
|
|
|
commands = {
|
|
|
|
type="strings",
|
|
|
|
default={
|
|
|
|
"tar cf - %{ins} | (cd %{dir} && tar xf -)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function (e)
|
|
|
|
local outleaves = {}
|
|
|
|
local commands = {}
|
2016-06-12 18:59:43 +00:00
|
|
|
for _, f in fpairs(e.srcs) do
|
2016-06-09 04:55:44 +00:00
|
|
|
local localf = basename(f)
|
|
|
|
outleaves[#outleaves+1] = localf
|
|
|
|
commands[#commands+1] = "cp "..f.." %{dir}/"..localf
|
|
|
|
end
|
2016-06-07 02:20:08 +00:00
|
|
|
|
2016-06-09 04:55:44 +00:00
|
|
|
return normalrule {
|
|
|
|
name = e.name,
|
|
|
|
ins = e.srcs,
|
|
|
|
outleaves = outleaves,
|
|
|
|
label = e.label,
|
|
|
|
commands = commands
|
|
|
|
}
|
|
|
|
end
|
|
|
|
)
|
2016-06-07 02:20:08 +00:00
|
|
|
|
2016-06-09 04:55:44 +00:00
|
|
|
definerule("clibrary",
|
|
|
|
{
|
|
|
|
srcs = { type="targets" },
|
2016-06-29 11:28:45 +00:00
|
|
|
hdrs = { type="targets", default={} },
|
2016-06-09 04:55:44 +00:00
|
|
|
deps = { type="targets", default={} },
|
2016-06-12 18:59:43 +00:00
|
|
|
cflags = { type="strings", default={} },
|
2016-06-09 04:55:44 +00:00
|
|
|
commands = {
|
|
|
|
type="strings",
|
|
|
|
default={
|
2016-06-29 11:28:45 +00:00
|
|
|
"rm -f %{outs[1]}",
|
|
|
|
"$(AR) cqs %{outs[1]} %{ins}",
|
2016-06-09 04:55:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function (e)
|
|
|
|
local csrcs = filenamesof(e.srcs, "%.c$")
|
|
|
|
if (#csrcs < 1) then
|
|
|
|
error("you must supply at least one C source file")
|
|
|
|
end
|
|
|
|
|
|
|
|
local hsrcs = filenamesof(e.srcs, "%.h$")
|
|
|
|
|
|
|
|
local ins = {}
|
2016-06-12 18:59:43 +00:00
|
|
|
for _, csrc in fpairs(csrcs) do
|
2016-06-09 04:55:44 +00:00
|
|
|
local n = basename(csrc):gsub("%.%w*$", "")
|
|
|
|
ins[#ins+1] = cfile {
|
|
|
|
name = e.name.."/"..n,
|
|
|
|
srcs = {csrc, unpack(hsrcs)},
|
|
|
|
deps = e.deps,
|
2016-06-12 18:59:43 +00:00
|
|
|
cflags = {
|
|
|
|
"-I"..cwd(),
|
|
|
|
unpack(e.cflags)
|
|
|
|
},
|
2016-06-09 04:55:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-06-29 11:28:45 +00:00
|
|
|
local hdrs = filenamesof(e.hdrs, "%.h$")
|
|
|
|
|
|
|
|
local commands = {}
|
|
|
|
for _, s in ipairs(e.commands) do
|
|
|
|
commands[#commands+1] = s
|
|
|
|
end
|
|
|
|
if (#hdrs > 0) then
|
|
|
|
commands[#commands+1] = "cp %{hdrs} %{dir}"
|
|
|
|
end
|
|
|
|
|
2016-06-09 04:55:44 +00:00
|
|
|
return normalrule {
|
|
|
|
name = e.name,
|
|
|
|
ins = ins,
|
2016-06-29 11:28:45 +00:00
|
|
|
outleaves = { e.name..".a", unpack(basename(hdrs)) },
|
2016-06-09 04:55:44 +00:00
|
|
|
label = e.label,
|
2016-06-29 11:28:45 +00:00
|
|
|
commands = commands,
|
|
|
|
vars = {
|
|
|
|
hdrs = hdrs
|
|
|
|
}
|
2016-06-09 04:55:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2016-06-09 05:14:41 +00:00
|
|
|
definerule("cprogram",
|
|
|
|
{
|
|
|
|
srcs = { type="targets", default={} },
|
|
|
|
deps = { type="targets", default={} },
|
2016-06-29 11:28:45 +00:00
|
|
|
cflags = { type="strings", default={} },
|
2016-06-09 05:14:41 +00:00
|
|
|
commands = {
|
|
|
|
type="strings",
|
|
|
|
default={
|
2016-06-19 07:32:45 +00:00
|
|
|
"$(CC) -o %{outs[1]} %{ins} %{ins}"
|
2016-06-09 05:14:41 +00:00
|
|
|
},
|
|
|
|
}
|
2016-06-09 04:55:44 +00:00
|
|
|
},
|
2016-06-09 05:14:41 +00:00
|
|
|
function (e)
|
|
|
|
local libs = filenamesof(e.deps, "%.a$")
|
|
|
|
if (#e.srcs > 0) then
|
|
|
|
for _, f in pairs(filenamesof(
|
|
|
|
{
|
|
|
|
clibrary {
|
|
|
|
name = e.name .. "/main",
|
|
|
|
srcs = e.srcs,
|
2016-06-29 11:28:45 +00:00
|
|
|
deps = e.deps,
|
|
|
|
cflags = e.cflags,
|
2016-06-09 05:14:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"%.a$"
|
|
|
|
)) do
|
|
|
|
libs[#libs+1] = f
|
2016-06-06 22:10:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-09 05:14:41 +00:00
|
|
|
return normalrule {
|
|
|
|
name = e.name,
|
|
|
|
ins = libs,
|
|
|
|
outleaves = { e.name },
|
|
|
|
commands = e.commands,
|
2016-06-06 22:10:22 +00:00
|
|
|
}
|
|
|
|
end
|
2016-06-09 05:14:41 +00:00
|
|
|
)
|
2016-06-06 22:10:22 +00:00
|
|
|
|