Replaced the fairly complex environment code with a much simpler

emitter object.
This commit is contained in:
David Given 2016-07-26 23:43:31 +02:00
parent a8a9d1bbfa
commit cdeea836f2
2 changed files with 24 additions and 72 deletions

View file

@ -9,7 +9,7 @@ local posix = require("posix")
-- is = { set of rule types which made the target }
-- }
local environment = {}
local emitter = {}
local rules = {}
local targets = {}
local buildfiles = {}
@ -433,7 +433,6 @@ local function definerule(rulename, types, cb)
error(string.format("don't know what to do with property '%s'", propname))
end
args.environment = environment
if not args.cwd then
args.cwd = cwd
end
@ -465,9 +464,9 @@ end
-- DEFAULT RULES --
-----------------------------------------------------------------------------
local function install_make_environment()
local function install_make_emitter()
emit("hide = @\n")
function environment:rule(name, ins, outs)
function emitter:rule(name, ins, outs)
emit(".INTERMEDIATE:", name, "\n")
for i = 1, #ins do
emit(name..":", ins[i], "\n")
@ -483,28 +482,28 @@ local function install_make_environment()
end
end
function environment:phony(name, ins, outs)
function emitter:phony(name, ins, outs)
emit(".PHONY:", name, "\n")
self:rule(name, ins, outs)
end
function environment:label(...)
function emitter:label(...)
local s = table.concat({...}, " ")
emit("\t@echo", s, "\n")
end
function environment:exec(commands)
function emitter:exec(commands)
for _, s in ipairs(commands) do
emit("\t$(hide)", s, "\n")
end
end
function environment:endrule()
function emitter:endrule()
emit("\n")
end
end
local function install_ninja_environment()
local function install_ninja_emitter()
emit("rule build\n")
emit(" command = $command\n")
emit("\n")
@ -521,7 +520,7 @@ local function install_ninja_environment()
)
end
function environment:rule(name, ins, outs)
function emitter:rule(name, ins, outs)
if (#outs == 0) then
emit("build", name, ": phony", unmake(ins), "\n")
else
@ -530,14 +529,14 @@ local function install_ninja_environment()
end
end
function environment:label(...)
function emitter:label(...)
end
function environment:exec(commands)
function emitter:exec(commands)
emit(" command =", table.concat(unmake(commands), " && "), "\n")
end
function environment:endrule()
function emitter:endrule()
emit("\n")
end
end
@ -552,18 +551,18 @@ definerule("simplerule",
vars = { type="table", default={} },
},
function (e)
e.environment:rule(e.fullname,
emitter:rule(e.fullname,
concat(filenamesof(e.ins), filenamesof(e.deps)),
e.outs)
e.environment:label(e.fullname, " ", e.label or "")
emitter:label(e.fullname, " ", e.label or "")
local vars = inherit(e.vars, {
ins = e.ins,
outs = e.outs
})
e.environment:exec(templateexpand(e.commands, vars))
e.environment:endrule()
emitter:exec(templateexpand(e.commands, vars))
emitter:endrule()
return {
outs = e.outs
@ -600,12 +599,12 @@ definerule("installable",
end
end
e.environment:rule(e.fullname, deps, dests)
e.environment:label(e.fullname, " ", e.label or "")
emitter:rule(e.fullname, deps, dests)
emitter:label(e.fullname, " ", e.label or "")
if (#commands > 0) then
e.environment:exec(commands)
emitter:exec(commands)
end
e.environment:endrule()
emitter:endrule()
end
)
@ -670,7 +669,6 @@ globals = {
definerule = definerule,
dirname = dirname,
emit = emit,
environment = environment,
filenamesof = filenamesof,
fpairs = fpairs,
include = loadbuildfile,
@ -695,16 +693,16 @@ setmetatable(globals,
)
do
local environment_type = install_make_environment
local emitter_type = install_make_emitter
parse_arguments(
{
["make"] = function()
environment_type = install_make_environment
emitter_type = install_make_emitter
return 1
end,
["ninja"] = function()
environment_type = install_ninja_environment
emitter_type = install_ninja_emitter
return 1
end,
@ -713,7 +711,7 @@ do
end,
[" files"] = function(files)
environment_type()
emitter_type()
for _, f in ipairs(files) do
loadbuildfile(f)
end

View file

@ -74,49 +74,3 @@ definerule("ncgg",
}
end
)
--[[
D := util/ncgg
define build-ncgg-impl
$(call reset)
$(eval cflags += -I$D)
$(call yacc, $(OBJDIR)/$D, $D/cgg.y)
$(call flex, $(OBJDIR)/$D, $D/scan.l)
$(call dependson, $(OBJDIR)/$D/y.tab.h)
$(call cfile, $D/subr.c)
$(call cfile, $D/main.c)
$(call cfile, $D/coerc.c)
$(call cfile, $D/error.c)
$(call cfile, $D/emlookup.c)
$(call cfile, $D/expr.c)
$(call cfile, $D/instruct.c)
$(call cfile, $D/iocc.c)
$(call cfile, $D/lookup.c)
$(call cfile, $D/output.c)
$(call cfile, $D/set.c)
$(call cfile, $D/strlookup.c)
$(call cfile, $D/var.c)
$(call cfile, $D/hall.c)
$(eval CLEANABLES += $(OBJDIR)/$D/enterkeyw.c)
$(OBJDIR)/$D/enterkeyw.c: $D/cvtkeywords $D/keywords $(OBJDIR)/$D/y.tab.h
@echo KEYWORDS $$@
@mkdir -p $$(dir $$@)
$(hide) cd $$(dir $$@) && sh $(abspath $D/cvtkeywords) $(abspath $D/keywords)
$(call cfile, $(OBJDIR)/$D/enterkeyw.c)
$(eval $q: $(INCDIR)/em_spec.h)
$(call rawfile, $(LIBEM_DATA))
$(call cprogram, $(BINDIR)/ncgg)
$(eval NCGG := $o)
endef
$(eval $(build-ncgg-impl))
--]]