1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # Robin H. Johnson <robbat2@gentoo.org>, 12 Nov 2007:
5 # This still needs major work.
6 # But it is significently better than the previous version.
7 # In that it will now build on biarch systems, such as ppc64-32ul.
11 # We need to bring in the kernel sources seperately
12 # Because they have to be configured in a way that differs from the copy in
13 # /usr/src/. The sys-kernel/linux-headers are too stripped down to use
15 # This will be able to go away once the klibc author updates his code
16 # to build again the headers provided by the kernel's 'headers_install' target.
21 inherit eutils toolchain-funcs flag-o-matic
23 DESCRIPTION="A minimal libc subset for use with initramfs"
24 HOMEPAGE="https://www.zytor.com/mailman/listinfo/klibc/ https://www.kernel.org/pub/linux/libs/klibc/"
25 KV_MAJOR="4" KV_MINOR="x" KV_SUB="14"
27 if [[ ${PKV_EXTRA} ]]; then
28 if [[ ${KV_MAJOR} == 2 ]]; then
29 PKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
31 PKV="${KV_MAJOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
33 PATCH_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/patch-${PKV}.${K_TARBALL_SUFFIX}"
35 if [[ ${KV_MAJOR} == 2 ]]; then
36 OKV="${KV_MAJOR}.${KV_MINOR}.${KV_SUB}"
38 OKV="${KV_MAJOR}.${KV_SUB}"
41 https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/linux-${OKV}.tar.${K_TARBALL_SUFFIX}
42 https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/testing/linux-${OKV}.tar.${K_TARBALL_SUFFIX}"
45 DEBIAN_A="${PN}_${DEBIAN_PV}-${DEBIAN_PR}.debian.tar.xz"
47 https://www.kernel.org/pub/linux/libs/klibc/${PV:0:3}/${P}.tar.${K_TARBALL_SUFFIX}
48 mirror://debian/pool/main/k/klibc/${DEBIAN_A}
52 LICENSE="|| ( GPL-2 LGPL-2 )"
53 KEYWORDS="~alpha amd64 ~arm ia64 -mips ~ppc ~ppc64 ~sparc x86"
55 IUSE="debug test custom-cflags"
57 RDEPEND="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 # Build interp.o with EXTRA_KLIBCAFLAGS (.S source)
100 "${FILESDIR}"/${PN}-1.4.11-interp-flags.patch
101 # Fix usage of -s, bug #201006
102 "${FILESDIR}"/klibc-1.5.7-strip-fix-dash-s.patch
103 # The inline definition from sys/stat.h does not seem to get used
104 # So just copy it to make this compile for now
105 "${FILESDIR}"/klibc-2.0.2-mkfifo.patch
106 # Newer kernels have some headers in the uapi dir
107 "${FILESDIR}"/klibc-2.0.3-kernel-uapi.patch
111 [[ ${PKV} ]] && eapply "${DISTDIR}"/patch-${PKV}.${K_TARBALL_SUFFIX}
113 # Symlink /usr/src/linux to ${S}/linux
114 ln -snf "${KS}" linux
115 #ln -snf "/usr" linux
117 # Borrow the debian fixes too
118 for p in $(<"${WORKDIR}"/debian/patches/series) ; do
119 PATCHES+=( "${WORKDIR}/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' \
168 -e '1iCONFIG_ARM_UNWIND=y' \
172 emake prepare CC="${CC}" HOSTCC="${HOSTCC}" ARCH="${KLIBCASMARCH}" || die "Failed to prepare kernel sources for header usage"
176 use debug && myargs="${myargs} V=1"
177 use test && myargs="${myargs} test"
178 append-ldflags -z noexecstack
179 append-flags -nostdlib
181 # Parallel build intermittantly fails when doing `LIST usr/klibc/syscalls/klib.list'
183 EXTRA_KLIBCAFLAGS="-Wa,--noexecstack" \
184 EXTRA_KLIBCLDFLAGS="-z noexecstack" \
185 HOSTLDFLAGS="-z noexecstack" \
186 KLIBCOPTFLAGS='-nostdlib' \
187 HOSTCC="${HOSTCC}" CC="${CC}" \
188 HOSTLD="${HOSTLD}" LD="${LD}" \
189 INSTALLDIR="/usr/${libdir}/klibc" \
190 KLIBCARCH=${KLIBCARCH} \
191 KLIBCASMARCH=${KLIBCASMARCH} \
192 SHLIBDIR="/${libdir}" \
193 libdir="/usr/${libdir}" \
194 mandir="/usr/share/man" \
196 $(use custom-cflags || echo SKIP_)HOSTCFLAGS="${CFLAGS}" \
197 $(use custom-cflags || echo SKIP_)HOSTLDFLAGS="${LDFLAGS}" \
198 $(use custom-cflags || echo SKIP_)KLIBCOPTFLAGS="${CFLAGS}" \
199 ${myargs} || die "Compile failed!"
201 #SHLIBDIR="/${libdir}" \
203 ARCH="${myARCH}" ABI="${myABI}"
208 local myARCH="${ARCH}" myABI="${ABI}"
209 # TODO: For cross-compiling
210 # You should set ARCH and ABI here
212 HOSTCC="$(tc-getBUILD_CC)"
213 KLIBCARCH="$(klibc_arch ${ARCH})"
214 KLIBCASMARCH="$(kernel_asm_arch ${ARCH})"
215 libdir="$(get_libdir)"
216 # This should be the defconfig corresponding to your userspace!
217 # NOT your kernel. PPC64-32ul would choose 'ppc' for example.
218 defconfig=$(kernel_defconfig ${ARCH})
220 use debug && myargs="${myargs} V=1"
223 if tc-is-cross-compiler ; then
224 klibc_prefix=$("${S}/klcc/${KLIBCARCH}-klcc" -print-klibc-prefix)
226 klibc_prefix=$("${S}/klcc/klcc" -print-klibc-prefix)
229 unset ABI ARCH # Unset these, because they interfere
230 unset KBUILD_OUTPUT # we are using a private copy
233 EXTRA_KLIBCAFLAGS="-Wa,--noexecstack" \
234 EXTRA_KLIBCLDFLAGS="-z noexecstack" \
235 HOSTLDFLAGS="-z noexecstack" \
236 KLIBCOPTFLAGS='-nostdlib' \
237 HOSTCC="${HOSTCC}" CC="${CC}" \
238 HOSTLD="${HOSTLD}" LD="${LD}" \
239 INSTALLDIR="/usr/${libdir}/klibc" \
241 KLIBCARCH=${KLIBCARCH} \
242 KLIBCASMARCH=${KLIBCASMARCH} \
243 SHLIBDIR="/${libdir}" \
244 libdir="/usr/${libdir}" \
245 mandir="/usr/share/man" \
247 $(use custom-cflags || echo SKIP_)HOSTCFLAGS="${CFLAGS}" \
248 $(use custom-cflags || echo SKIP_)HOSTLDFLAGS="${LDFLAGS}" \
249 $(use custom-cflags || echo SKIP_)KLIBCOPTFLAGS="${CFLAGS}" \
251 install || die "Install failed!"
253 #SHLIBDIR="/${libdir}" \
255 # klibc doesn't support prelinking, so we need to mask it
256 cat > "${T}/70klibc" <<-EOF
257 PRELINK_PATH_MASK="/usr/${libdir}/klibc"
260 doenvd "${T}"/70klibc
262 # Fix the permissions (bug #178053) on /usr/${libdir}/klibc/include
263 # Actually I have no idea, why the includes have those weird-ass permissions
264 # on a particular system, might be due to inherited permissions from parent
266 # NOTE: This totally violates sandbox <asturm@gentoo.org>
267 # find "${D}"/usr/${libdir}/klibc/include | xargs chmod o+rX
268 find "${D}"/usr/${libdir}/klibc/include -type f \
269 \( -name '.install' -o -name '..install.cmd' \) -delete || die
271 # Hardlinks becoming copies
272 for x in gunzip zcat ; do
273 rm -f "${D}/${klibc_prefix}/bin/${x}"
274 dosym gzip "${klibc_prefix}/bin/${x}"
277 # Restore now, so we can use the tc- functions
278 ARCH="${myARCH}" ABI="${myABI}"
279 if ! tc-is-cross-compiler ; then
281 insinto /usr/share/aclocal
282 doins contrib/klibc.m4
284 dodoc README usr/klibc/CAVEATS
285 docinto gzip; dodoc usr/gzip/README
289 # Mainly for merged arches
290 linkname="${D}/usr/${libdir}/klibc/include/asm"
291 if [ -L "${linkname}" ] && [ ! -e "${linkname}" ] ; then
292 ln -snf asm-${KLIBCASMARCH} "${linkname}"
297 if ! tc-is-cross-compiler ; then
298 cd "${S}"/usr/klibc/tests
299 ALL_TESTS="$(ls *.c |sed 's,\.c$,,g')"
300 BROKEN_TESTS="fcntl fnmatch testrand48"
302 for t in $ALL_TESTS ; do
303 if has $t $BROKEN_TESTS ; then
307 ./$t </dev/null >/dev/null
309 if [ $rc -eq 0 ]; then
317 [ $failed -ne 0 ] && die "Some tests failed."