summaryrefslogtreecommitdiffstats
path: root/debian/rules.real
blob: 45f91261ad745d59324525d7957d48e0a85a39f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#
# This Makefile executes the unpack/build/binary targets for a single
# subarch, which is passed in the subarch variable. Empty subarch
# variable means that we are building for an arch without the subarch.
# Additionally, variables version, abiname and ltver are
# expected to be available (need to be exported from the parent process).
#

include /usr/share/dpkg/default.mk

SHELL  := bash -e
MAINTAINER := $(shell sed -ne 's,^Maintainer: .[^<]*<\([^>]*\)>,\1,p' debian/control)
SOURCE_DATE_UTC_ISO := $(shell date -u -d '@$(SOURCE_DATE_EPOCH)' +%Y-%m-%d)

include debian/rules.defs

ifdef ARCH
  ifneq ($(DEB_HOST_ARCH),$(ARCH))
    $(error Attempting to build a $(ARCH) target but host architecture is $(DEB_HOST_ARCH).  Use dpkg-architecture to override the host architecture)
  endif
endif

ifneq ($(DEB_BUILD_ARCH),$(or $(KERNEL_DEB_ARCH),$(DEB_HOST_ARCH)))
  CROSS_COMPILE := $(or $(KERNEL_GNU_TYPE),$(DEB_HOST_GNU_TYPE))-
else
  CROSS_COMPILE :=
endif
export CROSS_COMPILE

export DH_OPTIONS
export DEB_HOST_ARCH DEB_HOST_GNU_TYPE DEB_BUILD_ARCH
export DEB_BUILD_PATH = $(CURDIR)  # for dpkg-buildflags
export DEB_RULES_REQUIRES_ROOT ?= no
export LC_ALL = C.UTF-8
undefine LANGUAGE

stamp = [ -d $(dir $@) ] || mkdir $(dir $@); touch $@

setup_env := env -u ABINAME -u ARCH -u FEATURESET -u FLAVOUR -u VERSION -u LOCALVERSION
setup_env += DISTRIBUTION_OFFICIAL_BUILD=1 DISTRIBUTOR="$(DEB_VENDOR)" DISTRIBUTION_VERSION="$(SOURCEVERSION)" KBUILD_BUILD_TIMESTAMP="@$(SOURCE_DATE_EPOCH)" KBUILD_BUILD_VERSION_TIMESTAMP="$(DEB_VENDOR) $(SOURCEVERSION) ($(SOURCE_DATE_UTC_ISO))" KBUILD_BUILD_USER="$(word 1,$(subst @, ,$(MAINTAINER)))" KBUILD_BUILD_HOST="$(word 2,$(subst @, ,$(MAINTAINER)))"
setup_env += KBUILD_VERBOSE=$(if $(filter terse,$(DEB_BUILD_OPTIONS)),0,1)

# XXX: All the tools leak flags between host and build all the time, just don't care.  See #1050991.
MAKE_CLEAN = $(setup_env) $(MAKE) KCFLAGS=-fdebug-prefix-map=$(CURDIR)/= KBUILD_HOSTCFLAGS='' HOSTCFLAGS='' KBUILD_HOSTLDFLAGS=''
MAKE_SELF := $(MAKE) -f debian/rules.real $(MAKEOVERRIDES)
MAKEOVERRIDES =

