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