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