f44829e
# Macros to constrain resource use during the build process
f44829e
deb0434
# outputs build flag overrides to be used in conjunction with
deb0434
# %%make_build, %%cmake_build etc.
deb0434
#
deb0434
# if no override is needed, this macro outputs nothing
deb0434
#
deb0434
# - m   memory limit in MBs per core; default is 1024
deb0434
#
deb0434
# Usage:
deb0434
# e.g. %make_build %{limit_build -m 2048}
deb0434
#   => /usr/bin/make -O -j16 V=1 VERBOSE=1
deb0434
#      %make_build %{limit_build -m 40960}
deb0434
#   => /usr/bin/make -O -j16 V=1 VERBOSE=1 -j1
deb0434
# 
deb0434
%limit_build(m:) %{lua:
deb0434
  local mem_per_process=rpm.expand("%{-m*}")
deb0434
  if mem_per_process == "" then
deb0434
    mem_per_process = 1024
deb0434
  else
deb0434
    mem_per_process = tonumber(mem_per_process)
deb0434
  end
deb0434
  local mem_total = 0
deb0434
  for line in io.lines('/proc/meminfo') do
deb0434
    if line:sub(1, 9) == "MemTotal:" then
deb0434
      local tokens = {}
deb0434
      for token in line:gmatch("%w+") do
deb0434
        tokens[#tokens + 1] = token
deb0434
      end
deb0434
      mem_total = tonumber(tokens[2])
deb0434
      break
deb0434
    end
deb0434
  end
deb0434
  local max_jobs = mem_total // (mem_per_process * 1024)
deb0434
  if max_jobs < 1 then
deb0434
    max_jobs = 1
deb0434
  end
deb0434
  cur_max_jobs=tonumber(rpm.expand("%{_smp_build_ncpus}"))
deb0434
  if cur_max_jobs > max_jobs then
deb0434
    print("-j" .. max_jobs)
deb0434
  end
deb0434
}