summaryrefslogtreecommitdiff
blob: ffbe986f3e87b24632086d5377da1a7b5aafa37a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: kernel-2.eclass
# @MAINTAINER:
# Gentoo Kernel project <kernel@gentoo.org>
# @AUTHOR:
# John Mylchreest <johnm@gentoo.org>
# Mike Pagano <mpagano@gentoo.org>
# <so many, many others, please add yourself>
# @SUPPORTED_EAPIS: 7 8
# @BLURB: Eclass for kernel packages
# @DESCRIPTION:
# This is the kernel.eclass rewrite for a clean base regarding the 2.6
# series of kernel with back-compatibility for 2.4
# Please direct your bugs to the current eclass maintainer :)
# added functionality:
# unipatch		- a flexible, singular method to extract, add and remove patches.

# @ECLASS_VARIABLE: CTARGET
# @INTERNAL
# @DESCRIPTION:
# Utilized for 32-bit userland on ppc64.

# @ECLASS_VARIABLE: CKV
# @DEFAULT_UNSET
# @DESCRIPTION:
# Used as a comparison kernel version, which is used when
# PV doesn't reflect the genuine kernel version.
# This gets set to the portage style versioning. ie:
# CKV=2.6.11_rc4

# @ECLASS_VARIABLE: EXTRAVERSION
# @DEFAULT_UNSET
# @DESCRIPTION:
# The additional version appended to OKV (-gentoo/-gentoo-r1)

# @ECLASS_VARIABLE: H_SUPPORTEDARCH
# @DEFAULT_UNSET
# @DESCRIPTION:
# this should be a space separated list of ARCH's which
# can be supported by the headers ebuild

# @ECLASS_VARIABLE: K_BASE_VER
# @DEFAULT_UNSET
# @DESCRIPTION:
# for git-sources, declare the base version this patch is
# based off of.

# @ECLASS_VARIABLE: K_DEBLOB_AVAILABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# A value of "0" will disable all of the optional deblob
# code. If empty, will be set to "1" if deblobbing is
# possible. Test ONLY for "1".

# @ECLASS_VARIABLE: K_DEBLOB_TAG
# @DEFAULT_UNSET
# @DESCRIPTION:
# This will be the version of deblob script. It's a upstream SVN tag
# such asw -gnu or -gnu1.

# @ECLASS_VARIABLE: K_DEFCONFIG
# @DEFAULT_UNSET
# @DESCRIPTION:
# Allow specifying a different defconfig target.
# If length zero, defaults to "defconfig".

# @ECLASS_VARIABLE: K_EXP_GENPATCHES_PULL
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set, we pull "experimental" regardless of the USE FLAG
# but expect the ebuild maintainer to use K_EXP_GENPATCHES_LIST.

# @ECLASS_VARIABLE: K_EXP_GENPATCHES_NOUSE
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set, no USE flag will be provided for "experimental";
# as a result the user cannot choose to apply those patches.

# @ECLASS_VARIABLE: K_EXP_GENPATCHES_LIST
# @DEFAULT_UNSET
# @DESCRIPTION:
# A list of patches to pick from "experimental" to apply when
# the USE flag is unset and K_EXP_GENPATCHES_PULL is set.

# @ECLASS_VARIABLE: K_EXTRAEINFO
# @DEFAULT_UNSET
# @DESCRIPTION:
# this is a new-line separated list of einfo displays in
# postinst and can be used to carry additional postinst
# messages

# @ECLASS_VARIABLE: K_EXTRAELOG
# @DEFAULT_UNSET
# @DESCRIPTION:
# same as K_EXTRAEINFO except using elog instead of einfo

# @ECLASS_VARIABLE: K_EXTRAEWARN
# @DEFAULT_UNSET
# @DESCRIPTION:
# same as K_EXTRAEINFO except using ewarn instead of einfo

# @ECLASS_VARIABLE: K_FROM_GIT
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set, this variable signals that the kernel sources derives
# from a git tree and special handling will be applied so that
# any patches that are applied will actually apply.

# @ECLASS_VARIABLE: K_GENPATCHES_VER
# @DEFAULT_UNSET
# @DESCRIPTION:
# The version of the genpatches tarball(s) to apply.
# A value of "5" would apply genpatches-2.6.12-5 to
# my-sources-2.6.12.ebuild

# @ECLASS_VARIABLE: K_LONGTERM
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set, the eclass will search for the kernel source
# in the long term directories on the upstream servers
# as the location has been changed by upstream

# @ECLASS_VARIABLE: K_NODRYRUN
# @DEFAULT_UNSET
# @DESCRIPTION:
# if this is set then patch --dry-run will not
# be run. Certain patches will fail with this parameter
# See bug #507656

# @ECLASS_VARIABLE: K_NOSETEXTRAVERSION
# @DEFAULT_UNSET
# @DESCRIPTION:
# if this is set then EXTRAVERSION will not be
# automatically set within the kernel Makefile

# @ECLASS_VARIABLE: K_NOUSENAME
# @DEFAULT_UNSET
# @DESCRIPTION:
# if this is set then EXTRAVERSION will not include the
# first part of ${PN} in EXTRAVERSION

# @ECLASS_VARIABLE: K_NOUSEPR
# @DEFAULT_UNSET
# @DESCRIPTION:
# if this is set then EXTRAVERSION will not include the
# anything based on ${PR}.

# @ECLASS_VARIABLE: K_PREDEBLOBBED
# @DEFAULT_UNSET
# @DESCRIPTION:
# This kernel was already deblobbed elsewhere.
# If false, either optional deblobbing will be available
# or the license will note the inclusion of linux-firmware code.

# @ECLASS_VARIABLE: K_PREPATCHED
# @DEFAULT_UNSET
# @DESCRIPTION:
# if the patchset is prepatched (ie: pf-sources,
# zen-sources etc) it will use PR (ie: -r5) as the
# patchset version for and not use it as a true package
# revision

# @ECLASS_VARIABLE: K_SECURITY_UNSUPPORTED
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set, this kernel is unsupported by Gentoo Security
# to the current eclass maintainer :)

# @ECLASS_VARIABLE: K_SYMLINK
# @DEFAULT_UNSET
# @DESCRIPTION:
# if this is set, then forcibly create symlink anyway

# @ECLASS_VARIABLE: K_USEPV
# @DEFAULT_UNSET
# @DESCRIPTION:
# When setting the EXTRAVERSION variable, it should
# add PV to the end.
# this is useful for things like wolk. IE:
# EXTRAVERSION would be something like : -wolk-4.19-r1

# @ECLASS_VARIABLE: K_WANT_GENPATCHES
# @DEFAULT_UNSET
# @DESCRIPTION:
# Apply genpatches to kernel source. Provide any
# combination of "base", "extras" or "experimental".

# @ECLASS_VARIABLE: KERNEL_URI
# @DEFAULT_UNSET
# @DESCRIPTION:
# Upstream kernel src URI

# @ECLASS_VARIABLE: KV
# @DEFAULT_UNSET
# @OUTPUT_VARIABLE
# @DESCRIPTION:
# Kernel Version (2.6.0-gentoo/2.6.0-test11-gentoo-r1)

# @ECLASS_VARIABLE: KV_FULL
# @DEFAULT_UNSET
# @OUTPUT_VARIABLE
# @DESCRIPTION:
# Kernel full version

# @ECLASS_VARIABLE: KV_MAJOR
# @DEFAULT_UNSET
# @OUTPUT_VARIABLE
# @DESCRIPTION:
# Kernel major version from <KV_MAJOR>.<KV_MINOR>.<KV_PATCH

# @ECLASS_VARIABLE: KV_MINOR
# @DEFAULT_UNSET
# @OUTPUT_VARIABLE
# @DESCRIPTION:
# Kernel minor version from <KV_MAJOR>.<KV_MINOR>.<KV_PATCH

# @ECLASS_VARIABLE: KV_PATCH
# @DEFAULT_UNSET
# @OUTPUT_VARIABLE
# @DESCRIPTION:
# Kernel patch version from <KV_MAJOR>.<KV_MINOR>.<KV_PATCH

# @ECLASS_VARIABLE: LINUX_HOSTCFLAGS
# @DEFAULT_UNSET
# @DESCRIPTION:
# Default cflags if not already set

