1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: check-reqs.eclass
6 # QA Team <qa@gentoo.org>
8 # Bo Ørsted Andresen <zlin@gentoo.org>
9 # Original Author: Ciaran McCreesh <ciaranm@gentoo.org>
10 # @BLURB: Provides a uniform way of handling ebuild which have very high build requirements
12 # This eclass provides a uniform way of handling ebuilds which have very high
13 # build requirements in terms of memory or disk space. It provides a function
14 # which should usually be called during pkg_setup().
16 # The chosen action only happens when the system's resources are detected
17 # correctly and only if they are below the threshold specified by the package.
20 # # need this much memory (does *not* check swap)
21 # CHECKREQS_MEMORY="256M"
23 # # need this much temporary build space
24 # CHECKREQS_DISK_BUILD="2G"
26 # # install will need this much space in /usr
27 # CHECKREQS_DISK_USR="1G"
29 # # install will need this much space in /var
30 # CHECKREQS_DISK_VAR="1024M"
34 # If you don't specify a value for, say, CHECKREQS_MEMORY, then the test is not
37 # These checks should probably mostly work on non-Linux, and they should
38 # probably degrade gracefully if they don't. Probably.
40 if [[ ! ${_CHECK_REQS_ECLASS_} ]]; then
42 # @ECLASS-VARIABLE: CHECKREQS_MEMORY
45 # How much RAM is needed? Eg.: CHECKREQS_MEMORY=15M
47 # @ECLASS-VARIABLE: CHECKREQS_DISK_BUILD
50 # How much diskspace is needed to build the package? Eg.: CHECKREQS_DISK_BUILD=2T
52 # @ECLASS-VARIABLE: CHECKREQS_DISK_USR
55 # How much space in /usr is needed to install the package? Eg.: CHECKREQS_DISK_USR=15G
57 # @ECLASS-VARIABLE: CHECKREQS_DISK_VAR
60 # How much space is needed in /var? Eg.: CHECKREQS_DISK_VAR=3000M
62 EXPORT_FUNCTIONS pkg_setup
65 4|5|6) EXPORT_FUNCTIONS pkg_pretend ;;
66 *) die "EAPI=${EAPI} is not supported" ;;
69 # Obsolete function executing all the checks and printing out results
71 eerror "Package calling old ${FUNCNAME} function."
72 eerror "It should call check-reqs_pkg_pretend and check-reqs_pkg_setup."
73 die "${FUNCNAME} is banned"
76 # @FUNCTION: check-reqs_pkg_setup
78 # Exported function running the resources checks in pkg_setup phase.
79 # It should be run in both phases to ensure condition changes between
80 # pkg_pretend and pkg_setup won't affect the build.
81 check-reqs_pkg_setup() {
82 debug-print-function ${FUNCNAME} "$@"
89 # @FUNCTION: check-reqs_pkg_pretend
91 # Exported function running the resources checks in pkg_pretend phase.
92 check-reqs_pkg_pretend() {
93 debug-print-function ${FUNCNAME} "$@"
95 check-reqs_pkg_setup "$@"
98 # @FUNCTION: check-reqs_prepare
101 # Internal function that checks the variables that should be defined.
102 check-reqs_prepare() {
103 debug-print-function ${FUNCNAME} "$@"
105 if [[ -z ${CHECKREQS_MEMORY} &&
106 -z ${CHECKREQS_DISK_BUILD} &&
107 -z ${CHECKREQS_DISK_USR} &&
108 -z ${CHECKREQS_DISK_VAR} ]]; then
109 eerror "Set some check-reqs eclass variables if you want to use it."
110 eerror "If you are user and see this message file a bug against the package."
111 die "${FUNCNAME}: check-reqs eclass called but not actualy used!"
115 # @FUNCTION: check-reqs_run
118 # Internal function that runs the check based on variable settings.
120 debug-print-function ${FUNCNAME} "$@"
122 # some people are *censored*
123 unset CHECKREQS_FAILED
125 [[ ${EAPI:-0} == [0123] ]] && local MERGE_TYPE=""
127 # use != in test, because MERGE_TYPE only exists in EAPI 4 and later
128 if [[ ${MERGE_TYPE} != binary ]]; then
129 [[ -n ${CHECKREQS_MEMORY} ]] && \
133 [[ -n ${CHECKREQS_DISK_BUILD} ]] && \
136 "${CHECKREQS_DISK_BUILD}"
139 if [[ ${MERGE_TYPE} != buildonly ]]; then
140 [[ -n ${CHECKREQS_DISK_USR} ]] && \
143 "${CHECKREQS_DISK_USR}"
145 [[ -n ${CHECKREQS_DISK_VAR} ]] && \
148 "${CHECKREQS_DISK_VAR}"
152 # @FUNCTION: check-reqs_get_kibibytes
155 # Internal function that returns number in KiB.
156 # Returns 1024**2 for 1G or 1024**3 for 1T.
157 check-reqs_get_kibibytes() {
158 debug-print-function ${FUNCNAME} "$@"
160 [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
163 local size=${1%[GMT]}
166 M) echo $((1024 * size)) ;;
167 G) echo $((1024 * 1024 * size)) ;;
168 T) echo $((1024 * 1024 * 1024 * size)) ;;
170 die "${FUNCNAME}: Unknown unit: ${unit}"
175 # @FUNCTION: check-reqs_get_number
178 # Internal function that returns the numerical value without the unit.
179 # Returns "1" for "1G" or "150" for "150T".
180 check-reqs_get_number() {
181 debug-print-function ${FUNCNAME} "$@"
183 [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
185 local size=${1%[GMT]}
186 [[ ${size} == ${1} ]] && die "${FUNCNAME}: Missing unit: ${1}"
191 # @FUNCTION: check-reqs_get_unit
194 # Internal function that returns the unit without the numerical value.
195 # Returns "GiB" for "1G" or "TiB" for "150T".
196 check-reqs_get_unit() {
197 debug-print-function ${FUNCNAME} "$@"
199 [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
208 die "${FUNCNAME}: Unknown unit: ${unit}"
213 # @FUNCTION: check-reqs_output
216 # Internal function that prints the warning and dies if required based on
218 check-reqs_output() {
219 debug-print-function ${FUNCNAME} "$@"
223 [[ ${EBUILD_PHASE} == "pretend" && -z ${I_KNOW_WHAT_I_AM_DOING} ]] && msg="eerror"
224 if [[ -n ${CHECKREQS_FAILED} ]]; then
226 ${msg} "Space constraints set in the ebuild were not met!"
227 ${msg} "The build will most probably fail, you should enhance the space"
228 ${msg} "as per failed tests."
231 [[ ${EBUILD_PHASE} == "pretend" && -z ${I_KNOW_WHAT_I_AM_DOING} ]] && \
232 die "Build requirements not met!"
236 # @FUNCTION: check-reqs_memory
239 # Internal function that checks size of RAM.
240 check-reqs_memory() {
241 debug-print-function ${FUNCNAME} "$@"
243 [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
248 check-reqs_start_phase \
252 if [[ -r /proc/meminfo ]] ; then
253 actual_memory=$(awk '/MemTotal/ { print $2 }' /proc/meminfo)
255 actual_memory=$(sysctl hw.physmem 2>/dev/null )
257 actual_memory=$(echo $actual_memory | sed -e 's/^[^:=]*[:=]//' )
259 if [[ -n ${actual_memory} ]] ; then
260 if [[ ${actual_memory} -lt $(check-reqs_get_kibibytes ${size}) ]] ; then
262 check-reqs_unsatisfied \
270 ewarn "Couldn't determine amount of memory, skipping..."
274 # @FUNCTION: check-reqs_disk
277 # Internal function that checks space on the harddrive.
279 debug-print-function ${FUNCNAME} "$@"
281 [[ -z ${2} ]] && die "Usage: ${FUNCNAME} [path] [size]"
287 check-reqs_start_phase \
289 "disk space at \"${path}\""
291 space_kbi=$(df -Pk "${1}" 2>/dev/null | awk 'FNR == 2 {print $4}')
293 if [[ $? == 0 && -n ${space_kbi} ]] ; then
294 if [[ ${space_kbi} -lt $(check-reqs_get_kibibytes ${size}) ]] ; then
296 check-reqs_unsatisfied \
298 "disk space at \"${path}\""
304 ewarn "Couldn't determine disk space, skipping..."
308 # @FUNCTION: check-reqs_start_phase
311 # Internal function that inform about started check
312 check-reqs_start_phase() {
313 debug-print-function ${FUNCNAME} "$@"
315 [[ -z ${2} ]] && die "Usage: ${FUNCNAME} [size] [location]"
319 local sizeunit="$(check-reqs_get_number ${size}) $(check-reqs_get_unit ${size})"
321 ebegin "Checking for at least ${sizeunit} ${location}"
324 # @FUNCTION: check-reqs_unsatisfied
327 # Internal function that inform about check result.
328 # It has different output between pretend and setup phase,
329 # where in pretend phase it is fatal.
330 check-reqs_unsatisfied() {
331 debug-print-function ${FUNCNAME} "$@"
333 [[ -z ${2} ]] && die "Usage: ${FUNCNAME} [size] [location]"
338 local sizeunit="$(check-reqs_get_number ${size}) $(check-reqs_get_unit ${size})"
340 [[ ${EBUILD_PHASE} == "pretend" && -z ${I_KNOW_WHAT_I_AM_DOING} ]] && msg="eerror"
341 ${msg} "There is NOT at least ${sizeunit} ${location}"
343 # @ECLASS-VARIABLE: CHECKREQS_FAILED
346 # If set the checks failed and eclass should abort the build.
347 # Internal, do not set yourself.
348 CHECKREQS_FAILED="true"
351 _CHECK_REQS_ECLASS_=1