1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
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.
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
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.
22 inherit eutils multilib toolchain-funcs flag-o-matic
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"
28 if [[ ${PKV_EXTRA} ]]; then
29 if [[ ${KV_MAJOR} == 2 ]]; then
30 PKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
32 PKV="${KV_MAJOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
34 PATCH_URI="mirror://kernel/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/patch-${PKV}.${K_TARBALL_SUFFIX}"
36 if [[ ${KV_MAJOR} == 2 ]]; then
37 OKV="${KV_MAJOR}.${KV_MINOR}.${KV_SUB}"
39 OKV="${KV_MAJOR}.${KV_SUB}"
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}"
46 DEBIAN_A="${PN}_${DEBIAN_PV}-${DEBIAN_PR}.debian.tar.gz"
48 mirror://kernel/linux/libs/klibc/${PV:0:3}/${P}.tar.${K_TARBALL_SUFFIX}
49 mirror://debian/pool/main/k/klibc/${DEBIAN_A}
53 LICENSE="|| ( GPL-2 LGPL-2 )"
54 KEYWORDS="~alpha ~amd64 ~arm ~ia64 -mips ~ppc ~ppc64 ~sparc ~x86"
56 IUSE="debug test custom-cflags"
58 DEPEND="dev-lang/perl"
61 KS="${WORKDIR}/linux-${OKV}"
63 # Klibc has no PT_GNU_STACK support, so scanning for execstacks is moot
72 x86) echo i386 ;; # for build on x86 userspace & 64bit kernel
76 alpha|arm|arm64|ia64|m68k|mips|sh|sparc*) echo ${1} ;;
77 *) die "TODO: Update the code for your asm-ARCH symlink" ;;
81 # For a given Gentoo ARCH,
82 # specify the kernel defconfig most relevant
85 # most, but not all arches have a sanely named defconfig
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." ;;
95 unpack linux-${OKV}.tar.${K_TARBALL_SUFFIX} ${P}.tar.${K_TARBALL_SUFFIX} ${DEBIAN_A}
99 [[ ${PKV} ]] && EPATCH_OPTS="-d ${KS} -p1" epatch "${DISTDIR}"/patch-${PKV}.${K_TARBALL_SUFFIX}
102 # Symlink /usr/src/linux to ${S}/linux
103 ln -snf "${KS}" linux
104 #ln -snf "/usr" linux
106 # Build interp.o with EXTRA_KLIBCAFLAGS (.S source)
107 epatch "${FILESDIR}"/${PN}-1.4.11-interp-flags.patch
109 # Fix usage of -s, bug #201006
110 epatch "${FILESDIR}"/klibc-1.5.7-strip-fix-dash-s.patch
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
116 # Newer kernels have some headers in the uapi dir
117 epatch "${FILESDIR}"/klibc-2.0.3-kernel-uapi.patch
119 # Borrow the debian fixes too
120 for p in $(<"${S}"/debian/patches/series) ; do
121 epatch "${S}/debian/patches/${p}"
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
133 mips) die 'TODO: Use the $ABI' ;;
141 local myARCH="${ARCH}" myABI="${ABI}"
142 # TODO: For cross-compiling
143 # You should set ARCH and ABI here
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
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.
164 -e '/CONFIG_AEABI/d' \
165 -e '1iCONFIG_AEABI=y' \
166 -e '/CONFIG_OABI_COMPAT/d' \
167 -e '1iCONFIG_OABI_COMPAT=y' \
171 emake prepare CC="${CC}" HOSTCC="${HOSTCC}" ARCH="${KLIBCASMARCH}" || die "Failed to prepare kernel sources for header usage"
175 use debug && myargs="${myargs} V=1"
176 use test && myargs="${myargs} test"
177 append-ldflags -z noexecstack
178 append-flags -nostdlib
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" \
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!"
199 #SHLIBDIR="/${libdir}" \
201 ARCH="${myARCH}" ABI="${myABI}"
206 local myARCH="${ARCH}" myABI="${ABI}"
207 # TODO: For cross-compiling
208 # You should set ARCH and ABI here
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})
218 use debug && myargs="${myargs} V=1"
221 if tc-is-cross-compiler ; then
222 klibc_prefix=$("${S}/klcc/${KLIBCARCH}-klcc" -print-klibc-prefix)
224 klibc_prefix=$("${S}/klcc/klcc" -print-klibc-prefix)
227 unset ABI ARCH # Unset these, because they interfere
228 unset KBUILD_OUTPUT # we are using a private copy
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" \
239 KLIBCARCH=${KLIBCARCH} \
240 KLIBCASMARCH=${KLIBCASMARCH} \
241 SHLIBDIR="/${libdir}" \
242 libdir="/usr/${libdir}" \
243 mandir="/usr/share/man" \
245 $(use custom-cflags || echo SKIP_)HOSTCFLAGS="${CFLAGS}" \
246 $(use custom-cflags || echo SKIP_)HOSTLDFLAGS="${LDFLAGS}" \
247 $(use custom-cflags || echo SKIP_)KLIBCOPTFLAGS="${CFLAGS}" \
249 install || die "Install failed!"
251 #SHLIBDIR="/${libdir}" \
253 # klibc doesn't support prelinking, so we need to mask it
254 cat > "${T}/70klibc" <<-EOF
255 PRELINK_PATH_MASK="/usr/${libdir}/klibc"
258 doenvd "${T}"/70klibc
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
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
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}"
274 # Restore now, so we can use the tc- functions
275 ARCH="${myARCH}" ABI="${myABI}"
276 if ! tc-is-cross-compiler ; then
278 insinto /usr/share/aclocal
279 doins contrib/klibc.m4
281 dodoc README usr/klibc/CAVEATS
282 docinto gzip; dodoc usr/gzip/README
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}"
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"
299 for t in $ALL_TESTS ; do
300 if has $t $BROKEN_TESTS ; then
304 ./$t </dev/null >/dev/null
306 if [ $rc -eq 0 ]; then
314 [ $failed -ne 0 ] && die "Some tests failed."