dev-python/objgraph: keyworded 3.4.1 for ia64, bug #717946
[gentoo.git] / eclass / kernel-2.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: kernel-2.eclass
5 # @MAINTAINER:
6 # Gentoo Kernel project <kernel@gentoo.org>
7 # @AUTHOR:
8 # John Mylchreest <johnm@gentoo.org>
9 # Mike Pagano <mpagano@gentoo.org>
10 # <so many, many others, please add yourself>
11 # @SUPPORTED_EAPIS: 2 3 4 5 6
12 # @BLURB: Eclass for kernel packages
13 # @DESCRIPTION:
14 # This is the kernel.eclass rewrite for a clean base regarding the 2.6
15 # series of kernel with back-compatibility for 2.4
16 # Please direct your bugs to the current eclass maintainer :)
17 # added functionality:
18 # unipatch              - a flexible, singular method to extract, add and remove patches.
19
20 # @ECLASS-VARIABLE: K_USEPV
21 # @DEFAULT_UNSET
22 # @DESCRIPTION:
23 # When setting the EXTRAVERSION variable, it should
24 # add PV to the end.
25 # this is useful for things like wolk. IE:
26 # EXTRAVERSION would be something like : -wolk-4.19-r1
27
28 # @ECLASS-VARIABLE:  K_NOSETEXTRAVERSION
29 # @DEFAULT_UNSET
30 # @DESCRIPTION:
31 # if this is set then EXTRAVERSION will not be
32 # automatically set within the kernel Makefile
33
34 # @ECLASS-VARIABLE: K_NOUSENAME
35 # @DEFAULT_UNSET
36 # @DESCRIPTION:
37 # if this is set then EXTRAVERSION will not include the
38 # first part of ${PN} in EXTRAVERSION
39
40 # @ECLASS-VARIABLE: K_NOUSEPR
41 # @DEFAULT_UNSET
42 # @DESCRIPTION:
43 # if this is set then EXTRAVERSION will not include the
44 # anything based on ${PR}.
45
46 # @ECLASS-VARIABLE: K_PREPATCHED
47 # @DEFAULT_UNSET
48 # @DESCRIPTION:
49 # if the patchset is prepatched (ie: mm-sources,
50 # ck-sources, ac-sources) it will use PR (ie: -r5) as
51 # the patchset version for
52 # and not use it as a true package revision
53
54 # @ECLASS-VARIABLE:  K_EXTRAEINFO
55 # @DEFAULT_UNSET
56 # @DESCRIPTION:
57 # this is a new-line seperated list of einfo displays in
58 # postinst and can be used to carry additional postinst
59 # messages
60
61 # @ECLASS-VARIABLE:  K_EXTRAELOG
62 # @DEFAULT_UNSET
63 # @DESCRIPTION:
64 # same as K_EXTRAEINFO except using elog instead of einfo
65
66 # @ECLASS-VARIABLE:  K_EXTRAEWARN
67 # @DEFAULT_UNSET
68 # @DESCRIPTION:
69 # same as K_EXTRAEINFO except using ewarn instead of einfo
70
71 # @ECLASS-VARIABLE:  K_SYMLINK
72 # @DEFAULT_UNSET
73 # @DESCRIPTION:
74 # if this is set, then forcably create symlink anyway
75
76 # @ECLASS-VARIABLE:  K_BASE_VER
77 # @DEFAULT_UNSET
78 # @DESCRIPTION:
79 # for git-sources, declare the base version this patch is
80 # based off of.
81
82 # @ECLASS-VARIABLE:  K_DEFCONFIG
83 # @DEFAULT_UNSET
84 # @DESCRIPTION:
85 # Allow specifying a different defconfig target.
86 # If length zero, defaults to "defconfig".
87
88 # @ECLASS-VARIABLE:  K_WANT_GENPATCHES
89 # @DEFAULT_UNSET
90 # @DESCRIPTION:
91 # Apply genpatches to kernel source. Provide any
92 # combination of "base", "extras" or "experimental".
93
94 # @ECLASS-VARIABLE:  K_EXP_GENPATCHES_PULL
95 # @DEFAULT_UNSET
96 # @DESCRIPTION:
97 # If set, we pull "experimental" regardless of the USE FLAG
98 # but expect the ebuild maintainer to use K_EXP_GENPATCHES_LIST.
99
100 # @ECLASS-VARIABLE:  K_EXP_GENPATCHES_NOUSE
101 # @DEFAULT_UNSET
102 # @DESCRIPTION:
103 # If set, no USE flag will be provided for "experimental";
104 # as a result the user cannot choose to apply those patches.
105
106 # @ECLASS-VARIABLE:  K_EXP_GENPATCHES_LIST
107 # @DEFAULT_UNSET
108 # @DESCRIPTION:
109 # A list of patches to pick from "experimental" to apply when
110 # the USE flag is unset and K_EXP_GENPATCHES_PULL is set.
111
112 # @ECLASS-VARIABLE:  K_FROM_GIT
113 # @DEFAULT_UNSET
114 # @DESCRIPTION:
115 # If set, this variable signals that the kernel sources derives
116 # from a git tree and special handling will be applied so that
117 # any patches that are applied will actually apply.
118
119 # @ECLASS-VARIABLE:  K_GENPATCHES_VER
120 # @DEFAULT_UNSET
121 # @DESCRIPTION:
122 # The version of the genpatches tarball(s) to apply.
123 # A value of "5" would apply genpatches-2.6.12-5 to
124 # my-sources-2.6.12.ebuild
125
126 # @ECLASS-VARIABLE:  K_SECURITY_UNSUPPORTED
127 # @DEFAULT_UNSET
128 # @DESCRIPTION:
129 # If set, this kernel is unsupported by Gentoo Security
130 # to the current eclass maintainer :)
131
132 # @ECLASS-VARIABLE:  K_DEBLOB_AVAILABLE
133 # @DEFAULT_UNSET
134 # @DESCRIPTION:
135 # A value of "0" will disable all of the optional deblob
136 # code. If empty, will be set to "1" if deblobbing is
137 # possible. Test ONLY for "1".
138
139 # @ECLASS-VARIABLE:  K_DEBLOB_TAG
140 # @DEFAULT_UNSET
141 # @DESCRIPTION:
142 # This will be the version of deblob script. It's a upstream SVN tag
143 # such asw -gnu or -gnu1.
144
145 # @ECLASS-VARIABLE:  K_PREDEBLOBBED
146 # @DEFAULT_UNSET
147 # @DESCRIPTION:
148 # This kernel was already deblobbed elsewhere.
149 # If false, either optional deblobbing will be available
150 # or the license will note the inclusion of linux-firmware code.
151
152 # @ECLASS-VARIABLE:  K_LONGTERM
153 # @DEFAULT_UNSET
154 # @DESCRIPTION:
155 # If set, the eclass will search for the kernel source
156 # in the long term directories on the upstream servers
157 # as the location has been changed by upstream
158
159 # @ECLASS-VARIABLE:  H_SUPPORTEDARCH
160 # @DEFAULT_UNSET
161 # @DESCRIPTION:
162 # this should be a space separated list of ARCH's which
163 # can be supported by the headers ebuild
164
165 # @ECLASS-VARIABLE:  UNIPATCH_LIST
166 # @DEFAULT_UNSET
167 # @DESCRIPTION:
168 # space delimetered list of patches to be applied to the kernel
169
170 # @ECLASS-VARIABLE:  UNIPATCH_EXCLUDE
171 # @DEFAULT_UNSET
172 # @DESCRIPTION:
173 # An addition var to support exlusion based completely
174 # on "<passedstring>*" and not "<passedno#>_*"
175 # this should _NOT_ be used from the ebuild as this is
176 # reserved for end users passing excludes from the cli
177
178 # @ECLASS-VARIABLE:  UNIPATCH_DOCS
179 # @DEFAULT_UNSET
180 # @DESCRIPTION:
181 # space delimemeted list of docs to be installed to
182 # the doc dir
183
184 # @ECLASS-VARIABLE:  UNIPATCH_STRICTORDER
185 # @DEFAULT_UNSET
186 # @DESCRIPTION:
187 # if this is set places patches into directories of
188 # order, so they are applied in the order passed
189 # Changing any other variable in this eclass is not supported; you can request
190 # for additional variables to be added by contacting the current maintainer.
191 # If you do change them, there is a chance that we will not fix resulting bugs;
192 # that of course does not mean we're not willing to help.
193
194 inherit toolchain-funcs
195 [[ ${EAPI:-0} == [012345] ]] && inherit epatch
196 [[ ${EAPI:-0} == [0123456] ]] && inherit estack eapi7-ver
197 case ${EAPI:-0} in
198         2|3|4|5|6)
199                 EXPORT_FUNCTIONS src_{unpack,prepare,compile,install,test} \
200                         pkg_{setup,preinst,postinst,postrm} ;;
201         *) die "${ECLASS}: EAPI ${EAPI} not supported" ;;
202 esac
203
204 # Added by Daniel Ostrow <dostrow@gentoo.org>
205 # This is an ugly hack to get around an issue with a 32-bit userland on ppc64.
206 # I will remove it when I come up with something more reasonable.
207 [[ ${PROFILE_ARCH} == "ppc64" ]] && CHOST="powerpc64-${CHOST#*-}"
208
209 export CTARGET=${CTARGET:-${CHOST}}
210 if [[ ${CTARGET} == ${CHOST} && ${CATEGORY/cross-} != ${CATEGORY} ]]; then
211         export CTARGET=${CATEGORY/cross-}
212 fi
213
214 HOMEPAGE="https://www.kernel.org/ https://wiki.gentoo.org/wiki/Kernel ${HOMEPAGE}"
215 : ${LICENSE:="GPL-2"}
216
217 # This is the latest KV_PATCH of the deblob tool available from the
218 # libre-sources upstream. If you bump this, you MUST regenerate the Manifests
219 # for ALL kernel-2 consumer packages where deblob is available.
220 : ${DEBLOB_MAX_VERSION:=38}
221
222 # No need to run scanelf/strip on kernel sources/headers (bug #134453).
223 RESTRICT="binchecks strip"
224
225 # set LINUX_HOSTCFLAGS if not already set
226 : ${LINUX_HOSTCFLAGS:="-Wall -Wstrict-prototypes -Os -fomit-frame-pointer -I${S}/include"}
227
228
229 # @FUNCTION: debug-print-kernel2-variables
230 # @USAGE:
231 # @DESCRIPTION:
232 # this function exists only to help debug kernel-2.eclass
233 # if you are adding new functionality in, put a call to it
234 # at the start of src_unpack, or during SRC_URI/dep generation.
235
236 debug-print-kernel2-variables() {
237         for v in PVR CKV OKV KV KV_FULL KV_MAJOR KV_MINOR KV_PATCH RELEASETYPE \
238                         RELEASE UNIPATCH_LIST_DEFAULT UNIPATCH_LIST_GENPATCHES \
239                         UNIPATCH_LIST S KERNEL_URI K_WANT_GENPATCHES ; do
240                 debug-print "${v}: ${!v}"
241         done
242 }
243
244 # @FUNCTION: handle_genpatches
245 # @USAGE: [--set-unipatch-list]
246 # @DESCRIPTION:
247 # add genpatches to list of patches to apply if wanted
248
249 handle_genpatches() {
250         local tarball want_unipatch_list
251         [[ -z ${K_WANT_GENPATCHES} || -z ${K_GENPATCHES_VER} ]] && return 1
252
253         if [[ -n ${1} ]]; then
254                 # set UNIPATCH_LIST_GENPATCHES only on explicit request
255                 # since that requires 'use' call which can be used only in phase
256                 # functions, while the function is also called in global scope
257                 if [[ ${1} == --set-unipatch-list ]]; then
258                         want_unipatch_list=1
259                 else
260                         die "Usage: ${FUNCNAME} [--set-unipatch-list]"
261                 fi
262         fi
263
264         debug-print "Inside handle_genpatches"
265         local OKV_ARRAY
266         IFS="." read -r -a OKV_ARRAY <<<"${OKV}"
267
268         # for > 3.0 kernels, handle genpatches tarball name
269         # genpatches for 3.0 and 3.0.1 might be named
270         # genpatches-3.0-1.base.tar.xz and genpatches-3.0-2.base.tar.xz
271         # respectively.  Handle this.
272
273         for i in ${K_WANT_GENPATCHES} ; do
274                 if [[ ${KV_MAJOR} -ge 3 ]]; then
275                         if [[ ${#OKV_ARRAY[@]} -ge 3 ]]; then
276                                 tarball="genpatches-${KV_MAJOR}.${KV_MINOR}-${K_GENPATCHES_VER}.${i}.tar.xz"
277                         else
278                                 tarball="genpatches-${KV_MAJOR}.${KV_PATCH}-${K_GENPATCHES_VER}.${i}.tar.xz"
279                         fi
280                 else
281                         tarball="genpatches-${OKV}-${K_GENPATCHES_VER}.${i}.tar.xz"
282                 fi
283
284                 local use_cond_start="" use_cond_end=""
285
286                 if [[ "${i}" == "experimental" && -z ${K_EXP_GENPATCHES_PULL} && -z ${K_EXP_GENPATCHES_NOUSE} ]] ; then
287                         use_cond_start="experimental? ( "
288                         use_cond_end=" )"
289
290                         if [[ -n ${want_unipatch_list} ]] && use experimental ; then
291                                 UNIPATCH_LIST_GENPATCHES+=" ${DISTDIR}/${tarball}"
292                                 debug-print "genpatches tarball: $tarball"
293                         fi
294                 elif [[ -n ${want_unipatch_list} ]]; then
295                         UNIPATCH_LIST_GENPATCHES+=" ${DISTDIR}/${tarball}"
296                         debug-print "genpatches tarball: $tarball"
297                 fi
298                 GENPATCHES_URI+=" ${use_cond_start}$(echo https://dev.gentoo.org/~{alicef,mpagano,whissi}/dist/genpatches/${tarball})${use_cond_end}"
299         done
300 }
301
302 # @FUNCTION: detect_version
303 # @USAGE:
304 # @DESCRIPTION:
305 # this function will detect and set
306 # - OKV: Original Kernel Version (2.6.0/2.6.0-test11)
307 # - KV: Kernel Version (2.6.0-gentoo/2.6.0-test11-gentoo-r1)
308 # - EXTRAVERSION: The additional version appended to OKV (-gentoo/-gentoo-r1)
309 detect_version() {
310
311         # We've already run, so nothing to do here.
312         [[ -n ${KV_FULL} ]] && return 0
313
314         # CKV is used as a comparison kernel version, which is used when
315         # PV doesnt reflect the genuine kernel version.
316         # this gets set to the portage style versioning. ie:
317         #   CKV=2.6.11_rc4
318         CKV=${CKV:-${PV}}
319         OKV=${OKV:-${CKV}}
320         OKV=${OKV/_beta/-test}
321         OKV=${OKV/_rc/-rc}
322         OKV=${OKV/-r*}
323         OKV=${OKV/_p*}
324
325         KV_MAJOR=$(ver_cut 1 ${OKV})
326         # handle if OKV is X.Y or X.Y.Z (e.g. 3.0 or 3.0.1)
327         local OKV_ARRAY
328         IFS="." read -r -a OKV_ARRAY <<<"${OKV}"
329
330         # if KV_MAJOR >= 3, then we have no more KV_MINOR
331         #if [[ ${KV_MAJOR} -lt 3 ]]; then
332         if [[ ${#OKV_ARRAY[@]} -ge 3 ]]; then
333                 KV_MINOR=$(ver_cut 2 ${OKV})
334                 KV_PATCH=$(ver_cut 3 ${OKV})
335                 if [[ ${KV_MAJOR}${KV_MINOR}${KV_PATCH} -ge 269 ]]; then
336                         KV_EXTRA=$(ver_cut 4- ${OKV})
337                         KV_EXTRA=${KV_EXTRA/[-_]*}
338                 else
339                         KV_PATCH=$(ver_cut 3- ${OKV})
340                 fi
341         else
342                 KV_PATCH=$(ver_cut 2 ${OKV})
343                 KV_EXTRA=$(ver_cut 3- ${OKV})
344                 KV_EXTRA=${KV_EXTRA/[-_]*}
345         fi
346
347         debug-print "KV_EXTRA is ${KV_EXTRA}"
348
349         KV_PATCH=${KV_PATCH/[-_]*}
350
351         local v n=0 missing
352         #if [[ ${KV_MAJOR} -lt 3 ]]; then
353         if [[ ${#OKV_ARRAY[@]} -ge 3 ]]; then
354                 for v in CKV OKV KV_{MAJOR,MINOR,PATCH} ; do
355                         [[ -z ${!v} ]] && n=1 && missing="${missing}${v} ";
356                 done
357         else
358                 for v in CKV OKV KV_{MAJOR,PATCH} ; do
359                         [[ -z ${!v} ]] && n=1 && missing="${missing}${v} ";
360                 done
361         fi
362
363         [[ $n -eq 1 ]] && \
364                 eerror "Missing variables: ${missing}" && \
365                 die "Failed to extract kernel version (try explicit CKV in ebuild)!"
366         unset v n missing
367
368 #       if [[ ${KV_MAJOR} -ge 3 ]]; then
369         if [[ ${#OKV_ARRAY[@]} -lt 3 ]]; then
370                 KV_PATCH_ARR=(${KV_PATCH//\./ })
371
372                 # at this point 031412, Linus is putting all 3.x kernels in a
373                 # 3.x directory, may need to revisit when 4.x is released
374                 KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.x"
375
376                 [[ -n "${K_LONGTERM}" ]] &&
377                         KERNEL_BASE_URI="${KERNEL_BASE_URI}/longterm/v${KV_MAJOR}.${KV_PATCH_ARR}"
378         else
379                 #KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.0"
380                 #KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}"
381                 if [[ ${KV_MAJOR} -ge 3 ]]; then
382                         KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.x"
383                 else
384                         KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}"
385                 fi
386
387                 [[ -n "${K_LONGTERM}" ]] &&
388                         #KERNEL_BASE_URI="${KERNEL_BASE_URI}/longterm"
389                         KERNEL_BASE_URI="${KERNEL_BASE_URI}/longterm/v${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}"
390         fi
391
392         debug-print "KERNEL_BASE_URI is ${KERNEL_BASE_URI}"
393
394         if [[ ${#OKV_ARRAY[@]} -ge 3 ]] && [[ ${KV_MAJOR} -ge 3 ]]; then
395                 # handle non genpatch using sources correctly
396                 if [[ -z ${K_WANT_GENPATCHES} && -z ${K_GENPATCHES_VER} && ${KV_PATCH} -gt 0 ]]; then
397                         KERNEL_URI="${KERNEL_BASE_URI}/patch-${OKV}.xz"
398                         UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV}.xz"
399                 fi
400                 KERNEL_URI="${KERNEL_URI} ${KERNEL_BASE_URI}/linux-${KV_MAJOR}.${KV_MINOR}.tar.xz"
401         else
402                 KERNEL_URI="${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
403         fi
404
405         RELEASE=${CKV/${OKV}}
406         RELEASE=${RELEASE/_beta}
407         RELEASE=${RELEASE/_rc/-rc}
408         RELEASE=${RELEASE/_pre/-pre}
409         # We cannot trivally call kernel_is here, because it calls us to detect the
410         # version
411         #kernel_is ge 2 6 && RELEASE=${RELEASE/-pre/-git}
412         [ $(($KV_MAJOR * 1000 + ${KV_MINOR:-0})) -ge 2006 ] && RELEASE=${RELEASE/-pre/-git}
413         RELEASETYPE=${RELEASE//[0-9]}
414
415         # Now we know that RELEASE is the -rc/-git
416         # and RELEASETYPE is the same but with its numerics stripped
417         # we can work on better sorting EXTRAVERSION.
418         # first of all, we add the release
419         EXTRAVERSION="${RELEASE}"
420         debug-print "0 EXTRAVERSION:${EXTRAVERSION}"
421         [[ -n ${KV_EXTRA} ]] && [[ ${KV_MAJOR} -lt 3 ]] && EXTRAVERSION=".${KV_EXTRA}${EXTRAVERSION}"
422
423         debug-print "1 EXTRAVERSION:${EXTRAVERSION}"
424         if [[ -n "${K_NOUSEPR}" ]]; then
425                 # Don't add anything based on PR to EXTRAVERSION
426                 debug-print "1.0 EXTRAVERSION:${EXTRAVERSION}"
427         elif [[ -n ${K_PREPATCHED} ]]; then
428                 debug-print "1.1 EXTRAVERSION:${EXTRAVERSION}"
429                 EXTRAVERSION="${EXTRAVERSION}-${PN/-*}${PR/r}"
430         elif [[ "${ETYPE}" = "sources" ]]; then
431                 debug-print "1.2 EXTRAVERSION:${EXTRAVERSION}"
432                 # For some sources we want to use the PV in the extra version
433                 # This is because upstream releases with a completely different
434                 # versioning scheme.
435                 case ${PN/-*} in
436                      wolk) K_USEPV=1;;
437                   vserver) K_USEPV=1;;
438                 esac
439
440                 [[ -z "${K_NOUSENAME}" ]] && EXTRAVERSION="${EXTRAVERSION}-${PN/-*}"
441                 [[ -n "${K_USEPV}" ]]     && EXTRAVERSION="${EXTRAVERSION}-${PV//_/-}"
442                 [[ -n "${PR//r0}" ]] && EXTRAVERSION="${EXTRAVERSION}-${PR}"
443         fi
444         debug-print "2 EXTRAVERSION:${EXTRAVERSION}"
445
446         # The only messing around which should actually effect this is for KV_EXTRA
447         # since this has to limit OKV to MAJ.MIN.PAT and strip EXTRA off else
448         # KV_FULL evaluates to MAJ.MIN.PAT.EXT.EXT after EXTRAVERSION
449
450         if [[ -n ${KV_EXTRA} ]]; then
451                 if [[ -n ${KV_MINOR} ]]; then
452                         OKV="${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}"
453                 else
454                         OKV="${KV_MAJOR}.${KV_PATCH}"
455                 fi
456                 KERNEL_URI="${KERNEL_BASE_URI}/patch-${CKV}.xz
457                                         ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
458                 UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV}.xz"
459         fi
460
461         # We need to set this using OKV, but we need to set it before we do any
462         # messing around with OKV based on RELEASETYPE
463         KV_FULL=${OKV}${EXTRAVERSION}
464
465         # we will set this for backwards compatibility.
466         S="${WORKDIR}"/linux-${KV_FULL}
467         KV=${KV_FULL}
468
469         # -rc-git pulls can be achieved by specifying CKV
470         # for example:
471         #   CKV="2.6.11_rc3_pre2"
472         # will pull:
473         #   linux-2.6.10.tar.xz & patch-2.6.11-rc3.xz & patch-2.6.11-rc3-git2.xz
474
475         if [[ ${KV_MAJOR}${KV_MINOR} -eq 26 ]]; then
476
477                 if [[ ${RELEASETYPE} == -rc ]] || [[ ${RELEASETYPE} == -pre ]]; then
478                         OKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_PATCH} - 1))"
479                         KERNEL_URI="${KERNEL_BASE_URI}/testing/patch-${CKV//_/-}.xz
480                                                 ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
481                         UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV//_/-}.xz"
482                 fi
483
484                 if [[ ${RELEASETYPE} == -git ]]; then
485                         KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${OKV}${RELEASE}.xz
486                                                 ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
487                         UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${OKV}${RELEASE}.xz"
488                 fi
489
490                 if [[ ${RELEASETYPE} == -rc-git ]]; then
491                         OKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_PATCH} - 1))"
492                         KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE}.xz
493                                                 ${KERNEL_BASE_URI}/testing/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE/-git*}.xz
494                                                 ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
495
496                         UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE/-git*}.xz ${DISTDIR}/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE}.xz"
497                 fi
498         else
499                 KV_PATCH_ARR=(${KV_PATCH//\./ })
500
501                 # the different majorminor versions have different patch start versions
502                 OKV_DICT=(["2"]="${KV_MAJOR}.$((${KV_PATCH_ARR} - 1))" ["3"]="2.6.39" ["4"]="3.19")
503
504                 if [[ ${RELEASETYPE} == -rc ]] || [[ ${RELEASETYPE} == -pre ]]; then
505
506                         OKV=${K_BASE_VER:-$OKV_DICT["${KV_MAJOR}"]}
507
508                         # as of 12/5/2017, the rc patch is no longer offered as a compressed
509                         # file, and no longer is it mirrored on kernel.org
510                         if ver_test "${KV_MAJOR}.${KV_PATCH}" -ge "4.12"; then
511                                 KERNEL_URI="https://git.kernel.org/torvalds/p/v${KV_FULL}/v${OKV} -> patch-${KV_FULL}.patch
512                                                         ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
513                                 UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV//_/-}.patch"
514                         else
515                                 KERNEL_URI="${KERNEL_BASE_URI}/testing/patch-${CKV//_/-}.xz
516                                                         ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
517                                 UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV//_/-}.xz"
518                         fi
519                 fi
520
521                 if [[ ${RELEASETYPE} == -git ]]; then
522                         KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${OKV}${RELEASE}.xz
523                                                 ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
524                         UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${OKV}${RELEASE}.xz"
525                 fi
526
527                 if [[ ${RELEASETYPE} == -rc-git ]]; then
528                         OKV=${K_BASE_VER:-$OKV_DICT["${KV_MAJOR}"]}
529                         KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE}.xz
530                                                 ${KERNEL_BASE_URI}/testing/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE/-git*}.xz
531                                                 ${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
532
533                         UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE/-git*}.xz ${DISTDIR}/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE}.xz"
534                 fi
535
536
537         fi
538
539         debug-print-kernel2-variables
540
541         handle_genpatches
542 }
543
544 # @FUNCTION: kernel_is
545 # @USAGE: <conditional version | version>
546 # @DESCRIPTION:
547 # user for comparing kernel versions
548 # or just identifying a version
549 # e.g kernel_is 2 4
550 # e.g kernel_is ge 4.8.11
551 # Note: duplicated in linux-info.eclass
552 kernel_is() {
553         # ALL of these should be set before we can safely continue this function.
554         # some of the sources have in the past had only one set.
555         local v n=0
556         for v in OKV KV_{MAJOR,MINOR,PATCH} ; do [[ -z ${!v} ]] && n=1 ; done
557         [[ $n -eq 1 ]] && detect_version
558         unset v n
559
560         # Now we can continue
561         local operator test value
562
563         case ${1#-} in
564           lt) operator="-lt"; shift;;
565           gt) operator="-gt"; shift;;
566           le) operator="-le"; shift;;
567           ge) operator="-ge"; shift;;
568           eq) operator="-eq"; shift;;
569            *) operator="-eq";;
570         esac
571         [[ $# -gt 3 ]] && die "Error in kernel-2_kernel_is(): too many parameters"
572
573         : $(( test = (KV_MAJOR << 16) + (KV_MINOR << 8) + KV_PATCH ))
574         : $(( value = (${1:-${KV_MAJOR}} << 16) + (${2:-${KV_MINOR}} << 8) + ${3:-${KV_PATCH}} ))
575         [ ${test} ${operator} ${value} ]
576 }
577
578 # @FUNCTION: kernel_is_2_4
579 # @USAGE:
580 # @DESCRIPTION:
581 # return true if kernel is version 2.4
582 kernel_is_2_4() {
583         kernel_is 2 4
584 }
585
586 # @FUNCTION: kernel_is_2_6
587 # @USAGE:
588 # @DESCRIPTION:
589 # return true if kernel is version 2.6
590 kernel_is_2_6() {
591         kernel_is 2 6 || kernel_is 2 5
592 }
593
594 # Capture the sources type and set DEPENDs
595 if [[ ${ETYPE} == sources ]]; then
596         DEPEND="!build? (
597                 sys-apps/sed
598                 >=sys-devel/binutils-2.11.90.0.31
599         )"
600         RDEPEND="!build? (
601                 dev-lang/perl
602                 sys-devel/bc
603                 sys-devel/bison
604                 sys-devel/flex
605                 sys-devel/make
606                 >=sys-libs/ncurses-5.2
607                 virtual/libelf
608         )"
609
610         SLOT="${PVR}"
611         DESCRIPTION="Sources based on the Linux Kernel."
612         IUSE="symlink build"
613
614         # Bug #266157, deblob for libre support
615         if [[ -z ${K_PREDEBLOBBED} ]] ; then
616                 # Bug #359865, force a call to detect_version if needed
617                 kernel_is ge 2 6 27 && \
618                         [[ -z "${K_DEBLOB_AVAILABLE}" ]] && \
619                                 kernel_is le 2 6 ${DEBLOB_MAX_VERSION} && \
620                                         K_DEBLOB_AVAILABLE=1
621                 if [[ ${K_DEBLOB_AVAILABLE} == "1" ]] ; then
622                         PYTHON_COMPAT=( python2_7 )
623
624                         inherit python-any-r1
625
626                         IUSE="${IUSE} deblob"
627
628                         # Reflect that kernels contain firmware blobs unless otherwise
629                         # stripped. Starting with version 4.14, the whole firmware
630                         # tree has been dropped from the kernel.
631                         kernel_is lt 4 14 && LICENSE+=" !deblob? ( linux-firmware )"
632
633                         DEPEND+=" deblob? ( ${PYTHON_DEPS} )"
634
635                         if [[ -n KV_MINOR ]]; then
636                                 DEBLOB_PV="${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}"
637                         else
638                                 DEBLOB_PV="${KV_MAJOR}.${KV_PATCH}"
639                         fi
640
641                         if [[ ${KV_MAJOR} -ge 3 ]]; then
642                                 DEBLOB_PV="${KV_MAJOR}.${KV_MINOR}"
643                         fi
644
645                         # deblob svn tag, default is -gnu, to change, use K_DEBLOB_TAG in ebuild
646                         K_DEBLOB_TAG=${K_DEBLOB_TAG:--gnu}
647                         DEBLOB_A="deblob-${DEBLOB_PV}"
648                         DEBLOB_CHECK_A="deblob-check-${DEBLOB_PV}"
649                         DEBLOB_HOMEPAGE="https://www.fsfla.org/svn/fsfla/software/linux-libre/releases/tags/"
650                         DEBLOB_URI_PATH="${DEBLOB_PV}${K_DEBLOB_TAG}"
651                         DEBLOB_CHECK_URI="${DEBLOB_HOMEPAGE}/${DEBLOB_URI_PATH}/deblob-check -> ${DEBLOB_CHECK_A}"
652                         DEBLOB_URI="${DEBLOB_HOMEPAGE}/${DEBLOB_URI_PATH}/${DEBLOB_A}"
653                         HOMEPAGE="${HOMEPAGE} ${DEBLOB_HOMEPAGE}"
654
655                         KERNEL_URI="${KERNEL_URI}
656                                 deblob? (
657                                         ${DEBLOB_URI}
658                                         ${DEBLOB_CHECK_URI}
659                                 )"
660                 elif kernel_is lt 4 14; then
661                         # Deblobbing is not available, so just mark kernels older
662                         # than 4.14 as tainted with non-libre materials.
663                         LICENSE+=" linux-firmware"
664                 fi
665         fi
666
667 elif [[ ${ETYPE} == headers ]]; then
668         DESCRIPTION="Linux system headers"
669         IUSE="headers-only"
670
671         # Since we should NOT honour KBUILD_OUTPUT in headers
672         # lets unset it here.
673         unset KBUILD_OUTPUT
674
675         SLOT="0"
676 fi
677
678 # Cross-compile support functions
679
680 # @FUNCTION: kernel_header_destdir
681 # @USAGE:
682 # @DESCRIPTION:
683 # return header destination directory
684 kernel_header_destdir() {
685         [[ ${CTARGET} == ${CHOST} ]] \
686                 && echo /usr/include \
687                 || echo /usr/${CTARGET}/usr/include
688 }
689
690 # @FUNCTION: cross_pre_c_headers
691 # @USAGE:
692 # @DESCRIPTION:
693 # set use if necessary for cross compile support
694 cross_pre_c_headers() {
695         use headers-only && [[ ${CHOST} != ${CTARGET} ]]
696 }
697
698 # @FUNCTION: env_setup_xmakeopts
699 # @USAGE:
700 # @DESCRIPTION:
701 # set the ARCH/CROSS_COMPILE when cross compiling
702
703 env_setup_xmakeopts() {
704         # Kernel ARCH != portage ARCH
705         export KARCH=$(tc-arch-kernel)
706
707         # When cross-compiling, we need to set the ARCH/CROSS_COMPILE
708         # variables properly or bad things happen !
709         xmakeopts="ARCH=${KARCH}"
710         if [[ ${CTARGET} != ${CHOST} ]] && ! cross_pre_c_headers ; then
711                 xmakeopts="${xmakeopts} CROSS_COMPILE=${CTARGET}-"
712         elif type -p ${CHOST}-ar > /dev/null ; then
713                 xmakeopts="${xmakeopts} CROSS_COMPILE=${CHOST}-"
714         fi
715         export xmakeopts
716 }
717
718 # @FUNCTION: unpack_2_4
719 # @USAGE:
720 # @DESCRIPTION:
721 # unpack and generate .config for 2.4 kernels
722
723 unpack_2_4() {
724         # this file is required for other things to build properly,
725         # so we autogenerate it
726         make -s mrproper ${xmakeopts} || die "make mrproper failed"
727         make -s symlinks ${xmakeopts} || die "make symlinks failed"
728         make -s include/linux/version.h ${xmakeopts} || die "make include/linux/version.h failed"
729         echo ">>> version.h compiled successfully."
730 }
731
732 # @FUNCTION: unpack_2_6
733 # @USAGE:
734 # @DESCRIPTION:
735 # unpack and generate .config for 2.6 kernels
736
737 unpack_2_6() {
738         # this file is required for other things to build properly, so we
739         # autogenerate it ... generate a .config to keep version.h build from
740         # spitting out an annoying warning
741         make -s mrproper ${xmakeopts} 2>/dev/null \
742                 || die "make mrproper failed"
743
744         # quick fix for bug #132152 which triggers when it cannot include linux
745         # headers (ie, we have not installed it yet)
746         if ! make -s defconfig ${xmakeopts} &>/dev/null 2>&1 ; then
747                 touch .config
748                 eerror "make defconfig failed."
749                 eerror "assuming you dont have any headers installed yet and continuing"
750         fi
751
752         make -s include/linux/version.h ${xmakeopts} 2>/dev/null \
753                 || die "make include/linux/version.h failed"
754         rm -f .config >/dev/null
755 }
756
757 # @FUNCTION: universal_unpack
758 # @USAGE:
759 # @DESCRIPTION:
760 # unpack kernel sources
761
762 universal_unpack() {
763         debug-print "Inside universal_unpack"
764
765         local OKV_ARRAY
766         IFS="." read -r -a OKV_ARRAY <<<"${OKV}"
767
768         cd "${WORKDIR}"
769         if [[ ${#OKV_ARRAY[@]} -ge 3 ]] && [[ ${KV_MAJOR} -ge 3 ]]; then
770                 unpack linux-${KV_MAJOR}.${KV_MINOR}.tar.xz
771         else
772                 unpack linux-${OKV}.tar.xz
773         fi
774
775         if [[ -d "linux" ]]; then
776                 debug-print "Moving linux to linux-${KV_FULL}"
777                 mv linux linux-${KV_FULL} \
778                         || die "Unable to move source tree to ${KV_FULL}."
779         elif [[ "${OKV}" != "${KV_FULL}" ]]; then
780                 if [[ ${#OKV_ARRAY[@]} -ge 3 ]] && [[ ${KV_MAJOR} -ge 3 ]] &&
781                         [[ "${ETYPE}" = "sources" ]]; then
782                         debug-print "moving linux-${KV_MAJOR}.${KV_MINOR} to linux-${KV_FULL} "
783                         mv linux-${KV_MAJOR}.${KV_MINOR} linux-${KV_FULL} \
784                                 || die "Unable to move source tree to ${KV_FULL}."
785                 else
786                         debug-print "moving linux-${OKV} to linux-${KV_FULL} "
787                         mv linux-${OKV} linux-${KV_FULL} \
788                                 || die "Unable to move source tree to ${KV_FULL}."
789                 fi
790         elif [[ ${#OKV_ARRAY[@]} -ge 3 ]] && [[ ${KV_MAJOR} -ge 3 ]]; then
791                 mv linux-${KV_MAJOR}.${KV_MINOR} linux-${KV_FULL} \
792                         || die "Unable to move source tree to ${KV_FULL}."
793         fi
794         cd "${S}"
795
796         # remove all backup files
797         find . -iname "*~" -exec rm {} \; 2> /dev/null
798
799 }
800
801 # @FUNCTION: unpack_set_extraversion
802 # @USAGE:
803 # @DESCRIPTION:
804 # handle EXTRAVERSION
805
806 unpack_set_extraversion() {
807         cd "${S}"
808         sed -i -e "s:^\(EXTRAVERSION =\).*:\1 ${EXTRAVERSION}:" Makefile
809         cd "${OLDPWD}"
810 }
811
812 # @FUNCTION: unpack_fix_install_path
813 # @USAGE:
814 # @DESCRIPTION:
815 # Should be done after patches have been applied
816 # Otherwise patches that modify the same area of Makefile will fail
817
818 unpack_fix_install_path() {
819         cd "${S}"
820         sed     -i -e 's:#export\tINSTALL_PATH:export\tINSTALL_PATH:' Makefile
821 }
822
823 # Compile Functions
824
825 # @FUNCTION: compile_headers
826 # @USAGE:
827 # @DESCRIPTION:
828 # header compilation
829
830 compile_headers() {
831         env_setup_xmakeopts
832
833         # if we couldnt obtain HOSTCFLAGS from the Makefile,
834         # then set it to something sane
835         local HOSTCFLAGS=$(getfilevar HOSTCFLAGS "${S}"/Makefile)
836         HOSTCFLAGS=${HOSTCFLAGS:--Wall -Wstrict-prototypes -O2 -fomit-frame-pointer}
837
838         if kernel_is 2 4; then
839                 yes "" | make oldconfig ${xmakeopts}
840                 echo ">>> make oldconfig complete"
841                 make dep ${xmakeopts}
842         elif kernel_is 2 6; then
843                 # 2.6.18 introduces headers_install which means we dont need any
844                 # of this crap anymore :D
845                 kernel_is ge 2 6 18 && return 0
846
847                 # autoconf.h isnt generated unless it already exists. plus, we have
848                 # no guarantee that any headers are installed on the system...
849                 [[ -f ${EROOT}usr/include/linux/autoconf.h ]] \
850                         || touch include/linux/autoconf.h
851
852                 # if K_DEFCONFIG isn't set, force to "defconfig"
853                 # needed by mips
854                 if [[ -z ${K_DEFCONFIG} ]]; then
855                         if kernel_is ge 2 6 16 ; then
856                                 case ${CTARGET} in
857                                         powerpc64*)     K_DEFCONFIG="ppc64_defconfig";;
858                                         powerpc*)       K_DEFCONFIG="pmac32_defconfig";;
859                                         *)                      K_DEFCONFIG="defconfig";;
860                                 esac
861                         else
862                                 K_DEFCONFIG="defconfig"
863                         fi
864                 fi
865
866                 # if there arent any installed headers, then there also isnt an asm
867                 # symlink in /usr/include/, and make defconfig will fail, so we have
868                 # to force an include path with $S.
869                 HOSTCFLAGS="${HOSTCFLAGS} -I${S}/include/"
870                 ln -sf asm-${KARCH} "${S}"/include/asm || die
871                 cross_pre_c_headers && return 0
872
873                 make ${K_DEFCONFIG} HOSTCFLAGS="${HOSTCFLAGS}" ${xmakeopts} || die "defconfig failed (${K_DEFCONFIG})"
874                 if compile_headers_tweak_config ; then
875                         yes "" | make oldconfig HOSTCFLAGS="${HOSTCFLAGS}" ${xmakeopts} || die "2nd oldconfig failed"
876                 fi
877                 make prepare HOSTCFLAGS="${HOSTCFLAGS}" ${xmakeopts} || die "prepare failed"
878                 make prepare-all HOSTCFLAGS="${HOSTCFLAGS}" ${xmakeopts} || die "prepare failed"
879         fi
880 }
881
882 # @FUNCTION: compile_headers_tweak_config
883 # @USAGE:
884 # @DESCRIPTION:
885 # some targets can be very very picky, so let's finesse the
886 # .config based upon any info we may have
887
888 compile_headers_tweak_config() {
889         case ${CTARGET} in
890         sh*)
891                 sed -i '/CONFIG_CPU_SH/d' .config || die
892                 echo "CONFIG_CPU_SH${CTARGET:2:1}=y" >> .config
893                 return 0;;
894         esac
895
896         # no changes, so lets do nothing
897         return 1
898 }
899
900 # install functions
901
902 # @FUNCTION: install_universal
903 # @USAGE:
904 # @DESCRIPTION:
905 # Fix permissions in tarball
906
907 install_universal() {
908         cd "${WORKDIR}"
909         chown -R 0:0 * >& /dev/null
910         chmod -R a+r-w+X,u+w *
911         cd ${OLDPWD}
912 }
913
914 # @FUNCTION: install_headers
915 # @USAGE:
916 # @DESCRIPTION:
917 # Install headers
918
919 install_headers() {
920         local ddir=$(kernel_header_destdir)
921
922         # 2.6.18 introduces headers_install which means we dont need any
923         # of this crap anymore :D
924         if kernel_is ge 2 6 18 ; then
925                 env_setup_xmakeopts
926                 emake headers_install INSTALL_HDR_PATH="${ED}"${ddir}/.. ${xmakeopts} || die
927
928                 # let other packages install some of these headers
929                 rm -rf "${ED}"${ddir}/scsi || die #glibc/uclibc/etc...
930                 return 0
931         fi
932
933         # Do not use "linux/*" as that can cause problems with very long
934         # $S values where the cmdline to cp is too long
935         pushd "${S}" >/dev/null
936         dodir ${ddir}/linux
937         cp -pPR "${S}"/include/linux "${ED}"${ddir}/ || die
938         rm -rf "${ED}"${ddir}/linux/modules || die
939
940         dodir ${ddir}/asm
941         cp -pPR "${S}"/include/asm/* "${ED}"${ddir}/asm || die
942
943         if kernel_is 2 6 ; then
944                 dodir ${ddir}/asm-generic
945                 cp -pPR "${S}"/include/asm-generic/* "${ED}"${ddir}/asm-generic || die
946         fi
947
948         # clean up
949         find "${D}" -name '*.orig' -exec rm -f {} \;
950
951         popd >/dev/null
952 }
953
954 # @FUNCTION: install_sources
955 # @USAGE:
956 # @DESCRIPTION:
957 # Install sources
958
959 install_sources() {
960         local file
961
962         cd "${S}"
963         dodir /usr/src
964         echo ">>> Copying sources ..."
965
966         file="$(find ${WORKDIR} -iname "docs" -type d)"
967         if [[ -n ${file} ]]; then
968                 for file in $(find ${file} -type f); do
969                         echo "${file//*docs\/}" >> "${S}"/patches.txt
970                         echo "===================================================" >> "${S}"/patches.txt
971                         cat ${file} >> "${S}"/patches.txt
972                         echo "===================================================" >> "${S}"/patches.txt
973                         echo "" >> "${S}"/patches.txt
974                 done
975         fi
976
977         mv "${WORKDIR}"/linux* "${ED}"usr/src || die
978
979         if [[ -n "${UNIPATCH_DOCS}" ]] ; then
980                 for i in ${UNIPATCH_DOCS}; do
981                         dodoc "${T}"/${i}
982                 done
983         fi
984 }
985
986 # @FUNCTION: preinst_headers
987 # @USAGE:
988 # @DESCRIPTION:
989 # Headers preinst steps
990
991 preinst_headers() {
992         local ddir=$(kernel_header_destdir)
993         [[ -L ${EPREFIX}${ddir}/linux ]] && { rm "${EPREFIX}"${ddir}/linux || die; }
994         [[ -L ${EPREFIX}${ddir}/asm ]] && { rm "${EPREFIX}"${ddir}/asm || die; }
995 }
996
997 # @FUNCTION: postinst_sources
998 # @USAGE:
999 # @DESCRIPTION:
1000 # Sources post installation function.
1001 # see inline comments
1002
1003 postinst_sources() {
1004         local MAKELINK=0
1005
1006         # if we have USE=symlink, then force K_SYMLINK=1
1007         use symlink && K_SYMLINK=1
1008
1009         # We do support security on a deblobbed kernel, bug #555878.
1010         # If some particular kernel version doesn't have security
1011         # supported because of USE=deblob or otherwise, one can still
1012         # set K_SECURITY_UNSUPPORTED on a per ebuild basis.
1013         #[[ $K_DEBLOB_AVAILABLE == 1 ]] && \
1014         #       use deblob && \
1015         #       K_SECURITY_UNSUPPORTED=deblob
1016
1017         # if we are to forcably symlink, delete it if it already exists first.
1018         if [[ ${K_SYMLINK} -gt 0 ]]; then
1019                 [[ -h ${EROOT}usr/src/linux ]] && { rm "${EROOT}"usr/src/linux || die; }
1020                 MAKELINK=1
1021         fi
1022
1023         # if the link doesnt exist, lets create it
1024         [[ ! -h ${EROOT}usr/src/linux ]] && MAKELINK=1
1025
1026         if [[ ${MAKELINK} == 1 ]]; then
1027                 ln -sf linux-${KV_FULL} "${EROOT}"usr/src/linux || die
1028         fi
1029
1030         # Don't forget to make directory for sysfs
1031         [[ ! -d ${EROOT}sys ]] && kernel_is 2 6 && { mkdir "${EROOT}"sys || die ; }
1032
1033         echo
1034         elog "If you are upgrading from a previous kernel, you may be interested"
1035         elog "in the following document:"
1036         elog "  - General upgrade guide: https://wiki.gentoo.org/wiki/Kernel/Upgrade"
1037         echo
1038
1039         # if K_EXTRAEINFO is set then lets display it now
1040         if [[ -n ${K_EXTRAEINFO} ]]; then
1041                 echo ${K_EXTRAEINFO} | fmt |
1042                 while read -s ELINE; do einfo "${ELINE}"; done
1043         fi
1044
1045         # if K_EXTRAELOG is set then lets display it now
1046         if [[ -n ${K_EXTRAELOG} ]]; then
1047                 echo ${K_EXTRAELOG} | fmt |
1048                 while read -s ELINE; do elog "${ELINE}"; done
1049         fi
1050
1051         # if K_EXTRAEWARN is set then lets display it now
1052         if [[ -n ${K_EXTRAEWARN} ]]; then
1053                 echo ${K_EXTRAEWARN} | fmt |
1054                 while read -s ELINE; do ewarn "${ELINE}"; done
1055         fi
1056
1057         # optionally display security unsupported message
1058         #  Start with why
1059         if [[ -n ${K_SECURITY_UNSUPPORTED} ]]; then
1060                 ewarn "${PN} is UNSUPPORTED by Gentoo Security."
1061         fi
1062         #  And now the general message.
1063         if [[ -n ${K_SECURITY_UNSUPPORTED} ]]; then
1064                 ewarn "This means that it is likely to be vulnerable to recent security issues."
1065                 echo
1066                 ewarn "Upstream kernel developers recommend always running the latest "
1067                 ewarn "release of any current long term supported Linux kernel version."
1068                 ewarn "To see a list of these versions, their most current release and "
1069                 ewarn "long term support status, please go to https://www.kernel.org ."
1070                 echo
1071                 ewarn "For specific information on why this kernel is unsupported, please read:"
1072                 ewarn "https://wiki.gentoo.org/wiki/Project:Kernel_Security"
1073         fi
1074
1075         # warn sparc users that they need to do cross-compiling with >= 2.6.25(bug #214765)
1076         KV_MAJOR=$(ver_cut 1 ${OKV})
1077         KV_MINOR=$(ver_cut 2 ${OKV})
1078         KV_PATCH=$(ver_cut 3 ${OKV})
1079         if [[ "$(tc-arch)" = "sparc" ]]; then
1080                 if [[ $(gcc-major-version) -lt 4 && $(gcc-minor-version) -lt 4 ]]; then
1081                         if [[ ${KV_MAJOR} -ge 3 ]] || ver_test ${KV_MAJOR}.${KV_MINOR}.${KV_PATCH} -gt 2.6.24 ; then
1082                                 echo
1083                                 elog "NOTE: Since 2.6.25 the kernel Makefile has changed in a way that"
1084                                 elog "you now need to do"
1085                                 elog "  make CROSS_COMPILE=sparc64-unknown-linux-gnu-"
1086                                 elog "instead of just"
1087                                 elog "  make"
1088                                 elog "to compile the kernel. For more information please browse to"
1089                                 elog "https://bugs.gentoo.org/show_bug.cgi?id=214765"
1090                                 echo
1091                         fi
1092                 fi
1093         fi
1094 }
1095
1096 # pkg_setup functions
1097
1098 # @FUNCTION: setup_headers
1099 # @USAGE:
1100 # @DESCRIPTION:
1101 # Determine if ${PN} supports arch
1102
1103 setup_headers() {
1104         [[ -z ${H_SUPPORTEDARCH} ]] && H_SUPPORTEDARCH=${PN/-*/}
1105         for i in ${H_SUPPORTEDARCH}; do
1106                 [[ $(tc-arch) == "${i}" ]] && H_ACCEPT_ARCH="yes"
1107         done
1108
1109         if [[ ${H_ACCEPT_ARCH} != "yes" ]]; then
1110                 echo
1111                 eerror "This version of ${PN} does not support $(tc-arch)."
1112                 eerror "Please merge the appropriate sources, in most cases"
1113                 eerror "(but not all) this will be called $(tc-arch)-headers."
1114                 die "Package unsupported for $(tc-arch)"
1115         fi
1116 }
1117
1118 # @FUNCTION: unipatch
1119 # @USAGE: <list of patches to apply>
1120 # @DESCRIPTION:
1121 # Universal function that will apply patches to source
1122
1123 unipatch() {
1124         local i x y z extention PIPE_CMD UNIPATCH_DROP KPATCH_DIR PATCH_DEPTH ELINE
1125         local STRICT_COUNT PATCH_LEVEL myLC_ALL myLANG
1126
1127         # set to a standard locale to ensure sorts are ordered properly.
1128         myLC_ALL="${LC_ALL}"
1129         myLANG="${LANG}"
1130         LC_ALL="C"
1131         LANG=""
1132
1133         [ -z "${KPATCH_DIR}" ] && KPATCH_DIR="${WORKDIR}/patches/"
1134         [ ! -d ${KPATCH_DIR} ] && mkdir -p ${KPATCH_DIR}
1135
1136         # We're gonna need it when doing patches with a predefined patchlevel
1137         eshopts_push -s extglob
1138
1139         # This function will unpack all passed tarballs, add any passed patches,
1140         # and remove any passed patchnumbers
1141         # usage can be either via an env var or by params
1142         # although due to the nature we pass this within this eclass
1143         # it shall be by param only.
1144         # -z "${UNIPATCH_LIST}" ] && UNIPATCH_LIST="${@}"
1145         UNIPATCH_LIST="${@}"
1146
1147         #unpack any passed tarballs
1148         for i in ${UNIPATCH_LIST}; do
1149                 if echo ${i} | grep -qs -e "\.tar" -e "\.tbz" -e "\.tgz" ; then
1150                         if [ -n "${UNIPATCH_STRICTORDER}" ]; then
1151                                 unset z
1152                                 STRICT_COUNT=$((10#${STRICT_COUNT} + 1))
1153                                 for((y=0; y<$((6 - ${#STRICT_COUNT})); y++));
1154                                         do z="${z}0";
1155                                 done
1156                                 PATCH_ORDER="${z}${STRICT_COUNT}"
1157
1158                                 mkdir -p "${KPATCH_DIR}/${PATCH_ORDER}"
1159                                 pushd "${KPATCH_DIR}/${PATCH_ORDER}" >/dev/null
1160                                 unpack ${i##*/}
1161                                 popd >/dev/null
1162                         else
1163                                 pushd "${KPATCH_DIR}" >/dev/null
1164                                 unpack ${i##*/}
1165                                 popd >/dev/null
1166                         fi
1167
1168                         [[ ${i} == *:* ]] && echo ">>> Strict patch levels not currently supported for tarballed patchsets"
1169                 else
1170                         extention=${i/*./}
1171                         extention=${extention/:*/}
1172                         PIPE_CMD=""
1173                         case ${extention} in
1174                                      xz) PIPE_CMD="xz -dc";;
1175                                    lzma) PIPE_CMD="lzma -dc";;
1176                                     bz2) PIPE_CMD="bzip2 -dc";;
1177                                  patch*) PIPE_CMD="cat";;
1178                                    diff) PIPE_CMD="cat";;
1179                                  gz|Z|z) PIPE_CMD="gzip -dc";;
1180                                 ZIP|zip) PIPE_CMD="unzip -p";;
1181                                       *) UNIPATCH_DROP="${UNIPATCH_DROP} ${i/:*/}";;
1182                         esac
1183
1184                         PATCH_LEVEL=${i/*([^:])?(:)}
1185                         i=${i/:*/}
1186                         x=${i/*\//}
1187                         x=${x/\.${extention}/}
1188
1189                         if [ -n "${PIPE_CMD}" ]; then
1190                                 if [ ! -r "${i}" ]; then
1191                                         echo
1192                                         eerror "FATAL: unable to locate:"
1193                                         eerror "${i}"
1194                                         eerror "for read-only. The file either has incorrect permissions"
1195                                         eerror "or does not exist."
1196                                         die Unable to locate ${i}
1197                                 fi
1198
1199                                 if [ -n "${UNIPATCH_STRICTORDER}" ]; then
1200                                         unset z
1201                                         STRICT_COUNT=$((10#${STRICT_COUNT} + 1))
1202                                         for((y=0; y<$((6 - ${#STRICT_COUNT})); y++));
1203                                                 do z="${z}0";
1204                                         done
1205                                         PATCH_ORDER="${z}${STRICT_COUNT}"
1206
1207                                         mkdir -p ${KPATCH_DIR}/${PATCH_ORDER}/
1208                                         $(${PIPE_CMD} ${i} > ${KPATCH_DIR}/${PATCH_ORDER}/${x}.patch${PATCH_LEVEL}) || die "uncompressing patch failed"
1209                                 else
1210                                         $(${PIPE_CMD} ${i} > ${KPATCH_DIR}/${x}.patch${PATCH_LEVEL}) || die "uncompressing patch failed"
1211                                 fi
1212                         fi
1213                 fi
1214
1215                 # If experimental was not chosen by the user, drop experimental patches not in K_EXP_GENPATCHES_LIST.
1216                 if [[ "${i}" == *"genpatches-"*".experimental."* && -n ${K_EXP_GENPATCHES_PULL} ]] ; then
1217                         if [[ -z ${K_EXP_GENPATCHES_NOUSE} ]] && use experimental; then
1218                                 continue
1219                         fi
1220
1221                         local j
1222                         for j in ${KPATCH_DIR}/*/50*_*.patch*; do
1223                                 for k in ${K_EXP_GENPATCHES_LIST} ; do
1224                                         [[ "$(basename ${j})" == ${k}* ]] && continue 2
1225                                 done
1226                                 UNIPATCH_DROP+=" $(basename ${j})"
1227                         done
1228                 else
1229                         UNIPATCH_LIST_GENPATCHES+=" ${DISTDIR}/${tarball}"
1230                         debug-print "genpatches tarball: $tarball"
1231
1232                         local GCC_MAJOR_VER=$(gcc-major-version)
1233                         local GCC_MINOR_VER=$(gcc-minor-version)
1234
1235                         # optimization patch for gcc < 8.X and kernel > 4.13
1236                         if kernel_is ge 4 13 ; then 
1237                                 if [[ ${GCC_MAJOR_VER} -lt 8 ]] && [[ ${GCC_MAJOR_VER} -gt 4 ]]; then
1238                                         UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
1239                                         UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
1240                                 # optimization patch for gcc >= 8 and kernel ge 4.13
1241                                 elif [[ "${GCC_MAJOR_VER}" -eq 8 ]]; then
1242                                         # support old kernels for a period. For now, remove as all gcc versions required are masked
1243                                         UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
1244                                         UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
1245                                         UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
1246                                 elif [[ "${GCC_MAJOR_VER}" -ge 9 ]] && [[ ${GCC_MINOR_VER} -ge 1 ]]; then
1247                                         UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
1248                                         UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
1249                                         UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
1250                                 else
1251                                         UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
1252                                         UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
1253                                         UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
1254                                         UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
1255                                 fi
1256                         else
1257                                 UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
1258                                 UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
1259                                 UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
1260                                 UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
1261                         fi
1262                 fi
1263         done
1264
1265         #populate KPATCH_DIRS so we know where to look to remove the excludes
1266         x=${KPATCH_DIR}
1267         KPATCH_DIR=""
1268         for i in $(find ${x} -type d | sort -n); do
1269                 KPATCH_DIR="${KPATCH_DIR} ${i}"
1270         done
1271
1272         # do not apply fbcondecor patch to sparc/sparc64 as it breaks boot
1273         # bug #272676
1274         if [[ "$(tc-arch)" = "sparc" || "$(tc-arch)" = "sparc64" ]]; then
1275                 if [[ ${KV_MAJOR} -ge 3 ]] || ver_test ${KV_MAJOR}.${KV_MINOR}.${KV_PATCH} -gt 2.6.28 ; then
1276                         if [[ ! -z ${K_WANT_GENPATCHES} ]] ; then
1277                                 UNIPATCH_DROP="${UNIPATCH_DROP} *_fbcondecor*.patch"
1278                                 echo
1279                                 ewarn "fbcondecor currently prevents sparc/sparc64 from booting"
1280                                 ewarn "for kernel versions >= 2.6.29. Removing fbcondecor patch."
1281                                 ewarn "See https://bugs.gentoo.org/show_bug.cgi?id=272676 for details"
1282                                 echo
1283                         fi
1284                 fi
1285         fi
1286
1287         #so now lets get rid of the patchno's we want to exclude
1288         UNIPATCH_DROP="${UNIPATCH_EXCLUDE} ${UNIPATCH_DROP}"
1289         for i in ${UNIPATCH_DROP}; do
1290                 ebegin "Excluding Patch #${i}"
1291                 for x in ${KPATCH_DIR}; do rm -f ${x}/${i}* 2>/dev/null; done
1292                 eend $?
1293         done
1294
1295         # and now, finally, we patch it :)
1296         for x in ${KPATCH_DIR}; do
1297                 for i in $(find ${x} -maxdepth 1 -iname "*.patch*" -or -iname "*.diff*" | sort -n); do
1298                         STDERR_T="${T}/${i/*\//}"
1299                         STDERR_T="${STDERR_T/.patch*/.err}"
1300
1301                         [ -z ${i/*.patch*/} ] && PATCH_DEPTH=${i/*.patch/}
1302                         #[ -z ${i/*.diff*/} ]  && PATCH_DEPTH=${i/*.diff/}
1303
1304                         if [ -z "${PATCH_DEPTH}" ]; then PATCH_DEPTH=0; fi
1305
1306                         ####################################################################
1307                         # IMPORTANT: This is temporary code to support Linux git 3.15_rc1! #
1308                         #                                                                  #
1309                         # The patch contains a removal of a symlink, followed by addition  #
1310                         # of a file with the same name as the symlink in the same          #
1311                         # location; this causes the dry-run to fail, filed bug #507656.    #
1312                         #                                                                  #
1313                         # https://bugs.gentoo.org/show_bug.cgi?id=507656                   #
1314                         ####################################################################
1315                         if [[ -n ${K_FROM_GIT} ]] ; then
1316                                 if [[ ${KV_MAJOR} -gt 3 || ( ${KV_MAJOR} -eq 3 && ${KV_PATCH} -gt 15 ) &&
1317                                         ${RELEASETYPE} == -rc ]] ; then
1318                                         ebegin "Applying ${i/*\//} (-p1)"
1319                                         if [ $(patch -p1 --no-backup-if-mismatch -f < ${i} >> ${STDERR_T}) "$?" -le 2 ]; then
1320                                                 eend 0
1321                                                 rm ${STDERR_T} || die
1322                                                 break
1323                                         else
1324                                                 eend 1
1325                                                 eerror "Failed to apply patch ${i/*\//}"
1326                                                 eerror "Please attach ${STDERR_T} to any bug you may post."
1327                                                 eshopts_pop
1328                                                 die "Failed to apply ${i/*\//} on patch depth 1."
1329                                         fi
1330                                 fi
1331                         fi
1332                         ####################################################################
1333
1334                         while [ ${PATCH_DEPTH} -lt 5 ]; do
1335                                 echo "Attempting Dry-run:" >> ${STDERR_T}
1336                                 echo "cmd: patch -p${PATCH_DEPTH} --no-backup-if-mismatch --dry-run -f < ${i}" >> ${STDERR_T}
1337                                 echo "=======================================================" >> ${STDERR_T}
1338                                 if [ $(patch -p${PATCH_DEPTH} --no-backup-if-mismatch --dry-run -f < ${i} >> ${STDERR_T}) $? -eq 0 ]; then
1339                                         ebegin "Applying ${i/*\//} (-p${PATCH_DEPTH})"
1340                                         echo "Attempting patch:" > ${STDERR_T}
1341                                         echo "cmd: patch -p${PATCH_DEPTH} --no-backup-if-mismatch -f < ${i}" >> ${STDERR_T}
1342                                         echo "=======================================================" >> ${STDERR_T}
1343                                         if [ $(patch -p${PATCH_DEPTH} --no-backup-if-mismatch -f < ${i} >> ${STDERR_T}) "$?" -eq 0 ]; then
1344                                                 eend 0
1345                                                 rm ${STDERR_T} || die
1346                                                 break
1347                                         else
1348                                                 eend 1
1349                                                 eerror "Failed to apply patch ${i/*\//}"
1350                                                 eerror "Please attach ${STDERR_T} to any bug you may post."
1351                                                 eshopts_pop
1352                                                 die "Failed to apply ${i/*\//} on patch depth ${PATCH_DEPTH}."
1353                                         fi
1354                                 else
1355                                         PATCH_DEPTH=$((${PATCH_DEPTH} + 1))
1356                                 fi
1357                         done
1358                         if [ ${PATCH_DEPTH} -eq 5 ]; then
1359                                 eerror "Failed to dry-run patch ${i/*\//}"
1360                                 eerror "Please attach ${STDERR_T} to any bug you may post."
1361                                 eshopts_pop
1362                                 die "Unable to dry-run patch on any patch depth lower than 5."
1363                         fi
1364                 done
1365         done
1366
1367         # When genpatches is used, we want to install 0000_README which documents
1368         # the patches that were used; such that the user can see them, bug #301478.
1369         if [[ ! -z ${K_WANT_GENPATCHES} ]] ; then
1370                 UNIPATCH_DOCS="${UNIPATCH_DOCS} 0000_README"
1371         fi
1372
1373         # When files listed in UNIPATCH_DOCS are found in KPATCH_DIR's, we copy it
1374         # to the temporary directory and remember them in UNIPATCH_DOCS to install
1375         # them during the install phase.
1376         local tmp
1377         for x in ${KPATCH_DIR}; do
1378                 for i in ${UNIPATCH_DOCS}; do
1379                         if [[ -f ${x}/${i} ]] ; then
1380                                 tmp="${tmp} ${i}"
1381                                 cp -f "${x}/${i}" "${T}"/ || die
1382                         fi
1383                 done
1384         done
1385         UNIPATCH_DOCS="${tmp}"
1386
1387         # clean up  KPATCH_DIR's - fixes bug #53610
1388         for x in ${KPATCH_DIR}; do rm -Rf ${x}; done
1389
1390         LC_ALL="${myLC_ALL}"
1391         LANG="${myLANG}"
1392         eshopts_pop
1393 }
1394
1395 # @FUNCTION: getfilevar
1396 # @USAGE: <variable> <configfile>
1397 # @DESCRIPTION:
1398 # pulled from linux-info
1399
1400 getfilevar() {
1401         local workingdir basefname basedname xarch=$(tc-arch-kernel)
1402
1403         if [[ -z ${1} ]] && [[ ! -f ${2} ]]; then
1404                 echo -e "\n"
1405                 eerror "getfilevar requires 2 variables, with the second a valid file."
1406                 eerror "   getfilevar <VARIABLE> <CONFIGFILE>"
1407         else
1408                 workingdir=${PWD}
1409                 basefname=$(basename ${2})
1410                 basedname=$(dirname ${2})
1411                 unset ARCH
1412
1413                 cd ${basedname}
1414                 echo -e "include ${basefname}\ne:\n\t@echo \$(${1})" | \
1415                         make ${BUILD_FIXES} -s -f - e 2>/dev/null
1416                 cd ${workingdir}
1417
1418                 ARCH=${xarch}
1419         fi
1420 }
1421
1422 # @FUNCTION: detect_arch
1423 # @USAGE:
1424 # @DESCRIPTION:
1425 # This function sets ARCH_URI and ARCH_PATCH
1426 # with the neccessary info for the arch sepecific compatibility
1427 # patchsets.
1428
1429 detect_arch() {
1430
1431         local ALL_ARCH LOOP_ARCH LOOP_ARCH_L COMPAT_URI i TC_ARCH_KERNEL
1432
1433         # COMPAT_URI is the contents of ${ARCH}_URI
1434         # ARCH_URI is the URI for all the ${ARCH}_URI patches
1435         # ARCH_PATCH is ARCH_URI broken into files for UNIPATCH
1436
1437         ARCH_URI=""
1438         ARCH_PATCH=""
1439         TC_ARCH_KERNEL=""
1440         ALL_ARCH="ALPHA AMD64 ARM HPPA IA64 M68K MIPS PPC PPC64 S390 SH SPARC X86"
1441
1442         for LOOP_ARCH in ${ALL_ARCH}; do
1443                 COMPAT_URI="${LOOP_ARCH}_URI"
1444                 COMPAT_URI="${!COMPAT_URI}"
1445
1446                 declare -l LOOP_ARCH_L=${LOOP_ARCH}
1447
1448                 [[ -n ${COMPAT_URI} ]] && \
1449                         ARCH_URI="${ARCH_URI} ${LOOP_ARCH_L}? ( ${COMPAT_URI} )"
1450
1451                 declare -u TC_ARCH_KERNEL=$(tc-arch-kernel)
1452                 if [[ ${LOOP_ARCH} == ${TC_ARCH_KERNEL} ]]; then
1453                         for i in ${COMPAT_URI}; do
1454                                 ARCH_PATCH="${ARCH_PATCH} ${DISTDIR}/${i/*\//}"
1455                         done
1456                 fi
1457
1458         done
1459 }
1460
1461 # @FUNCTION: headers___fix
1462 # @USAGE:
1463 # @DESCRIPTION:
1464 # Voodoo to partially fix broken upstream headers.
1465 # note: do not put inline/asm/volatile together (breaks "inline asm volatile")
1466
1467 headers___fix() {
1468         sed -i \
1469                 -e '/^\#define.*_TYPES_H/{:loop n; bloop}' \
1470                 -e 's:\<\([us]\(8\|16\|32\|64\)\)\>:__\1:g' \
1471                 -e "s/\([[:space:]]\)inline\([[:space:](]\)/\1__inline__\2/g" \
1472                 -e "s/\([[:space:]]\)asm\([[:space:](]\)/\1__asm__\2/g" \
1473                 -e "s/\([[:space:]]\)volatile\([[:space:](]\)/\1__volatile__\2/g" \
1474                 "$@"
1475 }
1476
1477 # @FUNCTION: kernel-2_src_unpack
1478 # @USAGE:
1479 # @DESCRIPTION:
1480 # unpack sources, handle genpatches, deblob
1481
1482 kernel-2_src_unpack() {
1483         universal_unpack
1484         debug-print "Doing unipatch"
1485
1486         # request UNIPATCH_LIST_GENPATCHES in phase since it calls 'use'
1487         handle_genpatches --set-unipatch-list
1488         [[ -n ${UNIPATCH_LIST} || -n ${UNIPATCH_LIST_DEFAULT} || -n ${UNIPATCH_LIST_GENPATCHES} ]] && \
1489                 unipatch "${UNIPATCH_LIST_DEFAULT} ${UNIPATCH_LIST_GENPATCHES} ${UNIPATCH_LIST}"
1490
1491         debug-print "Doing premake"
1492
1493         # allow ebuilds to massage the source tree after patching but before
1494         # we run misc `make` functions below
1495         [[ $(type -t kernel-2_hook_premake) == "function" ]] && kernel-2_hook_premake
1496
1497         debug-print "Doing unpack_set_extraversion"
1498
1499         [[ -z ${K_NOSETEXTRAVERSION} ]] && unpack_set_extraversion
1500         unpack_fix_install_path
1501
1502         # Setup xmakeopts and cd into sourcetree.
1503         env_setup_xmakeopts
1504         cd "${S}"
1505
1506         # We dont need a version.h for anything other than headers
1507         # at least, I should hope we dont. If this causes problems
1508         # take out the if/fi block and inform me please.
1509         # unpack_2_6 should now be 2.6.17 safe anyways
1510         if [[ ${ETYPE} == headers ]]; then
1511                 kernel_is 2 4 && unpack_2_4
1512                 kernel_is 2 6 && unpack_2_6
1513         fi
1514
1515         if [[ $K_DEBLOB_AVAILABLE == 1 ]] && use deblob ; then
1516                 cp "${DISTDIR}/${DEBLOB_A}" "${T}" || die "cp ${DEBLOB_A} failed"
1517                 cp "${DISTDIR}/${DEBLOB_CHECK_A}" "${T}/deblob-check" || die "cp ${DEBLOB_CHECK_A} failed"
1518                 chmod +x "${T}/${DEBLOB_A}" "${T}/deblob-check" || die "chmod deblob scripts failed"
1519         fi
1520
1521         # fix a problem on ppc where TOUT writes to /usr/src/linux breaking sandbox
1522         # only do this for kernel < 2.6.27 since this file does not exist in later
1523         # kernels
1524         if [[ -n ${KV_MINOR} ]] && ver_test ${KV_MAJOR}.${KV_MINOR}.${KV_PATCH} -lt 2.6.27 ; then
1525                 sed -i \
1526                         -e 's|TOUT      := .tmp_gas_check|TOUT  := $(T).tmp_gas_check|' \
1527                         "${S}"/arch/ppc/Makefile
1528         else
1529                 sed -i \
1530                         -e 's|TOUT      := .tmp_gas_check|TOUT  := $(T).tmp_gas_check|' \
1531                         "${S}"/arch/powerpc/Makefile
1532         fi
1533 }
1534
1535 # @FUNCTION: kernel-2_src_prepare
1536 # @USAGE:
1537 # @DESCRIPTION:
1538 # Apply any user patches
1539
1540 kernel-2_src_prepare() {
1541
1542         debug-print "Applying any user patches"
1543
1544         # apply any user patches
1545         case ${EAPI:-0} in
1546                 0|1|2|3|4|5) epatch_user ;;
1547                 6) eapply_user ;;
1548         esac
1549 }
1550
1551 # @FUNCTION: kernel-2_src_compile
1552 # @USAGE:
1553 # @DESCRIPTION:
1554 # conpile headers or run deblob script
1555
1556 kernel-2_src_compile() {
1557         cd "${S}"
1558         [[ ${ETYPE} == headers ]] && compile_headers
1559
1560         if [[ $K_DEBLOB_AVAILABLE == 1 ]] && use deblob ; then
1561                 echo ">>> Running deblob script ..."
1562                 python_setup
1563                 sh "${T}/${DEBLOB_A}" --force || die "Deblob script failed to run!!!"
1564         fi
1565 }
1566
1567 # @FUNCTION: kernel-2_src_test
1568 # @USAGE:
1569 # @DESCRIPTION:
1570 # if you leave it to the default src_test, it will run make to
1571 # find whether test/check targets are present; since "make test"
1572 # actually produces a few support files, they are installed even
1573 # though the package is binchecks-restricted.
1574 #
1575 # Avoid this altogether by making the function moot.
1576 kernel-2_src_test() { :; }
1577
1578 # @FUNCTION: kernel-2_pkg_preinst
1579 # @DESCRIPTION:
1580 # if ETYPE = headers, call preinst_headers
1581
1582 kernel-2_pkg_preinst() {
1583         [[ ${ETYPE} == headers ]] && preinst_headers
1584 }
1585
1586 # @FUNCTION: kernel-2_src_install
1587 # @USAGE:
1588 # @DESCRIPTION:
1589 # Install headers or sources dependant on ETYPE
1590
1591 kernel-2_src_install() {
1592         install_universal
1593         [[ ${ETYPE} == headers ]] && install_headers
1594         [[ ${ETYPE} == sources ]] && install_sources
1595 }
1596
1597 # @FUNCTION: kernel-2_pkg_postinst
1598 # @USAGE:
1599 # @DESCRIPTION:
1600 # call postinst_sources for ETYPE = sources
1601
1602 kernel-2_pkg_postinst() {
1603         [[ ${ETYPE} == sources ]] && postinst_sources
1604 }
1605
1606 # @FUNCTION: kernel-2_pkg_setup
1607 # @USAGE:
1608 # @DESCRIPTION:
1609 # check for supported kernel version, die if ETYPE is unknown, call setup_headers
1610 # if necessary
1611
1612 kernel-2_pkg_setup() {
1613         if kernel_is 2 4; then
1614                 if [[ $(gcc-major-version) -ge 4 ]] ; then
1615                         echo
1616                         ewarn "Be warned !! >=sys-devel/gcc-4.0.0 isn't supported with linux-2.4!"
1617                         ewarn "Either switch to another gcc-version (via gcc-config) or use a"
1618                         ewarn "newer kernel that supports gcc-4."
1619                         echo
1620                         ewarn "Also be aware that bugreports about gcc-4 not working"
1621                         ewarn "with linux-2.4 based ebuilds will be closed as INVALID!"
1622                         echo
1623                 fi
1624         fi
1625
1626         ABI="${KERNEL_ABI}"
1627         if [[ ${ETYPE} != sources ]] && [[ ${ETYPE} != headers ]]; then
1628                 eerror "Unknown ETYPE=\"${ETYPE}\", must be \"sources\" or \"headers\""
1629                 die "Unknown ETYPE=\"${ETYPE}\", must be \"sources\" or \"headers\""
1630         fi
1631
1632         [[ ${ETYPE} == headers ]] && setup_headers
1633         [[ ${ETYPE} == sources ]] && echo ">>> Preparing to unpack ..."
1634 }
1635
1636 # @FUNCTION: kernel-2_pkg_postrm
1637 # @USAGE:
1638 # @DESCRIPTION:
1639 # Notify the user that after a depclean, there may be sources
1640 # left behind that need to be manually cleaned
1641
1642 kernel-2_pkg_postrm() {
1643         # This warning only makes sense for kernel sources.
1644         [[ ${ETYPE} == headers ]] && return 0
1645
1646         # If there isn't anything left behind, then don't complain.
1647         [[ -e ${EROOT}usr/src/linux-${KV_FULL} ]] || return 0
1648         echo
1649         ewarn "Note: Even though you have successfully unmerged "
1650         ewarn "your kernel package, directories in kernel source location: "
1651         ewarn "${EROOT}usr/src/linux-${KV_FULL}"
1652         ewarn "with modified files will remain behind. By design, package managers"
1653         ewarn "will not remove these modified files and the directories they reside in."
1654         echo
1655         ewarn "For more detailed kernel removal instructions, please see: "
1656         ewarn "https://wiki.gentoo.org/wiki/Kernel/Removal"
1657         echo
1658 }