File: rules

package info (click to toggle)
kernel-package 8.135
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,008 kB
  • ctags: 696
  • sloc: perl: 3,089; makefile: 2,344; sh: 538
file content (2731 lines) | stat: -rwxr-xr-x 106,717 bytes parent folder | download
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
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
#! /usr/bin/make -f
############################ -*- Mode: Makefile -*- ###########################
## debian.rules ---
## Author	    : Manoj Srivastava ( srivasta@pilgrim.umass.edu )
## Created On	    : Sat Apr 27 06:36:31 1996
## Created On Node  : melkor.pilgrim.umass.edu
## Last Modified By : Manoj Srivastava
## Last Modified On : Sat May  7 02:40:22 2005
## Last Machine Used: glaurung.internal.golden-gryphon.com
## Update Count	    : 812
## Status	    : Unknown, Use with caution!
## HISTORY	    :
## Description	    :
## arch-tag: aa70d4e5-79bf-405c-95ec-5fa9f7ae7b69
###############################################################################

DPKG_ARCH := dpkg-architecture

ifeq ($(strip $(KPKG_ARCH)),um)
  MAKING_VIRTUAL_IMAGE:=YES
endif
ifeq ($(strip $(KPKG_ARCH)),xen)
  MAKING_VIRTUAL_IMAGE:=YES
endif

ifdef KPKG_ARCH
  ifeq ($(strip $(MAKING_VIRTUAL_IMAGE)),)
      ha:=-a$(KPKG_ARCH)
  endif
endif