BUILDDEB_ARGS := -Zxz $(if $(filter pkg.linux.quick,$(DEB_BUILD_PROFILES)),-z0)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  THREAD_COUNT = $(subst parallel=,,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
  THREAD_COUNT = 0
endif

#
# Targets
#

packages_enabled := $(shell dh_listpackages)
define if_package
$(if $(filter $(1),$(packages_enabled)),$(2))
endef
define if_profile
$(if $(filter $(1),$(DEB_BUILD_PROFILES)),$(2))
endef

source: $(STAMPS_DIR)/source_$(FEATURESET)

$(BUILD_DIR)/config.$(ARCH)_$(FEATURESET)_$(FLAVOUR): $(KCONFIG)
	mkdir -p '$(dir $@)'
	debian/bin/kconfig.py '$@' $(KCONFIG) $(KCONFIG_OPTIONS) \
		$(call if_profile, pkg.linux.nokerneldbginfo pkg.linux.quick,-o DEBUG_INFO_NONE=y -o DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=n)

define copy_source
mkdir -p '$(1)'
cp -al $(filter-out debian .pc .git, $(wildcard * .[^.]*)) '$(1)'
endef

$(BUILD_DIR)/$(SOURCE_BASENAME)-source-$(UPSTREAMVERSION).tar.xz: DIR = $(BUILD_DIR)/$(SOURCE_BASENAME)-source-$(UPSTREAMVERSION)
$(BUILD_DIR)/$(SOURCE_BASENAME)-source-$(UPSTREAMVERSION).tar.xz: $(STAMPS_DIR)/source
	rm -rf '$@' '$(DIR)'
	$(call copy_source,$(DIR))
	chmod -R u+rw,go=rX '$(DIR)'
	find '$(DIR)' -depth -newermt '@$(SOURCE_DATE_EPOCH)' -print0 | \
		xargs -0r touch --no-dereference --date='@$(SOURCE_DATE_EPOCH)'
	cd '$(BUILD_DIR)'; \
	find '$(SOURCE_BASENAME)-source-$(UPSTREAMVERSION)' \
		-name '*.pyc' -prune -o \
		-print0 | \
		sort -z | \
		tar --owner=root --group=root --numeric-owner \
			--no-recursion --null -T - -c | xz -T$(THREAD_COUNT) > '$(SOURCE_BASENAME)-source-$(UPSTREAMVERSION).tar.xz'
	rm -rf '$(DIR)'

$(BUILD_DIR)/linux-patch-$(UPSTREAMVERSION)-%.patch.xz: $(STAMPS_DIR)/source_none $(STAMPS_DIR)/source_%
	set -o pipefail;						\
	(cd '$(BUILD_DIR)';						\
	 set +e;							\
         diff -urN -p -x debian -x .pc -x .git -x '*.pyc' source_none source_$*; \
	 test $$? -eq 1) |						\
	filterdiff --remove-timestamps --strip=1 --addoldprefix=a/ --addnewprefix=b/ | \
	xz -c >$@ ||							\
	(rm -f $@; exit 1)

$(STAMPS_DIR)/source:
	test -d .pc
	set +e; QUILT_PC=.pc quilt unapplied --quiltrc - >/dev/null && echo 'Patch series not fully applied'; test $$? -eq 1
	@$(stamp)

$(STAMPS_DIR)/source_%: SOURCE_DIR=$(BUILD_DIR)/source
$(STAMPS_DIR)/source_%: DIR=$(BUILD_DIR)/source_$*
$(STAMPS_DIR)/source_%: $(STAMPS_DIR)/source
	mkdir -p '$(BUILD_DIR)'
	rm -rf '$(DIR)'
	$(call copy_source,$(DIR))
	cd '$(DIR)' && QUILT_PATCHES='$(CURDIR)/debian/patches-$*' QUILT_PC=.pc quilt push --quiltrc - -a -q --fuzz=0
	@$(stamp)
.PRECIOUS: $(STAMPS_DIR)/source_%

$(STAMPS_DIR)/source_none: DIR=$(BUILD_DIR)/source_none
$(STAMPS_DIR)/source_none: $(STAMPS_DIR)/source
	mkdir -p '$(BUILD_DIR)'
	rm -f '$(DIR)'
	ln -s '$(CURDIR)' '$(DIR)'
	@$(stamp)

$(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR): CONFIG=$(BUILD_DIR)/config.$(ARCH)_$(FEATURESET)_$(FLAVOUR)
$(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR): SOURCE_DIR=$(BUILD_DIR)/source_$(FEATURESET)
$(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR): DIR=$(BUILD_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
$(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR): KERNEL_DEB_ARCH?=$(DEB_HOST_ARCH)
$(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR): KERNEL_GNU_TYPE?=$(DEB_HOST_GNU_TYPE)
$(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR): $(STAMPS_DIR)/source_$(FEATURESET) $(BUILD_DIR)/config.$(ARCH)_$(FEATURESET)_$(FLAVOUR)
	rm -rf '$(DIR)'
	mkdir '$(DIR)'
	cp '$(CONFIG)' '$(DIR)/.config'
	echo 'override ARCH = $(KERNEL_ARCH)' >> '$(DIR)/.kernelvariables'
	echo 'override KERNELRELEASE = $(ABINAME)$(LOCALVERSION_IMAGE)' >> '$(DIR)/.kernelvariables'
	echo 'CCACHE = ccache' >> '$(DIR)/.kernelvariables'
ifeq (./,$(dir $(COMPILER)))
	echo 'CC = $$(if $$(DEBIAN_KERNEL_USE_CCACHE),$$(CCACHE)) $$(CROSS_COMPILE)$(COMPILER)' >> '$(DIR)/.kernelvariables'
else
# To allow for cross-compiles, we want to split up the configured
# compiler filename and prepend the directory to $PATH.  For a native
# compile, so long as the configured compiler is installed it should
# always be found first.
# However, $PATH changes don't work consistently in make (bug #895835).
# Instead we do the path lookup here and generate a new absolute
# filename.
	echo 'CC = $$(if $$(DEBIAN_KERNEL_USE_CCACHE),$$(CCACHE)) $$(word 1,$$(foreach dir,$(dir $(COMPILER)) $$(subst :, ,$$(PATH)),$$(wildcard $$(dir)/$$(CROSS_COMPILE)$(notdir $(COMPILER)))))' >> '$(DIR)/.kernelvariables'
endif
# TODO: Should we set CROSS_COMPILE always?
	echo 'DEB_BUILD_ARCH ?= $$(shell dpkg --print-architecture)' >> '$(DIR)/.kernelvariables'
	echo 'ifneq ($$(DEB_BUILD_ARCH),$(KERNEL_DEB_ARCH))' >> '$(DIR)/.kernelvariables'
	echo 'override CROSS_COMPILE = $(KERNEL_GNU_TYPE)-' >> '$(DIR)/.kernelvariables'
	echo 'endif' >> '$(DIR)/.kernelvariables'
ifdef KCFLAGS
	echo 'KCFLAGS += $(KCFLAGS)' >> '$(DIR)/.kernelvariables'
endif
ifdef COMPAT_DEB_ARCH
	echo 'ifneq ($$(DEB_BUILD_ARCH),$(COMPAT_DEB_ARCH))' >> '$(DIR)/.kernelvariables'
	echo 'override CROSS_COMPILE_COMPAT = $(COMPAT_GNU_TYPE)-' >> '$(DIR)/.kernelvariables'
	echo 'override CROSS32_COMPILE = $(COMPAT_GNU_TYPE)-' >> '$(DIR)/.kernelvariables'
	echo 'endif' >> '$(DIR)/.kernelvariables'
endif
	echo 'DEBIAN_KERNEL_NO_CC_VERSION_CHECK = y' >> '$(DIR)/.kernelvariables'
	+$(MAKE_CLEAN) -C '$(SOURCE_DIR)' O='$(CURDIR)/$(DIR)' listnewconfig
	+yes "" | $(MAKE_CLEAN) -C '$(SOURCE_DIR)' O='$(CURDIR)/$(DIR)' oldconfig >/dev/null
	@$(stamp)

$(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR): DIR=$(BUILD_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
$(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR): $(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR)

$(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR):
	+$(MAKE_CLEAN) -C '$(DIR)'
	debian/bin/buildcheck.py $(DIR) $(ARCH) $(FEATURESET) $(FLAVOUR)
	@$(stamp)

$(STAMPS_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR): REAL_VERSION = $(ABINAME)$(LOCALVERSION)
$(STAMPS_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR): DIR=$(BUILD_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
$(STAMPS_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR): INSTALL_DIR=$(BUILD_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
$(STAMPS_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR): DT_INSTALL_DIR = $(INSTALL_DIR)/usr/lib/linux-image-$(REAL_VERSION)
$(STAMPS_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR): $(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
	install -D -m644 '$(DIR)/$(IMAGE_FILE)' $(INSTALL_DIR)/boot/$(IMAGE_INSTALL_STEM)-$(REAL_VERSION)
ifeq ($(IMAGE_FILE),vmlinux)
# This is the unprocessed ELF image, so we need to strip debug symbols
	$(CROSS_COMPILE)strip --strip-debug $(INSTALL_DIR)/boot/$(IMAGE_INSTALL_STEM)-$(REAL_VERSION)
endif
	+$(MAKE_CLEAN) -C $(DIR) modules_install DEPMOD='$(CURDIR)/debian/bin/no-depmod' INSTALL_MOD_PATH='$(CURDIR)/$(INSTALL_DIR)' INSTALL_MOD_STRIP=1
ifneq ($(filter arm64 armel armhf mipsel mips64el mipsr6 mipsr6el mips64r6 mips64r6el riscv64,$(ARCH)),)
	+$(MAKE_CLEAN) -C $(DIR) dtbs
	+$(MAKE_CLEAN) -C $(DIR) dtbs_install INSTALL_DTBS_PATH='$(CURDIR)/$(DT_INSTALL_DIR)'
	# dtbs_install sets the executable bit on dtb[o] files, which they shouldn't have
	find $(DT_INSTALL_DIR) -type f \( -name '*.dtb' -o -name '*.dtbo' \) -exec chmod 644 {} \;
endif
	rm -f $(INSTALL_DIR)/lib/modules/$(REAL_VERSION)/build
	rm -f $(INSTALL_DIR)/lib/modules/$(REAL_VERSION)/source
	@$(stamp)

$(STAMPS_DIR)/build-doc: DIR=$(BUILD_DIR)/build-doc
# sphinx commands in /usr/bin are managed by the alternatives system.
# Change $PATH to make sure we consistently use the Python 3 version.
$(STAMPS_DIR)/build-doc: PATH:=/usr/share/sphinx/scripts/python3:$(PATH)
$(STAMPS_DIR)/build-doc: $(STAMPS_DIR)/source
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	mkdir -p '$(BUILD_DIR)'
	rm -rf '$(DIR)'
	$(call copy_source,$(DIR))
	+$(MAKE_CLEAN) -C '$(DIR)' PYTHON=python3 xmldocs
	+$(MAKE_CLEAN) -C '$(DIR)' PYTHON=python3 htmldocs
endif
	@$(stamp)

define dh_binary_pre
	dh_testroot
	dh_prep
	dh_installdirs
endef

define dh_binary_post
	dh_install $(DH_INSTALL_ARGS)
	dh_installdocs $(if $(INSTALLDOCS_LINK_DOC),--link-doc=$(INSTALLDOCS_LINK_DOC))
	dh_installchangelogs
	dh_installexamples
	dh_installman
	dh_installudev
	dh_bugfiles
	dh_ucf
	dh_lintian
	dh_icons
	dh_link
	dh_compress
	dh_fixperms
	dh_missing
	dh_strip
	dh_makeshlibs
	dh_shlibdeps $(DH_SHLIBDEPS_ARGS)
	dh_installdeb
	if command -v dh_movetousr >/dev/null; then dh_movetousr; fi
	dh_gencontrol -- $(GENCONTROL_ARGS)
	dh_md5sums
	dh_builddeb -- $(BUILDDEB_ARGS)
endef

build_meta:

binary_meta:
	$(dh_binary_pre)
	$(dh_binary_post)

build_doc: $(STAMPS_DIR)/build-doc

binary_doc: DIR = $(BUILD_DIR)/build-doc
binary_doc: OUT_DIR = $(DESTDIR)/usr/share/doc/$(PACKAGE_NAME)
binary_doc: $(STAMPS_DIR)/build-doc
	$(dh_binary_pre)
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	mkdir -p $(OUT_DIR)
	set -o pipefail; \
	find CREDITS MAINTAINERS README Documentation \
		-name '.gitignore' -prune -o -name DocBook -prune -o \
		-path Documentation/media -prune -o \
		-path Documentation/sphinx -prune -o \
		-name 'Makefile*' -prune -o \
		-print | \
	cpio -pd --preserve-modification-time '$(OUT_DIR)'
	set -o pipefail; \
	cd $(DIR)/Documentation/output; \
	find * -name '*.rst' -prune -o -name objects.inv -prune \
		-o -name xml -prune -o -print | \
	cpio -pd --preserve-modification-time '$(OUT_DIR)/html'
	find $(OUT_DIR)/Documentation -type f | xargs gzip -9n
# Fix up symlinks to gzipped docs
	cd $(OUT_DIR)/Documentation; \
	find -type l | while read link; do \
		dest="$$(readlink -f "$$link")"; \
		if [ -f "$$dest.gz" ]; then \
			rm -f "$$link"; \
			ln -sr "$$dest.gz" "$$link.gz"; \
		fi; \
	done
endif
	$(dh_binary_post)

build_headers-common:

binary_headers-common: PACKAGE_NAME_KBUILD = linux-kbuild-$(ABINAME)
binary_headers-common: BASE_DIR = /usr/src/$(PACKAGE_NAME)
binary_headers-common: SOURCE_DIR = $(BUILD_DIR)/source_$(FEATURESET)
binary_headers-common: DIR = $(DESTDIR)/$(BASE_DIR)
binary_headers-common: $(STAMPS_DIR)/source_$(FEATURESET)
	$(dh_binary_pre)

	set -o pipefail; \
	cd $(SOURCE_DIR); \
	( \
		echo Makefile; \
		for arch in $(ALL_KERNEL_ARCHES); do \
			find arch/$$arch -maxdepth 1 -name 'Makefile*' -print; \
			find arch/$$arch \( -name 'Kbuild.platforms' -o -name 'Platform' \) -print; \
			find $$(find arch/$$arch \( -name include -o -name scripts \) -type d -print) -print; \
		done; \
		find include -print; \
	) \
	| \
	cpio -pd --preserve-modification-time '$(DIR)'

	dh_link /usr/lib/$(PACKAGE_NAME_KBUILD)/scripts $(BASE_DIR)/scripts
	dh_link /usr/lib/$(PACKAGE_NAME_KBUILD)/tools $(BASE_DIR)/tools

	$(dh_binary_post)

build_headers:

binary_headers: REAL_VERSION = $(ABINAME)$(LOCALVERSION)
binary_headers: PACKAGE_NAME_COMMON = linux-headers-$(ABINAME)-common$(LOCALVERSION_HEADERS)
binary_headers: PACKAGE_NAME_KBUILD = linux-kbuild-$(ABINAME)
binary_headers: BASE_DIR = /usr/src/$(PACKAGE_NAME)
binary_headers: BASE_DIR_COMMON = /usr/src/$(PACKAGE_NAME_COMMON)
binary_headers: SOURCE_DIR = $(BUILD_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
binary_headers: REF_DIR = $(BUILD_DIR)/source_$(FEATURESET)
binary_headers: DIR = $(DESTDIR)/$(BASE_DIR)
binary_headers: $(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
	$(dh_binary_pre)

	mkdir -p $(DIR)/arch/$(KERNEL_ARCH)/kernel
	cp -a $(SOURCE_DIR)/{.config,.kernel*,Module.symvers,include} $(DIR)
	cp -a $(SOURCE_DIR)/arch/$(KERNEL_ARCH)/include $(DIR)/arch/$(KERNEL_ARCH)
	find $(DIR) -name '*.cmd' -delete

	if [ -f $(SOURCE_DIR)/arch/$(KERNEL_ARCH)/lib/crtsavres.o ]; then \
		mkdir $(DIR)/arch/$(KERNEL_ARCH)/lib; \
		cp -a $(SOURCE_DIR)/arch/$(KERNEL_ARCH)/lib/crtsavres.o $(DIR)/arch/$(KERNEL_ARCH)/lib; \
	fi

	cp -a $(SOURCE_DIR)/scripts/module.lds $(DIR)/arch/$(KERNEL_ARCH)

	@echo 'include $(BASE_DIR_COMMON)/Makefile' >> $(DIR)/Makefile

	dh_link /usr/lib/$(PACKAGE_NAME_KBUILD)/scripts $(BASE_DIR)/scripts
	dh_link /usr/lib/$(PACKAGE_NAME_KBUILD)/tools $(BASE_DIR)/tools

	mkdir -p $(DESTDIR)/lib/modules/$(REAL_VERSION)
	ln -s /usr/src/$(PACKAGE_NAME) $(DESTDIR)/lib/modules/$(REAL_VERSION)/build
	ln -s /usr/src/$(PACKAGE_NAME_COMMON) $(DESTDIR)/lib/modules/$(REAL_VERSION)/source

	$(dh_binary_post)

$(STAMPS_DIR)/build_libc-dev: DIR=$(BUILD_DIR)/build_libc-dev
$(STAMPS_DIR)/build_libc-dev:
	rm -rf '$(DIR)/output'
	+$(foreach ARCH,$(ALL_LIBCDEV_KERNELARCHES), \
	   $(MAKE_CLEAN) O='$(CURDIR)/$(DIR)' headers_install ARCH=$(ARCH) INSTALL_HDR_PATH='output/usr'; \
	   mkdir -p '$(DIR)/output/usr/lib/linux/uapi/$(ARCH)'; \
	   mv '$(DIR)/output/usr/include/asm' '$(DIR)/output/usr/lib/linux/uapi/$(ARCH)/asm'; )
	@$(stamp)

build_libc-dev: $(STAMPS_DIR)/build_libc-dev

binary_libc-dev: DIR = $(BUILD_DIR)/build_libc-dev/output
binary_libc-dev: OUT_DIR = debian/$(PACKAGE_NAME)/usr/include
binary_libc-dev: $(STAMPS_DIR)/build_libc-dev
	$(dh_binary_pre)

	dh_install --all --sourcedir $(DIR) usr

# Generate symlink farms for every supported multiarch identifier
	for spec in $(ALL_LIBCDEV_MULTIARCHES); do \
		IFS=: read -r MULTIARCH KERNELARCH <<< "$$spec"; \
		dh_link --all $$(find $(DIR)/usr/lib/linux/uapi/$$KERNELARCH/asm -type f -name '*.h' -printf "usr/lib/linux/uapi/$$KERNELARCH/asm/%P usr/include/$$MULTIARCH/asm/%P\\n");  \
	done

	$(dh_binary_post)

build_support:

binary_support: PACKAGE_ROOT = /usr/share/$(PACKAGE_NAME)
binary_support:
	$(dh_binary_pre)
	dh_installdirs $(PACKAGE_ROOT)/lib/python/debian_linux $(PACKAGE_ROOT)/modules
	cp debian/config.defines.dump $(DESTDIR)$(PACKAGE_ROOT)
	cp -R debian/installer $(DESTDIR)$(PACKAGE_ROOT)/installer
	cp debian/lib/python/debian_linux/*.py $(DESTDIR)$(PACKAGE_ROOT)/lib/python/debian_linux
	dh_python3
	dh_link $(PACKAGE_ROOT) /usr/src/$(PACKAGE_NAME)
	$(dh_binary_post)

setup_image: $(STAMPS_DIR)/setup_$(ARCH)_$(FEATURESET)_$(FLAVOUR)

build_image: $(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)

binary_image: REAL_VERSION = $(ABINAME)$(LOCALVERSION)
binary_image: INSTALL_DIR = $(DESTDIR)/boot
binary_image: DIR = $(BUILD_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
binary_image: DIR2 = $(BUILD_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
binary_image: $(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR) $(STAMPS_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
binary_image:
	$(dh_binary_pre)
	dh_install --sourcedir=$(DIR2) boot
	dh_install --sourcedir=$(DIR2) lib
ifneq ($(filter arm64 armel armhf mipsel mips64el mipsr6 mipsr6el mips64r6 mips64r6el riscv64,$(ARCH)),)
	dh_install --sourcedir=$(DIR2) usr
endif
	sed '/CONFIG_\(MODULE_SIG_\(ALL\|KEY\)\|SYSTEM_TRUSTED_KEYS\|BUILD_SALT\)[ =]/d' $(DIR)/.config > $(DESTDIR)/boot/config-$(REAL_VERSION)
	echo "ffffffffffffffff B The real System.map is in the linux-image-<version>-dbg package" \
		> $(DESTDIR)/boot/System.map-$(REAL_VERSION)
	rm $(DESTDIR)/lib/firmware -rf
	+$(MAKE_SELF) \
	  binary_image_bug \
	  DESTDIR='$(DESTDIR)' PACKAGE_NAME='$(PACKAGE_NAME)' REAL_VERSION='$(REAL_VERSION)'
	dh_strip --no-automatic-dbgsym -Xvmlinux -Xvmlinuz
	$(dh_binary_post)

binary_image_bug:
	dh_installdirs usr/share/bug/$(PACKAGE_NAME)
	dh_install debian/templates/image.bug/* usr/share/bug/$(PACKAGE_NAME)
	chmod 755 $(DESTDIR)/usr/share/bug/$(PACKAGE_NAME)/script
	printf "RELEASE='$(REAL_VERSION)'\nDISTRIBUTOR='$(DEB_VENDOR)'\nSOURCEVERSION='$(SOURCEVERSION)'\nPACKAGE_NAME='$(PACKAGE_NAME)'\nPACKAGE_VERSION='$(SOURCEVERSION)'" > $(DESTDIR)/usr/share/bug/$(PACKAGE_NAME)/info

build_image-dbg: $(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)

binary_image-dbg: REAL_VERSION = $(ABINAME)$(LOCALVERSION)
binary_image-dbg: DEBUG_DIR = $(DESTDIR)/usr/lib/debug
binary_image-dbg: DIR = $(BUILD_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
binary_image-dbg: $(STAMPS_DIR)/build_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
	$(dh_binary_pre)
	dh_installdirs usr/lib/debug usr/lib/debug/boot usr/share/lintian/overrides/
	install -m644 $(DIR)/vmlinux $(DEBUG_DIR)/boot/vmlinux-$(REAL_VERSION)
	install -m644 $(DIR)/System.map $(DEBUG_DIR)/boot/System.map-$(REAL_VERSION)

# cmd_sign=: Don't sign modules
# suffix-y=: Don't compress modules
	+$(MAKE_CLEAN) -C $(DIR) modules_install \
		cmd_sign= \
		suffix-y= \
		DEPMOD='$(CURDIR)/debian/bin/no-depmod' \
		INSTALL_MOD_PATH=$(DEBUG_DIR)

# Verify we don't have any signed modules installed
	@if find $(DEBUG_DIR)/lib/modules/$(REAL_VERSION) -name '*.ko' | xargs grep -q '~Module signature appended~'; then \
		echo; \
		echo 'Signed modules to be installed into debug package $(PACKAGE_NAME)'; \
		echo; \
		exit 1; \
	fi

# Verify we don't have any compressed modules installed
	@if ! find $(DEBUG_DIR)/lib/modules/$(REAL_VERSION) -name '*.ko.*' -exec false {} +; then \
		echo; \
		echo 'Compressed modules to be installed into debug package $(PACKAGE_NAME)'; \
		echo; \
		exit 1; \
	fi

	find $(DEBUG_DIR)/lib/modules/$(REAL_VERSION)/ -mindepth 1 -maxdepth 1 \! -name kernel -exec rm {} \+
	rm $(DEBUG_DIR)/lib/firmware -rf
ifeq ($(VDSO),True)
	+$(MAKE_CLEAN) -C $(DIR) vdso_install INSTALL_MOD_PATH=$(DEBUG_DIR)
# Create .build-id links.  Some architectures already do so, but in the
# wrong place, so delete those first.
	rm -rf $(DEBUG_DIR)/lib/modules/$(REAL_VERSION)/vdso/.build-id
	find $(DEBUG_DIR)/lib/modules/$(REAL_VERSION)/vdso/ -type f | while read vdso; do \
		if buildid="$$(readelf -n "$$vdso" | sed -rne 's/^\s+Build ID: (.*)$$/\1/p')" && [ "$$buildid" ]; then \
			mkdir -p "$(DEBUG_DIR)/.build-id/$${buildid:0:2}"; \
			ln -s -r "$$vdso" "$(DEBUG_DIR)/.build-id/$${buildid:0:2}/$${buildid:2}.debug"; \
		fi; \
	done
endif
# Add symlinks to vmlinux from the locations expected by kdump-tools,
# systemtap and others
	ln -s boot/vmlinux-$(REAL_VERSION) $(DEBUG_DIR)/
	mkdir -p $(DEBUG_DIR)/lib/modules/$(REAL_VERSION)
	ln -s ../../../boot/vmlinux-$(REAL_VERSION) $(DEBUG_DIR)/lib/modules/$(REAL_VERSION)/vmlinux
	$(dh_binary_post)

build_signed-template:

binary_signed-template: GEN_DIR = debian/generated.signed-$(ARCH)
binary_signed-template: CODE_SIGNING_DIR = $(DESTDIR)/usr/share/code-signing/$(PACKAGE_NAME)
binary_signed-template: DEBIAN_DIR = $(CODE_SIGNING_DIR)/source-template/debian
binary_signed-template: VERSION_TEMPLATE = $(shell echo $(DEB_VERSION) | sed -Ee 's/\+b(\d+)$$/.b\1/' -e 's/-/+/g')
binary_signed-template:
	$(dh_binary_pre)
	rm -rf $(CODE_SIGNING_DIR)
	mkdir -p $(DEBIAN_DIR)/source
	for i in $$(find $(GEN_DIR) -type f -printf '%P\n'); do \
		sed -E \
			-e 's/@signedtemplate_binaryversion@/$(DEB_VERSION)/g' \
			-e 's/@signedtemplate_sourceversion@/$(VERSION_TEMPLATE)/g' \
			$(GEN_DIR)/$$i > $(DEBIAN_DIR)/$$i; \
	done
	for i in $$(find debian/signing_templates -type f -printf '%P\n'); do \
		sed -E \
			-e 's/@signedtemplate_binaryversion@/$(DEB_VERSION)/g' \
			-e 's/@signedtemplate_sourceversion@/$(VERSION_TEMPLATE)/g' \
			debian/signing_templates/$$i > $(DEBIAN_DIR)/$$i; \
	done
	mv $(DEBIAN_DIR)/changelog.head $(DEBIAN_DIR)/changelog
	tail +2 debian/changelog >> $(DEBIAN_DIR)/changelog
	mv $(DEBIAN_DIR)/files.json $(CODE_SIGNING_DIR)/
	chmod 755 $(DEBIAN_DIR)/rules
	cp -R	debian/copyright \
		debian/installer \
		$(CODE_SIGNING_DIR)/source-template/debian/
	$(dh_binary_post)

build_installer:
build_installer-test:

binary_installer binary_installer-test: export KW_DEFCONFIG_DIR=debian/installer
binary_installer binary_installer-test: export KW_CONFIG_DIR=debian/installer
binary_installer binary_installer-test: export SOURCEDIR = $(BUILD_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
binary_installer binary_installer-test: REAL_VERSION = $(ABINAME)$(LOCALVERSION)

binary_installer: DH_OPTIONS_IMAGE = -p$(IMAGE_PACKAGE_NAME) --sourcedir=$(SOURCEDIR)
binary_installer: $(STAMPS_DIR)/install_$(ARCH)_$(FEATURESET)_$(FLAVOUR)
	$(dh_binary_pre)
	DH_OPTIONS="$(DH_OPTIONS_IMAGE)" dh_install boot/$(IMAGE_INSTALL_STEM)-$(REAL_VERSION)
	DH_OPTIONS="$(DH_OPTIONS_IMAGE)" dh_install lib/modules/$(REAL_VERSION)/modules.builtin
	DH_OPTIONS="$(DH_OPTIONS_IMAGE)" dh_install lib/modules/$(REAL_VERSION)/modules.order
ifneq ($(filter arm64 armel armhf mipsel mips64el mipsr6 mipsr6el mips64r6 mips64r6el riscv64,$(ARCH)),)
	DH_OPTIONS="$(DH_OPTIONS_IMAGE)" dh_install usr/lib/linux-image-$(REAL_VERSION)
endif
	kernel-wedge copy-modules $(ABINAME) $(FLAVOUR) $(REAL_VERSION)
	kernel-wedge find-dups $(REAL_VERSION)
	kernel-wedge find-unpackaged $(REAL_VERSION) $(REAL_VERSION)
	kernel-wedge check $$(dh_listpackages)
	dh_fixperms
	dh_gencontrol
	dh_builddeb -- $(BUILDDEB_ARGS)

binary_installer-test: export DEB_BUILD_PROFILES += pkg.linux.udeb-unsigned-test-build
binary_installer-test:
	if [ -n "$$(dh_listpackages)" ]; then \
		dh_testdir; \
		dh_prep; \
		kernel-wedge copy-modules $(ABINAME) $(FLAVOUR) $(REAL_VERSION); \
		kernel-wedge find-dups $(REAL_VERSION); \
		kernel-wedge find-unpackaged $(REAL_VERSION) $(REAL_VERSION); \
		kernel-wedge check $$(dh_listpackages); \
	fi

build_source:

binary_source: BUILDDEB_ARGS = -Zxz -z0
binary_source: $(BUILD_DIR)/$(SOURCE_BASENAME)-source-$(UPSTREAMVERSION).tar.xz $(foreach FEATURESET,$(filter-out none,$(ALL_FEATURESETS)),$(BUILD_DIR)/linux-patch-$(UPSTREAMVERSION)-$(FEATURESET).patch.xz)
	$(dh_binary_pre)
	dh_install $^ /usr/src
	$(dh_binary_post)

build_config:

binary_config: TRIPLETS = $(subst $(BUILD_DIR)/build_,,$(wildcard $(BUILD_DIR)/build_$(ARCH)_*_*))
binary_config:
	$(dh_binary_pre)
	dh_installdirs /usr/src/linux-config-$(UPSTREAMVERSION)
# Fix the module signing configuration to work for custom kernels.  Also delete
# CONFIG_BUILD_SALT which makes no sense for custom kernels.
	for triplet in $(TRIPLETS); do \
		sed '/CONFIG_\(MODULE_SIG_\(ALL\|KEY\)\|SYSTEM_TRUSTED_KEYS\|BUILD_SALT\)[ =]/d' $(BUILD_DIR)/build_$$triplet/.config | xz -c >debian/$(PACKAGE_NAME)/usr/src/linux-config-$(UPSTREAMVERSION)/config.$$triplet.xz; \
	done
	$(dh_binary_post)

define make-tools
+mkdir -p $(BUILD_DIR)/build-tools/$(1) && $(MAKE_CLEAN) -C $(BUILD_DIR)/build-tools/$(1) -f $(CURDIR)/debian/rules.d/$(1)/Makefile top_srcdir=$(CURDIR) top_rulesdir=$(CURDIR)/debian/rules.d OUTDIR=$(1) VERSION=$(VERSION) KERNEL_ARCH=$(KERNEL_ARCH)
endef

$(STAMPS_DIR)/build-tools-headers:
	mkdir -p $(BUILD_DIR)/build-tools/headers-tools
	$(MAKE) ARCH=$(KERNEL_ARCH) O=$(BUILD_DIR)/build-tools/headers-tools \
		INSTALL_HDR_PATH=$(CURDIR)/$(BUILD_DIR)/build-tools \
		headers_install
	@$(stamp)

build_bpftool: $(STAMPS_DIR)/build-tools-headers
	$(call make-tools,tools/bpf/bpftool)

binary_bpftool: BPFTOOL_VERSION_MAJOR = $(shell sed -ne 's,^#define LIBBPF_MAJOR_VERSION \(.*\)$$,\1,p' tools/lib/bpf/libbpf_version.h)
binary_bpftool: BPFTOOL_VERSION_MINOR = $(shell sed -ne 's,^#define LIBBPF_MINOR_VERSION \(.*\)$$,\1,p' tools/lib/bpf/libbpf_version.h)
binary_bpftool: BPFTOOL_VERSION_PATCH = $(shell sed -ne 's,^#define BPFTOOL_PATCH_VERSION \(.*\)$$,\1,p' tools/bpf/bpftool/main.c)
binary_bpftool: BPFTOOL_VERSION = $(shell expr $(BPFTOOL_VERSION_MAJOR) + 6).$(BPFTOOL_VERSION_MINOR).$(BPFTOOL_VERSION_PATCH)
binary_bpftool: GENCONTROL_ARGS = -v$(BPFTOOL_VERSION)+$(DEB_VERSION)
binary_bpftool: build_bpftool
	$(dh_binary_pre)
	$(call make-tools,tools/bpf/bpftool) install
	$(dh_binary_post)

build_kbuild: $(STAMPS_DIR)/build-tools-headers
	$(call make-tools,certs)
	$(call make-tools,scripts)
	$(call make-tools,tools/objtool)

binary_kbuild: PREFIX_DIR = /usr/lib/$(PACKAGE_NAME)
binary_kbuild: build_kbuild
	$(dh_binary_pre)
	$(call make-tools,certs) install prefix=$(PREFIX_DIR)
	$(call make-tools,scripts) install prefix=$(PREFIX_DIR)
	$(call make-tools,tools/objtool) install prefix=$(PREFIX_DIR)
	dh_link $(PREFIX_DIR) /usr/src/$(PACKAGE_NAME)
	$(dh_binary_post)

build_cpupower: $(STAMPS_DIR)/build-tools-headers
	$(call make-tools,tools/power/cpupower)
ifneq ($(filter i386 amd64 x32,$(DEB_HOST_ARCH)),)
	$(call make-tools,tools/power/x86)
endif

binary_cpupower: DIR = $(CURDIR)/debian/cpupower-tmp
binary_cpupower: DH_INSTALL_ARGS = --sourcedir=$(DIR)
binary_cpupower: build_cpupower
	$(dh_binary_pre)
	$(call make-tools,tools/power/cpupower) install DESTDIR=$(DIR)
ifneq ($(filter i386 amd64 x32,$(DEB_HOST_ARCH)),)
	$(call make-tools,tools/power/x86) install DESTDIR=$(DIR)
	echo >> debian/linux-cpupower.substvars 'cpupower:Arch-Description='\
'The turbostat command reports topology, frequency, idle power-state$${Newline}'\
'statistics, temperature and power consumption information as provided$${Newline}'\
'by the CPU.$${Newline}'\
'$${Newline}'\
'The x86_energy_perf_policy command sets a general policy on some Intel$${Newline}'\
'CPU models for the relative importance of performance versus energy$${Newline}'\
'savings.'
endif
	$(dh_binary_post)

binary_perf build_perf: export _PYTHON_SYSCONFIGDATA_NAME = _sysconfigdata__$(DEB_HOST_MULTIARCH)

build_perf: $(STAMPS_DIR)/build-tools-headers
	$(call make-tools,tools/perf)

binary_perf: DH_SHLIBDEPS_ARGS = -Xperf-read-vdso
binary_perf: build_perf
	$(dh_binary_pre)
	$(call make-tools,tools/perf) install
	# do not ship python2 script
	rm -f $(DIR)/usr/lib/perf-core/scripts/python/call-graph-from-sql.py
	dh_perl /usr/lib/perf-core/scripts/perl/Perf-Trace-Util/lib/
	dh_python3 /usr/lib/perf-core/scripts/python/Perf-Trace-Util/lib/
	$(dh_binary_post)

build_usbip: $(STAMPS_DIR)/build-tools-headers
	$(call make-tools,tools/usb/usbip)

binary_usbip: DIR = $(CURDIR)/debian/usbip-tmp
binary_usbip: DH_INSTALL_ARGS = --sourcedir=$(DIR)
binary_usbip: USBIP_VERSION = $(shell sed -Ene 's/^AC_INIT\(\[\S+\],\s*\[(\S+)\].*\)$$/\1/p' tools/usb/usbip/configure.ac)
binary_usbip: GENCONTROL_ARGS = -v$(USBIP_VERSION)+$(DEB_VERSION)
binary_usbip: build_usbip
	$(dh_binary_pre)
	$(call make-tools,tools/usb/usbip) install DESTDIR=$(DIR)
	$(dh_binary_post)

build_hyperv-daemons: $(STAMPS_DIR)/build-tools-headers
	$(call make-tools,tools/hv)

binary_hyperv-daemons: build_hyperv-daemons
	$(dh_binary_pre)
	$(call make-tools,tools/hv) install
	for service in fcopy kvp vss; do \
		dh_installsystemd --name hv-$$service-daemon --no-enable --no-start \
			|| break; \
		dh_installinit --name hv-$$service-daemon \
			|| break; \
	done
	$(dh_binary_post)

build_rtla: $(STAMPS_DIR)/build-tools-headers
	$(call make-tools,tools/tracing/rtla)

binary_rtla: build_rtla
	$(dh_binary_pre)
	$(call make-tools,tools/tracing/rtla) install
	$(dh_binary_post)

setup_%:
	@:

.PHONY: binary_% build_% setup_% binary-% build-% install-% setup-% source

# vim: filetype=make