From cdbd60580388631bf95fbcce30dc97b3c0433b6e Mon Sep 17 00:00:00 2001 From: David Given Date: Thu, 14 Jul 2016 23:53:34 +0200 Subject: [PATCH] concat, targetnamesof. deps is now handled more consistently (treated the same as ins but not passed in as inputs). --- first/ackbuilder.lua | 53 ++++++++++++++++++++++++++++++++++++++------ first/build.lua | 7 ++++-- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/first/ackbuilder.lua b/first/ackbuilder.lua index 52dd97dcd..2b6522246 100644 --- a/first/ackbuilder.lua +++ b/first/ackbuilder.lua @@ -53,6 +53,16 @@ local function asstring(o) 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 p = table.concat({...}, "/") return p:gsub("/+", "/"):gsub("^%./", ""):gsub("/%./", "/") @@ -85,6 +95,27 @@ local function filenamesof(targets, pattern) return f end +local function targetnamesof(targets) + local f = {} + if targets then + if targets.is then + targets = {targets} + end + + for _, r in pairs(targets) do + if (type(r) == "table") and r.is then + f[#f+1] = r.fullname + elseif (type(r) == "string") then + f[#f+1] = r + else + error(string.format("list of targets contains a %s which isn't a target", + type(r))) + end + end + end + return f +end + local function dotocollection(collection, callback) if (type(collection) == "string") then return callback(collection) @@ -389,25 +420,28 @@ local function definerule(rulename, types, cb) end args.environment = environment - args.fullname = cwd.."+"..args.name - args.rulecwd = rulecwd if not args.cwd then args.cwd = cwd end + args.fullname = args.cwd.."+"..args.name - local oldcwd = cwd - cwd = rulecwd local result = cb(args) or {} - cwd = oldcwd result.is = result.is or {} result.is[rulename] = true result.fullname = args.fullname + + if targets[arg.fullname] and (targets[arg.fullname] ~= result) then + error(string.format("target '%s' is already defined", args.fullname)) + end targets[result.fullname] = result return result end if rulename then + if rules[rulename] then + error(string.format("rule '%s' is already defined", rulename)) + end rules[rulename] = rule end return rule @@ -498,12 +532,15 @@ definerule("simplerule", { ins = { type="targets" }, outs = { type="strings" }, + deps = { type="targets", default={} }, label = { type="string", optional=true }, commands = { type="strings" }, vars = { type="table", default={} }, }, function (e) - e.environment:rule(e.fullname, filenamesof(e.ins), e.outs) + e.environment:rule(e.fullname, + concat(filenamesof(e.ins), filenamesof(e.deps)), + e.outs) e.environment:label(e.fullname, " ", e.label or "") local vars = inherit(e.vars, { @@ -611,6 +648,7 @@ globals = { abspath = abspath, asstring = asstring, basename = basename, + concat = concat, concatpath = concatpath, cwd = function() return cwd end, definerule = definerule, @@ -618,12 +656,13 @@ globals = { emit = emit, environment = environment, filenamesof = filenamesof, + fpairs = fpairs, include = loadbuildfile, inherit = inherit, replace = replace, selectof = selectof, startswith = startswith, - fpairs = fpairs, + targetnamesof = targetnamesof, uniquify = uniquify, } setmetatable(globals, diff --git a/first/build.lua b/first/build.lua index 1501500c8..ffd163a6e 100644 --- a/first/build.lua +++ b/first/build.lua @@ -5,6 +5,7 @@ end definerule("normalrule", { ins = { type="targets" }, + deps = { type="targets", default={} }, outleaves = { type="strings" }, label = { type="string", optional=true }, objdir = { type="string", optional=true }, @@ -25,6 +26,7 @@ definerule("normalrule", local result = simplerule { name = e.name, ins = e.ins, + deps = e.deps, outs = realouts, label = e.label, commands = e.commands, @@ -69,7 +71,8 @@ definerule("cfile", return normalrule { name = e.name, cwd = e.cwd, - ins = {csrcs[1], unpack(hsrcs)}, + ins = {csrcs[1]}, + deps = e.deps, outleaves = {outleaf}, label = e.label, commands = e.commands, @@ -127,7 +130,6 @@ definerule("clibrary", }, function (e) local csrcs = filenamesof(e.srcs, "%.c$") - local hsrcs = filenamesof(e.srcs, "%.h$") local ins = {} @@ -203,6 +205,7 @@ definerule("cprogram", return normalrule { name = e.name, cwd = e.cwd, + deps = e.deps, ins = libs, outleaves = { e.name }, commands = e.commands,