Backport to Lua 5.1 (because 5.2 on Travis doesn't have luaposix...).

This commit is contained in:
David Given 2016-08-14 22:52:57 +02:00
parent 38c6a87ed5
commit 53f043ff40
3 changed files with 25 additions and 11 deletions

View file

@ -1,5 +1,5 @@
before_install: before_install:
- sudo apt-get install ed lua5.2 lua-posix ninja - sudo apt-get install ed lua5.1 liblua5.1-posix1 ninja
language: c language: c
script: script:
- make PREFIX=/tmp/acki -j4 - make PREFIX=/tmp/acki -j4

View file

@ -91,7 +91,7 @@ endif
$(BUILDDIR)/rules.ninja: first/ackbuilder.lua $(BUILD_FILES) $(BUILDDIR)/rules.ninja: first/ackbuilder.lua $(BUILD_FILES)
@mkdir -p $(BUILDDIR) @mkdir -p $(BUILDDIR)
@lua5.2 first/ackbuilder.lua first/build.lua build.lua --ninja > $(BUILDDIR)/rules.ninja @lua5.1 first/ackbuilder.lua first/build.lua build.lua --ninja > $(BUILDDIR)/rules.ninja
$(BUILDDIR)/rules.mk: first/ackbuilder.lua $(BUILD_FILES) $(BUILDDIR)/rules.mk: first/ackbuilder.lua $(BUILD_FILES)
@mkdir -p $(BUILDDIR) @mkdir -p $(BUILDDIR)

View file

@ -346,10 +346,11 @@ local function templateexpand(list, vars)
o[#o+1] = s:gsub("%%%b{}", o[#o+1] = s:gsub("%%%b{}",
function(expr) function(expr)
expr = expr:sub(3, -2) expr = expr:sub(3, -2)
local chunk, e = load("return ("..expr..")", expr, "text", vars) local chunk, e = loadstring("return ("..expr..")", expr)
if e then if e then
error(string.format("error evaluating expression: %s", e)) error(string.format("error evaluating expression: %s", e))
end end
setfenv(chunk, vars)
local value = chunk() local value = chunk()
if (value == nil) then if (value == nil) then
error(string.format("template expression '%s' expands to nil (probably an undefined variable)", expr)) error(string.format("template expression '%s' expands to nil (probably an undefined variable)", expr))
@ -381,7 +382,10 @@ local function loadbuildfile(filename)
local thisglobals = {} local thisglobals = {}
thisglobals._G = thisglobals thisglobals._G = thisglobals
setmetatable(thisglobals, {__index = globals}) setmetatable(thisglobals, {__index = globals})
chunk, e = load(data, "@"..filename, "text", thisglobals) chunk, e = loadstring(data, "@"..filename)
if not e then
setfenv(chunk, thisglobals)
end
end end
end end
if e then if e then
@ -396,6 +400,22 @@ local function loadbuildfile(filename)
loadingstack[#loadingstack] = nil loadingstack[#loadingstack] = nil
end end
local function loadbuildfilefor(filepart, targetpart)
local normalname = concatpath(filepart, "/build.lua")
if posix.access(normalname, "r") then
loadbuildfile(normalname)
return
end
local extendedname = concatpath(filepart, "/build-"..targetpart..".lua")
if posix.access(extendedname, "r") then
loadbuildfile(extendedname)
return
end
error(string.format("could not access either '%s' or '%s'", normalname, extendedname))
end
loadtarget = function(targetname) loadtarget = function(targetname)
if targets[targetname] then if targets[targetname] then
return targets[targetname] return targets[targetname]
@ -428,13 +448,7 @@ loadtarget = function(targetname)
if (filepart == "") then if (filepart == "") then
filepart = cwd filepart = cwd
end end
local filename = concatpath(filepart, "/build.lua") loadbuildfilefor(filepart, targetpart)
if posix.access(filename, "r") then
loadbuildfile(filename)
else
filename = concatpath(filepart, "/build-"..targetpart..".lua")
loadbuildfile(filename)
end
target = targets[targetname] target = targets[targetname]
if not target then if not target then