# @ECLASS_VARIABLE: OKV
# @DEFAULT_UNSET
# @DESCRIPTION:
# Original Kernel Version (2.6.0/2.6.0-test11)

# @ECLASS_VARIABLE: RELEASE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Representative of the kernel release tag (-rc3/-git3)

# @ECLASS_VARIABLE: RELEASETYPE
# @DEFAULT_UNSET
# @DESCRIPTION:
# The same as RELEASE but with its numerics stripped (-rc/-git)

# @ECLASS_VARIABLE: UNIPATCH_DOCS
# @DEFAULT_UNSET
# @DESCRIPTION:
# space delimemeted list of docs to be installed to
# the doc dir

# @ECLASS_VARIABLE: UNIPATCH_EXCLUDE
# @DEFAULT_UNSET
# @DESCRIPTION:
# An addition var to support exclusion based completely
# on "<passedstring>*" and not "<passedno#>_*"
# this should _NOT_ be used from the ebuild as this is
# reserved for end users passing excludes from the cli

# @ECLASS_VARIABLE: UNIPATCH_LIST
# @DEFAULT_UNSET
# @DESCRIPTION:
# space delimetered list of patches to be applied to the kernel

# @ECLASS_VARIABLE: UNIPATCH_LIST_DEFAULT
# @INTERNAL
# @DESCRIPTION:
# Upstream kernel patch archive

# @ECLASS_VARIABLE: UNIPATCH_LIST_GENPATCHES
# @INTERNAL
# @DESCRIPTION:
# List of genpatches archives to apply to the kernel

# @ECLASS_VARIABLE: UNIPATCH_STRICTORDER
# @DEFAULT_UNSET
# @DESCRIPTION:
# if this is set places patches into directories of
# order, so they are applied in the order passed
# Changing any other variable in this eclass is not supported; you can request
# for additional variables to be added by contacting the current maintainer.
# If you do change them, there is a chance that we will not fix resulting bugs;
# that of course does not mean we're not willing to help.

# Added by Daniel Ostrow <dostrow@gentoo.org>
# This is an ugly hack to get around an issue with a 32-bit userland on ppc64.
# I will remove it when I come up with something more reasonable.
# Alfred Persson Forsberg <cat@catcream.org>
# Moved this above inherit as crossdev.eclass uses CHOST internally.
[[ ${PROFILE_ARCH} == ppc64 ]] && CHOST="powerpc64-${CHOST#*-}"

inherit crossdev estack multiprocessing optfeature toolchain-funcs

case ${EAPI} in
	7|8) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

HOMEPAGE="https://www.kernel.org/ https://wiki.gentoo.org/wiki/Kernel ${HOMEPAGE}"
: "${LICENSE:="GPL-2"}"

# No need to run scanelf/strip on kernel sources/headers (bug #134453).
RESTRICT="binchecks strip"

# set LINUX_HOSTCFLAGS if not already set
: "${LINUX_HOSTCFLAGS:="-Wall -Wstrict-prototypes -Os -fomit-frame-pointer -I${S}/include"}"

# @FUNCTION: debug-print-kernel2-variables
# @USAGE:
# @DESCRIPTION:
# this function exists only to help debug kernel-2.eclass
# if you are adding new functionality in, put a call to it
# at the start of src_unpack, or during SRC_URI/dep generation.

debug-print-kernel2-variables() {
	for v in PVR CKV OKV KV KV_FULL KV_MAJOR KV_MINOR KV_PATCH RELEASETYPE \
			RELEASE UNIPATCH_LIST_DEFAULT UNIPATCH_LIST_GENPATCHES \
			UNIPATCH_LIST S KERNEL_URI K_WANT_GENPATCHES ; do
		debug-print "${v}: ${!v}"
	done
}

# @FUNCTION: handle_genpatches
# @USAGE: [--set-unipatch-list]
# @DESCRIPTION:
# add genpatches to list of patches to apply if wanted

