Replace the hacky cflags variable with an equally hacky but vastly

more useful magic vars variable.
This commit is contained in:
David Given 2016-07-27 00:10:15 +02:00
parent cdeea836f2
commit f9c77fca03
6 changed files with 58 additions and 32 deletions

View file

@ -1,3 +1,7 @@
vars.cflags = {
"-g", "-O"
}
installable { installable {
name = "ack", name = "ack",
map = { map = {

View file

@ -15,6 +15,18 @@ local targets = {}
local buildfiles = {} local buildfiles = {}
local globals local globals
local cwd = "." local cwd = "."
local vars = {}
local parente = {}
local function concat(...)
local r = {}
for k, t in ipairs({...}) do
for _, v in ipairs(t) do
r[#r+1] = v
end
end
return r
end
local function inherit(high, low) local function inherit(high, low)
local o = {} local o = {}
@ -27,6 +39,12 @@ local function inherit(high, low)
return low[k] return low[k]
end end
}) })
for k, v in pairs(high) do
local _, _, kk = k:find("^%+(.*)$")
if kk then
o[kk] = concat(low[kk], v)
end
end
return o return o
end end
@ -53,16 +71,6 @@ local function asstring(o)
end end
end end
local function concat(...)
local r = {}
for _, t in ipairs({...}) do
for _, v in ipairs(t) do
r[#r+1] = v
end
end
return r
end
local function concatpath(...) local function concatpath(...)
local p = table.concat({...}, "/") local p = table.concat({...}, "/")
return (p:gsub("/+", "/"):gsub("^%./", ""):gsub("/%./", "/")) return (p:gsub("/+", "/"):gsub("^%./", ""):gsub("/%./", "/"))
@ -404,6 +412,7 @@ local function definerule(rulename, types, cb)
types.name = { type="string" } types.name = { type="string" }
types.cwd = { type="string", optional=true } types.cwd = { type="string", optional=true }
types.vars = { type="table", default={} }
for propname, typespec in pairs(types) do for propname, typespec in pairs(types) do
if not typeconverters[typespec.type] then if not typeconverters[typespec.type] then
@ -414,6 +423,11 @@ local function definerule(rulename, types, cb)
local rulecwd = cwd local rulecwd = cwd
local rule = function(e) local rule = function(e)
local definedprops = {}
for propname, _ in pairs(e) do
definedprops[propname] = true
end
local args = {} local args = {}
for propname, typespec in pairs(types) do for propname, typespec in pairs(types) do
if not e[propname] then if not e[propname] then
@ -424,11 +438,11 @@ local function definerule(rulename, types, cb)
args[propname] = typespec.default args[propname] = typespec.default
else else
args[propname] = typeconverters[typespec.type](propname, e[propname]) args[propname] = typeconverters[typespec.type](propname, e[propname])
e[propname] = nil definedprops[propname] = nil
end end
end end
local propname, _ = next(e) local propname, _ = next(definedprops)
if propname then if propname then
error(string.format("don't know what to do with property '%s'", propname)) error(string.format("don't know what to do with property '%s'", propname))
end end
@ -438,7 +452,11 @@ local function definerule(rulename, types, cb)
end end
args.fullname = args.cwd.."+"..args.name args.fullname = args.cwd.."+"..args.name
local oldparente = parente
parente = args
args.vars = inherit(args.vars, oldparente.vars)
local result = cb(args) or {} local result = cb(args) or {}
parente = oldparente
result.is = result.is or {} result.is = result.is or {}
result.is[rulename] = true result.is[rulename] = true
@ -678,6 +696,7 @@ globals = {
startswith = startswith, startswith = startswith,
targetnamesof = targetnamesof, targetnamesof = targetnamesof,
uniquify = uniquify, uniquify = uniquify,
vars = vars,
} }
setmetatable(globals, setmetatable(globals,
{ {
@ -692,6 +711,9 @@ setmetatable(globals,
} }
) )
vars.cflags = {}
parente.vars = vars
do do
local emitter_type = install_make_emitter local emitter_type = install_make_emitter
parse_arguments( parse_arguments(

View file

@ -10,7 +10,6 @@ definerule("normalrule",
label = { type="string", optional=true }, label = { type="string", optional=true },
objdir = { type="string", optional=true }, objdir = { type="string", optional=true },
commands = { type="strings" }, commands = { type="strings" },
vars = { type="table", default={} },
}, },
function (e) function (e)
local dir = e.objdir or objdir(e) local dir = e.objdir or objdir(e)
@ -41,7 +40,6 @@ definerule("cfile",
{ {
srcs = { type="targets" }, srcs = { type="targets" },
deps = { type="targets", default={} }, deps = { type="targets", default={} },
cflags = { type="strings", default={} },
commands = { commands = {
type="strings", type="strings",
default={ default={
@ -74,7 +72,6 @@ definerule("cfile",
commands = e.commands, commands = e.commands,
vars = { vars = {
hdrpaths = hdrpaths, hdrpaths = hdrpaths,
cflags = e.cflags,
} }
} }
end end
@ -85,7 +82,6 @@ definerule("cppfile",
srcs = { type="targets" }, srcs = { type="targets" },
deps = { type="targets", default={} }, deps = { type="targets", default={} },
outleaf = { type="string" }, outleaf = { type="string" },
cflags = { type="strings", default={} },
commands = { commands = {
type="strings", type="strings",
default={ default={
@ -114,7 +110,6 @@ definerule("cppfile",
commands = e.commands, commands = e.commands,
vars = { vars = {
hdrpaths = hdrpaths, hdrpaths = hdrpaths,
cflags = e.cflags,
} }
} }
end end
@ -155,7 +150,6 @@ definerule("clibrary",
srcs = { type="targets", default={} }, srcs = { type="targets", default={} },
hdrs = { type="targets", default={} }, hdrs = { type="targets", default={} },
deps = { type="targets", default={} }, deps = { type="targets", default={} },
cflags = { type="strings", default={} },
commands = { commands = {
type="strings", type="strings",
default={ default={
@ -176,9 +170,8 @@ definerule("clibrary",
cwd = e.cwd, cwd = e.cwd,
srcs = {csrc, unpack(hsrcs)}, srcs = {csrc, unpack(hsrcs)},
deps = e.deps, deps = e.deps,
cflags = { vars = {
"-I"..e.cwd, ["+cflags"] = { "-I"..e.cwd, },
unpack(e.cflags)
}, },
} }
end end
@ -212,7 +205,6 @@ definerule("cprogram",
{ {
srcs = { type="targets", default={} }, srcs = { type="targets", default={} },
deps = { type="targets", default={} }, deps = { type="targets", default={} },
cflags = { type="strings", default={} },
commands = { commands = {
type="strings", type="strings",
default={ default={
@ -230,7 +222,6 @@ definerule("cprogram",
cwd = e.cwd, cwd = e.cwd,
srcs = e.srcs, srcs = e.srcs,
deps = e.deps, deps = e.deps,
cflags = e.cflags,
} }
}, },
"%.a$" "%.a$"

View file

@ -79,10 +79,12 @@ local function build_variant(code, cflags)
"modules/src/system+lib", "modules/src/system+lib",
"util/data+em_data", "util/data+em_data",
}, },
cflags = { cflags } vars = {
["+cflags"] = cflags
},
} }
end end
build_variant("e", "-DREADABLE_EM") build_variant("e", { "-DREADABLE_EM" })
build_variant("k", "") build_variant("k", { })

View file

@ -27,11 +27,13 @@ normalrule {
clibrary { clibrary {
name = "lib_ev", name = "lib_ev",
cflags = { vars = {
"-DPRIVATE=static", ["+cflags"] = {
"-DEXPORT=", "-DPRIVATE=static",
"-DNDEBUG", "-DEXPORT=",
"-DCHECKING" "-DNDEBUG",
"-DCHECKING"
},
}, },
srcs = { srcs = {
"./EM_vars.c", "./EM_vars.c",

View file

@ -6,7 +6,12 @@ cprogram {
-- do this. -- do this.
srcs = { "./src/*.c" }, srcs = { "./src/*.c" },
cflags = { "-DLIBDIR=\\\""..posix.getcwd().."/"..cwd().."/lib\\\"", "-DNON_CORRECTING" }, vars = {
["+cflags"] = {
"-DLIBDIR=\\\""..posix.getcwd().."/"..cwd().."/lib\\\"",
"-DNON_CORRECTING"
},
}
} }
definerule("llgen", definerule("llgen",