Add better error checking to %constrain_build

This commit is contained in:
Jason Tibbitts 2021-11-01 16:35:33 -05:00
parent 4a171cb3bf
commit 4cf75bdaf9

View File

@ -9,10 +9,24 @@
# If no options are passed, sets _smp_build_ncpus to 1. # If no options are passed, sets _smp_build_ncpus to 1.
# _smp_build_ncpus will never be raised, only lowered. # _smp_build_ncpus will never be raised, only lowered.
%constrain_build(c:m:) %{lua: %constrain_build(c:m:) %{lua:
local mem_limit = math.tointeger(opt.m)
local cpu_limit = math.tointeger(opt.c) -- Check a value and clamp it to at least 1
local current_cpus = math.tointeger(macros._smp_build_ncpus) local function check_and_clamp(v, string)
local constrained_cpus = current_cpus if v == nil then return nil end
i = math.tointeger(v)
if i == nil then
macros.error({"%%%0: invalid "..string.." value "..v})
return nil
end
local clamp = math.max(1, math.floor(i))
if i ~= clamp then
macros.error({"%%%0: invalid "..string.." value "..v})
return nil
end
return clamp
end
-- Parse meminfo to find the total amount of memory in the system -- Parse meminfo to find the total amount of memory in the system
local function getmem() local function getmem()
@ -26,6 +40,11 @@
return mem return mem
end end
local mem_limit = check_and_clamp(opt.m, "mem limit")
local cpu_limit = check_and_clamp(opt.c, "cpu limit")
local current_cpus = math.tointeger(macros._smp_build_ncpus)
local constrained_cpus = current_cpus
if (not cpu_limit and not mem_limit) then if (not cpu_limit and not mem_limit) then
cpu_limit = 1 cpu_limit = 1
end end