Merge github#363: sys-process/atop: add systemd support
[gentoo.git] / dev-libs / klibc / klibc-2.0.3-r1.ebuild
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # Robin H. Johnson <robbat2@gentoo.org>, 12 Nov 2007:
6 # This still needs major work.
7 # But it is significently better than the previous version.
8 # In that it will now build on biarch systems, such as ppc64-32ul.
9
10 # NOTES:
11 # ======
12 # We need to bring in the kernel sources seperately
13 # Because they have to be configured in a way that differs from the copy in
14 # /usr/src/. The sys-kernel/linux-headers are too stripped down to use
15 # unfortunetly.
16 # This will be able to go away once the klibc author updates his code
17 # to build again the headers provided by the kernel's 'headers_install' target.
18
19 EAPI=5
20 K_TARBALL_SUFFIX="xz"
21
22 inherit eutils multilib toolchain-funcs flag-o-matic
23
24 DESCRIPTION="A minimal libc subset for use with initramfs"
25 HOMEPAGE="http://www.zytor.com/mailman/listinfo/klibc/ https://www.kernel.org/pub/linux/libs/klibc/"
26 KV_MAJOR="3" KV_MINOR="x" KV_SUB="12"
27 PKV_EXTRA=""
28 if [[ ${PKV_EXTRA} ]]; then
29         if [[ ${KV_MAJOR} == 2 ]]; then
30                 PKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
31         else
32                 PKV="${KV_MAJOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
33         fi
34         PATCH_URI="mirror://kernel/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/patch-${PKV}.${K_TARBALL_SUFFIX}"
35 fi
36 if [[ ${KV_MAJOR} == 2 ]]; then
37         OKV="${KV_MAJOR}.${KV_MINOR}.${KV_SUB}"
38 else
39         OKV="${KV_MAJOR}.${KV_SUB}"
40 fi
41 KERNEL_URI="
42         mirror://kernel/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/linux-${OKV}.tar.${K_TARBALL_SUFFIX}
43         mirror://kernel/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/testing/linux-${OKV}.tar.${K_TARBALL_SUFFIX}"
44 DEBIAN_PV=2.0.2
45 DEBIAN_PR=1
46 DEBIAN_A="${PN}_${DEBIAN_PV}-${DEBIAN_PR}.debian.tar.gz"
47 SRC_URI="
48         mirror://kernel/linux/libs/klibc/${PV:0:3}/${P}.tar.${K_TARBALL_SUFFIX}
49         mirror://debian/pool/main/k/klibc/${DEBIAN_A}
50         ${PATCH_URI}
51         ${KERNEL_URI}"
52
53 LICENSE="|| ( GPL-2 LGPL-2 )"
54 KEYWORDS="~alpha ~amd64 ~arm ~ia64 -mips ~ppc ~ppc64 ~sparc ~x86"
55 SLOT="0"
56 IUSE="debug test custom-cflags"
57
58 DEPEND="dev-lang/perl"
59 RDEPEND="${DEPEND}"
60
61 KS="${WORKDIR}/linux-${OKV}"
62
63 # Klibc has no PT_GNU_STACK support, so scanning for execstacks is moot
64 QA_EXECSTACK="*"
65 # Do not strip
66 RESTRICT="strip"
67
68 kernel_asm_arch() {
69         a="${1:${ARCH}}"
70         case ${a} in
71                 # Merged arches
72                 x86) echo i386 ;; # for build on x86 userspace & 64bit kernel
73                 amd64) echo x86 ;;
74                 ppc*) echo powerpc ;;
75                 # Non-merged
76                 alpha|arm|arm64|ia64|m68k|mips|sh|sparc*) echo ${1} ;;
77                 *) die "TODO: Update the code for your asm-ARCH symlink" ;;
78         esac
79 }
80
81 # For a given Gentoo ARCH,
82 # specify the kernel defconfig most relevant
83 kernel_defconfig() {
84         a="${1:${ARCH}}"
85         # most, but not all arches have a sanely named defconfig
86         case ${a} in
87                 ppc64) echo ppc64_defconfig ;;
88                 ppc) echo pmac32_defconfig ;;
89                 sh*) die "TODO: Your arch is not supported by the klibc ebuild. Please suggest a defconfig in a bug." ;;
90                 *) echo defconfig ;;
91         esac
92 }
93
94 src_unpack() {
95         unpack linux-${OKV}.tar.${K_TARBALL_SUFFIX} ${P}.tar.${K_TARBALL_SUFFIX} ${DEBIAN_A}
96 }
97
98 src_prepare() {
99         [[ ${PKV} ]] && EPATCH_OPTS="-d ${KS} -p1" epatch "${DISTDIR}"/patch-${PKV}.${K_TARBALL_SUFFIX}
100         cd "${S}"
101
102         # Symlink /usr/src/linux to ${S}/linux
103         ln -snf "${KS}" linux
104         #ln -snf "/usr" linux
105
106         # Build interp.o with EXTRA_KLIBCAFLAGS (.S source)
107         epatch "${FILESDIR}"/${PN}-1.4.11-interp-flags.patch
108
109         # Fix usage of -s, bug #201006
110         epatch "${FILESDIR}"/klibc-1.5.7-strip-fix-dash-s.patch
111
112         # The inline definition from sys/stat.h does not seem to get used
113         # So just copy it to  make this compile for now
114         epatch "${FILESDIR}"/klibc-2.0.2-mkfifo.patch
115
116         # Newer kernels have some headers in the uapi dir
117         epatch "${FILESDIR}"/klibc-2.0.3-kernel-uapi.patch
118
119         # Borrow the debian fixes too
120         for p in $(<"${S}"/debian/patches/series) ; do
121                 epatch "${S}/debian/patches/${p}"
122         done
123 }
124
125 # klibc has it's own ideas of arches
126 # They reflect userspace strictly.
127 # This functions maps from a Gentoo ARCH, to an arch that klibc expects
128 # Look at klibc-${S}/usr/klibc/arch for a list of these arches
129 klibc_arch() {
130         a="${1:${ARCH}}"
131         case ${a} in
132                 amd64) echo x86_64;;
133                 mips) die 'TODO: Use the $ABI' ;;
134                 x86) echo i386;;
135                 *) echo ${a} ;;
136         esac
137 }
138
139 src_compile() {
140         local myargs="all"
141         local myARCH="${ARCH}" myABI="${ABI}"
142         # TODO: For cross-compiling
143         # You should set ARCH and ABI here
144         CC="$(tc-getCC)"
145         LD="$(tc-getLD)"
146         HOSTCC="$(tc-getBUILD_CC)"
147         HOSTLD="$(tc-getBUILD_LD)"
148         KLIBCARCH="$(klibc_arch ${ARCH})"
149         KLIBCASMARCH="$(kernel_asm_arch ${ARCH})"
150         libdir="$(get_libdir)"
151         # This should be the defconfig corresponding to your userspace!
152         # NOT your kernel. PPC64-32ul would choose 'ppc' for example.
153         defconfig=$(kernel_defconfig ${ARCH})
154         unset ABI ARCH # Unset these, because they interfere
155         unset KBUILD_OUTPUT # we are using a private copy
156
157         cd "${KS}"
158         emake ${defconfig} CC="${CC}" HOSTCC="${HOSTCC}" ARCH="${KLIBCASMARCH}" || die "No defconfig"
159         if [[ "${KLIBCARCH/arm}" != "${KLIBCARCH}" ]] && \
160            [[ "${CHOST/eabi}" != "${CHOST}" ]]; then
161                 # The delete and insert are seperate statements
162                 # so that they are reliably used.
163                 sed -i \
164                 -e '/CONFIG_AEABI/d' \
165                 -e '1iCONFIG_AEABI=y' \
166                 -e '/CONFIG_OABI_COMPAT/d' \
167                 -e '1iCONFIG_OABI_COMPAT=y' \
168                 "${KS}"/.config \
169                 "${S}"/defconfig
170         fi
171         emake prepare CC="${CC}" HOSTCC="${HOSTCC}" ARCH="${KLIBCASMARCH}" || die "Failed to prepare kernel sources for header usage"
172
173         cd "${S}"
174
175         use debug && myargs="${myargs} V=1"
176         use test && myargs="${myargs} test"
177         append-ldflags -z noexecstack
178         append-flags -nostdlib
179
180         emake \
181                 EXTRA_KLIBCAFLAGS="-Wa,--noexecstack" \
182                 EXTRA_KLIBCLDFLAGS="-z noexecstack" \
183                 HOSTLDFLAGS="-z noexecstack" \
184                 KLIBCOPTFLAGS='-nostdlib' \
185                 HOSTCC="${HOSTCC}" CC="${CC}" \
186                 HOSTLD="${HOSTLD}" LD="${LD}" \
187                 INSTALLDIR="/usr/${libdir}/klibc" \
188                 KLIBCARCH=${KLIBCARCH} \
189                 KLIBCASMARCH=${KLIBCASMARCH} \
190                 SHLIBDIR="/${libdir}" \
191                 libdir="/usr/${libdir}" \
192                 mandir="/usr/share/man" \
193                 T="${T}" \
194                 $(use custom-cflags || echo SKIP_)HOSTCFLAGS="${CFLAGS}" \
195                 $(use custom-cflags || echo SKIP_)HOSTLDFLAGS="${LDFLAGS}" \
196                 $(use custom-cflags || echo SKIP_)KLIBCOPTFLAGS="${CFLAGS}" \
197                 ${myargs} || die "Compile failed!"
198
199                 #SHLIBDIR="/${libdir}" \
200
201         ARCH="${myARCH}" ABI="${myABI}"
202 }
203
204 src_install() {
205         local myargs
206         local myARCH="${ARCH}" myABI="${ABI}"
207         # TODO: For cross-compiling
208         # You should set ARCH and ABI here
209         CC="$(tc-getCC)"
210         HOSTCC="$(tc-getBUILD_CC)"
211         KLIBCARCH="$(klibc_arch ${ARCH})"
212         KLIBCASMARCH="$(kernel_asm_arch ${ARCH})"
213         libdir="$(get_libdir)"
214         # This should be the defconfig corresponding to your userspace!
215         # NOT your kernel. PPC64-32ul would choose 'ppc' for example.
216         defconfig=$(kernel_defconfig ${ARCH})
217
218         use debug && myargs="${myargs} V=1"
219
220         local klibc_prefix
221         if tc-is-cross-compiler ; then
222                 klibc_prefix=$("${S}/klcc/${KLIBCARCH}-klcc" -print-klibc-prefix)
223         else
224                 klibc_prefix=$("${S}/klcc/klcc" -print-klibc-prefix)
225         fi
226
227         unset ABI ARCH # Unset these, because they interfere
228         unset KBUILD_OUTPUT # we are using a private copy
229
230         emake \
231                 EXTRA_KLIBCAFLAGS="-Wa,--noexecstack" \
232                 EXTRA_KLIBCLDFLAGS="-z noexecstack" \
233                 HOSTLDFLAGS="-z noexecstack" \
234                 KLIBCOPTFLAGS='-nostdlib' \
235                 HOSTCC="${HOSTCC}" CC="${CC}" \
236                 HOSTLD="${HOSTLD}" LD="${LD}" \
237                 INSTALLDIR="/usr/${libdir}/klibc" \
238                 INSTALLROOT="${D}" \
239                 KLIBCARCH=${KLIBCARCH} \
240                 KLIBCASMARCH=${KLIBCASMARCH} \
241                 SHLIBDIR="/${libdir}" \
242                 libdir="/usr/${libdir}" \
243                 mandir="/usr/share/man" \
244                 T="${T}" \
245                 $(use custom-cflags || echo SKIP_)HOSTCFLAGS="${CFLAGS}" \
246                 $(use custom-cflags || echo SKIP_)HOSTLDFLAGS="${LDFLAGS}" \
247                 $(use custom-cflags || echo SKIP_)KLIBCOPTFLAGS="${CFLAGS}" \
248                 ${myargs} \
249                 install || die "Install failed!"
250
251                 #SHLIBDIR="/${libdir}" \
252
253         # klibc doesn't support prelinking, so we need to mask it
254         cat > "${T}/70klibc" <<-EOF
255                 PRELINK_PATH_MASK="/usr/${libdir}/klibc"
256         EOF
257
258         doenvd "${T}"/70klibc
259
260         # Fix the permissions (bug #178053) on /usr/${libdir}/klibc/include
261         # Actually I have no idea, why the includes have those weird-ass permissions
262         # on a particular system, might be due to inherited permissions from parent
263         # directory
264         find "${D}"/usr/${libdir}/klibc/include | xargs chmod o+rX
265         find "${D}"/usr/${libdir}/klibc/include -type f \
266                 \( -name '.install' -o -name '..install.cmd' \) -delete || die
267
268         # Hardlinks becoming copies
269         for x in gunzip zcat ; do
270                 rm -f "${D}/${klibc_prefix}/bin/${x}"
271                 dosym gzip "${klibc_prefix}/bin/${x}"
272         done
273
274         # Restore now, so we can use the tc- functions
275         ARCH="${myARCH}" ABI="${myABI}"
276         if ! tc-is-cross-compiler ; then
277                 cd "${S}"
278                 insinto /usr/share/aclocal
279                 doins contrib/klibc.m4
280
281                 dodoc README usr/klibc/CAVEATS
282                 docinto gzip; dodoc usr/gzip/README
283         fi
284
285         # Fix up the symlink
286         # Mainly for merged arches
287         linkname="${D}/usr/${libdir}/klibc/include/asm"
288         if [ -L "${linkname}" ] && [ ! -e "${linkname}" ] ; then
289                 ln -snf asm-${KLIBCASMARCH} "${linkname}"
290         fi
291 }
292
293 src_test() {
294         if ! tc-is-cross-compiler ; then
295                 cd "${S}"/usr/klibc/tests
296                 ALL_TESTS="$(ls *.c |sed 's,\.c$,,g')"
297                 BROKEN_TESTS="fcntl fnmatch testrand48"
298                 failed=0
299                 for t in $ALL_TESTS ; do
300                         if has $t $BROKEN_TESTS ; then
301                                 echo "=== $t SKIP"
302                         else
303                                 echo -n "=== $t "
304                                 ./$t </dev/null >/dev/null
305                                 rc=$?
306                                 if [ $rc -eq 0 ]; then
307                                         echo PASS
308                                 else
309                                         echo FAIL
310                                         failed=1
311                                 fi
312                         fi
313                 done
314                 [ $failed -ne 0 ] && die "Some tests failed."
315         fi
316 }