handle_genpatches() {
	local tarball want_unipatch_list
	[[ -z ${K_WANT_GENPATCHES} || -z ${K_GENPATCHES_VER} ]] && return 1

	if [[ -n ${1} ]]; then
		# set UNIPATCH_LIST_GENPATCHES only on explicit request
		# since that requires 'use' call which can be used only in phase
		# functions, while the function is also called in global scope
		if [[ ${1} == --set-unipatch-list ]]; then
			want_unipatch_list=1
		else
			die "Usage: ${FUNCNAME} [--set-unipatch-list]"
		fi
	fi

	debug-print "Inside handle_genpatches"
	local OKV_ARRAY
	IFS="." read -r -a OKV_ARRAY <<<"${OKV}"

	# for > 3.0 kernels, handle genpatches tarball name
	# genpatches for 3.0 and 3.0.1 might be named
	# genpatches-3.0-1.base.tar.xz and genpatches-3.0-2.base.tar.xz
	# respectively.  Handle this.

	for i in ${K_WANT_GENPATCHES} ; do
		if [[ ${KV_MAJOR} -ge 3 ]]; then
			if [[ ${#OKV_ARRAY[@]} -ge 3 ]]; then
				tarball="genpatches-${KV_MAJOR}.${KV_MINOR}-${K_GENPATCHES_VER}.${i}.tar.xz"
			else
				tarball="genpatches-${KV_MAJOR}.${KV_PATCH}-${K_GENPATCHES_VER}.${i}.tar.xz"
			fi
		else
			tarball="genpatches-${OKV}-${K_GENPATCHES_VER}.${i}.tar.xz"
		fi

		local use_cond_start="" use_cond_end=""

		if [[ ${i} == experimental && -z ${K_EXP_GENPATCHES_PULL} && -z ${K_EXP_GENPATCHES_NOUSE} ]]; then
			use_cond_start="experimental? ( "
			use_cond_end=" )"

			if [[ -n ${want_unipatch_list} ]] && use experimental; then
				UNIPATCH_LIST_GENPATCHES+=" ${DISTDIR}/${tarball}"
				debug-print "genpatches tarball: ${tarball}"
			fi
		elif [[ -n ${want_unipatch_list} ]]; then
			UNIPATCH_LIST_GENPATCHES+=" ${DISTDIR}/${tarball}"
			debug-print "genpatches tarball: ${tarball}"
		fi
		GENPATCHES_URI+=" ${use_cond_start}$(echo https://dev.gentoo.org/~{alicef,mpagano}/dist/genpatches/${tarball})${use_cond_end}"
	done
}

# @FUNCTION: detect_version
# @USAGE:
# @DESCRIPTION:
# this function will detect and set
# - OKV: Original Kernel Version (2.6.0/2.6.0-test11)
# - KV: Kernel Version (2.6.0-gentoo/2.6.0-test11-gentoo-r1)
# - EXTRAVERSION: The additional version appended to OKV (-gentoo/-gentoo-r1)
detect_version() {
	# We've already run, so nothing to do here.
	[[ -n ${KV_FULL} ]] && return 0

	# CKV is used as a comparison kernel version, which is used when
	# PV doesn't reflect the genuine kernel version.
	# this gets set to the portage style versioning. ie:
	#   CKV=2.6.11_rc4
	CKV=${CKV:-${PV}}
	OKV=${OKV:-${CKV}}
	OKV=${OKV/_beta/-test}
	OKV=${OKV/_rc/-rc}
	OKV=${OKV/-r*}
	OKV=${OKV/_p*}

	KV_MAJOR=$(ver_cut 1 ${OKV})
	# handle if OKV is X.Y or X.Y.Z (e.g. 3.0 or 3.0.1)
	local OKV_ARRAY
	IFS="." read -r -a OKV_ARRAY <<<"${OKV}"

	# if KV_MAJOR >= 3, then we have no more KV_MINOR
	#if [[ ${KV_MAJOR} -lt 3 ]]; then
	if [[ ${#OKV_ARRAY[@]} -ge 3 ]]; then
		KV_MINOR=$(ver_cut 2 ${OKV})
		KV_PATCH=$(ver_cut 3 ${OKV})
		if [[ ${KV_MAJOR}${KV_MINOR}${KV_PATCH} -ge 269 ]]; then
			KV_EXTRA=$(ver_cut 4- ${OKV})
			KV_EXTRA=${KV_EXTRA/[-_]*}
		else
			KV_PATCH=$(ver_cut 3- ${OKV})
		fi
	else
		KV_PATCH=$(ver_cut 2 ${OKV})
		KV_EXTRA=$(ver_cut 3- ${OKV})
		KV_EXTRA=${KV_EXTRA/[-_]*}
	fi

	debug-print "KV_EXTRA is ${KV_EXTRA}"

	KV_PATCH=${KV_PATCH/[-_]*}

	local v n=0 missing
	#if [[ ${KV_MAJOR} -lt 3 ]]; then
	if [[ ${#OKV_ARRAY[@]} -ge 3 ]]; then
		for v in CKV OKV KV_{MAJOR,MINOR,PATCH} ; do
			[[ -z ${!v} ]] && n=1 && missing="${missing}${v} ";
		done
	else
		for v in CKV OKV KV_{MAJOR,PATCH} ; do
			[[ -z ${!v} ]] && n=1 && missing="${missing}${v} ";
		done
	fi

	[[ ${n} -eq 1 ]] && \
		eerror "Missing variables: ${missing}" && \
		die "Failed to extract kernel version (try explicit CKV in ebuild)!"
	unset v n missing

#	if [[ ${KV_MAJOR} -ge 3 ]]; then
	if [[ ${#OKV_ARRAY[@]} -lt 3 ]]; then
		KV_PATCH_ARR=(${KV_PATCH//\./ })

		# at this point 031412, Linus is putting all 3.x kernels in a
		# 3.x directory, may need to revisit when 4.x is released
		KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.x"

		[[ -n ${K_LONGTERM} ]] &&
			KERNEL_BASE_URI="${KERNEL_BASE_URI}/longterm/v${KV_MAJOR}.${KV_PATCH_ARR}"
	else
		#KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.0"
		#KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}"
		if [[ ${KV_MAJOR} -ge 3 ]]; then
			KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.x"
		else
			KERNEL_BASE_URI="https://www.kernel.org/pub/linux/kernel/v${KV_MAJOR}.${KV_MINOR}"
		fi

		[[ -n ${K_LONGTERM} ]] &&
			#KERNEL_BASE_URI="${KERNEL_BASE_URI}/longterm"
			KERNEL_BASE_URI="${KERNEL_BASE_URI}/longterm/v${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}"
	fi

	debug-print "KERNEL_BASE_URI is ${KERNEL_BASE_URI}"

	if [[ ${#OKV_ARRAY[@]} -ge 3 && ${KV_MAJOR} -ge 3 ]]; then
		# handle non genpatch using sources correctly
		if [[ -z ${K_WANT_GENPATCHES} && -z ${K_GENPATCHES_VER} && ${KV_PATCH} -gt 0 ]]; then
			KERNEL_URI="${KERNEL_BASE_URI}/patch-${OKV}.xz"
			UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV}.xz"
		fi
		KERNEL_URI="${KERNEL_URI} ${KERNEL_BASE_URI}/linux-${KV_MAJOR}.${KV_MINOR}.tar.xz"
	else
		KERNEL_URI="${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
	fi

	RELEASE=${CKV/${OKV}}
	RELEASE=${RELEASE/_beta}
	RELEASE=${RELEASE/_rc/-rc}
	RELEASE=${RELEASE/_pre/-pre}
	# We cannot trivially call kernel_is here, because it calls us to detect the
	# version
	#kernel_is ge 2 6 && RELEASE=${RELEASE/-pre/-git}
	(( KV_MAJOR * 1000 + ${KV_MINOR:-0} >= 2006 )) && RELEASE=${RELEASE/-pre/-git}
	RELEASETYPE=${RELEASE//[0-9]}

	# Now we know that RELEASE is the -rc/-git
	# and RELEASETYPE is the same but with its numerics stripped
	# we can work on better sorting EXTRAVERSION.
	# first of all, we add the release
	EXTRAVERSION="${RELEASE}"
	debug-print "0 EXTRAVERSION:${EXTRAVERSION}"
	[[ -n ${KV_EXTRA} && ${KV_MAJOR} -lt 3 ]] && EXTRAVERSION=".${KV_EXTRA}${EXTRAVERSION}"

	debug-print "1 EXTRAVERSION:${EXTRAVERSION}"
	if [[ -n ${K_NOUSEPR} ]]; then
		# Don't add anything based on PR to EXTRAVERSION
		debug-print "1.0 EXTRAVERSION:${EXTRAVERSION}"
	elif [[ -n ${K_PREPATCHED} ]]; then
		debug-print "1.1 EXTRAVERSION:${EXTRAVERSION}"
		EXTRAVERSION="${EXTRAVERSION}-${PN/-*}${PR/r}"
	elif [[ ${ETYPE} = sources ]]; then
		debug-print "1.2 EXTRAVERSION:${EXTRAVERSION}"
		# For some sources we want to use the PV in the extra version
		# This is because upstream releases with a completely different
		# versioning scheme.
		case ${PN/-*} in
		     wolk) K_USEPV=1;;
		  vserver) K_USEPV=1;;
		esac

		[[ -z ${K_NOUSENAME} ]] && EXTRAVERSION="${EXTRAVERSION}-${PN/-*}"
		[[ -n ${K_USEPV} ]]     && EXTRAVERSION="${EXTRAVERSION}-${PV//_/-}"
		[[ -n ${PR//r0} ]] && EXTRAVERSION="${EXTRAVERSION}-${PR}"
	fi
	debug-print "2 EXTRAVERSION:${EXTRAVERSION}"

	# The only messing around which should actually effect this is for KV_EXTRA
	# since this has to limit OKV to MAJ.MIN.PAT and strip EXTRA off else
	# KV_FULL evaluates to MAJ.MIN.PAT.EXT.EXT after EXTRAVERSION

	if [[ -n ${KV_EXTRA} ]]; then
		if [[ -n ${KV_MINOR} ]]; then
			OKV="${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}"
		else
			OKV="${KV_MAJOR}.${KV_PATCH}"
		fi
		KERNEL_URI="${KERNEL_BASE_URI}/patch-${CKV}.xz
					${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
		UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV}.xz"
	fi

	# We need to set this using OKV, but we need to set it before we do any
	# messing around with OKV based on RELEASETYPE
	KV_FULL=${OKV}${EXTRAVERSION}

	# we will set this for backwards compatibility.
	S="${WORKDIR}"/linux-${KV_FULL}
	KV=${KV_FULL}

	# -rc-git pulls can be achieved by specifying CKV
	# for example:
	#   CKV="2.6.11_rc3_pre2"
	# will pull:
	#   linux-2.6.10.tar.xz & patch-2.6.11-rc3.xz & patch-2.6.11-rc3-git2.xz

	if [[ ${KV_MAJOR}${KV_MINOR} -eq 26 ]]; then

		if [[ ${RELEASETYPE} == -rc || ${RELEASETYPE} == -pre ]]; then
			OKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_PATCH} - 1))"
			KERNEL_URI="${KERNEL_BASE_URI}/testing/patch-${CKV//_/-}.xz
						${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
			UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV//_/-}.xz"
		fi

		if [[ ${RELEASETYPE} == -git ]]; then
			KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${OKV}${RELEASE}.xz
						${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
			UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${OKV}${RELEASE}.xz"
		fi

		if [[ ${RELEASETYPE} == -rc-git ]]; then
			OKV="${KV_MAJOR}.${KV_MINOR}.$((${KV_PATCH} - 1))"
			KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE}.xz
						${KERNEL_BASE_URI}/testing/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE/-git*}.xz
						${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"

			UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE/-git*}.xz ${DISTDIR}/patch-${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}${RELEASE}.xz"
		fi
	else
		KV_PATCH_ARR=(${KV_PATCH//\./ })

		# the different majorminor versions have different patch start versions
		OKV_DICT=(["2"]="${KV_MAJOR}.$((${KV_PATCH_ARR} - 1))" ["3"]="2.6.39" ["4"]="3.19")

		if [[ ${RELEASETYPE} == -rc || ${RELEASETYPE} == -pre ]]; then

			OKV=${K_BASE_VER:-${OKV_DICT["${KV_MAJOR}"]}}

			# as of 12/5/2017, the rc patch is no longer offered as a compressed
			# file, and no longer is it mirrored on kernel.org
			if ver_test "${KV_MAJOR}.${KV_PATCH}" -ge "4.12"; then
				KERNEL_URI="https://git.kernel.org/torvalds/p/v${KV_FULL}/v${OKV} -> patch-${KV_FULL}.patch
							${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
				UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV//_/-}.patch"
			else
				KERNEL_URI="${KERNEL_BASE_URI}/testing/patch-${CKV//_/-}.xz
							${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
				UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${CKV//_/-}.xz"
			fi
		fi

		if [[ ${RELEASETYPE} == -git ]]; then
			KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${OKV}${RELEASE}.xz
						${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"
			UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${OKV}${RELEASE}.xz"
		fi

		if [[ ${RELEASETYPE} == -rc-git ]]; then
			OKV=${K_BASE_VER:-${OKV_DICT["${KV_MAJOR}"]}}
			KERNEL_URI="${KERNEL_BASE_URI}/snapshots/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE}.xz
						${KERNEL_BASE_URI}/testing/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE/-git*}.xz
						${KERNEL_BASE_URI}/linux-${OKV}.tar.xz"

			UNIPATCH_LIST_DEFAULT="${DISTDIR}/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE/-git*}.xz ${DISTDIR}/patch-${KV_MAJOR}.${KV_PATCH}${RELEASE}.xz"
		fi
	fi

	debug-print-kernel2-variables

	handle_genpatches
}

# @FUNCTION: kernel_is
# @USAGE: <conditional version | version>
# @DESCRIPTION:
# user for comparing kernel versions
# or just identifying a version
# e.g kernel_is 2 4
# e.g kernel_is ge 4.8.11
# Note: duplicated in linux-info.eclass
kernel_is() {
	# ALL of these should be set before we can safely continue this function.
	# some of the sources have in the past had only one set.
	local v n=0
	for v in OKV KV_{MAJOR,MINOR,PATCH} ; do [[ -z ${!v} ]] && n=1 ; done
	[[ ${n} -eq 1 ]] && detect_version

	# Now we can continue
	local operator

	case ${1#-} in
	  lt) operator="-lt"; shift;;
	  gt) operator="-gt"; shift;;
	  le) operator="-le"; shift;;
	  ge) operator="-ge"; shift;;
	  eq) operator="-eq"; shift;;
	   *) operator="-eq";;
	esac
	[[ $# -gt 3 ]] && die "Error in ${ECLASS}_${FUNCNAME}(): too many parameters"

	ver_test \
		"${KV_MAJOR:-0}.${KV_MINOR:-0}.${KV_PATCH:-0}" \
		"${operator}" \
		"${1:-${KV_MAJOR:-0}}.${2:-${KV_MINOR:-0}}.${3:-${KV_PATCH:-0}}"
}

# Capture the sources type and set DEPENDs
if [[ ${ETYPE} == sources ]]; then
	RDEPEND="!build? (
		app-alternatives/cpio
		dev-lang/perl
		app-alternatives/bc
		dev-build/make
		sys-devel/bison
		sys-devel/flex
		>=sys-libs/ncurses-5.2
		virtual/libelf
		virtual/pkgconfig
	)"

	SLOT=${SLOT:=${PVR}}
	DESCRIPTION="Sources based on the Linux Kernel"
	IUSE="symlink build"

	# Bug #266157, deblob for libre support
	if [[ -z ${K_PREDEBLOBBED} ]]; then
		if [[ ${K_DEBLOB_AVAILABLE} == 1 ]]; then
			IUSE="${IUSE} deblob"

			# Reflect that kernels contain firmware blobs unless otherwise
			# stripped. Starting with version 4.14, the whole firmware
			# tree has been dropped from the kernel.
			kernel_is lt 4 14 &&
				LICENSE+=" !deblob? ( linux-fw-redistributable all-rights-reserved )"

			if [[ -n KV_MINOR ]]; then
				DEBLOB_PV="${KV_MAJOR}.${KV_MINOR}.${KV_PATCH}"
			else
				DEBLOB_PV="${KV_MAJOR}.${KV_PATCH}"
			fi

			if [[ ${KV_MAJOR} -ge 3 ]]; then
				DEBLOB_PV="${KV_MAJOR}.${KV_MINOR}"
			fi

			# deblob svn tag, default is -gnu, to change, use K_DEBLOB_TAG in ebuild
			K_DEBLOB_TAG=${K_DEBLOB_TAG:--gnu}
			DEBLOB_A="deblob-${DEBLOB_PV}"
			DEBLOB_CHECK_A="deblob-check-${DEBLOB_PV}"
			DEBLOB_HOMEPAGE="https://www.fsfla.org/svn/fsfla/software/linux-libre/releases/tags/"
			DEBLOB_URI_PATH="${DEBLOB_PV}${K_DEBLOB_TAG}"
			DEBLOB_CHECK_URI="${DEBLOB_HOMEPAGE}/${DEBLOB_URI_PATH}/deblob-check -> ${DEBLOB_CHECK_A}"
			DEBLOB_URI="${DEBLOB_HOMEPAGE}/${DEBLOB_URI_PATH}/${DEBLOB_A}"
			HOMEPAGE="${HOMEPAGE} ${DEBLOB_HOMEPAGE}"

			KERNEL_URI="${KERNEL_URI}
				deblob? (
					${DEBLOB_URI}
					${DEBLOB_CHECK_URI}
				)"
		elif kernel_is lt 4 14; then
			# Deblobbing is not available, so just mark kernels older
			# than 4.14 as tainted with non-libre materials.
			LICENSE+=" linux-fw-redistributable all-rights-reserved"
		fi
	fi

elif [[ ${ETYPE} == headers ]]; then
	DESCRIPTION="Linux system headers"
	IUSE="headers-only"

	# Since we should NOT honour KBUILD_OUTPUT in headers
	# lets unset it here.
	unset KBUILD_OUTPUT

	SLOT="0"
fi

# Cross-compile support functions

# @FUNCTION: kernel_header_destdir
# @USAGE:
# @DESCRIPTION:
# return header destination directory
kernel_header_destdir() {
	[[ ${CTARGET} == ${CHOST} ]] \
		&& echo /usr/include \
		|| echo /usr/${CTARGET}/usr/include
}

# @FUNCTION: cross_pre_c_headers
# @USAGE:
# @DESCRIPTION:
# set use if necessary for cross compile support
cross_pre_c_headers() {
	use headers-only && [[ ${CHOST} != ${CTARGET} ]]
}

# @FUNCTION: env_setup_kernel_makeopts
# @USAGE:
# @DESCRIPTION:
# Set the toolchain variables, as well as ARCH and CROSS_COMPILE when
# cross-compiling.

env_setup_kernel_makeopts() {
	# Kernel ARCH != portage ARCH
	export KARCH=$(tc-arch-kernel)

	# When cross-compiling, we need to set the ARCH/CROSS_COMPILE
	# variables properly or bad things happen !
	KERNEL_MAKEOPTS=( ARCH="${KARCH}" )
	if [[ ${CTARGET} != ${CHOST} ]] && ! cross_pre_c_headers; then
		KERNEL_MAKEOPTS+=( CROSS_COMPILE="${CTARGET}-" )
	elif type -p ${CHOST}-ar >/dev/null; then
		KERNEL_MAKEOPTS+=( CROSS_COMPILE="${CHOST}-" )
	fi
	KERNEL_MAKEOPTS+=(
		HOSTCC="$(tc-getBUILD_CC)"
		CC="$(tc-getCC)"
		LD="$(tc-getLD)"
		AR="$(tc-getAR)"
		NM="$(tc-getNM)"
		OBJCOPY="$(tc-getOBJCOPY)"
		READELF="$(tc-getREADELF)"
		STRIP="$(tc-getSTRIP)"
	)
	export KERNEL_MAKEOPTS
}

# @FUNCTION: universal_unpack
# @USAGE:
# @DESCRIPTION:
# unpack kernel sources

universal_unpack() {
	debug-print "Inside universal_unpack"

	local OKV_ARRAY
	IFS="." read -r -a OKV_ARRAY <<<"${OKV}"

	cd "${WORKDIR}" || die
	if [[ ${#OKV_ARRAY[@]} -ge 3 && ${KV_MAJOR} -ge 3 ]]; then
		unpack linux-${KV_MAJOR}.${KV_MINOR}.tar.xz
	else
		unpack linux-${OKV}.tar.xz
	fi

	if [[ -d linux ]]; then
		debug-print "Moving linux to linux-${KV_FULL}"
		mv linux linux-${KV_FULL} \
			|| die "Unable to move source tree to ${KV_FULL}."
	elif [[ ${OKV} != ${KV_FULL} ]]; then
		if [[ ${#OKV_ARRAY[@]} -ge 3 && ${KV_MAJOR} -ge 3 ]] &&
			[[ ${ETYPE} = sources ]]; then
			debug-print "moving linux-${KV_MAJOR}.${KV_MINOR} to linux-${KV_FULL} "
			mv linux-${KV_MAJOR}.${KV_MINOR} linux-${KV_FULL} \
				|| die "Unable to move source tree to ${KV_FULL}."
		else
			debug-print "moving linux-${OKV} to linux-${KV_FULL} "
			mv linux-${OKV} linux-${KV_FULL} \
				|| die "Unable to move source tree to ${KV_FULL}."
		fi
	elif [[ ${#OKV_ARRAY[@]} -ge 3 && ${KV_MAJOR} -ge 3 ]]; then
		mv linux-${KV_MAJOR}.${KV_MINOR} linux-${KV_FULL} \
			|| die "Unable to move source tree to ${KV_FULL}."
	fi
	cd "${S}" || die

	# remove all backup files
	find . -iname "*~" -exec rm {} \; 2>/dev/null

}

# @FUNCTION: unpack_set_extraversion
# @USAGE:
# @DESCRIPTION:
# handle EXTRAVERSION

unpack_set_extraversion() {
	sed -i -e "s:^\(EXTRAVERSION =\).*:\1 ${EXTRAVERSION}:" "${S}"/Makefile || die
}

# @FUNCTION: unpack_fix_install_path
# @USAGE:
# @DESCRIPTION:
# Should be done after patches have been applied
# Otherwise patches that modify the same area of Makefile will fail

unpack_fix_install_path() {
	sed -i -e 's:#export\tINSTALL_PATH:export\tINSTALL_PATH:' "${S}"/Makefile || die
}

# install functions

# @FUNCTION: install_universal
# @USAGE:
# @DESCRIPTION:
# Fix permissions in tarball

install_universal() {
	chown -R 0:0 "${WORKDIR}"/* &>/dev/null
	chmod -R a+r-w+X,u+w "${WORKDIR}"/*
}

# @FUNCTION: install_headers
# @USAGE:
# @DESCRIPTION:
# Install headers

install_headers() {
	local ddir=$(kernel_header_destdir)

	env_setup_kernel_makeopts
	emake headers_install INSTALL_HDR_PATH="${ED}"${ddir}/.. "${KERNEL_MAKEOPTS[@]}"

	# let other packages install some of these headers
	rm -rf "${ED}"${ddir}/scsi || die #glibc/uclibc/etc...
	return 0
}

# @FUNCTION: install_sources
# @USAGE:
# @DESCRIPTION:
# Install sources

install_sources() {
	local file

	cd "${S}" || die
	dodir /usr/src
	einfo ">>> Copying sources ..."

	file="$(find "${WORKDIR}" -iname "docs" -type d)"
	if [[ -n ${file} ]]; then
		for file in $(find ${file} -type f); do
			echo "${file//*docs\/}" >> "${S}"/patches.txt
			echo "===================================================" >> "${S}"/patches.txt
			cat ${file} >> "${S}"/patches.txt
			echo "===================================================" >> "${S}"/patches.txt
			echo "" >> "${S}"/patches.txt
		done
	fi

	cp -R "${WORKDIR}"/linux* "${ED}"/usr/src || die

	if [[ -n ${UNIPATCH_DOCS} ]]; then
		for i in ${UNIPATCH_DOCS}; do
			dodoc "${T}"/${i}
		done
	fi
}

# @FUNCTION: preinst_headers
# @USAGE:
# @DESCRIPTION:
# Headers preinst steps

preinst_headers() {
	local ddir=$(kernel_header_destdir)
	[[ -L ${EPREFIX}${ddir}/linux ]] && { rm "${EPREFIX}"${ddir}/linux || die; }
	[[ -L ${EPREFIX}${ddir}/asm ]] && { rm "${EPREFIX}"${ddir}/asm || die; }
}

# @FUNCTION: postinst_sources
# @USAGE:
# @DESCRIPTION:
# Sources post installation function.
# see inline comments

postinst_sources() {
	# if we have USE=symlink, then force K_SYMLINK=1
	use symlink && K_SYMLINK=1

	# We do support security on a deblobbed kernel, bug #555878.
	# If some particular kernel version doesn't have security
	# supported because of USE=deblob or otherwise, one can still
	# set K_SECURITY_UNSUPPORTED on a per ebuild basis.
	#[[ ${K_DEBLOB_AVAILABLE} == 1 ]] && \
	#	use deblob && \
	#	K_SECURITY_UNSUPPORTED=deblob

	# if we are to forcibly symlink, delete it if it already exists first.
	if [[ ${K_SYMLINK} -gt 0 ]]; then
		if [[ -e ${EROOT}/usr/src/linux && ! -L ${EROOT}/usr/src/linux ]] ; then
			die "${EROOT}/usr/src/linux exists and is not a symlink"
		fi

		ln -snf linux-${KV_FULL} "${EROOT}"/usr/src/linux || die
	fi

	# Don't forget to make directory for sysfs
	[[ ! -d ${EROOT}/sys ]] && kernel_is 2 6 && { mkdir "${EROOT}"/sys || die ; }

	elog "If you are upgrading from a previous kernel, you may be interested"
	elog "in the following document:"
	elog "  - General upgrade guide: https://wiki.gentoo.org/wiki/Kernel/Upgrade"

	# if K_EXTRAEINFO is set then lets display it now
	if [[ -n ${K_EXTRAEINFO} ]]; then
		echo ${K_EXTRAEINFO} | fmt |
		while read -s ELINE; do einfo "${ELINE}"; done
	fi

	# if K_EXTRAELOG is set then lets display it now
	if [[ -n ${K_EXTRAELOG} ]]; then
		echo ${K_EXTRAELOG} | fmt |
		while read -s ELINE; do elog "${ELINE}"; done
	fi

	# if K_EXTRAEWARN is set then lets display it now
	if [[ -n ${K_EXTRAEWARN} ]]; then
		echo ${K_EXTRAEWARN} | fmt |
		while read -s ELINE; do ewarn "${ELINE}"; done
	fi

	# optionally display security unsupported message
	#  Start with why
	if [[ -n ${K_SECURITY_UNSUPPORTED} ]]; then
		ewarn "${PN} is UNSUPPORTED by Gentoo Security."
	fi
	#  And now the general message.
	if [[ -n ${K_SECURITY_UNSUPPORTED} ]]; then
		ewarn "This means that it is likely to be vulnerable to recent security issues."
		ewarn "Upstream kernel developers recommend always running the latest "
		ewarn "release of any current long term supported Linux kernel version."
		ewarn "To see a list of these versions, their most current release and "
		ewarn "long term support status, please go to https://www.kernel.org ."
		ewarn "For specific information on why this kernel is unsupported, please read:"
		ewarn "https://wiki.gentoo.org/wiki/Project:Kernel_Security"
	fi

	# warn sparc users that they need to do cross-compiling with >= 2.6.25(bug #214765)
	KV_MAJOR=$(ver_cut 1 ${OKV})
	KV_MINOR=$(ver_cut 2 ${OKV})
	KV_PATCH=$(ver_cut 3 ${OKV})
	if [[ $(tc-arch) = sparc ]]; then
		if [[ $(gcc-major-version) -lt 4 && $(gcc-minor-version) -lt 4 ]]; then
			if [[ ${KV_MAJOR} -ge 3 ]] || ver_test ${KV_MAJOR}.${KV_MINOR}.${KV_PATCH} -gt 2.6.24; then
				elog "NOTE: Since 2.6.25 the kernel Makefile has changed in a way that"
				elog "you now need to do"
				elog "  make CROSS_COMPILE=sparc64-unknown-linux-gnu-"
				elog "instead of just"
				elog "  make"
				elog "to compile the kernel. For more information please browse to"
				elog "https://bugs.gentoo.org/show_bug.cgi?id=214765"
			fi
		fi
	fi

	optfeature "versioned kernel image installation and optionally automating tasks such as generating an initramfs or unified kernel image" \
		"sys-kernel/installkernel"
}

# pkg_setup functions

# @FUNCTION: setup_headers
# @USAGE:
# @DESCRIPTION:
# Determine if ${PN} supports arch

setup_headers() {
	[[ -z ${H_SUPPORTEDARCH} ]] && H_SUPPORTEDARCH=${PN/-*/}
	for i in ${H_SUPPORTEDARCH}; do
		[[ $(tc-arch) == ${i} ]] && H_ACCEPT_ARCH="yes"
	done

	if [[ ${H_ACCEPT_ARCH} != yes ]]; then
		eerror "This version of ${PN} does not support $(tc-arch)."
		eerror "Please merge the appropriate sources, in most cases"
		eerror "(but not all) this will be called $(tc-arch)-headers."
		die "Package unsupported for $(tc-arch)"
	fi
}

# @FUNCTION: unipatch
# @USAGE: <list of patches to apply>
# @DESCRIPTION:
# Universal function that will apply patches to source

unipatch() {
	local i x y z extension PIPE_CMD UNIPATCH_DROP KPATCH_DIR PATCH_DEPTH ELINE
	local STRICT_COUNT PATCH_LEVEL myLC_ALL myLANG

	# set to a standard locale to ensure sorts are ordered properly.
	myLC_ALL="${LC_ALL}"
	myLANG="${LANG}"
	LC_ALL="C"
	LANG=""

	[[ -z ${KPATCH_DIR} ]] && KPATCH_DIR="${WORKDIR}/patches/"
	[[ ! -d ${KPATCH_DIR} ]] && mkdir -p ${KPATCH_DIR}

	# We're gonna need it when doing patches with a predefined patchlevel
	eshopts_push -s extglob

	# This function will unpack all passed tarballs, add any passed patches,
	# and remove any passed patchnumbers
	# usage can be either via an env var or by params
	# although due to the nature we pass this within this eclass
	# it shall be by param only.
	# -z "${UNIPATCH_LIST}" ] && UNIPATCH_LIST="${@}"
	UNIPATCH_LIST="${@}"

	#unpack any passed tarballs
	for i in ${UNIPATCH_LIST}; do
		if echo ${i} | grep -qs -e "\.tar" -e "\.tbz" -e "\.tgz"; then
			if [[ -n ${UNIPATCH_STRICTORDER} ]]; then
				unset z
				STRICT_COUNT=$((10#${STRICT_COUNT:=0} + 1))
				for((y=0; y<$((6 - ${#STRICT_COUNT})); y++));
					do z="${z}0";
				done
				PATCH_ORDER="${z}${STRICT_COUNT}"

				mkdir -p "${KPATCH_DIR}/${PATCH_ORDER}"
				pushd "${KPATCH_DIR}/${PATCH_ORDER}" >/dev/null || die
				unpack ${i##*/}
				popd >/dev/null || die
			else
				pushd "${KPATCH_DIR}" >/dev/null || die
				unpack ${i##*/}
				popd >/dev/null || die
			fi

			[[ ${i} == *:* ]] && elog ">>> Strict patch levels not currently supported for tarballed patchsets"
		else
			extension=${i/*./}
			extension=${extension/:*/}
			PIPE_CMD=""
			case ${extension} in
				     xz) PIPE_CMD="xz -T$(makeopts_jobs) -dc";;
				   lzma) PIPE_CMD="lzma -dc";;
				    bz2) PIPE_CMD="bzip2 -dc";;
				 patch*) PIPE_CMD="cat";;
				   diff) PIPE_CMD="cat";;
				 gz|Z|z) PIPE_CMD="gzip -dc";;
				ZIP|zip) PIPE_CMD="unzip -p";;
				      *) UNIPATCH_DROP="${UNIPATCH_DROP} ${i/:*/}";;
			esac

			PATCH_LEVEL=${i/*([^:])?(:)}
			i=${i/:*/}
			x=${i/*\//}
			x=${x/\.${extension}/}

			if [[ -n ${PIPE_CMD} ]]; then
				if [[ ! -r ${i} ]]; then
					eerror "FATAL: unable to locate:"
					eerror "${i}"
					eerror "for read-only. The file either has incorrect permissions"
					eerror "or does not exist."
					die Unable to locate ${i}
				fi

				if [[ -n ${UNIPATCH_STRICTORDER} ]]; then
					unset z
					STRICT_COUNT=$((10#${STRICT_COUNT:=0} + 1))
					for((y=0; y<$((6 - ${#STRICT_COUNT})); y++));
						do z="${z}0";
					done
					PATCH_ORDER="${z}${STRICT_COUNT}"

					mkdir -p ${KPATCH_DIR}/${PATCH_ORDER}/
					$(${PIPE_CMD} ${i} > ${KPATCH_DIR}/${PATCH_ORDER}/${x}.patch${PATCH_LEVEL}) || die "uncompressing patch failed"
				else
					$(${PIPE_CMD} ${i} > ${KPATCH_DIR}/${x}.patch${PATCH_LEVEL}) || die "uncompressing patch failed"
				fi
			fi
		fi

		# If experimental was not chosen by the user, drop experimental patches not in K_EXP_GENPATCHES_LIST.
		if [[ ${i} == *genpatches-*.experimental.* && -n ${K_EXP_GENPATCHES_PULL} ]]; then
			if [[ -z ${K_EXP_GENPATCHES_NOUSE} ]] && use experimental; then
				continue
			fi

			local j
			for j in ${KPATCH_DIR}/*/50*_*.patch*; do
				for k in ${K_EXP_GENPATCHES_LIST} ; do
					[[ $(basename ${j}) == ${k}* ]] && continue 2
				done
				UNIPATCH_DROP+=" $(basename ${j})"
			done
		else
			UNIPATCH_LIST_GENPATCHES+=" ${DISTDIR}/${tarball}"
			debug-print "genpatches tarball: ${tarball}"

			local GCC_MAJOR_VER=$(gcc-major-version)
			local GCC_MINOR_VER=$(gcc-minor-version)

			# this section should be the target state to handle the cpu opt
			# patch for kernels > 4.19.189, 5.4.115, 5.10.33 and 5.11.17,
			# 5.12.0 and gcc >= 9  The patch now handles the
			# gcc version enabled on the system through the Kconfig file as
			# 'depends'. The legacy section can hopefully be retired in the future
			# Note the patch for 4.19-5.8 version are the same and the patch for
			# 5.8+ version is the same
			# eventually we can remove everything except the gcc ver <9 check
			# based on stablization, time, kernel removals or a combo of all three
			if ( kernel_is eq 4 19 && kernel_is gt 4 19 189 ) ||
				( kernel_is eq 5 4 && kernel_is gt 5 4 115 ) ||
				( kernel_is eq 5 10 && kernel_is gt 5 10 33 ) ||
				( kernel_is eq 5 11 && kernel_is gt 5 11 17 ) ||
				( kernel_is eq 5 12 && kernel_is gt 5 12 0 ) ||
				( kernel_is ge 5 13); then
				UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
				UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
				UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
				UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
				UNIPATCH_DROP+=" 5013_enable-cpu-optimizations-for-gcc10.patch"
				if [[ ${GCC_MAJOR_VER} -lt 9 ]] && ! tc-is-clang; then
					UNIPATCH_DROP+=" 5010_enable-cpu-optimizations-universal.patch"
				fi
				# this legacy section should be targeted for removal
				# optimization patch for gcc < 8.X and kernel > 4.13 and <  4.19
			elif kernel_is ge 4 13; then
				UNIPATCH_DROP+=" 5010_enable-cpu-optimizations-universal.patch"
				if [[ ${GCC_MAJOR_VER} -lt 8 && ${GCC_MAJOR_VER} -gt 4 ]]; then
					UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
					UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
					UNIPATCH_DROP+=" 5013_enable-cpu-optimizations-for-gcc10.patch"
				# optimization patch for gcc >= 8 and kernel ge 4.13
				elif [[ ${GCC_MAJOR_VER} -eq 8 ]]; then
					# support old kernels for a period. For now, remove as all gcc versions required are masked
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
					UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
					UNIPATCH_DROP+=" 5013_enable-cpu-optimizations-for-gcc10.patch"
				elif [[ ${GCC_MAJOR_VER} -eq 9 && ${GCC_MINOR_VER} -ge 1 ]]; then
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
					UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
					UNIPATCH_DROP+=" 5013_enable-cpu-optimizations-for-gcc10.patch"
				elif [[ ${GCC_MAJOR_VER} -eq 10 && ${GCC_MINOR_VER} -ge 1 ]]; then
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
					UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
					UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
				else
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
					UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
					UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
					UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
					UNIPATCH_DROP+=" 5013_enable-cpu-optimizations-for-gcc10.patch"
				fi
			else
				UNIPATCH_DROP+=" 5010_enable-cpu-optimizations-universal.patch"
				UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc.patch"
				UNIPATCH_DROP+=" 5010_enable-additional-cpu-optimizations-for-gcc-4.9.patch"
				UNIPATCH_DROP+=" 5011_enable-cpu-optimizations-for-gcc8.patch"
				UNIPATCH_DROP+=" 5012_enable-cpu-optimizations-for-gcc91.patch"
				UNIPATCH_DROP+=" 5013_enable-cpu-optimizations-for-gcc10.patch"
			fi
		fi
	done

	# Populate KPATCH_DIRS so we know where to look to remove the excludes
	x=${KPATCH_DIR}
	KPATCH_DIR=""
	for i in $(find ${x} -type d | sort -n); do
		KPATCH_DIR="${KPATCH_DIR} ${i}"
	done

	# So now lets get rid of the patch numbers we want to exclude
	UNIPATCH_DROP="${UNIPATCH_EXCLUDE} ${UNIPATCH_DROP}"
	for i in ${UNIPATCH_DROP}; do
		ebegin "Excluding Patch #${i}"
		for x in ${KPATCH_DIR}; do rm -f ${x}/${i}* 2>/dev/null; done
		eend $?
	done

	# and now, finally, we patch it :)
	for x in ${KPATCH_DIR}; do
		for i in $(find ${x} -maxdepth 1 -iname "*.patch*" -or -iname "*.diff*" | sort -n); do
			STDERR_T="${T}/${i/*\//}"
			STDERR_T="${STDERR_T/.patch*/.err}"

			[[ -z ${i/*.patch*/} ]] && PATCH_DEPTH=${i/*.patch/}
			#[[ -z ${i/*.diff*/} ]]  && PATCH_DEPTH=${i/*.diff/}

			if [[ -z ${PATCH_DEPTH} ]]; then PATCH_DEPTH=0; fi

			####################################################################
			# IMPORTANT: This code is to support kernels which cannot be       #
			# tested with the --dry-run parameter                              #
			#                                                                  #
			# These patches contain a removal of a symlink, followed by        #
			# addition of a file with the same name as the symlink in the      #
			# same location; this causes the dry-run to fail, see bug #507656. #
			#                                                                  #
			# https://bugs.gentoo.org/507656                                   #
			####################################################################
			if [[ -n ${K_NODRYRUN} ]]; then
				ebegin "Applying ${i/*\//} (-p1)"
				patch -p1 --no-backup-if-mismatch -f < ${i} >> ${STDERR_T}
				if [[ $? -le 2 ]]; then
					eend 0
					rm ${STDERR_T} || die
				else
					eend 1
					eerror "Failed to apply patch ${i/*\//}"
					eerror "Please attach ${STDERR_T} to any bug you may post."
					eshopts_pop
					die "Failed to apply ${i/*\//} on patch depth 1."
				fi
			fi
			####################################################################

			while [[ ${PATCH_DEPTH} -lt 5 && -z ${K_NODRYRUN} ]]; do
				echo "Attempting Dry-run:" >> ${STDERR_T}
				echo "cmd: patch -p${PATCH_DEPTH} --no-backup-if-mismatch --dry-run -f < ${i}" >> ${STDERR_T}
				echo "=======================================================" >> ${STDERR_T}
				patch -p${PATCH_DEPTH} --no-backup-if-mismatch --dry-run -f < ${i} >> ${STDERR_T}
				if [[ $? -eq 0 ]]; then
					ebegin "Applying ${i/*\//} (-p${PATCH_DEPTH})"
					echo "Attempting patch:" > ${STDERR_T}
					echo "cmd: patch -p${PATCH_DEPTH} --no-backup-if-mismatch -f < ${i}" >> ${STDERR_T}
					echo "=======================================================" >> ${STDERR_T}
					patch -p${PATCH_DEPTH} --no-backup-if-mismatch -f < ${i} >> ${STDERR_T}
					if [[ $? -eq 0 ]]; then
						eend 0
						rm ${STDERR_T} || die
						break
					else
						eend 1
						eerror "Failed to apply patch ${i/*\//}"
						eerror "Please attach ${STDERR_T} to any bug you may post."
						eshopts_pop
						die "Failed to apply ${i/*\//} on patch depth ${PATCH_DEPTH}."
					fi
				else
					PATCH_DEPTH=$((${PATCH_DEPTH} + 1))
				fi
			done
			if [[ ${PATCH_DEPTH} -eq 5 ]]; then
				eerror "Failed to dry-run patch ${i/*\//}"
				eerror "Please attach ${STDERR_T} to any bug you may post."
				eshopts_pop
				die "Unable to dry-run patch on any patch depth lower than 5."
			fi
		done
	done

	# When genpatches is used, we want to install 0000_README which documents
	# the patches that were used; such that the user can see them, bug #301478.
	if [[ ! -z ${K_WANT_GENPATCHES} ]]; then
		UNIPATCH_DOCS="${UNIPATCH_DOCS} 0000_README"
	fi

	# When files listed in UNIPATCH_DOCS are found in KPATCH_DIR's, we copy it
	# to the temporary directory and remember them in UNIPATCH_DOCS to install
	# them during the install phase.
	local tmp
	for x in ${KPATCH_DIR}; do
		for i in ${UNIPATCH_DOCS}; do
			if [[ -f ${x}/${i} ]]; then
				tmp="${tmp} ${i}"
				cp -f "${x}/${i}" "${T}"/ || die
			fi
		done
	done
	UNIPATCH_DOCS="${tmp}"

	# clean up  KPATCH_DIR's - fixes bug #53610
	for x in ${KPATCH_DIR}; do rm -Rf ${x}; done

	LC_ALL="${myLC_ALL}"
	LANG="${myLANG}"
	eshopts_pop
}

# @FUNCTION: getfilevar
# @USAGE: <variable> <configfile>
# @DESCRIPTION:
# pulled from linux-info

getfilevar() {
	local basefname basedname xarch=$(tc-arch-kernel)

	if [[ -z ${1} && ! -f ${2} ]]; then
		eerror "getfilevar requires 2 variables, with the second a valid file."
		eerror "   getfilevar <VARIABLE> <CONFIGFILE>"
	else
		basefname=$(basename ${2})
		basedname=$(dirname ${2})
		unset ARCH

		echo -e "include ${basefname}\ne:\n\t@echo \$(${1})" |
			make -C "${basedname}" ${BUILD_FIXES} -s -f - e 2>/dev/null

		ARCH=${xarch}
	fi
}

# @FUNCTION: detect_arch
# @USAGE:
# @DESCRIPTION:
# This function sets ARCH_URI and ARCH_PATCH
# with the necessary info for the arch specific compatibility
# patchsets.

detect_arch() {
	local ALL_ARCH LOOP_ARCH LOOP_ARCH_L COMPAT_URI i TC_ARCH_KERNEL

	# COMPAT_URI is the contents of ${ARCH}_URI
	# ARCH_URI is the URI for all the ${ARCH}_URI patches
	# ARCH_PATCH is ARCH_URI broken into files for UNIPATCH

	ARCH_URI=""
	ARCH_PATCH=""
	TC_ARCH_KERNEL=""
	ALL_ARCH="ALPHA AMD64 ARM HPPA IA64 M68K MIPS PPC PPC64 S390 SH SPARC X86"

	for LOOP_ARCH in ${ALL_ARCH}; do
		COMPAT_URI="${LOOP_ARCH}_URI"
		COMPAT_URI="${!COMPAT_URI}"

		declare -l LOOP_ARCH_L=${LOOP_ARCH}

		[[ -n ${COMPAT_URI} ]] && \
			ARCH_URI="${ARCH_URI} ${LOOP_ARCH_L}? ( ${COMPAT_URI} )"

		declare -u TC_ARCH_KERNEL=$(tc-arch-kernel)
		if [[ ${LOOP_ARCH} == ${TC_ARCH_KERNEL} ]]; then
			for i in ${COMPAT_URI}; do
				ARCH_PATCH="${ARCH_PATCH} ${DISTDIR}/${i/*\//}"
			done
		fi

	done
}

# @FUNCTION: headers___fix
# @USAGE:
# @DESCRIPTION:
# Voodoo to partially fix broken upstream headers.
# note: do not put inline/asm/volatile together (breaks "inline asm volatile")

headers___fix() {
	sed -i \
		-e '/^\#define.*_TYPES_H/{:loop n; bloop}' \
		-e 's:\<\([us]\(8\|16\|32\|64\)\)\>:__\1:g' \
		-e "s/\([[:space:]]\)inline\([[:space:](]\)/\1__inline__\2/g" \
		-e "s/\([[:space:]]\)asm\([[:space:](]\)/\1__asm__\2/g" \
		-e "s/\([[:space:]]\)volatile\([[:space:](]\)/\1__volatile__\2/g" \
		"$@"
}

# @FUNCTION: kernel-2_src_unpack
# @USAGE:
# @DESCRIPTION:
# unpack sources, handle genpatches, deblob

kernel-2_src_unpack() {
	universal_unpack
	debug-print "Doing unipatch"

	# request UNIPATCH_LIST_GENPATCHES in phase since it calls 'use'
	handle_genpatches --set-unipatch-list
	[[ -n ${UNIPATCH_LIST} || -n ${UNIPATCH_LIST_DEFAULT} || -n ${UNIPATCH_LIST_GENPATCHES} ]] && \
		unipatch "${UNIPATCH_LIST_DEFAULT} ${UNIPATCH_LIST_GENPATCHES} ${UNIPATCH_LIST}"

	debug-print "Doing premake"

	# allow ebuilds to massage the source tree after patching but before
	# we run misc `make` functions below
	if [[ $(type -t kernel-2_hook_premake) == "function" ]]; then
		ewarn "The function name: kernel-2_hook_premake is being deprecated and"
		ewarn "being changed to:  kernel-2_insert_premake to comply with pms policy."
		ewarn "See bug #843686 "
		ewarn "The call to the old function name will be removed on or about July 1st, 2022 "
		ewarn "Please update your ebuild before this date."
		kernel-2_hook_premake
	else
		[[ $(type -t kernel-2_insert_premake) == "function" ]] && kernel-2_insert_premake
	fi

	debug-print "Doing unpack_set_extraversion"

	[[ -z ${K_NOSETEXTRAVERSION} ]] && unpack_set_extraversion
	unpack_fix_install_path

	# Setup KERNEL_MAKEOPTS and cd into sourcetree.
	env_setup_kernel_makeopts
	cd "${S}" || die

	if [[ ${K_DEBLOB_AVAILABLE} == 1 ]] && use deblob; then
		cp "${DISTDIR}/${DEBLOB_A}" "${T}" || die "cp ${DEBLOB_A} failed"
		cp "${DISTDIR}/${DEBLOB_CHECK_A}" "${T}/deblob-check" || die "cp ${DEBLOB_CHECK_A} failed"
		chmod +x "${T}/${DEBLOB_A}" "${T}/deblob-check" || die "chmod deblob scripts failed"
	fi

	# fix a problem on ppc where TOUT writes to /usr/src/linux breaking sandbox
	# only do this for kernel < 2.6.27 since this file does not exist in later
	# kernels
	if [[ -n ${KV_MINOR} ]] && ver_test ${KV_MAJOR}.${KV_MINOR}.${KV_PATCH} -lt 2.6.27; then
		sed -i \
			-e 's|TOUT      := .tmp_gas_check|TOUT  := $(T).tmp_gas_check|' \
			"${S}"/arch/ppc/Makefile
	else
		sed -i \
			-e 's|TOUT      := .tmp_gas_check|TOUT  := $(T).tmp_gas_check|' \
			"${S}"/arch/powerpc/Makefile
	fi
}

# @FUNCTION: kernel-2_src_prepare
# @USAGE:
# @DESCRIPTION:
# Apply any user patches

kernel-2_src_prepare() {
	debug-print "Applying any user patches"
	eapply_user
}

# @FUNCTION: kernel-2_src_compile
# @USAGE:
# @DESCRIPTION:
# conpile headers or run deblob script

kernel-2_src_compile() {
	cd "${S}" || die

	if [[ ${K_DEBLOB_AVAILABLE} == 1 ]] && use deblob; then
		einfo ">>> Patching deblob script for forcing awk ..."
		sed -i '/check="\/bin\/sh $check"/a \  check="$check --use-awk"' \
			"${T}/${DEBLOB_A}" || die "Failed to patch ${DEBLOB_A}"
		einfo ">>> Running deblob script ..."
		sh "${T}/${DEBLOB_A}" --force || die "Deblob script failed to run!!!"
	fi
}

# @FUNCTION: kernel-2_src_test
# @USAGE:
# @DESCRIPTION:
# if you leave it to the default src_test, it will run make to
# find whether test/check targets are present; since "make test"
# actually produces a few support files, they are installed even
# though the package is binchecks-restricted.
#
# Avoid this altogether by making the function moot.
kernel-2_src_test() { :; }

# @FUNCTION: kernel-2_pkg_preinst
# @DESCRIPTION:
# if ETYPE = headers, call preinst_headers

kernel-2_pkg_preinst() {
	[[ ${ETYPE} == headers ]] && preinst_headers
}

# @FUNCTION: kernel-2_src_install
# @USAGE:
# @DESCRIPTION:
# Install headers or sources dependent on ETYPE

kernel-2_src_install() {
	install_universal
	[[ ${ETYPE} == headers ]] && install_headers
	[[ ${ETYPE} == sources ]] && install_sources
}

# @FUNCTION: kernel-2_pkg_postinst
# @USAGE:
# @DESCRIPTION:
# call postinst_sources for ETYPE = sources

kernel-2_pkg_postinst() {
	[[ ${ETYPE} == sources ]] && postinst_sources
}

# @FUNCTION: kernel-2_pkg_setup
# @USAGE:
# @DESCRIPTION:
# check for supported kernel version, die if ETYPE is unknown, call setup_headers
# if necessary

kernel-2_pkg_setup() {

	ABI="${KERNEL_ABI}"
	if [[ ${ETYPE} != sources && ${ETYPE} != headers ]]; then
		eerror "Unknown ETYPE=\"${ETYPE}\", must be \"sources\" or \"headers\""
		die "Unknown ETYPE=\"${ETYPE}\", must be \"sources\" or \"headers\""
	fi

	[[ ${ETYPE} == headers ]] && setup_headers
	[[ ${ETYPE} == sources ]] && einfo ">>> Preparing to unpack ..."
}

# @FUNCTION: kernel-2_pkg_postrm
# @USAGE:
# @DESCRIPTION:
# Notify the user that after a depclean, there may be sources
# left behind that need to be manually cleaned

kernel-2_pkg_postrm() {
	# This warning only makes sense for kernel sources.
	[[ ${ETYPE} == headers ]] && return 0

	# If there isn't anything left behind, then don't complain.
	[[ -e ${EROOT}/usr/src/linux-${KV_FULL} ]] || return 0
	ewarn "Note: Even though you have successfully unmerged "
	ewarn "your kernel package, directories in kernel source location: "
	ewarn "${EROOT}/usr/src/linux-${KV_FULL}"
	ewarn "with modified files will remain behind. By design, package managers"
	ewarn "will not remove these modified files and the directories they reside in."
	ewarn "For more detailed kernel removal instructions, please see: "
	ewarn "https://wiki.gentoo.org/wiki/Kernel/Removal"
}

EXPORT_FUNCTIONS src_{unpack,prepare,compile,install,test} \
	pkg_{setup,preinst,postinst,postrm}