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

View file

@ -74,49 +74,3 @@ definerule("ncgg",
} }
end 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))
--]]