dev-libs/klibc: Inline mirror://kernel
[gentoo.git] / dev-libs / klibc / klibc-2.0.4-r3.ebuild
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
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.
8
9 # NOTES:
10 # ======
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
14 # unfortunately.
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.
17
18 EAPI=6
19 K_TARBALL_SUFFIX="xz"
20
21 inherit eutils toolchain-funcs flag-o-matic
22
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"
26 PKV_EXTRA=""
27 if [[ ${PKV_EXTRA} ]]; then
28         if [[ ${KV_MAJOR} == 2 ]]; then
29                 PKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
30         else
31                 PKV="${KV_MAJOR}.$((${KV_SUB}+1))-${PKV_EXTRA}"
32         fi
33         PATCH_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}/patch-${PKV}.${K_TARBALL_SUFFIX}"
34 fi
35 if [[ ${KV_MAJOR} == 2 ]]; then
36         OKV="${KV_MAJOR}.${KV_MINOR}.${KV_SUB}"
37 else
38         OKV="${KV_MAJOR}.${KV_SUB}"
39 fi
40 KERNEL_URI="
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}"
43 DEBIAN_PV=2.0.4
44 DEBIAN_PR=9
45 DEBIAN_A="${PN}_${DEBIAN_PV}-${DEBIAN_PR}.debian.tar.xz"
46 SRC_URI="
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}
49         ${PATCH_URI}
50         ${KERNEL_URI}"
51
52 LICENSE="|| ( GPL-2 LGPL-2 )"
53 KEYWORDS="~alpha amd64 ~arm ia64 -mips ~ppc ~ppc64 ~sparc x86"
54 SLOT="0"
55 IUSE="debug test custom-cflags"
56
57 RDEPEND="dev-lang/perl"
58 DEPEND="${RDEPEND}
59         sys-devel/bc"
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 PATCHES=(
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
108 )
109
110 src_prepare() {
111         [[ ${PKV} ]] && eapply "${DISTDIR}"/patch-${PKV}.${K_TARBALL_SUFFIX}
112
113         # Symlink /usr/src/linux to ${S}/linux
114         ln -snf "${KS}" linux
115         #ln -snf "/usr" linux
116
117         # Borrow the debian fixes too
118         for p in $(<"${WORKDIR}"/debian/patches/series) ; do
119                 PATCHES+=( "${WORKDIR}/debian/patches/${p}" )
120         done
121
122         default
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                 -e '1iCONFIG_ARM_UNWIND=y' \
169                 "${KS}"/.config \
170                 "${S}"/defconfig
171         fi
172         emake prepare CC="${CC}" HOSTCC="${HOSTCC}" ARCH="${KLIBCASMARCH}" || die "Failed to prepare kernel sources for header usage"
173
174         cd "${S}"
175
176         use debug && myargs="${myargs} V=1"
177         use test && myargs="${myargs} test"
178         append-ldflags -z noexecstack
179         append-flags -nostdlib
180
181         # Parallel build intermittantly fails when doing `LIST usr/klibc/syscalls/klib.list'
182         emake -j1 \
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" \
195                 T="${T}" \
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!"
200
201                 #SHLIBDIR="/${libdir}" \
202
203         ARCH="${myARCH}" ABI="${myABI}"
204 }
205
206 src_install() {
207         local myargs
208         local myARCH="${ARCH}" myABI="${ABI}"
209         # TODO: For cross-compiling
210         # You should set ARCH and ABI here
211         CC="$(tc-getCC)"
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})
219
220         use debug && myargs="${myargs} V=1"
221
222         local klibc_prefix
223         if tc-is-cross-compiler ; then
224                 klibc_prefix=$("${S}/klcc/${KLIBCARCH}-klcc" -print-klibc-prefix)
225         else
226                 klibc_prefix=$("${S}/klcc/klcc" -print-klibc-prefix)
227         fi
228
229         unset ABI ARCH # Unset these, because they interfere
230         unset KBUILD_OUTPUT # we are using a private copy
231
232         emake \
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" \
240                 INSTALLROOT="${D}" \
241                 KLIBCARCH=${KLIBCARCH} \
242                 KLIBCASMARCH=${KLIBCASMARCH} \
243                 SHLIBDIR="/${libdir}" \
244                 libdir="/usr/${libdir}" \
245                 mandir="/usr/share/man" \
246                 T="${T}" \
247                 $(use custom-cflags || echo SKIP_)HOSTCFLAGS="${CFLAGS}" \
248                 $(use custom-cflags || echo SKIP_)HOSTLDFLAGS="${LDFLAGS}" \
249                 $(use custom-cflags || echo SKIP_)KLIBCOPTFLAGS="${CFLAGS}" \
250                 ${myargs} \
251                 install || die "Install failed!"
252
253                 #SHLIBDIR="/${libdir}" \
254
255         # klibc doesn't support prelinking, so we need to mask it
256         cat > "${T}/70klibc" <<-EOF
257                 PRELINK_PATH_MASK="/usr/${libdir}/klibc"
258         EOF
259
260         doenvd "${T}"/70klibc
261
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
265         # directory
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
270
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}"
275         done
276
277         # Restore now, so we can use the tc- functions
278         ARCH="${myARCH}" ABI="${myABI}"
279         if ! tc-is-cross-compiler ; then
280                 cd "${S}"
281                 insinto /usr/share/aclocal
282                 doins contrib/klibc.m4
283
284                 dodoc README usr/klibc/CAVEATS
285                 docinto gzip; dodoc usr/gzip/README
286         fi
287
288         # Fix up the symlink
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}"
293         fi
294 }
295
296 src_test() {
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"
301                 failed=0
302                 for t in $ALL_TESTS ; do
303                         if has $t $BROKEN_TESTS ; then
304                                 echo "=== $t SKIP"
305                         else
306                                 echo -n "=== $t "
307                                 ./$t </dev/null >/dev/null
308                                 rc=$?
309                                 if [ $rc -eq 0 ]; then
310                                         echo PASS
311                                 else
312                                         echo FAIL
313                                         failed=1
314                                 fi
315                         fi
316                 done
317                 [ $failed -ne 0 ] && die "Some tests failed."
318         fi
319 }