# set the dpkg-architecture vars
export DEB_BUILD_ARCH      := $(shell $(DPKG_ARCH) $(ha) -qDEB_BUILD_ARCH)
export DEB_BUILD_GNU_CPU   := $(shell $(DPKG_ARCH) $(ha) -qDEB_BUILD_GNU_CPU)
export DEB_BUILD_GNU_SYSTEM:= $(shell $(DPKG_ARCH) $(ha) -qDEB_BUILD_GNU_SYSTEM)
export DEB_BUILD_GNU_TYPE  := $(shell $(DPKG_ARCH) $(ha) -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_ARCH       := $(shell $(DPKG_ARCH) $(ha) -qDEB_HOST_ARCH)
export DEB_HOST_ARCH_OS    := $(shell $(DPKG_ARCH) $(ha) -qDEB_HOST_ARCH_OS     \
                                2>/dev/null|| true)
export DEB_HOST_GNU_CPU    := $(shell $(DPKG_ARCH) $(ha) -qDEB_HOST_GNU_CPU)
export DEB_HOST_GNU_SYSTEM := $(shell $(DPKG_ARCH) $(ha) -qDEB_HOST_GNU_SYSTEM)
export DEB_HOST_GNU_TYPE   := $(shell $(DPKG_ARCH) $(ha) -qDEB_HOST_GNU_TYPE)

# arrgh. future proofing
ifeq ($(DEB_HOST_GNU_SYSTEM), linux)
  DEB_HOST_GNU_SYSTEM=linux-gnu
endif
ifeq ($(DEB_HOST_ARCH_OS),)
  ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
    DEB_HOST_ARCH_OS := linux
  endif
  ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
    DEB_HOST_ARCH_OS := kfreebsd
  endif
endif
#
# VERSION=$(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | \
#                          sed 's/^Version: *//')
#

# The version of kernel-package this belongs to
kpkg_version := =K=V

# The maintainer information.
maintainer = Debian Kernel Team
email= debian-kernel@lists.debian.org

pgp=$(maintainer)

# Where we read our config information from
CONFLOC    :=$(shell if test -f ~/.kernel-pkg.conf; then\
                        echo ~/.kernel-pkg.conf; \
                     else \
                        echo /etc/kernel-pkg.conf; \
                     fi)
# Where the package libs are stored
LIBLOC     :=/usr/share/kernel-package
# Default location of the modules
ifeq ($(strip $(MODULE_LOC)),)
MODULE_LOC =/usr/src/modules
endif
#
DEBDIR      = $(LIBLOC)
DEBDIR_NAME = $(shell basename $(DEBDIR))

DEBIAN_FILES = kernel_version.mk config kpkg-vercheck Control Control.bin86 rules \
               README README.grub README.headers README.tecra README.modules      \
               sample.module.control Flavours Rationale copyright.source          \
               README.Debian src.postinst README.source include.postinst          \
               copyright.headers README.headers README.doc copyright.doc          \
               src.postinst image.postinst image.postrm image.preinst image.prerm \
               xen.postinst xen.prerm um.postinst um.prerm linux.1 $(loaderdoc)   \
               README.image copyright.image
DEBIAN_DIRS  = Config

#  Package specific stuff
# decide if image is meant to be in /boot rather than /
link_in_boot :=
# Can we use symlinks?
no_symlink :=
# If so, where is the real file (usually, vmlinuz-X.X.X is real, and
# vmlinuz is the link, this variable reverses it.
reverse_symlink :=

# The version numbers for kernel-image, kernel-headers and
# kernel-source are deduced from the Makefile (see below,
# and footnote 1 for details)

# Whether to look for and install kernel patches by default.
# Be very careful if you do this.
patch_the_kernel := AUTO

# do not create libc headers by default
make_libc_headers := NO

# run make clean after build
do_clean := NO

# install uncompressed kernel ELF-image (for oprofile)
int_install_vmlinux := NO

# what kernel config target to run in our configure target.
config_target := oldconfig


# The default architecture (all if architecture independent)
CROSS_ARG:=

ifdef KPKG_ARCH
  architecture:=$(KPKG_ARCH)
else
  #architecture:=$(shell CC=$(HOSTCC) dpkg --print-gnu-build-architecture)
  #architecture:=$(DEB_HOST_ARCH)
  architecture:=$(DEB_HOST_GNU_CPU)
  ifeq ($(architecture), x86_64)
    architecture:=amd64
  endif
endif

ifndef CROSS_COMPILE
  ifeq ($(strip $(MAKING_VIRTUAL_IMAGE)),)
    ifneq ($(strip $(architecture)),$(strip $(DEB_BUILD_ARCH)))
      #KERNEL_CROSS:=$(architecture)-$(strip $(DEB_HOST_ARCH_OS))-
      KERNEL_CROSS:=$(DEB_HOST_GNU_TYPE)-
      ifeq ($(architecture), amd64)
        KERNEL_CROSS:=$(architecture)-$(strip $(DEB_HOST_ARCH_OS))-
      endif
    endif
  endif
else
  KERNEL_CROSS:=$(CROSS_COMPILE)-
endif

KERNEL_CROSS:=$(shell echo $(KERNEL_CROSS) | sed -e 's,--$$,-,')

ifneq ($(strip $(KERNEL_CROSS)),)
  CROSS_ARG:=CROSS_COMPILE=$(KERNEL_CROSS)
endif

KERNEL_ARCH:=$(architecture)
DEBCONFIG = $(CONFDIR)/config
IMAGEDIR=/boot
INT_IMAGE_DESTDIR=debian/tmp-image$(IMAGEDIR)

comma:= ,
empty:=
space:= $(empty) $(empty)


ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
  PMAKE = PATH=/usr/lib/freebsd/:$(CURDIR)/bin:$(PATH) WERROR= MAKEFLAGS= freebsd-make
endif

# Install rules
install_file=    install -p    -o root -g root -m 644
install_program= install -p    -o root -g root -m 755
make_directory=  install -p -d -o root -g root -m 755
deb_rule = $(MAKE) -f $(DEBDIR)/rules

ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
  # localversion_files := $(wildcard localversion*)
  # VERSION =$(shell grep -E '^VERSION +=' Makefile 2>/dev/null | \
  #  sed -e 's/[^0-9]*\([0-9]*\)/\1/')
  # PATCHLEVEL =$(shell grep -E '^PATCHLEVEL +=' Makefile 2>/dev/null | \
  #  sed -e 's/[^0-9]*\([0-9]*\)/\1/')
  # SUBLEVEL =$(shell grep -E '^SUBLEVEL +=' Makefile 2>/dev/null | \
  #  sed -e 's/[^0-9]*\([0-9]*\)/\1/')
  # EXTRA_VERSION =$(shell grep -E '^EXTRAVERSION +=' Makefile 2>/dev/null | \
  #  sed -e 's/EXTRAVERSION *= *\([^ \t]*\)/\1/')
  # LOCALVERSION = $(subst $(space),, $(shell cat /dev/null $(localversion_files)) \
  #                  $(CONFIG_LOCALVERSION))

  # Could have used :=, but some patches do seem to patch the
  # Makefile. perhaps deferring the rule makes that better
  VERSION      :=$(shell $(MAKE) $(CROSS_ARG) --no-print-directory -sf $(DEBDIR)/kernel_version.mk debian_VERSION)
  PATCHLEVEL   :=$(shell $(MAKE) $(CROSS_ARG) --no-print-directory -sf $(DEBDIR)/kernel_version.mk debian_PATCHLEVEL)
  SUBLEVEL     :=$(shell $(MAKE) $(CROSS_ARG) --no-print-directory -sf $(DEBDIR)/kernel_version.mk debian_SUBLEVEL)
  EXTRA_VERSION:=$(shell $(MAKE) $(CROSS_ARG) --no-print-directory -sf $(DEBDIR)/kernel_version.mk debian_EXTRAVERSION)
  LOCALVERSION :=$(shell $(MAKE) $(CROSS_ARG) --no-print-directory -sf $(DEBDIR)/kernel_version.mk debian_LOCALVERSION)
else
  ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
    VERSION        =$(shell grep '^REVISION=' conf/newvers.sh | \
      sed -e 's/[^0-9]*\([0-9]\)\..*/\1/')
    PATCHLEVEL =$(shell grep '^REVISION=' conf/newvers.sh |     \
     sed -e 's/[^0-9]*[0-9]*\.\([0-9]*\)[^0-9]*/\1/')
    SUBLEVEL =0
    EXTRA_VERSION =$(shell grep '^RELEASE=' conf/newvers.sh |   \
     sed -e 's/[^0-9]*\([0-9]*\)[^0-9]*/\1/')
    LOCALVERSION = $(subst $(space),,                           \
       $(shell cat /dev/null $(localversion_files)) $(CONFIG_LOCALVERSION))
  endif
endif



HAVE_NEW_MODLIB =$(shell grep -E '\(INSTALL_MOD_PATH\)' Makefile 2>/dev/null )

ifneq ($(strip $(EXTRA_VERSION)),)
HAS_ILLEGAL_EXTRA_VERSION =$(shell                         \
    perl -e '$$i="$(EXTRA_VERSION)"; $$i !~ m/^[a-z\.\-\+][a-z\d\.\-\+]*$$/o && print YES;')
  ifneq ($(strip $(HAS_ILLEGAL_EXTRA_VERSION)),)
    $(error Error: The EXTRAVERSION may only contain lowercase alphanumerics \
 and  the  characters  - +  . The current value is: $(EXTRA_VERSION). Aborting.)
  endif
endif

AM_OFFICIAL := $(shell if [ -f debian/official ]; then echo YES; fi )

######################################################################
###          Architecture specific stuff                           ###
######################################################################
# Each architecture has the following specified for it
# (a) The kernel image type (i.e. zImage or bzImage)
# (b) The dependency on a loader, if any
# (c) The name of the loader
# (d) The name of the documentation file for the loader
# (e) The build target
# (f) The location of the kernelimage source
# (g) The location of the kernelimage destination
# (h) The name of the arch specific configuration file
# Some architectures has sub architectures

### m68k
ifeq ($(strip $(architecture)),m68k)
  ifeq (,$(findstring /$(KPKG_SUBARCH)/,/amiga/atari/mac/mvme147/mvme16x/bvme6000/))
    GUESS_SUBARCH:=$(shell awk '/Model/ { print $$2}' /proc/hardware)
    ifneq (,$(findstring Motorola,$(GUESS_SUBARCH)))
     GUESS_SUBARCH:=$(shell awk '/Model/ { print $$3}' /proc/hardware)
     ifneq (,$(findstring MVME147,$(GUESS_SUBARCH)))
      KPKG_SUBARCH:=mvme147
     else
      KPKG_SUBARCH:=mvme16x
     endif
    else
     ifneq (,$(findstring BVME,$(GUESS_SUBARCH)))
      KPKG_SUBARCH:=bvme6000
     else
      ifneq (,$(findstring Amiga,$(GUESS_SUBARCH)))
       KPKG_SUBARCH:=amiga
      else
       ifneq (,$(findstring Atari,$(GUESS_SUBARCH)))
        KPKG_SUBARCH:=atari
       else
        ifneq (,$(findstring Mac,$(GUESS_SUBARCH)))
         KPKG_SUBARCH:=mac
        endif
       endif
      endif
     endif
    endif
  endif
  NEED_DIRECT_GZIP_IMAGE=NO
  kimage := zImage
  target = $(kimage)
  kimagesrc = vmlinux.gz
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  kelfimagesrc = vmlinux
  kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  DEBCONFIG = $(CONFDIR)/config.$(KPKG_SUBARCH)
  ifneq (,$(findstring $(KPKG_SUBARCH),mvme147 mvme16x bvme6000))
    loaderdep=vmelilo
    loader=vmelilo
    loaderdoc=VmeliloDefault
  else
    loaderdep=
    loader=lilo
    loaderdoc=
  endif
endif

### ARM
ifeq ($(strip $(architecture)),arm)
  GUESS_SUBARCH:='netwinder'

  ifneq (,$(findstring $(KPKG_SUBARCH),netwinder))
    KPKG_SUBARCH:=$(GUESS_SUBARCH)
    kimage := zImage
    target = Image
    kimagesrc = arch/$(KERNEL_ARCH)/boot/Image
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    loaderdep=
    loader=nettrom
    loaderdoc=
    NEED_DIRECT_GZIP_IMAGE=NO
    DEBCONFIG= $(CONFDIR)/config.netwinder
  else
    kimage := zImage
    target = zImage
    NEED_DIRECT_GZIP_IMAGE=NO
    kimagesrc = arch/$(KERNEL_ARCH)/boot/zImage
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    DEBCONFIG = $(CONFDIR)/config.arm
  endif
  kelfimagesrc = vmlinux
  kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
endif

##### PowerPC64
ifeq ($(strip $(architecture)), powerpc64)
  KERNEL_ARCH=ppc64
  kimage := vmlinux
  loader=NoLoader
  target = $(kimage)
  kimagesrc = vmlinux
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  DEBCONFIG= $(CONFDIR)/config.$(KPKG_SUBARCH)
  kelfimagesrc = vmlinux
  kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
endif

### PowerPC
ifneq ($(strip $(filter ppc powerpc,$(architecture))),)
  ifeq ($(DEB_BUILD_ARCH),powerpc)
  # This is only meaningful when building on a PowerPC
    ifeq ($(GUESS_SUBARCH),)
      GUESS_SUBARCH:=$(shell awk '/machine/ { print $$3}' /proc/cpuinfo)
      ifneq (,$(findstring Power,$(GUESS_SUBARCH)))
        GUESS_SUBARCH:=pmac
      else
        # At the request of Colin Watson, changed from find string iMac.
        # Any powerpc system that would contain  Mac in /proc/cpuinfo is a
        # PowerMac system, according to arch/ppc/platforms/* in the kernel source
        ifneq (,$(findstring Mac,$(GUESS_SUBARCH)))
          GUESS_SUBARCH:=pmac
        endif
      endif
    else
      GUESS_SUBARCH:=pmac
    endif
    # Well NuBus powermacs are not pmac subarchs, but nubus ones.
    #ifeq (,$(shell grep NuBus /proc/cpuinfo))
    #  GUESS_SUBARCH:=nubus
    #endif
  endif

  ifeq (,$(findstring $(KPKG_SUBARCH),apus prpmc chrp mbx pmac prep Amiga APUs CHRP MBX PReP chrp-rs6k nubus ))
    KPKG_SUBARCH:=$(GUESS_SUBARCH)
  endif

  KERNEL_ARCH:=ppc

  ifneq (,$(findstring $(KPKG_SUBARCH),APUs apus Amiga))
    KPKG_SUBARCH:=apus
    loader := NoLoader
    kimage := vmapus.gz
    target = zImage
    kimagesrc = $(shell if [ -d arch/$(KERNEL_ARCH)/boot/images ]; then \
	echo arch/$(KERNEL_ARCH)/boot/images/vmapus.gz ; else \
	echo arch/$(KERNEL_ARCH)/boot/$(kimage) ; fi)
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
    DEBCONFIG = $(CONFDIR)/config.apus
  endif

  ifneq (,$(findstring $(KPKG_SUBARCH),chrp-rs6k))
    KPKG_SUBARCH:=chrp-rs6k
    loaderdep=quik
    loader=quik
    loaderdoc=QuikDefault
    kimage := zImage
    target = $(kimage)
    kimagesrc = $(shell if [ -d arch/$(KERNEL_ARCH)/chrpboot ]; then \
	echo arch/$(KERNEL_ARCH)/chrpboot/$(kimage) ; else \
	echo arch/$(KERNEL_ARCH)/boot/images/$(kimage).chrp-rs6k ; fi)
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
    DEBCONFIG = $(CONFDIR)/config.chrp
  endif

  ifneq (,$(findstring $(KPKG_SUBARCH),CHRP chrp))
    KPKG_SUBARCH:=chrp
    loaderdep=quik
    loader=quik
    loaderdoc=QuikDefault
    kimage := zImage
    target = $(kimage)
    kimagesrc = $(shell if [ -d arch/$(KERNEL_ARCH)/chrpboot ]; then \
         echo arch/$(KERNEL_ARCH)/chrpboot/$(kimage) ; else \
         echo arch/$(KERNEL_ARCH)/boot/images/$(kimage).chrp ; fi)
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
    DEBCONFIG = $(CONFDIR)/config.chrp
  endif

  ifneq (,$(findstring $(KPKG_SUBARCH),PRPMC prpmc))
    KPKG_SUBARCH:=prpmc
    loader := NoLoader
    kimage := zImage
    target = $(kimage)
    kimagesrc = arch/$(KERNEL_ARCH)/boot/images/zImage.pplus
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  endif

  ifneq (,$(findstring $(KPKG_SUBARCH),MBX mbx))
    KPKG_SUBARCH:=mbx
    loader := NoLoader
    kimage := zImage
    target = $(kimage)
    kimagesrc = $(shell if [ -d arch/$(KERNEL_ARCH)/mbxboot ]; then \
	echo arch/$(KERNEL_ARCH)/mbxboot/$(kimage) ; else \
	echo arch/$(KERNEL_ARCH)/boot/images/zvmlinux.embedded; fi)
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
    DEBCONFIG = $(CONFDIR)/config.mbx
  endif

  ifneq (,$(findstring $(KPKG_SUBARCH),pmac))
    KPKG_SUBARCH:=pmac
    target := zImage
    ifeq ($(DEB_BUILD_ARCH),powerpc)
      # This is only meaningful when building on a PowerPC
      ifneq (,$(shell grep NewWorld /proc/cpuinfo))
        loaderdep=yaboot
        loader=yaboot
        #loaderdoc=
      else
        loaderdep=quik
        loader=quik
        loaderdoc=QuikDefault
      endif
    else
      loaderdep=yaboot
      loader=yaboot
    endif
    kimagesrc = vmlinux
    kimage := vmlinux
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
    HAVE_COFF_IMAGE = YES
    coffsrc = $(shell if [ -d arch/$(KERNEL_ARCH)/coffboot ]; then \
         echo arch/$(KERNEL_ARCH)/coffboot/$(kimage).coff ; else \
         echo arch/$(KERNEL_ARCH)/boot/images/$(kimage).coff ; fi)
    coffdest=$(INT_IMAGE_DESTDIR)/vmlinux.coff-$(version)
    DEBCONFIG = $(CONFDIR)/config.pmac
  endif

  ifneq (,$(findstring $(KPKG_SUBARCH),PReP prep))
    KPKG_SUBARCH:=prep
    loader := NoLoader
    kimage := zImage
    target = $(kimage)
    kimagesrc = $(shell if [ -d arch/$(KERNEL_ARCH)/boot/images ]; then \
         echo arch/$(KERNEL_ARCH)/boot/images/$(kimage).prep ; else \
         echo arch/$(KERNEL_ARCH)/boot/$(kimage) ; fi)
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
    DEBCONFIG = $(CONFDIR)/config.prep
  endif

  ifneq (,$(findstring $(KPKG_SUBARCH), NuBuS nubus))
    KPKG_SUBARCH := nubus
    target := zImage
    loader= NoLoader
    kimagesrc = arch/$(KERNEL_ARCH)/appleboot/Mach\ Kernel
    kimage := vmlinux
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  endif

endif


##### Alpha
ifeq ($(strip $(architecture)),alpha)
  kimage := vmlinuz
  loaderdep=
  loader=milo
  loaderdoc=
  target = boot
  kimagesrc = arch/$(KERNEL_ARCH)/boot/vmlinux.gz
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  kelfimagesrc = vmlinux
  kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  DEBCONFIG = $(CONFDIR)/config.alpha
endif


##### Sparc
ifeq ($(strip $(architecture)),sparc)
  kimage := vmlinuz
  loaderdep = silo
  loader = silo
  loaderdoc=SiloDefault
  NEED_DIRECT_GZIP_IMAGE = YES
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  DEBCONFIG = $(CONFDIR)/config.sparc
  ifeq (,$(APPEND_TO_VERSION))
    ARCH_IN_NAME = YES
  endif

  ifeq (,$(KPKG_SUBARCH))
    ifeq (sparc64,$(strip $(shell uname -m)))
      KPKG_SUBARCH = sparc64
    else
      KPKG_SUBARCH = sparc32
    endif
  endif

  ifneq (,$(filter sparc64%,$(KPKG_SUBARCH)))
     KERNEL_ARCH = sparc64
  else
     ifneq (,$(filter sparc%,$(KPKG_SUBARCH)))
        KERNEL_ARCH = sparc
     else
        KERNEL_ARCH = $(strip $(shell uname -m))
     endif
  endif

  ifneq ($(shell if [ $(VERSION)  -ge  2 ] && [ $(PATCHLEVEL) -ge 5 ] &&  \
                    [ $(SUBLEVEL) -ge 41 ]; then echo new; \
               elif [ $(VERSION)  -ge  2 ] && [ $(PATCHLEVEL) -ge 6 ]; then \
                                            echo new; \
               elif [ $(VERSION)  -ge  3 ]; then echo new; fi),)
    target    = image
    kimagesrc = arch/$(KERNEL_ARCH)/boot/image
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  else
    target    = vmlinux
    kimagesrc = vmlinux
  endif
endif

##### amd64
ifeq ($(strip $(architecture)),amd64)
  KERNEL_ARCH=x86_64
  kimage := bzImage
  loaderdep=lilo (>= 19.1) | grub
  loader=lilo
  loaderdoc=LiloDefault
  target = $(kimage)
  kimagesrc = $(strip arch/$(KERNEL_ARCH)/boot/$(kimage))
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  DEBCONFIG= $(CONFDIR)/config.$(KPKG_SUBARCH)
  kelfimagesrc = vmlinux
  kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
endif


##### i386 and such
ifeq ($(strip $(architecture)),i386)
  # sub archs can be i386 i486 i586 i686
  GUESS_SUBARCH:=$(shell if test -f .config; then \
                        perl -nle '/^CONFIG_M(.86)=y/ && print "$$1"' .config;\
                       else \
                         uname -m;\
                       fi)
  ifeq (,$(findstring $(KPKG_SUBARCH),i386 i486 i586 i686))
    KPKG_SUBARCH:=$(GUESS_SUBARCH)
  endif
  DEBCONFIG= $(CONFDIR)/config.$(KPKG_SUBARCH)
  ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
    kimage := bzImage
    loaderdep=lilo (>= 19.1) | grub
    loader=lilo
    loaderdoc=LiloDefault
    target = $(kimage)
    kimagesrc = $(strip arch/$(KERNEL_ARCH)/boot/$(kimage))
    kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
    kelfimagesrc = vmlinux
    kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  else
    loaderdep=grub | grub2
    loader=grub
    ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
      kimagesrc = $(strip $(KERNEL_ARCH)/compile/GENERIC/kernel)
      kimagedest = $(INT_IMAGE_DESTDIR)/kfreebsd-$(version)
    endif
  endif
endif

##### S/390
ifeq ($(strip $(architecture)),s390)
  # make it possible to build s390x kernels on s390 for 2.4 kernels only
  # because 2.6 always use s390 as architecture.
  ifeq (4,$(PATCHLEVEL))
    ifeq (,$(findstring $(KPKG_SUBARCH),s390 s390x))
      KPKG_SUBARCH = s390
    endif
    KERNEL_ARCH = $(KPKG_SUBARCH)
    ifneq ($(shell uname -m),$(KPKG_SUBARCH))
      UNAME_MACHINE = $(KPKG_SUBARCH)
      export UNAME_MACHINE
    endif
  endif
  kimage := zimage
  loaderdep=zipl
  loader=zipl
  loaderdoc=
  target = image
  NEED_DIRECT_GZIP_IMAGE=NO
  kimagesrc = $(strip arch/$(KERNEL_ARCH)/boot/$(target))
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  DEBCONFIG= $(CONFDIR)/config.$(KPKG_SUBARCH)
  kelfimagesrc = vmlinux
  kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
endif

##### hppa
ifeq ($(strip $(architecture)),hppa)
  kimage := vmlinux
  loaderdep=palo
  loader=palo
  loaderdoc=
  target=$(kimage)
  NEED_DIRECT_GZIP_IMAGE=NO
  # Override arch name because hppa uses arch/parisc not arch/hppa
  KERNEL_ARCH := parisc
  kimagesrc=$(kimage)
  kimagedest=$(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  # This doesn't seem to work, but the other archs do it...
  DEBCONFIG=$(CONFDIR)/config.$(KPKG_SUBARCH)
endif

##### ia64
ifeq ($(strip $(architecture)),ia64)
  kimage := vmlinuz
  loaderdep=elilo
  loader=elilo
  loaderdoc=
  target=compressed
  NEED_DIRECT_GZIP_IMAGE=NO
  kimagesrc=vmlinux.gz
  kimagedest=$(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  kelfimagesrc = vmlinux
  kelfimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)
  DEBCONFIG=$(CONFDIR)/config.$(KPKG_SUBARCH)
endif

##### mips
ifeq ($(strip $(architecture)),mips)
  # SGI ELF32
  ifneq (,$(filter r4k-ip22 r5k-ip22 r5k-ip32 r10k-ip32,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux
    loaderdep = arcboot
    loader = arcboot
    loaderdoc =
  endif
  # SGI ELF64
  ifneq (,$(filter r10k-ip27 r10k-ip28 r10k-ip30,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux.64
    loaderdep = arcboot
    loader = arcboot
    loaderdoc =
  endif
  # Broadcom SWARM
  ifneq (,$(filter sb1-swarm-bn,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux
    loaderdep = sibyl
    loader = sibyl
    loaderdoc =
  endif

  # Default value for e.g. a generic header package
  ifeq (,$(kimage))
    kimage := vmlinux
  endif
  ifeq (,$(kimagesrc))
    kimagesrc := $(kimage)
  endif

  NEED_DIRECT_GZIP_IMAGE = NO
  kimagesrc = $(kimage)
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)

  ifneq ($(shell if [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge 5 ] &&    \
                    [ $(SUBLEVEL) -ge 41 ]; then echo new;                   \
               elif [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge 6 ]; then \
                                            echo new;                        \
               elif [ $(VERSION)  -ge  3 ]; then echo new; fi),)
    target =
  else
    target = boot
  endif

  ifneq (,$(filter mips64%,$(KPKG_SUBARCH)))
    KERNEL_ARCH = mips64
  endif
  ifneq (,$(filter %-64,$(KPKG_SUBARCH)))
    KERNEL_ARCH = mips64
  endif
endif

##### mipsel
ifeq ($(strip $(architecture)),mipsel)
  # DECstations
  ifneq (,$(filter r3k-kn02 r4k-kn04,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux
    kimagesrc = $(kimage)
    loaderdep = delo
    loader = delo
    loaderdoc =
  endif
  # Cobalt
  ifneq (,$(filter r5k-cobalt,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux
    kimagesrc = $(kimage)
    loaderdep = colo
    loader = colo
    loaderdoc =
  endif
  # LASAT
  ifneq (,$(filter r5k-lasat,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux
    kimagesrc = $(kimage)
    loaderdep =
    loader =
    loaderdoc =
  endif
  # Broadcom SWARM
  ifneq (,$(filter sb1-swarm-bn,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux
    kimagesrc = $(kimage)
    loaderdep = sibyl
    loader = sibyl
    loaderdoc =
  endif
  # xxs1500
  ifneq (,$(filter xxs1500,$(strip $(KPKG_SUBARCH))))
    kimage := vmlinux.srec
    kimagesrc = $(strip arch/$(KERNEL_ARCH)/boot/$(kimage))
    loaderdep =
    loader =
    loaderdoc =
  endif

  # Default value for e.g. a generic header package
  ifeq (,$(kimage))
    kimage := vmlinux
  endif
  ifeq (,$(kimagesrc))
    kimagesrc := $(kimage)
  endif

  NEED_DIRECT_GZIP_IMAGE = NO
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinux-$(version)

  ifneq ($(shell if [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge 5 ] &&    \
                    [ $(SUBLEVEL) -ge 41 ]; then echo new;                   \
               elif [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge 6 ]; then \
                                            echo new;                        \
               elif [ $(VERSION)  -ge  3 ]; then echo new; fi),)
    target =
  else
    target = boot
  endif

  KERNEL_ARCH = mips
  ifneq (,$(filter mips64el%,$(KPKG_SUBARCH)))
    KERNEL_ARCH = mips64
  endif
  ifneq (,$(filter %-64,$(KPKG_SUBARCH)))
    KERNEL_ARCH = mips64
  endif
endif

##### m32r
ifeq ($(strip $(architecture)),m32r)
  KERNEL_ARCH := m32r
  kimage := zImage
  loaderdep=
  loader=
  loaderdoc=
  target = $(kimage)
  kimagesrc = $(strip arch/$(KERNEL_ARCH)/boot/$(kimage))
  kimagedest = $(INT_IMAGE_DESTDIR)/vmlinuz-$(version)
  DEBCONFIG= $(CONFDIR)/config.$(KPKG_SUBARCH)
endif


# usermode linux
ifeq ($(strip $(architecture)),um)
  DEBCONFIG = $(CONFDIR)/config.um


  ifneq ($(shell if [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge 6 ] &&    \
                    [ $(SUBLEVEL) -ge 9 ]; then echo new;                   \
               elif [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge r ]; then \
                                            echo new;                        \
               elif [ $(VERSION)  -ge  3 ]; then echo new; fi),)
    target  = vmlinux
    kimage := vmlinux
  else
    target  = linux
    kimage := linux
  endif


  kimagesrc  = $(strip $(kimage))
  INT_IMAGE_DESTDIR=$(IMAGE_DOC)
  kimagedest = debian/tmp-image$(IMAGEDIR)/linux-$(version)
  loaderdep=
  loaderdoc=
  KERNEL_ARCH = um
  architecture = i386
  IMAGEDIR = /usr/bin
endif

# xen-linux
ifeq ($(strip $(architecture)),xen)
  KERNEL_ARCH = xen
  architecture = i386

  ifeq (,$(findstring $(KPKG_SUBARCH),xen0 xenu))
       KPKG_SUBARCH:=xen0
  endif
  DEBCONFIG = $(CONFDIR)/config.$(KPKG_SUBARCH)

  ifneq ($(shell if [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge 5 ] &&    \
                    [ $(SUBLEVEL) -ge 41 ]; then echo new;                   \
               elif [ $(VERSION)  -ge  2 ]  && [ $(PATCHLEVEL) -ge 6 ]; then \
                                            echo new;                        \
               elif [ $(VERSION)  -ge  3 ]; then echo new; fi),)
    target    = vmlinuz
  else
    target    = bzImage
  endif
  kimage := $(target)

  ifeq (,$(filter xen0,$(KPKG_SUBARCH)))
     # only domain-0 are bootable via xen so only domain0 subarch needs grub and xen-vm
     loaderdep=grub,xen-vm
     loader=grub
     loaderdoc=
  else
     loaderdep=
     loader=
     loaderdoc=
  endif

  kimagesrc = $(kimage)
  kimagedest = $(INT_IMAGE_DESTDIR)/xen-linux-$(version)
endif


######################################################################
######################################################################

ifneq ($(strip $(KPKG_STEM)),)
INT_STEM := $(KPKG_STEM)
else
INT_STEM := kernel
endif

ifneq ($(strip $(loaderdep)),)
  int_loaderdep := $(loaderdep),
else
  int_loaderdep :=
endif



# The following variable is used to simplify the process of making
# diffs and orig targets, Please set it if AND ONLY IF no patches
# have been applied to the upstream source, in other words, we have
# just added debian* files.  If any patches were applied, please
# leave it empty

# NO_SRC_CHANGES =
NO_SRC_CHANGES = YES


# NOTE: FLAVOUR is now obsolete
# If you want to have more than one kernel configuration per kernel
# version, set FLAVOUR in the top level kernel Makefile *before*
# invoking make-kpkg -- it will be appended to UTS_RELEASE in
# version.h (separated by a hyphen). This affects everything -- the
# names and versions of the image, source, headers, and doc packages,
# and where the modules are searched for in /lib/modules.

ifdef FLAVOUR
# uhm - should check if we really have a Makefile capable of Flavours?
endif

FLAVOUR:=$(shell grep ^FLAVOUR Makefile 2>/dev/null | \
                  perl -ple 's/FLAVOUR[\s:=]+//g')

ifeq ($(strip $(FLAVOUR_SEP)),)
FLAVOUR_SEP:= +
endif

ifneq ($(strip $(FLAVOUR)),)
INT_FLAV := $(FLAVOUR_SEP)$(FLAVOUR)
FLAV_ARG := FLAVOUR=$(FLAVOUR)
else
INT_FLAV :=
FLAV_ARG :=
endif

## This is the replacement for FLAVOUR
EXTRAVERSION =$(strip $(EXTRA_VERSION))
ifneq ($(strip $(APPEND_TO_VERSION)),)
iatv := $(strip $(APPEND_TO_VERSION))
EXTRAV_ARG := EXTRAVERSION=${EXTRA_VERSION}${iatv}
else
iatv :=
EXTRAV_ARG :=
endif

UTS_RELEASE_VERSION=$(shell if [ -f include/linux/version.h ]; then                     \
                 grep 'define UTS_RELEASE' include/linux/version.h |                    \
                 perl -nle  'm/^\s*\#define\s+UTS_RELEASE\s+("?)(\S+)\1/g && print $$2;';\
                 else echo "" ;                                                         \
                 fi)

version = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)$(iatv)$(INT_FLAV)$(LOCALVERSION)

# See if we are being run in the kernel directory
ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
  IN_KERNEL_DIR := $(shell if test -d drivers && test -d kernel && test -d fs && test \
                                   -d include/linux ; then                            \
                                      echo YES;                                       \
                           fi )
else
  ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
    IN_KERNEL_DIR := $(shell if test -d dev && test -d kern && test -d fs &&          \
                             test -d i386/include ; then echo YES; fi)
  endif
endif

IN_KERNEL_HEADERS=$(shell if [ -f $(INT_STEM)-headers.revision ]; then                \
                               cat $(INT_STEM)-headers.revision;                      \
                            else echo "" ;                                            \
                            fi)

ifeq ($(strip $(IN_KERNEL_DIR)),)
ifneq ($(strip $(IN_KERNEL_HEADERS)),)
version=$(UTS_RELEASE_VERSION)
debian :=$(IN_KERNEL_HEADERS)
endif
endif

# Bug out if the version number id not all lowercase
lc_version = $(shell echo $(version) | tr A-Z a-z)
ifneq ($(strip $(version)),$(strip $(lc_version)))
  ifeq ($(strip $(IGNORE_UPPERCASE_VERSION)),)
    $(error Error. The version number $(strip $(version)) is not all \
 lowercase. Since the version ends up in the package name of the \
 kernel image package, this is a Debian policy violation, and \
 the packaging system shall refuse to package the image. )
  else
    $(warn Error. The version number $(strip $(version)) is not all \
 lowercase. Since the version ends up in the package name of the \
 kernel image package, this is a Debian policy violation, and \
 the packaging system shall refuse to package the image. Lower -casing version.)

    version := $(strip $(lc_version))
  endif
endif



# KPKG_SUBARCH is used to distinguish Amiga, Atari, Macintosh, etc. kernels
# for Debian/m68k.  INT_SUBARCH is used in the names of the image file
# produced. It only affects the naming of the kernel-image as the
# source and doc packages are architecture independent and the
# kernel-headers do not vary from one sub-architecture to the next.

# This is the default
INT_SUBARCH :=

ifneq ($(strip $(ARCH_IN_NAME)),)
ifneq ($(strip $(KPKG_SUBARCH)),)
INT_SUBARCH := -$(KPKG_SUBARCH)
endif
endif

# The name of the package (for example, 'emacs').
package   := $(INT_STEM)-source-$(version)
h_package := $(INT_STEM)-headers-$(version)
ifeq ($(strip $(KERNEL_ARCH)),um)
	i_package := $(INT_STEM)-uml-$(version)$(INT_SUBARCH)
else
  ifeq ($(strip $(KERNEL_ARCH)),xen)
	i_package := $(INT_STEM)-$(KPKG_SUBARCH)-$(version)
  else
	i_package := $(INT_STEM)-image-$(version)$(INT_SUBARCH)
  endif
endif
d_package := $(INT_STEM)-doc-$(version)
l_package := libc-kheaders

SOURCE_TOP:= debian/tmp-source
HEADER_TOP:= debian/tmp-headers
IMAGE_TOP := debian/tmp-image
DOC_TOP   := debian/tmp-doc
MAN1DIR    = $(IMAGE_TOP)/usr/share/man/man1

SOURCE_DOC:= $(SOURCE_TOP)/usr/share/doc/$(package)
HEADER_DOC:= $(HEADER_TOP)/usr/share/doc/$(h_package)
IMAGE_DOC := $(IMAGE_TOP)/usr/share/doc/$(i_package)
DOC_DOC   := $(DOC_TOP)/usr/share/doc/$(d_package)

SOURCE_SRC:= $(SOURCE_TOP)/usr/src/$(package)
HEADER_SRC:= $(HEADER_TOP)/usr/src/$(h_package)
UML_DIR   := $(IMAGE_TOP)/usr/lib/uml/modules


FILES_TO_CLEAN  = modules/modversions.h modules/ksyms.ver debian/files \
                  conf.vars scripts/cramfs/cramfsck scripts/cramfs/mkcramfs \
                  applied_patches debian/buildinfo
STAMPS_TO_CLEAN = stamp-build stamp-configure stamp-source stamp-image \
                  stamp-headers stamp-src stamp-diff stamp-doc \
                  stamp-buildpackage stamp-libc-kheaders stamp-debian \
                  stamp-patch stamp-kernel-configure
DIRS_TO_CLEAN   = $(SOURCE_TOP) $(HEADER_TOP) $(IMAGE_TOP) $(DOC_TOP)

ifeq ($(strip $(VERSIONED_PATCH_DIR)),)
VERSIONED_PATCH_DIR         = $(shell if [ -d \
/usr/src/kernel-patches/$(architecture)/$(VERSION).$(PATCHLEVEL).$(SUBLEVEL) \
			       ]; then echo \
/usr/src/kernel-patches/$(architecture)/$(VERSION).$(PATCHLEVEL).$(SUBLEVEL); \
			    fi)
endif

ifeq ($(strip $(VERSIONED_ALL_PATCH_DIR)),)
VERSIONED_ALL_PATCH_DIR         = $(shell if [ -d \
/usr/src/kernel-patches/all/$(VERSION).$(PATCHLEVEL).$(SUBLEVEL) \
			       ]; then echo \
/usr/src/kernel-patches/all/$(VERSION).$(PATCHLEVEL).$(SUBLEVEL); \
			    fi)
endif

ifeq ($(strip $(PATCH_DIR)),)
PATCH_DIR  = $(shell if [ -d /usr/src/kernel-patches/$(architecture)/ ];\
                        then echo /usr/src/kernel-patches/$(architecture); \
	             fi)
endif

ifeq ($(strip $(ALL_PATCH_DIR)),)
ALL_PATCH_DIR  = $(shell if [ -d /usr/src/kernel-patches/all/ ]; \
                            then echo /usr/src/kernel-patches/all ;\
			 fi)
endif

VERSIONED_ALL_PATCH_APPLY   = $(VERSIONED_ALL_PATCH_DIR)/apply
VERSIONED_ALL_PATCH_UNPATCH = $(VERSIONED_ALL_PATCH_DIR)/unpatch

VERSIONED_DIR_PATCH_APPLY   = $(VERSIONED_PATCH_DIR)/apply
VERSIONED_DIR_PATCH_UNPATCH = $(VERSIONED_PATCH_DIR)/unpatch

ALL_PATCH_APPLY   = $(ALL_PATCH_DIR)/apply
ALL_PATCH_UNPATCH = $(ALL_PATCH_DIR)/unpatch

DIR_PATCH_APPLY   = $(PATCH_DIR)/apply
DIR_PATCH_UNPATCH = $(PATCH_DIR)/unpatch

# The destination of all .deb files
# (suggested by Rob Browning <osiris@cs.utexas.edu>)
DEB_DEST := ..
SRCTOP := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
INSTALL_MOD_PATH=$(SRCTOP)/$(IMAGE_TOP)
KPKG_DEST_DIR ?= $(SRCTOP)/..

# Include any site specific overrides here.
-include $(CONFLOC)

# Over ride the config file from the environment/command line
ifneq ($(strip $(KPKG_MAINTAINER)),)
maintainer=$(KPKG_MAINTAINER)
endif

ifneq ($(strip $(KPKG_EMAIL)),)
email=$(KPKG_EMAIL)
endif

# This should be a  name to feed the modules build for pgp signature,
# since we the maintainer would be different there.
ifneq ($(strip $(PGP_SIGNATURE)),)
pgp=$(PGP_SIGNATURE)
endif

ifneq ($(strip $(EXTRA_DOCS)),)
extra_docs = $(EXTRA_DOCS)
endif

ifneq ($(strip $(extra_docs)),)
HAVE_EXTRA_DOCS:=$(shell if [ -e $(extra_docs) ]; then echo YES; fi)
endif

ifneq ($(strip $(DEBIAN_REVISION_MANDATORY)),)
debian_revision_mandatory:=$(DEBIAN_REVISION_MANDATORY)
endif


ifneq ($(strip $(install_vmlinux)),)
int_install_vmlinux:=$(install_vmlinux)
endif

ifneq ($(strip $(KPKG_FOLLOW_SYMLINKS_IN_SRC)),)
int_follow_symlinks_in_src=YES
else
ifneq ($(strip $(kpkg_follow_symlinks_in_src)),)
int_follow_symlinks_in_src=YES
endif
endif





# The Debian revision
ifneq ($(strip $(DEBIAN_REVISION)),)
HAS_CHANGELOG := $(shell \
    if test -f debian/changelog && ( test -f stamp-debian || test -f debian/official );\
    then echo YES;\
    else echo NO; fi; )
else
HAS_CHANGELOG := $(shell if test -f debian/changelog; then echo YES;\
                          else echo NO; fi; )
endif
# If there is a changelog file, it overrides. The only exception is
# when there is no stamp-config, and there is no debian/official,
# *AND* there is a DEBIAN_REVISION, in which case the DEBIAN_REVISION
# over rides (since we are going to replace the changelog file soon
# anyway.  Else, use the commandline or env var setting. Or else
# default to 10.00.Custom, unless the human has requested that the
# revision is mandatory, in which case we raise an error

ifeq ($(strip $(HAS_CHANGELOG)),YES)
debian := $(shell if test -f debian/changelog; then \
                     perl -nle 'print /\((\S+)\)/; exit 0' debian/changelog;\
                  fi; )
else
ifneq ($(strip $(DEBIAN_REVISION)),)
debian := $(DEBIAN_REVISION)
else
ifeq ($(strip $(debian)),)
ifneq ($(strip $(debian_revision_mandatory)),)
$(error A Debian revision is mandatory, but none was provided)
else
debian := 10.00.Custom
endif
endif
endif
endif

# Hmm. The version that we have computed *MUST* match the one that is in the
# changelog.
ifeq ($(strip $(HAS_CHANGELOG)),YES)
saved_version := $(shell if test -f debian/changelog; then \
                     perl -nle 'print /^$(INT_STEM)-source-(\S+)/; exit 0' \
                          debian/changelog;\
                  fi; )
ifneq ($(strip $(saved_version)),)
ifneq ($(strip $(saved_version)),$(strip $(version)))
HAVE_VERSION_MISMATCH=YES
endif
endif
endif


ifneq ($(strip $(DELETE_BUILD_LINK)),)
delete_build_link := YES
else
ifeq ($(strip $(delete_build_link)),)
delete_build_link := $(shell if test -f debian/official; then echo YES;\
                          else echo NO; fi; )
endif
endif

ifneq ($(strip $(IMAGE_IN_BOOT)),)
link_in_boot := $(IMAGE_IN_BOOT)
endif

ifneq ($(strip $(LINK_IN_BOOT)),)
link_in_boot := $(LINK_IN_BOOT)
endif

ifneq ($(strip $(NO_SYMLINK)),)
no_symlink := $(NO_SYMLINK)
endif

ifneq ($(strip $(REVERSE_SYMLINK)),)
reverse_symlink := $(REVERSE_SYMLINK)
endif

ifneq ($(strip $(IMAGE_TYPE)),)
kimage = $(IMAGE_TYPE)
endif

ifneq ($(strip $(PATCH_THE_KERNEL)),)
patch_the_kernel = $(PATCH_THE_KERNEL)
endif

ifneq ($(strip $(KPKG_SELECTED_PATCHES)),)
ifeq ($(strip $(patch_the_kernel)),NO)
patch_the_kernel = NO
else
ifeq ($(strip $(patch_the_kernel)),no)
patch_the_kernel = NO
else
patch_the_kernel = YES
endif
endif
endif


ifeq ($(strip $(patch_the_kernel)),yes)
patch_the_kernel = YES
endif
ifeq ($(strip $(patch_the_kernel)),Yes)
patch_the_kernel = YES
endif
ifeq ($(strip $(patch_the_kernel)),YEs)
patch_the_kernel = YES
endif
ifeq ($(strip $(patch_the_kernel)),yEs)
patch_the_kernel = YES
endif
ifeq ($(strip $(patch_the_kernel)),yES)
patch_the_kernel = YES
endif
ifeq ($(strip $(patch_the_kernel)),yeS)
patch_the_kernel = YES
endif



ifneq ($(strip $(CONFIG_TARGET)),)
config_target := $(CONFIG_TARGET)
have_new_config_target := YES
endif

# If config_target doesn't end in 'config' then reset it to 'oldconfig'.
ifneq ($(patsubst %config,config,$(strip $(config_target))),config)
config_target := oldconfig
have_new_config_target :=
endif

ifneq ($(strip $(USE_SAVED_CONFIG)),)
use_saved_config = $(USE_SAVED_CONFIG)
endif

#ifeq ($(origin var),command line)
#$(warn You are setting an internal var from the cmdline. Use at your own risk)
#endif
#you can automated it a bit more with $(foreach) and $(if)


###
### In the following, we define these variables
### ROOT_CMD      -- set in the environment, plaing old sudo or fakeroot
### root_cmd      -- The same
### int_root_cmd  -- the argument passed to dpkg-buildpackage
###                  -r$(ROOT_CMD)
ifneq ($(strip $(ROOT_CMD)),)
 # ROOT_CMD is not supposed to have -r or -us and -uc
 int_dummy_root := $(ROOT_CMD)
 # remove -us and -uc
 ifneq ($(strip $(findstring -us, $(int_dummy_root))),)
   int_dummy_root := $(subst -us,, $(strip $(int_dummy_root)))
   int_us := -us
 endif
 ifneq ($(strip $(findstring -uc, $(int_dummy_root))),)
   int_dummy_root := $(subst -uc,, $(strip $(int_dummy_root)))
   int_uc := -uc
 endif
 ifneq ($(strip $(findstring -r, $(int_dummy_root))),)
   int_dummy_root := $(subst -r,, $(strip $(int_dummy_root)))
 endif
 # sanitize
 ROOT_CMD     :=   $(strip $(int_dummy_root))
 int_root_cmd := -r$(strip $(int_dummy_root))
else
  # well, ROOT_CMD is not set yet
  ifneq ($(strip $(root_cmd)),)
    # Try and set ROOT_CMD from root_cmd
    int_dummy_root := $(root_cmd)
    # remove -us and -uc
    ifneq ($(strip $(findstring -us, $(int_dummy_root))),)
      int_dummy_root := $(subst -us,, $(strip $(int_dummy_root)))
      int_us := -us
    endif
    ifneq ($(strip $(findstring -uc, $(int_dummy_root))),)
      int_dummy_root := $(subst -uc,, $(strip $(int_dummy_root)))
      int_uc := -uc
    endif
    # now that -us and -uc are gone, remove -r
    ifneq ($(strip $(findstring -r, $(int_dummy_root))),)
      int_dummy_root := $(subst -r,, $(strip $(int_dummy_root)))
    endif
    # Finally, sanitized
    ROOT_CMD     :=   $(strip $(int_dummy_root))
    int_root_cmd := -r$(strip $(int_dummy_root))
  endif
endif

# make sure that root_cmd and ROOT_CMD are the same
ifneq ($(strip $(ROOT_CMD)),)
  root_cmd := $(ROOT_CMD)
endif

ifneq ($(strip $(UNSIGN_SOURCE)),)
  int_us := -us
endif

ifneq ($(strip $(UNSIGN_CHANGELOG)),)
  int_uc := -uc
endif

int_am_root  := $(shell [ $$(id -u) -eq 0 ] && echo "YES" )


ifneq ($(strip $(CLEAN_SOURCE)),)
do_clean = $(CLEAN_SOURCE)
endif

ifneq ($(strip $(CONCURRENCY_LEVEL)),)
do_parallel = -j$(CONCURRENCY_LEVEL)

# Well, I wish there was something better than guessing by version number
CAN_DO_DEP_FAST=$(shell if   [ $(VERSION) -lt 2 ];    then echo '';  \
                        elif [ $(VERSION) -gt 2 ];    then echo YES; \
                        elif [ $(PATCHLEVEL) -lt 4 ]; then echo '';  \
                        else                             echo YES; \
                        fi)
ifneq ($(strip $(CAN_DO_DEP_FAST)),)
fast_dep= -j$(CONCURRENCY_LEVEL)
endif

endif

ifneq ($(strip $(SOURCE_CLEAN_HOOK)),)
source_clean_hook=$(SOURCE_CLEAN_HOOK)
endif
ifneq ($(strip $(HEADER_CLEAN_HOOK)),)
header_clean_hook=$(HEADER_CLEAN_HOOK)
endif
ifneq ($(strip $(DOC_CLEAN_HOOK)),)
doc_clean_hook=$(DOC_CLEAN_HOOK)
endif
ifneq ($(strip $(IMAGE_CLEAN_HOOK)),)
image_clean_hook=$(IMAGE_CLEAN_HOOK)
endif

ifneq ($(strip $(INITRD)),)
initrddep := initrd-tools (>= 0.1.48), # there is a space here
else
initrddep :=
endif

ifeq ($(strip $(CONFDIR)),)
ifeq ($(strip $(patch_the_kernel)),YES)
CONFDIR     = $(PATCH_DIR)
else
ifeq ($(strip $(patch_the_kernel)),yes)
CONFDIR     = $(PATCH_DIR)
else
CONFDIR     = $(DEBDIR)/Config
endif
endif
endif

# The file which has local configuration
CONFIG_FILE := $(shell if test -e .config ; then \
                           echo .config; \
                       elif test -e $(DEBCONFIG) ; then \
                           echo $(DEBCONFIG); \
                       elif test -e $(CONFDIR)/config ; then \
                           echo $(CONFDIR)/config ; \
                       elif test -e $(DEBDIR)/config ; then \
                           echo $(DEBDIR)/config ; \
                       elif test -e /boot/config-$(version) ; then \
                           echo /boot/config-$(version) ; \
                       elif test -e /boot/config-$$(uname -r) ; then \
                           echo /boot/config-$$(uname -r) ; \
                       else echo /dev/null ; \
                       fi)


# Deal with modules issues

# define MODULES_ENABLED if appropriate
ifneq ($(filter kfreebsd-gnu, $(DEB_HOST_GNU_SYSTEM)):$(strip $(shell grep -E ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),:)
  MODULES_ENABLED := YES
endif

# accept both space separated list of modules, as well as comma
# separated ones
valid_modules:=

# See what modules we are talking about
ifeq ($(strip $(MODULES_ENABLED)),YES)
ifneq ($(strip $(KPKG_SELECTED_MODULES)),)
canonical_modules=$(subst $(comma),$(space),$(KPKG_SELECTED_MODULES))
else
canonical_modules=$(shell test -e $(MODULE_LOC) && \
                       find $(MODULE_LOC) -follow -maxdepth 1 -type d -print |\
			   grep -E -v '^$(MODULE_LOC)/$$')
endif


# Now, if we have any modules at all, they are in canonical_modules
ifneq ($(strip $(canonical_modules)),)

# modules can have the full path, or just the name of the module. We
# make all the modules ahve absolute paths by fleshing them out.
path_modules   :=$(filter     /%, $(canonical_modules))
no_path_modules:=$(filter-out /%, $(canonical_modules))
fleshed_out    :=$(foreach mod,$(no_path_modules),$(MODULE_LOC)/$(mod))

# Hmmph. recreate the canonical modules; now everything has a full
# path name.

canonical_modules:=$(path_modules) $(fleshed_out)
# test to see if the dir names are real
valid_modules = $(shell for dir in $(canonical_modules); do \
                            if [ -d $$dir ] && [ -x $$dir/debian/rules ]; then \
                               echo $$dir;                  \
                            fi;                             \
                        done)


endif
endif

ifeq ($(strip $(patch_the_kernel)),YES)

# Well then. Let us see if we want to select the patches we apply.
ifneq ($(strip $(KPKG_SELECTED_PATCHES)),)
canonical_patches=$(subst $(comma),$(space),$(KPKG_SELECTED_PATCHES))

ifneq ($(strip $(canonical_patches)),)
# test to see if the patches exist
temp_valid_patches = $(shell for name in $(canonical_patches); do                \
                            if [ -x "$(VERSIONED_DIR_PATCH_APPLY)/$$name"   ] &&   \
                               [ -x "$(VERSIONED_DIR_PATCH_UNPATCH)/$$name" ];     \
                               then echo "$(VERSIONED_DIR_PATCH_APPLY)/$$name";    \
                            elif [ -x "$(VERSIONED_ALL_PATCH_APPLY)/$$name"   ] && \
                                 [ -x "$(VERSIONED_ALL_PATCH_UNPATCH)/$$name" ];   \
                               then echo "$(VERSIONED_ALL_PATCH_APPLY)/$$name";    \
                            elif [ -x "$(DIR_PATCH_APPLY)/$$name"   ] &&           \
                                 [ -x "$(DIR_PATCH_UNPATCH)/$$name" ]; then        \
                               echo "$(DIR_PATCH_APPLY)/$$name";                   \
                            elif [ -x "$(ALL_PATCH_APPLY)/$$name"   ] &&           \
                                 [ -x "$(ALL_PATCH_UNPATCH)/$$name" ]; then        \
                               echo "$(ALL_PATCH_APPLY)/$$name";                   \
                            else                                                 \
                               echo "$$name.error";                                \
                            fi;                                                  \
                        done)

temp_patch_not_found = $(filter %.error, $(temp_valid_patches))
patch_not_found = $(subst .error,,$(temp_patch_not_found))
ifneq ($(strip $(patch_not_found)),)
$(error Could not find patch for $(patch_not_found))
endif

valid_patches = $(filter-out %.error, $(temp_valid_patches))

ifeq ($(strip $(valid_patches)),)
$(error Could not find patch scripts for $(canonical_patches))
endif



canonical_unpatches = $(shell new="";                                         \
                              for name in $(canonical_patches); do            \
                                  new="$$name $$new";                         \
                              done;                                           \
                              echo $$new;)

temp_valid_unpatches = $(shell for name in $(canonical_unpatches); do            \
                            if [ -x "$(VERSIONED_DIR_PATCH_APPLY)/$$name"   ] &&   \
                               [ -x "$(VERSIONED_DIR_PATCH_UNPATCH)/$$name" ];     \
                              then echo "$(VERSIONED_DIR_PATCH_UNPATCH)/$$name";   \
                            elif [ -x "$(VERSIONED_ALL_PATCH_APPLY)/$$name"   ] && \
                                 [ -x "$(VERSIONED_ALL_PATCH_UNPATCH)/$$name" ];   \
                              then echo "$(VERSIONED_ALL_PATCH_UNPATCH)/$$name";   \
                            elif [ -x "$(DIR_PATCH_APPLY)/$$name"   ] &&           \
                                 [ -x "$(DIR_PATCH_UNPATCH)/$$name" ]; then        \
                               echo "$(DIR_PATCH_UNPATCH)/$$name";                 \
                            elif [ -x "$(ALL_PATCH_APPLY)/$$name"   ] &&           \
                                 [ -x "$(ALL_PATCH_UNPATCH)/$$name" ]; then        \
                               echo "$(ALL_PATCH_UNPATCH)/$$name";                 \
                            else                                                 \
                               echo $$name.error;                                \
                            fi;                                                  \
                        done)
temp_unpatch_not_found = $(filter %.error, $(temp_valid_unpatches))
unpatch_not_found = $(subst .error,,$(temp_unpatch_not_found))
ifneq ($(strip $(unpatch_not_found)),)
$(error Could not find unpatch for $(unpatch_not_found))
endif

valid_unpatches = $(filter-out %.error, $(temp_valid_unpatches))

ifeq ($(strip $(valid_unpatches)),)
$(error Could not find un-patch scripts for $(canonical_unpatches))
endif


endif
else
# OK. We want to patch the kernel, but there are no patches specified.
valid_patches = $(shell if [ -n "$(VERSIONED_PATCH_DIR)" ] &&                 \
                           [ -n "$(VERSIONED_DIR_PATCH_APPLY)" ] &&           \
                           [ -d "$(VERSIONED_DIR_PATCH_APPLY)" ]; then        \
                               run-parts --test $(VERSIONED_DIR_PATCH_APPLY); \
                        fi;                                                   \
                        if [ -n "$(VERSIONED_ALL_PATCH_DIR)" ] &&             \
                           [ -n "$(VERSIONED_ALL_PATCH_APPLY)" ] &&           \
                           [ -d "$(VERSIONED_ALL_PATCH_APPLY)" ]; then        \
                               run-parts --test $(VERSIONED_ALL_PATCH_APPLY); \
                        fi;                                                   \
                        if [ -n "$(PATCH_DIR)" ] &&                           \
                           [ -n "$(DIR_PATCH_APPLY)" ] &&                     \
                           [ -d "$(DIR_PATCH_APPLY)" ]; then                  \
                              run-parts --test $(DIR_PATCH_APPLY);            \
                        fi;                                                   \
                        if [ -n "$(ALL_PATCH_DIR)" ] &&                       \
                           [ -n "$(ALL_PATCH_APPLY)" ] &&                     \
                           [ -d "$(ALL_PATCH_APPLY)"  ]; then                 \
                              run-parts --test $(ALL_PATCH_APPLY);            \
                        fi)
valid_unpatches = $(shell ( if [ -n "$(VERSIONED_PATCH_DIR)"       ]  &&          \
                               [ -n "$(VERSIONED_DIR_PATCH_UNPATCH)" ] &&         \
                               [ -d "$(VERSIONED_DIR_PATCH_UNPATCH)" ]; then      \
                                 run-parts --test $(VERSIONED_DIR_PATCH_UNPATCH); \
                            fi;                                                   \
                            if [ -n "$(VERSIONED_ALL_PATCH_DIR)"    ]  &&         \
                               [ -n "$(VERSIONED_ALL_PATCH_UNPATCH)" ] &&         \
                               [ -d "$(VERSIONED_ALL_PATCH_UNPATCH)"  ]; then     \
                                 run-parts --test $(VERSIONED_ALL_PATCH_UNPATCH); \
                            fi;                                                   \
                            if [ -n "$(PATCH_DIR)"       ]  &&                    \
                               [ -n "$(DIR_PATCH_UNPATCH)" ] &&                   \
                               [ -d "$(DIR_PATCH_UNPATCH)" ]; then                \
                                 run-parts --test $(DIR_PATCH_UNPATCH);           \
                            fi;                                                   \
                            if [ -n "$(ALL_PATCH_DIR)"    ]  &&                   \
                               [ -n "$(ALL_PATCH_UNPATCH)" ] &&                   \
                               [ -d "$(ALL_PATCH_UNPATCH)"  ]; then               \
                                run-parts --test $(ALL_PATCH_UNPATCH);            \
                            fi) | tac)
endif
endif

old_applied_patches=$(shell if [ -f applied_patches ]; then                   \
                               cat applied_patches;                           \
                            else                                              \
                               echo '';                                       \
                            fi )

ifeq ($(strip $(valid_unpatches)),)
ifneq ($(strip $(old_applied_patches)),)
old_unpatches=$(shell new="";                                          \
                      for name in $(notdir $(old_applied_patches)); do \
                          new="$$name $$new";                          \
                      done;                                            \
                      echo $$new;)
temp_old_unpatches = $(shell for name in $(old_unpatches); do         \
                            if [ -x "$(VERSIONED_DIR_PATCH_UNPATCH)/$$name" ];  \
                              then echo "$(VERSIONED_DIR_PATCH_UNPATCH)/$$name";\
                            elif [ -x "$(VERSIONED_ALL_PATCH_UNPATCH)/$$name" ];\
                              then echo "$(VERSIONED_ALL_PATCH_UNPATCH)/$$name";\
                            elif [ -x "$(DIR_PATCH_UNPATCH)/$$name" ]; then     \
                               echo "$(DIR_PATCH_UNPATCH)/$$name";              \
                            elif [ -x "$(ALL_PATCH_UNPATCH)/$$name" ]; then     \
                               echo "$(ALL_PATCH_UNPATCH)/$$name";              \
                            else                                              \
                               echo "$$name.error";                             \
                            fi;                                               \
                        done)
temp_old_unpatch_not_found = $(filter %.error, $(temp_old_unpatches))
old_unpatch_not_found = $(subst .error,,$(temp_unpatch_not_found))
valid_unpatches = $(filter-out %.error, $(temp_old_unpatches))
endif
endif

# See if the version numbers are valid
HAVE_VALID_PACKAGE_VERSION := $(shell \
      if test -x $(DEBDIR)/kpkg-vercheck; then \
	$(DEBDIR)/kpkg-vercheck $(debian) ; \
      else \
        echo "Could not find $(DEBDIR)/kpkg-vercheck" ; \
      fi )

TAR_COMPRESSION := $(shell \
      if tar --help | grep -- \-\-bzip2 >/dev/null; then  \
         echo --bzip2; \
      else \
         echo --gzip; \
      fi )
TAR_SUFFIX := $(shell \
      if tar --help | grep -- \-\-bzip2 >/dev/null; then  \
         echo bz2; \
      else \
         echo gz; \
      fi )

STOP_FOR_BIN86 = NO
CONTROL=$(DEBDIR)/Control
ifeq ($(strip $(architecture)),i386)
NEED_BIN86 := $(shell if dpkg --compare-versions \
                  $(VERSION).$(PATCHLEVEL) lt 2.4 >/dev/null 2>&1; \
                  then echo YES; fi)
ifeq ($(strip $(NEED_BIN86)),YES)
CONTROL=$(DEBDIR)/Control.bin86
HAVE_BIN86 := $(shell if test -x /usr/bin/as86; then echo YES; \
                      else echo NO; fi )
ifeq ($(strip $(HAVE_BIN86)),NO)
STOP_FOR_BIN86 = YES
endif
endif
endif



ifeq (,$(strip $(kimagedest)))
$(error Error. I do not know where the kernel image goes to [kimagedest undefined]\
 The usual case for this is that I could not determine which arch or subarch \
 tihs machine belongs to. Please specify a subarch, and try again.)
endif
ifeq (,$(strip $(kimagesrc)))
$(error Error. I do not know where the kernel image goes to [kimagesrc undefined]\
 The usual case for this is that I could not determine which arch or subarch \
 tihs machine belongs to. Please specify a subarch, and try again.)
endif

# export variables
export root_cmd FLAVOUR INT_SUBARCH APPEND_TO_VERSION UNSIGN_CHANGELOG  \
       UNSIGN_SOURCE ROOT_CMD MODULE_LOC EXTRAVERSION ALL_PATCH_DIR     \
       ALL_PATCH_APPLY ALL_PATCH_UNPATCH DIR_PATCH_UNPATCH              \
       DIR_PATCH_APPLY VERSIONED_PATCH_DIR VERSIONED_ALL_PATCH_UNPATCH  \
       VERSIONED_ALL_PATCH_APPLY VERSIONED_DIR_PATCH_UNPATCH            \
       VERSIONED_DIR_PATCH_APPLY KPKG_SELECTED_PATCHES                  \
       KPKG_SELECTED_MODULES CONCURRENCY_LEVEL


ifeq ($(strip $(IN_KERNEL_DIR)),)
# Hah! Not in kernel directory!!
build configure clean binary kernel_source kernel-source kernel-headers\
stamp-source kernel_headers stamp-headers kernel_image stamp-image \
kernel-image kernel-doc kernel_doc stamp-doc buildpackage \
libc_kheaders libc-kheaders stamp-libc-kheaders kernel-image-deb debian:
	@echo "You should invoke this command from the top level directory of"
	@echo "a linux kernel source directory tree, and as far as I can tell,"
	@echo "the current directory:"
	@echo "	$(SRCTOP)"
	@echo "is not a top level linux kernel source directory. "
	@echo ""
	@echo "	(If I am wrong then kernel-packages and the linux kernel"
	@echo "	 are so out sync that you'd better get the latest versions"
	@echo "	 of the kernel-package package and the Linux sources)"
	@echo ""
	@echo "Please change directory to wherever linux kernel sources"
	@echo "reside and try again."
else
ifneq ($(strip $(HAVE_VALID_PACKAGE_VERSION)),YES)
# Hah! Bogus version number
build configure clean binary kernel_source kernel-source kernel-headers\
stamp-source kernel_headers stamp-headers kernel_image stamp-image \
kernel-image kernel-doc kernel_doc stamp-doc buildpackage kernel-image-deb \
debian:
	@echo "Problems ecountered with the version number $(debian)."
	@echo "$(HAVE_VALID_PACKAGE_VERSION)"
	@echo ""
	@echo "Please re-read the README file and try again."
else
ifeq ($(strip $(STOP_FOR__BIN86)),YES)
# Hah! we need bin 86, but it aint here
build configure clean binary kernel_source kernel-source kernel-headers\
stamp-source kernel_headers stamp-headers kernel_image stamp-image \
kernel-image kernel-doc kernel_doc stamp-doc buildpackage kernel-image-deb \
debian:
	@echo "You Need to install the package bin86 before you can "
	@echo "compile the kernel on this machine"
	@echo ""
	@echo "Please install bin86 and try again."
else
all build: debian configure stamp-build
stamp-build:
# Builds the binary package.
# debian.config contains the current idea of what the image should
# have.
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
ifneq ($(strip $(UTS_RELEASE_VERSION)), $(strip $(version)))
	if [ -f include/linux/version.h ]; then                                          \
             uts_ver=$$(grep 'define UTS_RELEASE' include/linux/version.h |                \
                perl -nle  'm/^\s*\#define\s+UTS_RELEASE\s+("?)(\S+)\1/g && print $$2;'); \
	    if [ "X$$uts_ver" != "X$(strip $(UTS_RELEASE_VERSION))" ]; then              \
                echo "The UTS Release version in include/linux/version.h";                \
	        echo "     \"$$uts_ver\" ";                                               \
                echo "does not match current version " ;                                  \
                echo "     \"$(strip $(version))\" " ;                                    \
                echo "Reconfiguring." ;                                                   \
                touch Makefile;                                                           \
             fi;                                                                          \
	fi
endif
	-test -f stamp-configure || $(deb_rule) configure
ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
	$(MAKE) $(do_parallel) $(EXTRAV_ARG) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) \
	                    $(CROSS_ARG) $(target)
  ifneq ($(strip $(shell grep -E ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),)
	$(MAKE) $(do_parallel) $(EXTRAV_ARG) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) \
	                    $(CROSS_ARG) modules
  endif
else
  ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
	$(PMAKE) -C $(architecture)/compile/GENERIC
  endif
endif
	COLUMNS=150 dpkg -l 'gcc*' perl dpkg 'libc6*' binutils ldso make dpkg-dev |\
         awk '$$1 ~ /[hi]i/ { printf("%s-%s\n", $$2, $$3) }'   > debian/buildinfo
	@echo this was built on a machine with the kernel: >> debian/buildinfo
	uname -a >> debian/buildinfo
	echo using the compiler: >> debian/buildinfo
	grep LINUX_COMPILER include/linux/compile.h | \
           sed -e 's/.*LINUX_COMPILER "//' -e 's/"$$//' >> debian/buildinfo
ifneq ($(strip $(shell test -f version.Debian && cat version.Debian)),)
	echo kernel source package used: >> debian/buildinfo
	COLUMNS=150 dpkg -l kernel-source-$(shell test -f version.Debian &&               \
                                              cat version.Debian | sed -e 's/-.*$$//') |  \
	 awk '$$1 ~ /[hi]i/ { printf("%s-%s\n", $$2, $$3) }' >> debian/buildinfo
endif
	echo applied kernel patches: >> debian/buildinfo
ifneq ($(strip $(valid_patches)),)
	COLUMNS=150 dpkg -l $(shell echo $(valid_patches) | tr ' ' '\n' |                 \
                              sed -ne 's/^.*\/\(.*\)/kernel-patch-\1/p') |                \
	      awk '$$1 ~ /[hi]i/  { printf("%s-%s\n", $$2, $$3) }' >> debian/buildinfo
endif
	echo done >  $@

buildpackage: clean stamp-buildpackage
stamp-buildpackage: configure
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
	test -f stamp-configure || $(deb_rule) configure
	echo 'Building Package' > stamp-building
	dpkg-buildpackage -nc $(strip $(int_root_cmd)) $(strip $(int_us)) $(strip $(int_uc))  \
             -m"$(maintainer) <$(email)>" -k"$(pgp)"
	rm -f stamp-building
	echo done >  $@

# stamp-debian and stamp-configure used to be a single target. Now
# they are split - the reason is that arch-indep packages need to be
# built before arch-dep packages, and make-kpkg tries to do 'make
# config' for both cases.  This used to work because the .config file
# resided with kernel-source, but now that it is in kernel-patch, it
# breaks down.  I think the cleanest way out of this is to only deal
# with config files when necessary, and thus the split. Herbert Xu
debian: stamp-debian
stamp-debian:
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
	# work around idiocy in recent kernel versions
	test ! -e scripts/package/builddeb || \
            mv -f scripts/package/builddeb scripts/package/builddeb.dist
	test ! -e scripts/package/Makefile || \
            (mv -f scripts/package/Makefile scripts/package/Makefile.dist && \
               (echo "# Dummy file "; echo "help:") >  scripts/package/Makefile)
	@test -f $(DEBDIR)/rules || \
            echo Error: Could not find $(DEBDIR)/rules
	-test ! -f stamp-debian && test ! -f debian/official && \
	      rm -rf ./debian && mkdir ./debian
ifeq ($(strip $(patch_the_kernel)),YES)
	-test -f applied_patches && rm -f applied_patches
ifneq ($(strip $(valid_patches)),)
	-for patch in $(valid_patches) ; do            \
          if test -x  $$patch; then                    \
              if $$patch; then                         \
                  echo "Patch $$patch processed fine"; \
		  echo "$(notdir $$patch)" >> applied_patches;   \
              else                                     \
                   echo "Patch $(notdir $$patch)  failed.";      \
                   echo "Hit return to Continue";      \
		   read ans;                           \
              fi;                                      \
	  fi;                                          \
        done
	echo done >  stamp-patch
endif
endif
	-test ! -f stamp-debian && \
               ( test ! -f debian/official || test ! -f debian/control) && \
	   sed -e 's/=V/$(version)/g'         -e 's/=D/$(debian)/g'        \
	       -e 's/=A/$(DEB_HOST_ARCH)/g'   -e 's/=SA/$(INT_SUBARCH)/g'  \
                -e 's/=L/$(int_loaderdep) /g' -e 's/=I/$(initrddep)/g'     \
                -e 's/=CV/$(VERSION).$(PATCHLEVEL)/g'                      \
                -e 's/=M/$(maintainer) <$(email)>/g'                       \
                -e 's/=ST/$(INT_STEM)/g'                                   \
		         $(CONTROL)> debian/control
	-test ! -f stamp-debian && test ! -f debian/official &&               \
	   sed -e 's/=V/$(version)/g' -e 's/=D/$(debian)/g'                   \
	    -e 's/=A/$(DEB_HOST_ARCH)/g' -e 's/=M/$(maintainer) <$(email)>/g' \
            -e 's/=ST/$(INT_STEM)/g'                                          \
		$(DEBDIR)/changelog > debian/changelog
	-test ! -f debian/rules &&                                       \
	   install -p -m 755 $(DEBDIR)/rules debian/rules
#	-test ! -f stamp-debian && test ! -f debian/official &&              \
#	for file in $(DEBIAN_FILES); do cp -f  $(LIBLOC)/$$file ./debian/; done
#	for dir  in $(DEBIAN_DIRS);  do cp -af $(LIBLOC)/$$dir  ./debian/; done
	echo done >  $@

conf.vars: Makefile .config
	@rm -f .mak
	@touch .mak
	@echo Please ignore the warning about overriding and ignoring targets above.
	@echo These are harmless. They are only invoked in a part of the process
	@echo that tries to snarf variable values for the conf.vars file.
	@echo "VERSION          = $(VERSION)"       >> .mak
	@echo "PATCHLEVEL       = $(PATCHLEVEL)"    >> .mak
	@echo "SUBLEVEL 	= $(SUBLEVEL)"      >> .mak
	@echo "EXTRAVERSION     = $(EXTRAVERSION)"  >> .mak
ifneq ($(strip $(iatv)),)
	@echo "APPEND_TO_VERSION = $(iatv)"         >> .mak
endif
ifeq ($(strip $(patch_the_kernel)),YES)
	@echo "KPKG_SELECTED_PATCHES = $(KPKG_SELECTED_PATCHES)" >> .mak
endif
ifeq ($(strip $(MODULES_ENABLED)),YES)
	@echo "KPKG_SELECTED_MODULES = $(KPKG_SELECTED_MODULES)" >> .mak
endif
	@echo "Debian Revision  = $(debian)"        >> .mak
	@echo "KPKG_ARCH        = $(KPKG_ARCH)"        >> .mak
# Fetch the rest of the information from the kernel's Makefile
ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
	@$(MAKE) --no-print-directory -sf $(DEBDIR)/kernel_version.mk  ARCH=$(KERNEL_ARCH) \
          $(CROSS_ARG) debian_conf_var              >> .mak
endif
	@echo "do_parallel      = $(do_parallel)"   >> .mak
	@echo "fast_dep         = $(fast_dep)"      >> .mak
#	@sed -e 's%$(TOPDIR)%$$(TOPDIR)%g' .mak     > conf.vars
# Use the kernel's Makefile to calculate the TOPDIR.
# TOPDIR is obsolete in 2.6 kernels, so the kernel_version.mk
# will get us the right answer
	@sed -e 's%$(shell $(MAKE) --no-print-directory -sf $(DEBDIR)/kernel_version.mk debian_TOPDIR)%$$(TOPDIR)%g' .mak     > conf.vars
	@rm -f .mak

dummy_do_dep:
ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
	+$(MAKE) $(EXTRAV_ARG) $(FLAV_ARG) $(CROSS_ARG) \
                                 ARCH=$(KERNEL_ARCH) $(fast_dep) dep
else
  ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
	$(PMAKE) -C $(architecture)/compile/GENERIC depend
  endif
endif

stamp-kernel-configure: stamp-debian .config
ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
	mkdir -p bin
	ln -sf `which gcc-3.4` bin/cc
	cd $(architecture)/conf && freebsd-config GENERIC
endif
ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
	$(MAKE) $(EXTRAV_ARG) $(FLAV_ARG) $(CROSS_ARG) \
                                 ARCH=$(KERNEL_ARCH) $(config_target)
ifeq ($(shell if [ $(VERSION) -ge 2 ] && [ $(PATCHLEVEL) -ge 5 ]; then \
                  echo new;fi),)
	+$(MAKE) -f ./debian/rules dummy_do_dep
	$(MAKE) $(EXTRAV_ARG) $(FLAV_ARG) $(CROSS_ARG) \
                                 ARCH=$(KERNEL_ARCH) clean
else
ifeq ($(strip $(MAKING_VIRTUAL_IMAGE)),)
	$(MAKE) $(EXTRAV_ARG) $(FLAV_ARG) $(CROSS_ARG) \
                                ARCH=$(KERNEL_ARCH) prepare
endif
endif
endif
	echo done >  $@

configure: debian .config stamp-configure
stamp-configure: stamp-debian .config conf.vars stamp-kernel-configure
	echo done >  $@



.config:
ifneq ($(strip $(use_saved_config)),NO)
	test -f .config || test ! -f .config.save || \
		            cp -pf .config.save .config
endif
	test -f .config || test ! -f $(CONFIG_FILE) || \
		            cp -pf $(CONFIG_FILE) .config
	test -f .config || test ! -f $(DEBDIR)/config || \
		            cp -pf $(DEBDIR)/config  .config
ifeq ($(strip $(have_new_config_target)),)
	test -f .config || (echo "*** Need a config file .config" && false)
endif
# if $(have_new_config_target) is set, then we need not have a .config
# file at this point

clean:
ifeq ($(strip $(int_am_root)),)
ifeq ($(strip $(ROOT_CMD)),)
	@echo "You may need root privileges - some parts may fail"
endif
	$(ROOT_CMD) $(deb_rule) real_stamp_clean
else
	$(deb_rule) real_stamp_clean
endif

# Perhaps a list of patches should be dumped to a file on patching? so we
# only unpatch what we have applied? That would be changed, though saner,
# behaviour
real_stamp_clean:
ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
	test ! -f .config || cp -pf .config config.precious
	-test -f Makefile && \
            $(MAKE) $(FLAV_ARG) $(EXTRAV_ARG) $(CROSS_ARG) ARCH=$(KERNEL_ARCH) distclean
	test ! -f config.precious || mv -f config.precious .config
else
	rm -f .config
  ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
	rm -rf bin
	if test -e $(architecture)/compile/GENERIC ; then     \
	  $(PMAKE) -C $(architecture)/compile/GENERIC clean ; \
	fi
  endif
endif
ifeq ($(strip $(patch_the_kernel)),YES)
	$(deb_rule) unpatch_now
endif
ifeq ($(strip $(NO_UNPATCH_BY_DEFAULT)),)
	test ! -f stamp-patch || $(deb_rule) unpatch_now
endif
	-test -f stamp-building || test -f debian/official || rm -rf debian
	# work around idiocy in recent kernel versions
	test ! -e scripts/package/builddeb.dist || \
            mv -f scripts/package/builddeb.dist scripts/package/builddeb
	test ! -e scripts/package/Makefile.dist || \
            mv -f scripts/package/Makefile.dist scripts/package/Makefile
	rm -f $(FILES_TO_CLEAN) $(STAMPS_TO_CLEAN)
	rm -rf $(DIRS_TO_CLEAN)


unpatch_now:
ifneq ($(strip $(valid_unpatches)),)
	-for patch in $(valid_unpatches) ; do              \
          if test -x  $$patch; then                        \
              if $$patch; then                             \
                  echo "Removed Patch $$patch ";           \
              else                                         \
                   echo "Patch $$patch  failed.";          \
                   echo "Hit return to Continue";          \
		   read ans;                               \
              fi;                                          \
	  fi;                                              \
        done
	rm -f stamp-patch
endif


binary:       binary-indep binary-arch
binary-indep: kernel_source kernel_doc
binary-arch:  kernel_image  kernel_headers


kernel-source kernel_source: stamp-source
stamp-source: stamp-debian
ifeq ($(strip $(int_am_root)),)
ifeq ($(strip $(ROOT_CMD)),)
	@echo need root privileges; exit 1
else
	$(ROOT_CMD) $(deb_rule) real_stamp_source
endif
else
	$(deb_rule) real_stamp_source
endif

real_stamp_source:
ifneq ($(strip $(MAKING_VIRTUAL_IMAGE)),)
	echo done >  stamp-source
else
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
	test -f stamp-debian || $(deb_rule) debian
	rm -rf $(SOURCE_TOP)
	$(make_directory) $(SOURCE_TOP)/DEBIAN
	$(make_directory) $(SOURCE_SRC)
	$(make_directory) $(SOURCE_DOC)
	sed -e 's/=P/$(package)/g' -e 's/=V/$(version)/g' \
	    $(DEBDIR)/src.postinst > $(SOURCE_TOP)/DEBIAN/postinst
	chmod 755 $(SOURCE_TOP)/DEBIAN/postinst
	$(install_file) debian/changelog               $(SOURCE_DOC)/changelog.Debian
	gzip -9qf                             	 $(SOURCE_DOC)/changelog.Debian
	$(install_file) $(DEBDIR)/README      	 $(SOURCE_DOC)/debian.README
	gzip -9qf                             	 $(SOURCE_DOC)/debian.README
	$(install_file) $(DEBDIR)/README.grub 	 $(SOURCE_DOC)/README.grub
	gzip -9qf                                $(SOURCE_DOC)/README.grub
	$(install_file) $(DEBDIR)/README.headers $(SOURCE_DOC)/README.headers
	gzip -9qf                                $(SOURCE_DOC)/README.headers
	$(install_file) $(DEBDIR)/README.tecra   $(SOURCE_DOC)/README.tecra
	gzip -9qf                                $(SOURCE_DOC)/README.tecra
	$(install_file) $(DEBDIR)/README.modules $(SOURCE_DOC)/README.modules
	gzip -9qf                                $(SOURCE_DOC)/README.modules
	$(install_file) $(DEBDIR)/sample.module.control \
                                            $(SOURCE_DOC)/sample.module.control
	gzip -9qf                           $(SOURCE_DOC)/sample.module.control
	$(install_file) README                     $(SOURCE_DOC)/README
	gzip -9qf                                  $(SOURCE_DOC)/README
	$(install_file) $(DEBDIR)/Flavours         $(SOURCE_DOC)/Flavours
	gzip -9qf                                  $(SOURCE_DOC)/Flavours
	$(install_file) $(DEBDIR)/Rationale        $(SOURCE_DOC)/Rationale
	gzip -9qf                                  $(SOURCE_DOC)/Rationale
	$(install_file) $(DEBDIR)/copyright.source $(SOURCE_DOC)/copyright
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	         $(SOURCE_DOC)/Buildinfo
ifneq ($(strip $(int_follow_symlinks_in_src)),)
	-tar cfh - $$(echo * | sed -e 's/ debian//g' -e 's/\.deb//g' ) \
	| (cd $(SOURCE_SRC); umask 000; tar xpsf -)
	(cd $(SOURCE_SRC)/include; rm -rf asm ; )
else
	-tar cf - $$(echo * | sed -e 's/ debian//g' -e 's/\.deb//g' ) \
	| (cd $(SOURCE_SRC); umask 000; tar xspf -)
	(cd $(SOURCE_SRC)/include; rm -f asm ; )
endif
	$(install_file) debian/changelog         $(SOURCE_SRC)/Debian.src.changelog
	(cd $(SOURCE_SRC); \
            $(MAKE) $(EXTRAV_ARG) $(FLAV_ARG) $(CROSS_ARG) ARCH=$(KERNEL_ARCH) distclean)
	(cd $(SOURCE_SRC);         rm -f stamp-building $(STAMPS_TO_CLEAN))
	(cd $(SOURCE_SRC);         \
         [ ! -d scripts/cramfs ]   || make -C scripts/cramfs distclean ; )
	if test -f debian/official && test -f debian/README.Debian ; then \
           $(install_file) debian/README.Debian $(SOURCE_SRC)/README.Debian ; \
           $(install_file) debian/README.Debian $(SOURCE_DOC)/README.Debian ; \
	   gzip -9qf $(SOURCE_DOC)/README.Debian;\
	else \
	    sed -e 's/=V/$(version)/g' -e 's/=A/$(DEB_HOST_ARCH)/g'       \
             -e 's/=ST/$(INT_STEM)/g'                                     \
                 $(DEBDIR)/README.source >  $(SOURCE_SRC)/README.Debian ; \
	fi
	if test -f README.Debian ; then                                   \
           $(install_file) README.Debian $(SOURCE_DOC)/README.Debian.1st; \
	   gzip -9qf                     $(SOURCE_DOC)/README.Debian.1st; \
	fi
ifneq ($(strip $(source_clean_hook)),)
	(cd $(SOURCE_SRC);              \
               test -x $(source_clean_hook) && $(source_clean_hook))
endif
	chmod -R og=rX $(SOURCE_TOP)
	chown -R root:root $(SOURCE_TOP)
	(cd $(SOURCE_TOP)/usr/src/ && \
           tar $(TAR_COMPRESSION) -cf $(package).tar.$(TAR_SUFFIX) $(package) &&\
             rm -rf $(package);)
	dpkg-gencontrol -isp -p$(package) -P$(SOURCE_TOP)/
	chmod -R og=rX $(SOURCE_TOP)
	chown -R root:root $(SOURCE_TOP)
	dpkg --build $(SOURCE_TOP) $(DEB_DEST)
	rm -f -r $(SOURCE_TOP)
	echo done >  stamp-source
endif

libc-kheaders libc_kheaders: stamp-libc-kheaders
stamp-libc-kheaders: configure
	@echo This target is now obsolete.


kernel-headers kernel_headers: stamp-headers
stamp-headers: configure
ifeq ($(strip $(int_am_root)),)
ifeq ($(strip $(ROOT_CMD)),)
	@echo need root privileges; exit 1
else
	$(ROOT_CMD) $(deb_rule) real_stamp_headers
endif
else
	$(deb_rule) real_stamp_headers
endif

ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
  config = .config
else
  ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
    config = $(architecture)/conf/GENERIC
  endif
endif


real_stamp_headers:
ifneq ($(strip $(MAKING_VIRTUAL_IMAGE)),)
	echo done >  stamp-headers
else
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
ifneq ($(strip $(UTS_RELEASE_VERSION)),$(strip $(version)))
	@echo "The UTS Release version in include/linux/version.h $(UTS_RELEASE_VERSION) does not match current version $(version), reconfiguring"
	touch Makefile
endif
	test -f stamp-configure || $(deb_rule) configure
	rm -rf $(HEADER_TOP)
	$(make_directory) $(HEADER_TOP)/DEBIAN
	$(make_directory) $(HEADER_SRC)
	$(make_directory) $(HEADER_DOC)
	$(make_directory) $(HEADER_SRC)/arch/$(KERNEL_ARCH)
	sed -e 's/=P/$(h_package)/g' -e 's/=V/$(version)/g' \
		$(DEBDIR)/include.postinst > $(HEADER_TOP)/DEBIAN/postinst
	chmod 755 $(HEADER_TOP)/DEBIAN/postinst
	$(install_file) $(DEBDIR)/copyright.headers $(HEADER_DOC)/copyright
	$(install_file) debian/changelog            $(HEADER_DOC)/changelog.Debian
	gzip -9qf                                   $(HEADER_DOC)/changelog.Debian
	$(install_file) $(DEBDIR)/README.headers    $(HEADER_DOC)/debian.README
	gzip -9qf                                   $(HEADER_DOC)/debian.README
	$(install_file) $(config)  	            $(HEADER_DOC)/config-$(version)
	$(install_file) conf.vars  	            $(HEADER_DOC)/conf.vars
	gzip -9qf                                   $(HEADER_DOC)/config-$(version)
	gzip -9qf                                   $(HEADER_DOC)/conf.vars
	$(install_file) CREDITS                     $(HEADER_DOC)/
	gzip -9qf                                   $(HEADER_DOC)/CREDITS
	$(install_file) MAINTAINERS                 $(HEADER_DOC)/
	gzip -9qf                                   $(HEADER_DOC)/MAINTAINERS
	$(install_file) REPORTING-BUGS              $(HEADER_DOC)/
	gzip -9qf                                   $(HEADER_DOC)/REPORTING-BUGS
	$(install_file) README                      $(HEADER_DOC)/
	gzip -9qf                                   $(HEADER_DOC)/README
	if test -f debian/official && test -f       debian/README.Debian ; then   \
           $(install_file) debian/README.Debian     $(HEADER_DOC)/README.Debian;  \
           gzip -9qf                                $(HEADER_DOC)/README.Debian;  \
           $(install_file) README.Debian            $(HEADER_DOC)/README.Debian;  \
	   gzip -9qf                                $(HEADER_DOC)/README.Debian;  \
	fi
	if test -f README.Debian ; then \
           $(install_file) README.Debian $(HEADER_DOC)/README.Debian.1st;\
	     gzip -9qf                   $(HEADER_DOC)/README.Debian.1st;\
	fi
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	         $(HEADER_DOC)/Buildinfo
	chmod 0644 $(HEADER_DOC)/Buildinfo
	$(install_file) Makefile       $(HEADER_SRC)
	test ! -e Rules.make || $(install_file) Rules.make     $(HEADER_SRC)
	test ! -e arch/$(KERNEL_ARCH)/Makefile ||                              \
            $(install_file) arch/$(KERNEL_ARCH)/Makefile $(HEADER_SRC)/arch/$(KERNEL_ARCH)
	test ! -e Rules.make || $(install_file) Rules.make     $(HEADER_SRC)
ifneq ($(strip $(int_follow_symlinks_in_src)),)
	-tar cfh - include |        (cd $(HEADER_SRC); umask 000; tar xsf -)
	(cd $(HEADER_SRC)/include; rm -rf asm; ln -s asm-$(KERNEL_ARCH) asm)
else
	-tar cf - include |        (cd $(HEADER_SRC); umask 000; tar xsf -)
	(cd $(HEADER_SRC)/include; rm -f asm; ln -s asm-$(KERNEL_ARCH) asm)
endif
	$(install_file) .config  	        $(HEADER_SRC)/.config
	echo $(debian)                    > $(HEADER_SRC)/$(INT_STEM)-headers.revision
ifneq ($(strip $(header_clean_hook)),)
	(cd $(HEADER_SRC);              \
               test -x $(header_clean_hook) && $(header_clean_hook))
endif
	dpkg-gencontrol -DArchitecture=$(DEB_HOST_ARCH) -isp \
                        -p$(h_package) -P$(HEADER_TOP)/
	chown -R root:root $(HEADER_TOP)
	chmod -R og=rX $(HEADER_TOP)
	dpkg --build $(HEADER_TOP) $(DEB_DEST)
	rm -rf $(HEADER_TOP)
	echo done >  stamp-headers
endif

kernel-doc kernel_doc: stamp-doc
stamp-doc: stamp-debian
ifeq ($(strip $(int_am_root)),)
ifeq ($(strip $(ROOT_CMD)),)
	@echo need root privileges; exit 1
else
	$(ROOT_CMD) $(deb_rule) real_stamp_doc
endif
else
	$(deb_rule) real_stamp_doc
endif

real_stamp_doc:
ifneq ($(strip $(MAKING_VIRTUAL_IMAGE)),)
	echo done >  stamp-doc
else
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
	test -f stamp-debian || $(deb_rule) debian
	rm -rf            $(DOC_TOP)
	$(make_directory) $(DOC_TOP)/DEBIAN
	$(make_directory) $(DOC_DOC)
	$(install_file) debian/changelog          $(DOC_DOC)/changelog.Debian
	$(install_file) $(DEBDIR)/README.doc      $(DOC_DOC)/README.Debian
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	           $(DOC_DOC)/Buildinfo
	chmod 0644 $(DOC_DOC)/Buildinfo
	if test -f debian/official && test -f debian/README.Debian ; then \
           $(install_file) debian/README.Debian $(DOC_DOC)/README.Debian;\
	fi
	if test -f README.Debian ; then \
           $(install_file) README.Debian $(DOC_DOC)/README.Debian.1st;\
	fi
ifneq ($(strip $(shell if [ -x /usr/bin/db2html ]; then echo YSE; fi)),)
	$(MAKE)  mandocs htmldocs
endif
	-tar cf - Documentation | (cd $(DOC_DOC); umask 000; tar xsf -)
ifneq ($(shell if [ $(VERSION) -ge 2 ] && [ $(PATCHLEVEL) -ge 5 ]; then \
                  echo new;fi),)
	find -name Kconfig -print0 | xargs -0r cat | \
	     (umask 000 ; cat > $(DOC_DOC)/Kconfig.collected)
# removing if empty should be faster than running find twice
	if ! test -s $(DOC_DOC)/Kconfig.collected ; then \
	    rm -f $(DOC_DOC)/Kconfig.collected ;          \
         fi
endif
ifneq ($(strip $(doc_clean_hook)),)
	(cd $(DOC_DOC);              \
               test -x $(doc_clean_hook) && $(doc_clean_hook))
endif
	-gzip -9qfr $(DOC_DOC)
	-find $(DOC_DOC) -type f -name \*.gz -perm +111 -exec gunzip {} \;
	$(install_file) $(DEBDIR)/copyright.doc $(DOC_DOC)/copyright
	sed -e 's/=P/$(d_package)/g' -e 's/=V/$(version)/g' \
		$(DEBDIR)/src.postinst > $(DOC_TOP)/DEBIAN/postinst
	chmod 755 $(DOC_TOP)/DEBIAN/postinst
	dpkg-gencontrol -isp -p$(d_package) -P$(DOC_TOP)/
	chmod -R og=rX $(DOC_TOP)
	chown -R root:root $(DOC_TOP)
	dpkg --build $(DOC_TOP) $(DEB_DEST)
	rm -rf $(DOC_TOP)
	echo done >  stamp-doc
endif

kernel-image kernel_image: stamp-image
stamp-image: configure build kernel-image-deb
# % make config
# % make-kpkg build
# % sudo make -f debian/rules kernel-image-deb
# seems to create a working .deb with a kernel that gives the correct
# user name (as opposed to root@...)
kernel-image-deb:
ifeq ($(strip $(int_am_root)),)
ifeq ($(strip $(ROOT_CMD)),)
	@echo need root privileges; exit 1
else
	$(ROOT_CMD) $(deb_rule) real_stamp_image
endif
else
	$(deb_rule) real_stamp_image
endif

real_stamp_image:
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
ifneq ($(strip $(UTS_RELEASE_VERSION)),$(strip $(version)))
	@echo "The UTS Release version in include/linux/version.h $(UTS_RELEASE_VERSION) does not match current version $(version), reconfiguring."
	touch Makefile
endif
	rm -f -r ./$(IMAGE_TOP) ./$(IMAGE_TOP).deb
	test -f stamp-configure     || $(deb_rule) configure
	test -f stamp-build         || $(deb_rule) build
	$(make_directory) $(IMAGE_TOP)/DEBIAN
	$(make_directory) $(IMAGE_TOP)/$(IMAGEDIR)
	$(make_directory) $(IMAGE_DOC)
ifneq ($(strip $(KERNEL_ARCH)),um)
  ifneq ($(strip $(KERNEL_ARCH)),xen)
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
             $(DEBDIR)/image.postinst > $(IMAGE_TOP)/DEBIAN/postinst
	chmod 755 $(IMAGE_TOP)/DEBIAN/postinst
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
             $(DEBDIR)/image.postrm > $(IMAGE_TOP)/DEBIAN/postrm
	chmod 755 $(IMAGE_TOP)/DEBIAN/postrm
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
             $(DEBDIR)/image.preinst > $(IMAGE_TOP)/DEBIAN/preinst
	chmod 755 $(IMAGE_TOP)/DEBIAN/preinst
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
             $(DEBDIR)/image.prerm > $(IMAGE_TOP)/DEBIAN/prerm
	chmod 755 $(IMAGE_TOP)/DEBIAN/prerm
  else
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
             $(DEBDIR)/xen.postinst > $(IMAGE_TOP)/DEBIAN/postinst
	chmod 755 $(IMAGE_TOP)/DEBIAN/postinst
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
             $(DEBDIR)/xen.prerm > $(IMAGE_TOP)/DEBIAN/prerm
	chmod 755 $(IMAGE_TOP)/DEBIAN/prerm
  endif
else
	$(make_directory) $(UML_DIR)
	$(make_directory) $(MAN1DIR)
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
              $(DEBDIR)/um.postinst > $(IMAGE_TOP)/DEBIAN/postinst
	chmod 755 $(IMAGE_TOP)/DEBIAN/postinst
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(link_in_boot)/g'    \
            -e 's/=ST/$(INT_STEM)/g'  -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
            -e 's/=I/$(INITRD)/g'     -e 's,=D,$(IMAGEDIR),g'        \
            -e 's@=M@$(MKIMAGE)@g'    -e 's/=OF/$(AM_OFFICIAL)/g'    \
            -e 's@=A@$(DEB_HOST_ARCH)@g'                             \
            -e 's/=S/$(no_symlink)/g'                                \
              $(DEBDIR)/um.prerm > $(IMAGE_TOP)/DEBIAN/prerm
	chmod 755 $(IMAGE_TOP)/DEBIAN/prerm
	$(install_file) $(DEBDIR)/linux.1 $(MAN1DIR)/linux-$(version).1
	gzip -9fq       $(MAN1DIR)/linux-$(version).1
endif
ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
	$(install_file) Documentation/Changes $(IMAGE_DOC)/
	gzip -9qf $(IMAGE_DOC)/Changes
endif
	$(install_file) debian/changelog        $(IMAGE_DOC)/changelog.Debian
	gzip -9qf                               $(IMAGE_DOC)/changelog.Debian
ifdef loaderdoc
	$(install_file) $(DEBDIR)/$(loaderdoc)  $(IMAGE_DOC)/$(loaderdoc)
	gzip -9qf                               $(IMAGE_DOC)/$(loaderdoc)
endif
	$(install_file) $(DEBDIR)/README.image  $(IMAGE_DOC)/debian.README
	gzip -9qf                               $(IMAGE_DOC)/debian.README
	$(install_file) $(DEBDIR)/copyright.image $(IMAGE_DOC)/copyright
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	           $(IMAGE_DOC)/Buildinfo
	chmod 0644 $(IMAGE_DOC)/Buildinfo
	$(install_file) $(config)        $(INT_IMAGE_DESTDIR)/config-$(version)
	$(install_file) conf.vars        $(IMAGE_DOC)/conf.vars
	gzip -9qf                        $(IMAGE_DOC)/conf.vars
	$(install_file) debian/buildinfo $(IMAGE_DOC)/buildinfo
	gzip -9qf                        $(IMAGE_DOC)/buildinfo
	if test -f debian/official && test -f debian/README.Debian ; then \
           $(install_file) debian/README.Debian  $(IMAGE_DOC)/README.Debian ; \
         gzip -9qf                               $(IMAGE_DOC)/README.Debian;\
	fi
	if test -f README.Debian ; then \
           $(install_file) README.Debian $(IMAGE_DOC)/README.Debian.1st;\
           gzip -9qf                     $(IMAGE_DOC)/README.Debian.1st;\
	fi
	if test -f Debian.src.changelog; then \
	  $(install_file) Debian.src.changelog  $(IMAGE_DOC)/; \
           gzip -9qf                             $(IMAGE_DOC)/Debian.src.changelog;\
	fi
ifeq ($(strip $(HAVE_EXTRA_DOCS)),YES)
	$(install_file) $(extra_docs) 	         $(IMAGE_DOC)/
endif
ifneq ($(filter kfreebsd-gnu, $(DEB_HOST_GNU_SYSTEM)):$(strip $(shell grep -E ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),:)
  ifeq  ($(DEB_HOST_GNU_SYSTEM):$(strip $(HAVE_NEW_MODLIB)),linux:)
	$(mod_inst_cmds)
  else
# could have also said DEPMOD=/bin/true instead of moving files
    ifeq ($(DEB_HOST_GNU_SYSTEM), linux-gnu)
      ifneq ($(strip $(KERNEL_CROSS)),)
	mv System.map System.precious
      endif
	$(MAKE) $(EXTRAV_ARG) INSTALL_MOD_PATH=$(INSTALL_MOD_PATH)           \
                $(CROSS_ARG) ARCH=$(KERNEL_ARCH) modules_install
      ifneq ($(strip $(KERNEL_CROSS)),)
	mv System.precious System.map
      endif
    else
      ifeq ($(DEB_HOST_GNU_SYSTEM), kfreebsd-gnu)
	mkdir -p $(INSTALL_MOD_PATH)/boot/defaults
	install -o root -g root -m 644                        \
                $(architecture)/conf/GENERIC.hints            \
                $(INSTALL_MOD_PATH)/boot/device.hints
	install -o root -g root -m 644 boot/forth/loader.conf \
                         $(INSTALL_MOD_PATH)/boot/loader.conf
	touch $(INSTALL_MOD_PATH)/boot/loader.conf
	install -o root -g root -m 644 boot/forth/loader.conf \
                $(INSTALL_MOD_PATH)/boot/defaults/loader.conf
	$(PMAKE) -C $(architecture)/compile/GENERIC install \
                    DESTDIR=$(INSTALL_MOD_PATH)
      endif
    endif
  endif
	test ! -e $(IMAGE_TOP)/lib/modules/$(version)/source ||                        \
	   mv $(IMAGE_TOP)/lib/modules/$(version)/source ./debian/source-link
	test ! -e $(IMAGE_TOP)/lib/modules/$(version)/build ||                         \
	   mv $(IMAGE_TOP)/lib/modules/$(version)/build ./debian/build-link
  ifeq ($(strip $(KERNEL_ARCH)),um)
	-depmod -q -FSystem.map -b $(IMAGE_TOP) \
           $(version)-$$(sed q include/linux/version.h | sed s/\"//g | awk -F\- '{print $$2}')
  else
    ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	-depmod -q -FSystem.map -b $(IMAGE_TOP) $(version);
    endif
  endif
	test ! -e ./debian/source-link ||                                              \
	   mv ./debian/source-link $(IMAGE_TOP)/lib/modules/$(version)/source
	test ! -e  ./debian/build-link ||                                              \
	   mv  ./debian/build-link $(IMAGE_TOP)/lib/modules/$(version)/build

endif
ifeq ($(strip $(NEED_DIRECT_GZIP_IMAGE)),YES)
	gzip -9vc $(kimagesrc) > $(kimagedest)
else
	cp $(kimagesrc) $(kimagedest)
endif
ifeq ($(strip $(KERNEL_ARCH)),um)
	chmod 755 $(kimagedest);
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip --strip-unneeded --remove-section=.note --remove-section=.comment  $(kimagedest);
endif
else
	chmod 644 $(kimagedest);
endif
ifeq ($(strip $(HAVE_COFF_IMAGE)),YES)
	cp $(coffsrc)   $(coffdest)
	chmod 644       $(coffdest)
endif
ifeq ($(strip $(int_install_vmlinux)),YES)
ifneq ($(strip $(kelfimagesrc)),)
	cp $(kelfimagesrc) $(kelfimagedest)
	chmod 644 $(kelfimagedest)
endif
endif
	if test -d $(SRCTOP)/debian/image.d ; then                             \
             IMAGE_TOP=$(IMAGE_TOP) version=$(version)                          \
                   run-parts --verbose $(SRCTOP)/debian/image.d ;               \
         fi
	if [ -x debian/post-install ]; then                                    \
		IMAGE_TOP=$(IMAGE_TOP) STEM=$(INT_STEM) version=$(version)    \
			debian/post-install;                                  \
	fi
ifneq ($(strip $(image_clean_hook)),)
	(cd $(IMAGE_TOP);              \
               test -x $(image_clean_hook) && $(image_clean_hook))
endif
	test ! -s applied_patches || cp applied_patches                        \
                        $(INT_IMAGE_DESTDIR)/patches-$(version)
	test ! -s applied_patches || chmod 644                                 \
                        $(INT_IMAGE_DESTDIR)/patches-$(version)
ifneq ($(strip $(KERNEL_ARCH)),um)
	test ! -f System.map ||  cp System.map                         \
                        $(INT_IMAGE_DESTDIR)/System.map-$(version);
	test ! -f System.map ||  chmod 644                             \
                        $(INT_IMAGE_DESTDIR)/System.map-$(version);
else
	mv $(INSTALL_MOD_PATH)/lib/modules/* $(UML_DIR)/ -v
	rm -rf $(INSTALL_MOD_PATH)/lib
endif
	# For LKCD enabled kernels
	test ! -f Kerntypes ||  cp Kerntypes                                   \
                        $(INT_IMAGE_DESTDIR)/Kerntypes-$(version)
	test ! -f Kerntypes ||  chmod 644                                      \
                        $(INT_IMAGE_DESTDIR)/Kerntypes-$(version)
ifeq ($(strip $(delete_build_link)),YES)
	rm -f $(IMAGE_TOP)/lib/modules/$(version)/build
endif
	dpkg-gencontrol -DArchitecture=$(DEB_HOST_ARCH) -isp                   \
                        -p$(i_package) -P$(IMAGE_TOP)/
	chmod -R og=rX $(IMAGE_TOP)
	chown -R root:root $(IMAGE_TOP)
	dpkg --build $(IMAGE_TOP) $(DEB_DEST)
	rm -f -r $(IMAGE_TOP)
ifeq ($(strip $(do_clean)),YES)
	$(MAKE) $(EXTRAV_ARG) $(FLAV_ARG) $(CROSS_ARG) ARCH=$(KERNEL_ARCH) clean
	rm -f stamp-build
endif
	echo done >  stamp-image

# This for STOP_FOR_BIN86
endif

# This endif is for HAVE_VALID_PACKAGE_VERSION
endif

#This  endif is for IN_KERNEL_DIR
endif


# only generate module image packages
modules-image modules_image: configure
ifeq ($(strip $(shell grep -E ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),)
	@echo Modules not configured, so not making $@
else
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
	-for module in $(valid_modules) ; do                       \
          if test -d  $$module; then                                \
	    (cd $$module;                                          \
              if ./debian/rules KVERS="$(version)" KSRC="$(SRCTOP)" \
                             KMAINT="$(pgp)" KEMAIL="$(email)"      \
                             KPKG_DEST_DIR="$(KPKG_DEST_DIR)"       \
                             KPKG_MAINTAINER="$(maintainer)"        \
                             KPKG_EXTRAV_ARG="$(EXTRAV_ARG)"        \
                             ARCH="$(KERNEL_ARCH)"                  \
                             KDREV="$(debian)" kdist_image; then    \
                  echo "Module $$module processed fine";            \
              else                                                  \
                   echo "Module $$module failed.";                  \
                   if [ "X$(strip $(ROOT_CMD))" != "X" ]; then      \
                      echo "Perhaps $$module does not understand --rootcmd?";  \
                      echo "If you see messages that indicate that it is not"; \
                      echo "in fact being built as root, please file a bug ";  \
                      echo "against $$module.";                     \
                   fi;                                              \
                   echo "Hit return to Continue";                   \
		 read ans;                                        \
              fi;                                                   \
	     );                                                    \
	  else                                                      \
               echo "Module $$module does not exist";               \
               echo "Hit return to Continue?";                      \
	  fi;                                                       \
        done
endif

# generate the modules packages and sign them
modules: configure
ifeq ($(strip $(shell grep -E ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),)
	@echo Modules not configured, so not making $@
else
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
	-for module in $(valid_modules) ; do                       \
          if test -d  $$module; then                                \
	    (cd $$module;                                          \
              if ./debian/rules KVERS="$(version)" KSRC="$(SRCTOP)" \
                             KMAINT="$(pgp)" KEMAIL="$(email)"      \
                             KPKG_DEST_DIR="$(KPKG_DEST_DIR)"       \
                             KPKG_MAINTAINER="$(maintainer)"        \
                             ARCH=$(KERNEL_ARCH)                    \
                             KPKG_EXTRAV_ARG="$(EXTRAV_ARG)"        \
                             KDREV="$(debian)" kdist; then          \
                  echo "Module $$module processed fine";            \
              else                                                  \
                   echo "Module $$module failed.";                  \
                   if [ "X$(strip $(ROOT_CMD))" != "X" ]; then      \
                      echo "Perhaps $$module does not understand --rootcmd?";  \
                      echo "If you see messages that indicate that it is not"; \
                      echo "in fact being built as root, please file a bug ";  \
                      echo "against $$module.";                     \
                   fi;                                              \
                   echo "Hit return to Continue?";                  \
		 read ans;                                          \
              fi;                                                   \
	     );                                                     \
	  else                                                      \
               echo "Module $$module does not exist";               \
               echo "Hit return to Continue?";                      \
	  fi;                                                       \
        done
endif

# configure the modules packages
modules-config modules_config: configure
ifeq ($(strip $(shell grep -E ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),)
	@echo Modules not configured, so not making $@
else
ifneq ($(strip $(HAVE_VERSION_MISMATCH)),)
	@(echo "The changelog says we are creating $(saved_version), but I thought the version is $(version)"; exit 1)
endif
	-for module in $(valid_modules) ; do                       \
          if test -d  $$module; then                                \
	    (cd $$module;                                          \
              if ./debian/rules KVERS="$(version)" KSRC="$(SRCTOP)" \
                             KMAINT="$(pgp)" KEMAIL="$(email)"      \
                             KPKG_DEST_DIR="$(KPKG_DEST_DIR)"       \
                             KPKG_MAINTAINER="$(maintainer)"        \
                             ARCH=$(KERNEL_ARCH)                    \
                             KPKG_EXTRAV_ARG="$(EXTRAV_ARG)"        \
                             KDREV="$(debian)" kdist_configure; then\
                  echo "Module $$module configured fine";           \
              else                                                  \
                   echo "Module $$module failed to configure";      \
                   echo "Hit return to Continue?";                  \
		 read ans;                                        \
              fi;                                                   \
	     );                                                    \
	  else                                                      \
               echo "Module $$module does not exist";               \
               echo "Hit return to Continue?";                      \
	  fi;                                                      \
        done
endif

modules-clean modules_clean: .config
ifeq ($(strip $(shell grep -E ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),)
	@echo Modules not configured, so not making $@
else
	-for module in $(valid_modules); do                        \
          if test -d  $$module; then                                \
	    (cd $$module;                                          \
              if ./debian/rules KVERS="$(version)" KSRC="$(SRCTOP)" \
                             KMAINT="$(pgp)" KEMAIL="$(email)"      \
                             KPKG_DEST_DIR="$(KPKG_DEST_DIR)"       \
                             KPKG_MAINTAINER="$(maintainer)"        \
                             ARCH=$(KERNEL_ARCH)                    \
                             KPKG_EXTRAV_ARG="$(EXTRAV_ARG)"        \
                             KDREV="$(debian)" kdist_clean; then    \
                  echo "Module $$module cleaned";                   \
              else                                                  \
                   echo "Module $$module failed to clean up";       \
                   echo "Hit return to Continue?";                  \
		 read ans;                                          \
              fi;                                                   \
	     );                                                     \
	  else                                                      \
               echo "Module $$module does not exist";               \
               echo "Hit return to Continue?";                      \
	  fi;                                                       \
        done
endif


source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

define mod_inst_cmds
        @(                                                           \
        MODLIB=$(INSTALL_MOD_PATH)/lib/modules/$(version);           \
        cd modules;                                                  \
        MODULES="";                                                  \
        inst_mod() { These="$$(cat $$1)"; MODULES="$$MODULES $$These"; \
                mkdir -p $$MODLIB/$$2; cp $$These $$MODLIB/$$2;               \
                echo Installing modules under $$MODLIB/$$2; \
        }; \
                                                                               \
	if [ -f BLOCK_MODULES    ]; then inst_mod BLOCK_MODULES    block; fi; \
	if [ -f NET_MODULES      ]; then inst_mod NET_MODULES      net;   fi; \
	if [ -f IPV4_MODULES     ]; then inst_mod IPV4_MODULES     ipv4;  fi; \
	if [ -f IPV6_MODULES     ]; then inst_mod IPV6_MODULES     ipv6;  fi; \
         if [ -f ATM_MODULES      ]; then inst_mod ATM_MODULES      atm;   fi; \
	if [ -f SCSI_MODULES     ]; then inst_mod SCSI_MODULES     scsi;  fi; \
	if [ -f FS_MODULES       ]; then inst_mod FS_MODULES       fs;    fi; \
	if [ -f NLS_MODULES      ]; then inst_mod NLS_MODULES      fs;    fi;        \
	if [ -f CDROM_MODULES    ]; then inst_mod CDROM_MODULES    cdrom; fi;        \
	if [ -f HAM_MODULES      ]; then inst_mod HAM_MODULES      net;   fi;        \
	if [ -f SOUND_MODULES    ]; then inst_mod SOUND_MODULES    sound; fi;        \
	if [ -f VIDEO_MODULES    ]; then inst_mod VIDEO_MODULES    video; fi;        \
	if [ -f FC4_MODULES      ]; then inst_mod FC4_MODULES      fc4;   fi;        \
	if [ -f IRDA_MODULES     ]; then inst_mod IRDA_MODULES     net;   fi;        \
         if [ -f USB_MODULES      ]; then inst_mod USB_MODULES      usb;   fi;        \
         if [ -f SK98LIN_MODULES  ]; then inst_mod SK98LIN_MODULES  net;   fi;        \
         if [ -f SKFP_MODULES     ]; then inst_mod SKFP_MODULES     net;   fi;        \
         if [ -f IEEE1394_MODULES ]; then inst_mod IEEE1394_MODULES ieee1394; fi;     \
         if [ -f PCMCIA_MODULES   ]; then inst_mod PCMCIA_MODULES pcmcia;   fi;       \
         if [ -f PCMCIA_NET_MODULES ]; then inst_mod PCMCIA_NET_MODULES pcmcia; fi;   \
         if [ -f PCMCIA_CHAR_MODULES ]; then inst_mod PCMCIA_CHAR_MODULES pcmcia; fi; \
         if [ -f PCMCIA_SCSI_MODULES ]; then inst_mod PCMCIA_SCSI_MODULES pcmcia; fi; \
                                                                                      \
        for f in *.o; do [ -r $$f ] && echo $$f; done > .allmods; \
        echo $$MODULES | tr ' ' '\n' | sort | comm -23 .allmods - > .misc; \
        if [ -s .misc ]; then inst_mod .misc misc; fi; \
        rm -f .misc .allmods; \
        )
endef

# 		2.0.38	2.2.12	2.3.1
# BLOCK_MODULES	X	X	X
# NET_MODULES	X	X	X
# IPV4_MODULES	X	X	X
# IPV6_MODULES		X	X
# ATM_MODULES			X
# SCSI_MODULES	X	X	X
# FS_MODULES	X	X	X
# NLS_MODULES		X	X
# CDROM_MODULES	X	X	X
# HAM_MODULES		X	X
# SOUND_MODULES		X	X
# VIDEO_MODULES		X	X
# FC4_MODULES		X	X
# IRDA_MODULES		X	X
# USB_MODULES			X

.PHONY: binary binary-arch binary-indep clean debian modules modules_image

test:
	echo version: $(version)
	echo KPKG_ARCH: $(KPKG_ARCH)
	echo $(DEB_BUILD_ARCH)
	echo $(DEB_BUILD_GNU_CPU)
	echo $(DEB_BUILD_GNU_TYPE)
	echo $(DEB_HOST_ARCH)
	echo $(DEB_HOST_GNU_CPU)
	echo $(DEB_HOST_GNU_SYSTEM)
	echo $(DEB_HOST_GNU_TYPE)
	echo $(DEB_BUILD_GNU_SYSTEM)