From 10746f8b97864d34b14f1842edcff9dc28f1b145 Mon Sep 17 00:00:00 2001 From: David Given Date: Sun, 14 Aug 2016 01:38:36 +0200 Subject: [PATCH] Add cycle detection, because it was ruining my day. Allow targets to be stored in a file called build-.lua to allow better dividing up of build rules (to break cycles). --- first/ackbuilder.lua | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/first/ackbuilder.lua b/first/ackbuilder.lua index b59cb2a67..1c4aca339 100644 --- a/first/ackbuilder.lua +++ b/first/ackbuilder.lua @@ -17,6 +17,7 @@ local globals local cwd = "." local vars = {} local parente = {} +local loadingstack = {} -- Forward references local loadtarget @@ -66,6 +67,16 @@ local function concat(...) return r end +-- Test table membership (crudely). +local function contains(needle, haystack) + for _, k in ipairs(haystack) do + if (k == needle) then + return true + end + end + return false +end + local function inherit(high, low) local o = {} setmetatable(o, { @@ -351,6 +362,12 @@ local function templateexpand(list, vars) end local function loadbuildfile(filename) + if contains(filename, loadingstack) then + error(string.format("build file cycle; '%s' refers to itself indirectly; stack is: %s %s", + filename, asstring(loadingstack), filename)) + end + + loadingstack[#loadingstack+1] = filename if not buildfiles[filename] then buildfiles[filename] = true @@ -376,6 +393,7 @@ local function loadbuildfile(filename) chunk() cwd = oldcwd end + loadingstack[#loadingstack] = nil end loadtarget = function(targetname) @@ -411,7 +429,12 @@ loadtarget = function(targetname) filepart = cwd end local filename = concatpath(filepart, "/build.lua") - loadbuildfile(concatpath(filename)) + if posix.access(filename, "r") then + loadbuildfile(filename) + else + filename = concatpath(filepart, "/build-"..targetpart..".lua") + loadbuildfile(filename) + end target = targets[targetname] if not target then