*/*: [QA] Remove redundant `|| die` guards
[gentoo.git] / sys-libs / libhugetlbfs / libhugetlbfs-2.22.ebuild
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=7
5
6 PYTHON_COMPAT=( python3_{5,6,7} )
7
8 inherit multilib toolchain-funcs python-any-r1
9
10 DESCRIPTION="easy hugepage access"
11 HOMEPAGE="https://github.com/libhugetlbfs/libhugetlbfs"
12 SRC_URI="https://github.com/libhugetlbfs/libhugetlbfs/archive/${PV}.tar.gz -> ${P}.tar.gz"
13
14 LICENSE="GPL-2"
15 SLOT="0"
16 KEYWORDS="~amd64 ~arm ~arm64 ~ppc ~ppc64 ~s390 ~x86"
17 IUSE="static-libs test"
18 RESTRICT="!test? ( test )"
19
20 DEPEND="test? ( ${PYTHON_DEPS} )"
21
22 PATCHES=(
23         "${FILESDIR}"/${PN}-2.6-fixup-testsuite.patch
24 )
25
26 src_prepare() {
27         default
28         sed -i \
29                 -e '/^PREFIX/s:/local::' \
30                 -e '1iBUILDTYPE = NATIVEONLY' \
31                 -e '1iV = 1' \
32                 -e '/gzip.*MANDIR/d' \
33                 -e "/^LIB\(32\)/s:=.*:= $(get_libdir):" \
34                 -e '/^CC\(32\|64\)/s:=.*:= $(CC):' \
35                 -e 's@^\(ARCH\) ?=@\1 =@' \
36                 Makefile || die "sed failed"
37         if [ "$(get_libdir)" == "lib64" ]; then
38                 sed -i \
39                         -e "/^LIB\(32\)/s:=.*:= lib32:" \
40                                 Makefile
41         fi
42
43         # Tarballs from github don't have the version set.
44         # https://github.com/libhugetlbfs/libhugetlbfs/issues/7
45         [[ -f version ]] || echo "${PV}" > version
46 }
47
48 src_compile() {
49         tc-export AR
50         emake CC="$(tc-getCC)" libs tools
51 }
52
53 src_install() {
54         default
55         use static-libs || rm -f "${ED}"/usr/$(get_libdir)/*.a
56 }
57
58 src_test_alloc_one() {
59         hugeadm="$1"
60         sign="$2"
61         pagesize="$3"
62         pagecount="$4"
63         ${hugeadm} \
64                 --pool-pages-max ${pagesize}:${sign}${pagecount} \
65         && \
66         ${hugeadm} \
67                 --pool-pages-min ${pagesize}:${sign}${pagecount}
68         return $?
69 }
70
71 # die is NOT allowed in this src_test block after the marked point, so that we
72 # can clean up memory allocation. You'll leak at LEAST 64MiB per run otherwise.
73 src_test() {
74         [[ $UID -eq 0 ]] || die "Need FEATURES=-userpriv to run this testsuite"
75         einfo "Building testsuite"
76         emake -j1 tests
77
78         local hugeadm='obj/hugeadm'
79         local allocated=''
80         local rc=0
81         # the testcases need 64MiB per pagesize.
82         local MIN_HUGEPAGE_RAM=$((64*1024*1024))
83
84         einfo "Planning allocation"
85         local PAGESIZES="$(${hugeadm} --page-sizes-all)"
86
87         # Need to do this before we can create the mountpoints.
88         local pagesize pagecount
89         for pagesize in ${PAGESIZES} ; do
90                 # The kernel depends on the location :-(
91                 mkdir -p /var/lib/hugetlbfs/pagesize-${pagesize}
92                 addwrite /var/lib/hugetlbfs/pagesize-${pagesize}
93         done
94         addwrite /proc/sys/vm/
95         addwrite /proc/sys/kernel/shmall
96         addwrite /proc/sys/kernel/shmmax
97         addwrite /proc/sys/kernel/shmmni
98
99         einfo "Checking HugeTLB mountpoints"
100         ${hugeadm} --create-mounts || die "Failed to set up hugetlb mountpoints."
101
102         # -----------------------------------------------------
103         # --------- die is unsafe after this point. -----------
104         # -----------------------------------------------------
105
106         einfo "Starting allocation"
107         for pagesize in ${PAGESIZES} ; do
108                 pagecount=$((${MIN_HUGEPAGE_RAM}/${pagesize}))
109                 einfo "  ${pagecount} @ ${pagesize}"
110                 addwrite /var/lib/hugetlbfs/pagesize-${pagesize}
111                 src_test_alloc_one "${hugeadm}" "+" "${pagesize}" "${pagecount}"
112                 rc=$?
113                 if [[ ${rc} -eq 0 ]]; then
114                         allocated="${allocated} ${pagesize}:${pagecount}"
115                 else
116                         eerror "Failed to add ${pagecount} pages of size ${pagesize}"
117                 fi
118         done
119
120         einfo "Allocation status"
121         ${hugeadm} --pool-list
122
123         if [[ -n "${allocated}" ]]; then
124                 # All our allocations worked, so time to run.
125                 einfo "Starting tests"
126                 cd "${S}"/tests || die
127                 local TESTOPTS="-t func"
128                 case ${ARCH} in
129                         amd64|ppc64)
130                                 TESTOPTS="${TESTOPTS} -b 64"
131                                 ;;
132                         x86)
133                                 TESTOPTS="${TESTOPTS} -b 32"
134                                 ;;
135                 esac
136                 # This needs a bit of work to give a nice exit code still.
137                 ./run_tests.py ${TESTOPTS}
138                 rc=$?
139         else
140                 eerror "Failed to make HugeTLB allocations."
141                 rc=1
142         fi
143
144         einfo "Cleaning up memory"
145         cd "${S}" || die
146         # Cleanup memory allocation
147         for alloc in ${allocated} ; do
148                 pagesize="${alloc/:*}"
149                 pagecount="${alloc/*:}"
150                 einfo "  ${pagecount} @ ${pagesize}"
151                 src_test_alloc_one "$hugeadm" "-" "${pagesize}" "${pagecount}"
152         done
153
154         # ---------------------------------------------------------
155         # --------- die is safe again after this point. -----------
156         # ---------------------------------------------------------
157
158         return ${rc}
159 }