summaryrefslogtreecommitdiffstats
path: root/debian/rules
blob: 46f802a991e762581977862c83d4652ac5af4967 (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
#!/usr/bin/make -f
SHELL		:= bash

deb_version		:= $(shell dpkg-parsechangelog | sed -ne "s/^Version: \(.*\)/\1/p")
upstream_version	:= $(shell echo $(deb_version) | sed -e "s/-[^-]*$$//")

package		:= grub2

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null)
DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)
DEB_HOST_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)

export DEB_BUILD_MAINT_OPTIONS = optimize=-lto

HOST_CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
# -O3 (default on Ubuntu ppc64el) introduces various -Werror failures, and
# isn't worth it here.
HOST_CFLAGS := -Wall -Wno-error=unused-result $(shell dpkg-buildflags --get CFLAGS | perl -pe 's/-O3\b/-O2/')
HOST_LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

export DEB_HOST_ARCH
export HOST_CPPFLAGS
export HOST_CFLAGS
export HOST_LDFLAGS
export TARGET_CPPFLAGS := -Wno-unused-but-set-variable
export TARGET_LDFLAGS := -no-pie

# Ensure that debhelper doesn't try to set these; we need to be careful
# about HOST_* vs. TARGET_*.
export CPPFLAGS :=
export CFLAGS :=
export LDFLAGS :=

ifeq (,$(shell command -v qemu-system-i386))
with_check := no
else
with_check := yes
endif

CC := gcc-10

confflags = \
	PACKAGE_VERSION="$(deb_version)" PACKAGE_STRING="GRUB $(deb_version)" \
	CC=$(CC) TARGET_CC=$(CC) \
	--libdir=\$${prefix}/lib --libexecdir=\$${prefix}/lib \
	--enable-grub-mkfont \
	--disable-grub-emu-usb
substvars =

AUTOGEN_DEB_FILES = config templates preinst postinst postrm dirs install links maintscript bash-completion

BUILD_PACKAGES := $(strip $(shell dh_listpackages))
# REAL_PACKAGES build an actual grub variant (and therefore have both configure
# and build stages). EXTRA_PACKAGES do not build grub but may depend on a
# REAL_PACKAGE (and therefore only have a build stage)
REAL_PACKAGES = grub-common grub-emu grub-pc grub-coreboot grub-efi-ia32 grub-efi-amd64 grub-efi-ia64 grub-efi-arm grub-efi-arm64 grub-ieee1275 grub-firmware-qemu grub-uboot grub-xen grub-yeeloong
EXTRA_PACKAGES = grub-rescue-pc grub-xen-host

ifneq (,$(filter i386 amd64,$(DEB_HOST_ARCH_CPU)))
COMMON_PLATFORM := pc
else ifneq (,$(filter powerpc ppc64 ppc64el sparc sparc64,$(DEB_HOST_ARCH_CPU)))
COMMON_PLATFORM := ieee1275
else ifeq (mipsel,$(DEB_HOST_ARCH_CPU))
COMMON_PLATFORM := yeeloong
else ifeq (ia64,$(DEB_HOST_ARCH_CPU))
COMMON_PLATFORM := efi-ia64
else ifeq (arm,$(DEB_HOST_ARCH_CPU))
COMMON_PLATFORM := uboot
else ifeq (arm64,$(DEB_HOST_ARCH_CPU))
COMMON_PLATFORM := efi-arm64
else
COMMON_PLATFORM := none
BUILD_PACKAGES += grub-none
endif

ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
DEFAULT_CMDLINE := quiet splash
DEFAULT_TIMEOUT := 10
ifeq ($(DEB_HOST_ARCH_CPU),ppc64el)
FLICKER_FREE_BOOT := no
else
FLICKER_FREE_BOOT := yes
endif
confflags += --enable-ubuntu-recovery
else ifeq (yes,$(shell dpkg-vendor --derives-from Tanglu && echo yes))
DEFAULT_CMDLINE := quiet splash
DEFAULT_TIMEOUT := 10
FLICKER_FREE_BOOT := yes
else
DEFAULT_CMDLINE := quiet splash
DEFAULT_TIMEOUT := 5
FLICKER_FREE_BOOT := no
endif

ifeq ($(FLICKER_FREE_BOOT),yes)
DEFAULT_HIDDEN_TIMEOUT := 0
DEFAULT_HIDDEN_TIMEOUT_BOOL := true
confflags += \
	--enable-quiet-boot \
	--enable-quick-boot \
	--enable-gfxpayload-dynamic \
	--enable-vt-handoff
substvars += \
	-Vlsb-base-depends="lsb-base (>= 3.0-6)" \
	-Vgfxpayload-depends="grub-gfxpayload-lists [any-i386 any-amd64]"
else
DEFAULT_HIDDEN_TIMEOUT :=
DEFAULT_HIDDEN_TIMEOUT_BOOL := false
endif

# Secure Boot
debian/stamps/build-grub-efi-ia32 install/grub-efi-ia32: export SB_PLATFORM := i386-efi
debian/stamps/build-grub-efi-ia32 install/grub-efi-ia32: export SB_EFI_NAME := ia32
debian/stamps/build-grub-efi-amd64 install/grub-efi-amd64: export SB_PLATFORM := x86_64-efi
debian/stamps/build-grub-efi-amd64 install/grub-efi-amd64: export SB_EFI_NAME := x64
debian/stamps/build-grub-efi-arm64 install/grub-efi-arm64: export SB_PLATFORM := arm64-efi
debian/stamps/build-grub-efi-arm64 install/grub-efi-arm64: export SB_EFI_NAME := aa64
SB_PACKAGE :=
ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
ifeq ($(DEB_HOST_ARCH),amd64)
SB_PACKAGE := grub-efi-amd64
endif
ifeq ($(DEB_HOST_ARCH),arm64)
SB_PACKAGE := grub-efi-arm64
endif
endif

# Downstream distributions that want to support SB and build images, but do not
# rebuild grub, need a programmatic way to get the vendor, as it's used by build-efi-images
# to create the monolithic Grub image and thus is needed to create the partitions on the EFI
# media. Add it to the control file user metadata: XB-Efi-Vendor: $vendor
SB_EFI_VENDOR ?= $(shell dpkg-vendor --query vendor | tr '[:upper:]' '[:lower:]')

%:
	dh $@ --with=bash_completion

override_dh_auto_configure: $(patsubst %,configure/%,$(BUILD_PACKAGES))

override_dh_auto_build: $(patsubst %,build/%,$(BUILD_PACKAGES))

override_dh_auto_install: $(patsubst %,install/%,$(BUILD_PACKAGES))

$(patsubst %,configure/%,$(REAL_PACKAGES)) :: configure/% : debian/stamps/configure-%
$(patsubst %,build/%,$(REAL_PACKAGES) $(EXTRA_PACKAGES)) :: build/% : debian/stamps/build-%

debian/stamps/configure-%: package = $(subst debian/stamps/configure-,,$@)
debian/stamps/configure-%: export DH_OPTIONS = -p$(package) -Bobj/$(package)

debian/stamps/build-%: package = $(subst debian/stamps/build-,,$@)
debian/stamps/build-%: export DH_OPTIONS = -p$(package) -Bobj/$(package)

install/%: package = $(subst install/,,$@)
install/%: package_bin = $(package)-bin
install/%: package_dbg = $(package)-dbg
install/grub-emu: package_bin = grub-emu
install/grub-emu: package_dbg = grub-emu-dbg
install/%: export DH_OPTIONS = -p$(package) -Bobj/$(patsubst grub-common,grub-$(COMMON_PLATFORM),$(package))

override_dh_autoreconf:
	mkdir -p debian/stamps
	rm -rf debian/grub-extras-enabled
	mkdir debian/grub-extras-enabled
	set -e; for extra in 915resolution ntldr-img; do \
		cp -a debian/grub-extras/$$extra debian/grub-extras-enabled/; \
	done
	env -u DH_OPTIONS GRUB_CONTRIB=$(CURDIR)/debian/grub-extras-enabled \
		PYTHON=python3 \
		dh_autoreconf -- ./autogen.sh

debian/stamps/configure-grub-common: debian/stamps/configure-grub-$(COMMON_PLATFORM)
	touch $@

debian/stamps/build-grub-common: debian/stamps/build-grub-$(COMMON_PLATFORM)
	touch $@

debian/stamps/configure-grub-none debian/stamps/configure-grub-pc debian/stamps/configure-grub-ieee1275 debian/stamps/configure-grub-coreboot debian/stamps/configure-grub-emu debian/stamps/configure-grub-uboot debian/stamps/configure-grub-yeeloong:
	mkdir -p debian/stamps obj/$(package)
	dh_auto_configure -- $(confflags) --with-platform=$(subst grub-,,$(package))
	touch $@

# This name scheme leaves room for things like amd32 someday
debian/stamps/configure-grub-efi-ia32:
	mkdir -p debian/stamps obj/$(package)
	dh_auto_configure -- $(confflags) --with-platform=efi --target=i386-pe --program-prefix=""
	touch $@
debian/stamps/configure-grub-efi-amd64:
	mkdir -p debian/stamps $(subst debian/stamps/configure-,obj/,$@)
	dh_auto_configure -- $(confflags) --with-platform=efi --target=amd64-pe --program-prefix=""
	touch $@
debian/stamps/configure-grub-efi-ia64 debian/stamps/configure-grub-efi-arm debian/stamps/configure-grub-efi-arm64:
	mkdir -p debian/stamps $(subst debian/stamps/configure-,obj/,$@)
	dh_auto_configure -- $(confflags) --with-platform=efi
	touch $@

debian/stamps/configure-grub-xen-i386 debian/stamps/configure-grub-xen-amd64:
	mkdir -p debian/stamps obj/$(package)
	dh_auto_configure -- $(confflags) --with-platform=xen --target=$(subst debian/stamps/configure-grub-xen-,,$@) --program-prefix=""
	touch $@
debian/stamps/configure-grub-xen-pvh-i386:
	mkdir -p debian/stamps obj/$(package)
	dh_auto_configure -- $(confflags) --with-platform=xen_pvh --target=i386 --program-prefix=""
	touch $@
debian/stamps/configure-grub-xen: debian/stamps/configure-grub-xen-i386 debian/stamps/configure-grub-xen-amd64 debian/stamps/configure-grub-xen-pvh-i386
	touch $@

debian/stamps/configure-grub-firmware-qemu:
	mkdir -p debian/stamps $(subst debian/stamps/configure-,obj/,$@)
	dh_auto_configure -- $(confflags) --with-platform=qemu
	touch $@

debian/stamps/build-grub-none debian/stamps/build-grub-efi-ia64 debian/stamps/build-grub-efi-arm debian/stamps/build-grub-coreboot debian/stamps/build-grub-emu debian/stamps/build-grub-uboot debian/stamps/build-grub-xen-i386 debian/stamps/build-grub-xen-amd64 debian/stamps/build-grub-xen-pvh-i386 debian/stamps/build-grub-yeeloong: debian/stamps/build-%: debian/stamps/configure-%
	dh_auto_build
	touch $@

debian/stamps/build-grub-efi-ia32 debian/stamps/build-grub-efi-amd64 debian/stamps/build-grub-efi-arm64: debian/stamps/build-%: debian/stamps/configure-% debian/stamps/build-grub-$(COMMON_PLATFORM)
	dh_auto_build
	grub_dir=`mktemp -d` ; \
	sed -e "s/@DEB_VERSION@/$(deb_version)/g" \
		-e "s/@UPSTREAM_VERSION@/$(upstream_version)/g" \
		<debian/sbat.$(SB_EFI_VENDOR).csv.in \
		>$${grub_dir}/sbat.$(SB_EFI_VENDOR).csv; \
	debian/build-efi-images \
		obj/grub-$(COMMON_PLATFORM)/grub-mkimage \
		obj/$(package)/grub-core \
		obj/monolithic/$(package) \
		$(DEB_HOST_ARCH) $(SB_PLATFORM) $(SB_EFI_NAME) \
		$${grub_dir}/sbat.$(SB_EFI_VENDOR).csv $(SB_EFI_VENDOR)
	touch $@

debian/stamps/build-grub-xen-host-i386: PVBOOT_ARCH := i386
debian/stamps/build-grub-xen-host-i386: debian/stamps/build-grub-xen-i386
debian/stamps/build-grub-xen-host-amd64: PVBOOT_ARCH := x86_64
debian/stamps/build-grub-xen-host-amd64: debian/stamps/build-grub-xen-amd64
debian/stamps/build-grub-xen-host-i386 debian/stamps/build-grub-xen-host-amd64:
	target=$(subst debian/stamps/build-grub-xen-host-,,$@) ; \
	obj_dir=$(CURDIR)/obj/grub-xen-$${target} ; \
	grub_dir=`mktemp -d` ; \
	grub_memdisk=`mktemp` ; \
	trap "rm -rf $${grub_dir} $${grub_memdisk}" EXIT HUP INT QUIT TERM ; \
	mkdir -p $${grub_dir}/boot/grub ; \
	mkdir -p $(CURDIR)/obj/$(package) ; \
	sed -e "s/@@PVBOOT_ARCH@@/$(PVBOOT_ARCH)/g" \
	    <debian/grub-xen-host_grub.cfg \
	    >$${grub_dir}/grub.cfg ; \
	tar -cf - -C $${grub_dir} grub.cfg > $${grub_memdisk} ; \
	$${obj_dir}/grub-mkimage \
		-O $(PVBOOT_ARCH)-xen \
		-c $(CURDIR)/debian/grub-xen-host_grub-bootstrap.cfg \
		-d $${obj_dir}/grub-core \
		$${obj_dir}/grub-core/*.mod \
		-m $${grub_memdisk} \
		-o $(CURDIR)/obj/$(package)/grub-$(PVBOOT_ARCH)-xen.bin
	touch $@

debian/stamps/build-grub-xen-host-pvh-i386: debian/stamps/build-grub-xen-pvh-i386
	target=$(subst debian/stamps/build-grub-xen-host-,,$@) ; \
	obj_dir=$(CURDIR)/obj/grub-xen-$${target} ; \
	grub_dir=`mktemp -d` ; \
	grub_memdisk=`mktemp` ; \
	trap "rm -rf $${grub_dir} $${grub_memdisk}" EXIT HUP INT QUIT TERM ; \
	mkdir -p $${grub_dir}/boot/grub ; \
	mkdir -p $(CURDIR)/obj/$(package) ; \
	cp -a debian/grub-xen-host_grub-pvh.cfg $${grub_dir}/grub.cfg ; \
	tar -cf - -C $${grub_dir} grub.cfg > $${grub_memdisk} ; \
	$${obj_dir}/grub-mkimage \
		-O i386-xen_pvh \
		-c $(CURDIR)/debian/grub-xen-host_grub-bootstrap.cfg \
		-d $${obj_dir}/grub-core \
		$${obj_dir}/grub-core/*.mod \
		-m $${grub_memdisk} \
		-o $(CURDIR)/obj/$(package)/grub-i386-xen_pvh.bin
	touch $@

debian/stamps/build-grub-xen-host: debian/stamps/build-grub-xen-host-i386 debian/stamps/build-grub-xen-host-amd64 debian/stamps/build-grub-xen-host-pvh-i386
	touch $@

debian/stamps/build-grub-pc: debian/stamps/configure-grub-pc
	dh_auto_build
ifeq ($(with_check), yes)
	LC_MESSAGES=C.UTF-8 LC_CTYPE=C.UTF-8 PATH="$$PATH:/sbin:/usr/sbin" VERBOSE=1 dh_auto_test --max-parallel=1
endif
	touch $@

debian/stamps/build-grub-ieee1275: debian/stamps/configure-grub-ieee1275
	dh_auto_build
ifneq (,$(filter powerpc ppc64 ppc64el,$(DEB_HOST_ARCH_CPU)))
	$(CC) $(HOST_CFLAGS) debian/prep-bootdev.c -o debian/prep-bootdev -lparted
endif
	touch $@

debian/stamps/build-grub-xen: debian/stamps/build-grub-xen-i386 debian/stamps/build-grub-xen-amd64 debian/stamps/build-grub-xen-pvh-i386
	touch $@

debian/stamps/build-grub-firmware-qemu: debian/stamps/configure-grub-firmware-qemu
	dh_auto_build
	grub_dir=`mktemp -d` ; \
	grub_memdisk=`mktemp` ; \
	trap "rm -rf $${grub_dir} $${grub_memdisk}" EXIT HUP INT QUIT TERM ; \
	mkdir -p $${grub_dir}/boot/grub ; \
	cp debian/grub-firmware-qemu_grub.cfg $${grub_dir}/boot/grub/grub.cfg ; \
	tar -cf - -C $${grub_dir} boot > $${grub_memdisk} ; \
	obj/$(package)/grub-mkimage \
		-O i386-qemu \
		-d $(CURDIR)/obj/$(package)/grub-core \
		$(CURDIR)/obj/$(package)/grub-core/*.mod \
		-m $${grub_memdisk} \
		-o $(CURDIR)/obj/$(package)/grub.bin
	touch $@

debian/stamps/build-grub-rescue-pc: debian/stamps/build-grub-pc
	mkdir -p obj/grub-rescue-pc/rescue-disk/boot/grub
	cp docs/grub.cfg obj/grub-rescue-pc/rescue-disk/boot/grub/
	rm -rf obj/grub-rescue-pc/grub-core
	cp -a obj/grub-pc/grub-core obj/grub-rescue-pc/grub-core
	cp -a obj/grub-pc/unicode.pf2 obj/grub-rescue-pc/
	pkgdatadir=$(CURDIR)/obj/grub-rescue-pc \
		obj/grub-pc/grub-mkrescue \
		--directory=$(CURDIR)/obj/grub-rescue-pc/grub-core \
		--locale-directory=$(CURDIR)/obj/grub-rescue-pc/grub-core/po \
		--output=$(CURDIR)/obj/grub-rescue-pc/grub-rescue-cdrom.iso \
		$(CURDIR)/obj/grub-rescue-pc/rescue-disk
	# save space for floppy image
	rm -rf obj/grub-rescue-pc/grub-core/po obj/grub-rescue-pc/unicode.pf2
	pkgdatadir=$(CURDIR)/obj/grub-rescue-pc \
		obj/grub-pc/grub-mkrescue \
		--directory=$(CURDIR)/obj/grub-rescue-pc/grub-core \
		--locale-directory=$(CURDIR)/obj/grub-rescue-pc/grub-core/po \
		--output=$(CURDIR)/obj/grub-rescue-pc/grub-rescue-floppy.img \
		--compress=xz \
		-no-pad \
		$(CURDIR)/obj/grub-rescue-pc/rescue-disk
	touch $@

platform_subst = \
	if [ -e debian/$(1) ]; then \
		debian/platform-subst \
			PACKAGE="$(2)" \
			DEFAULT_CMDLINE="$(DEFAULT_CMDLINE)" \
			DEFAULT_TIMEOUT="$(DEFAULT_TIMEOUT)" \
			DEFAULT_HIDDEN_TIMEOUT_BOOL="$(DEFAULT_HIDDEN_TIMEOUT_BOOL)" \
			debian/$(1) >> debian/$(2).$(3); \
	fi

install/grub-none:
	dh_auto_install --destdir=debian/tmp-$(package)

	# Avoid failures later if we're building from a tree with no .po
	# files.
	mkdir -p debian/tmp-$(package)/usr/share/locale

install/grub-pc install/grub-efi-ia32 install/grub-efi-amd64 install/grub-efi-ia64 install/grub-efi-arm install/grub-efi-arm64 install/grub-ieee1275 install/grub-coreboot install/grub-emu install/grub-uboot install/grub-xen install/grub-yeeloong:
	set -e ; \
	if [ "$@" = "install/grub-xen" ] ; then \
		dh_auto_install -Bobj/grub-xen-i386 --destdir=debian/tmp-$(package); \
		dh_auto_install -Bobj/grub-xen-amd64 --destdir=debian/tmp-$(package); \
		dh_auto_install -Bobj/grub-xen-pvh-i386 --destdir=debian/tmp-$(package); \
	else \
		dh_auto_install --destdir=debian/tmp-$(package); \
	fi

	set -e ; \
	if [ -d "obj/monolithic/$(package)" ] ; then \
		install -m 0755 -d debian/$(package_bin)/usr/lib/grub/$(SB_PLATFORM)/monolithic ; \
		install -m 0644 -t debian/$(package_bin)/usr/lib/grub/$(SB_PLATFORM)/monolithic obj/monolithic/$(package)/*.efi ; \
		debian/signing-template.generate "$(package)" ; \
	fi

	set -e ; \
	for i in $(AUTOGEN_DEB_FILES) ; do \
		> debian/$(package).$$i; \
		if [ "$@" != "install/grub-emu" ] ; then \
			$(call platform_subst,$$i.in,$(package),$$i); \
		fi ; \
		$(call platform_subst,$(package).$$i.in,$(package),$$i); \
		$(call platform_subst,$(package).$$i.$(DEB_HOST_ARCH_CPU).in,$(package),$$i); \
		$(call platform_subst,$(package).$$i.$(DEB_HOST_ARCH_OS).in,$(package),$$i); \
		$(call platform_subst,$(package).$$i.$(DEB_HOST_ARCH_OS)-$(DEB_HOST_ARCH_CPU).in,$(package),$$i); \
		[ -s debian/$(package).$$i ] || rm -f debian/$(package).$$i; \
		if [ "$@" != "install/grub-emu" ] ; then \
			> debian/$(package_bin).$$i; \
			$(call platform_subst,$$i-bin.in,$(package_bin),$$i); \
			$(call platform_subst,$(package_bin).$$i.in,$(package_bin),$$i); \
			$(call platform_subst,$(package_bin).$$i.$(DEB_HOST_ARCH_CPU).in,$(package_bin),$$i); \
			$(call platform_subst,$(package_bin).$$i.$(DEB_HOST_ARCH_OS).in,$(package_bin),$$i); \
			$(call platform_subst,$(package_bin).$$i.$(DEB_HOST_ARCH_OS)-$(DEB_HOST_ARCH_CPU).in,$(package_bin),$$i); \
			[ -s debian/$(package_bin).$$i ] || rm -f debian/$(package_bin).$$i; \
		fi ; \
		> debian/$(package_dbg).$$i; \
		if [ "$@" != "install/grub-emu" ] ; then \
			$(call platform_subst,$$i-dbg.in,$(package_dbg),$$i); \
		fi ; \
		$(call platform_subst,$(package_dbg).$$i.in,$(package_dbg),$$i); \
		$(call platform_subst,$(package_dbg).$$i.$(DEB_HOST_ARCH_CPU).in,$(package_dbg),$$i); \
		$(call platform_subst,$(package_dbg).$$i.$(DEB_HOST_ARCH_OS).in,$(package_dbg),$$i); \
		$(call platform_subst,$(package_dbg).$$i.$(DEB_HOST_ARCH_OS)-$(DEB_HOST_ARCH_CPU).in,$(package_dbg),$$i); \
		[ -s debian/$(package_dbg).$$i ] || rm -f debian/$(package_dbg).$$i; \
	done

	find debian/tmp-$(package)/usr/lib/grub -name modinfo.sh -print0 | \
		xargs -0r chmod +x
	find debian/tmp-$(package)/usr/lib/grub -name gdb_grub -print0 | \
		xargs -0r chmod -x
	find debian/tmp-$(package)/usr/lib/grub -name gmodule.pl -print0 | \
		xargs -0r chmod -x

	mkdir -p debian/$(package_bin)/usr/share/lintian/overrides
	echo "$(package_bin): unstripped-binary-or-object *.mod" \
		>> debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin)
	cd debian/tmp-$(package) && find usr/lib/grub -name kernel.img \
		| sed -e "s%.*%$(package_bin): statically-linked-binary &%g" \
	>> $(CURDIR)/debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin)
	cd debian/tmp-$(package) && find usr/lib/grub -name kernel.img \
		| sed -e "s%.*%$(package_bin): unstripped-binary-or-object &%g" \
	>> $(CURDIR)/debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin)
	if ([ "$@" = "install/grub-efi-amd64" ] && [ "$(DEB_HOST_ARCH_CPU)" = "i386" ]) || \
	   ([ "$@" != "install/grub-emu" ] && [ "$(DEB_HOST_ARCH_CPU):$(DEB_HOST_ARCH_BITS)" = "amd64:32" ]) || \
	   [ "$@" = "install/grub-xen" ]; then \
		echo "$(package_bin): binary-from-other-architecture *.mod" \
			>> debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin) ; \
		cd debian/tmp-$(package) && find usr/lib/grub -name kernel.img \
			| sed -e "s%.*%$(package_bin): binary-from-other-architecture &%g" \
		>> $(CURDIR)/debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin) ; \
	fi
	if ([ "$@" = "install/grub-pc" ] || \
	    [ "$@" = "install/grub-ieee1275" ] || \
	    [ "$@" = "install/grub-coreboot" ] || \
	    [ "$@" = "install/grub-xen" ] || \
	    [ "$@" = "install/grub-emu" ]) && \
	   [ "$(DEB_HOST_ARCH_OS)" = "linux" ] && \
	   [ "$(DEB_HOST_ARCH_CPU):$(DEB_HOST_ARCH_BITS)" != "amd64:64" ] ; then \
		echo "$(package_bin): binary-from-other-architecture *efiemu64.o" \
			>> debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin) ; \
	fi
	case $@ in \
		install/grub-efi*) \
			(echo "# These aren't Windows executables, and these features wouldn't be very useful." \
			 echo "$(package_bin): portable-executable-missing-security-features *.efi ASLR DEP/NX") \
				>> debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin) ;; \
	esac
	if [ "$@" = "install/grub-emu" ]; then \
		(echo "# This seems to be due to a readelf bug: https://bugs.debian.org/929585"; \
		 echo "$(package_bin): binary-or-shlib-defines-rpath usr/lib/grub/*-emu/macho.mod 0x0") \
			>> debian/$(package_bin)/usr/share/lintian/overrides/$(package_bin); \
	fi

	mkdir -p debian/$(package_dbg)/usr/share/lintian/overrides
	echo "$(package_dbg): unstripped-binary-or-object *.module" \
		>> debian/$(package_dbg)/usr/share/lintian/overrides/$(package_dbg)
	echo "$(package_dbg): statically-linked-binary *.image" \
		>> debian/$(package_dbg)/usr/share/lintian/overrides/$(package_dbg)
	cd debian/tmp-$(package) && find usr/lib/grub -name kernel.exec \
		| sed -e "s%.*%$(package_dbg): statically-linked-binary &%g" \
	>> $(CURDIR)/debian/$(package_dbg)/usr/share/lintian/overrides/$(package_dbg)
	if ([ "$@" = "install/grub-efi-amd64" ] && [ "$(DEB_HOST_ARCH_CPU)" = "i386" ]) || \
	   ([ "$@" != "install/grub-emu" ] && [ "$(DEB_HOST_ARCH_CPU):$(DEB_HOST_ARCH_BITS)" = "amd64:32" ]) || \
	   [ "$@" = "install/grub-xen" ] ; then \
		echo "$(package_dbg): binary-from-other-architecture *.module" \
			>> debian/$(package_dbg)/usr/share/lintian/overrides/$(package_dbg) ; \
		cd debian/tmp-$(package) && find usr/lib/grub -name kernel.exec \
			| sed -e "s%.*%$(package_dbg): binary-from-other-architecture &%g" \
		>> $(CURDIR)/debian/$(package_dbg)/usr/share/lintian/overrides/$(package_dbg) ; \
	fi

	# Avoid failures later if we're building from a tree with no .po
	# files.
	mkdir -p debian/tmp-$(package)/usr/share/locale

common_subst = \
	if [ -e debian/grub-common.$(1) ]; then \
		sed 's/@COMMON_PLATFORM@/$(COMMON_PLATFORM)/g' \
			debian/grub-common.$(1) >> debian/grub-common.$(2).new; \
	fi

install/grub-common:
	set -e ; for i in $(AUTOGEN_DEB_FILES) ; do \
		> debian/grub-common.$$i.new; \
		$(call common_subst,$$i.in,$$i); \
		$(call common_subst,$$i.$(DEB_HOST_ARCH_CPU).in,$$i); \
		$(call common_subst,$$i.$(DEB_HOST_ARCH_OS).in,$$i); \
		if [ -s debian/grub-common.$$i.new ] ; then \
			mv debian/grub-common.$$i.new debian/grub-common.$$i; \
		else \
			rm -f debian/grub-common.$$i.new; \
		fi; \
	done
ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
	install -D -m 0755 debian/grub-common.pm-sleep \
		$(CURDIR)/debian/$(package)/etc/pm/sleep.d/10_grub-common
endif

NON_PLATFORM_PACKAGES = $(filter grub2 grub-linuxbios grub-efi grub-rescue-pc grub-firmware-qemu grub-xen-host,$(BUILD_PACKAGES))
COMMON_PLATFORM_PACKAGES = $(filter grub-common grub2-common grub-theme-starfield grub-mount-udeb,$(BUILD_PACKAGES))
PLATFORM_PACKAGES = $(filter grub-pc grub-efi-ia32 grub-efi-amd64 grub-efi-ia64 grub-efi-arm grub-efi-arm64 grub-ieee1275 grub-coreboot grub-uboot grub-xen grub-yeeloong,$(BUILD_PACKAGES))

override_dh_install:
ifneq (,$(NON_PLATFORM_PACKAGES))
	dh_install $(patsubst %,-p%,$(NON_PLATFORM_PACKAGES))
endif
	dh_install $(patsubst %,-p%,$(COMMON_PLATFORM_PACKAGES)) --sourcedir=debian/tmp-grub-$(COMMON_PLATFORM)
	rm -f debian/grub2-common/usr/share/info/dir*
	rm -f debian/grub-theme-starfield/usr/share/grub/themes/starfield/COPYING.CC-BY-SA-3.0
ifneq (,$(PLATFORM_PACKAGES))
	set -e; for package in $(PLATFORM_PACKAGES); do \
		dh_install -p$$package --sourcedir=debian/tmp-$$package; \
		dh_install -p$$package-bin --sourcedir=debian/tmp-$$package; \
		dh_install -p$$package-dbg --sourcedir=debian/tmp-$$package; \
	done
endif
ifneq (,$(filter grub-emu,$(BUILD_PACKAGES)))
	dh_install -pgrub-emu --sourcedir=debian/tmp-grub-emu
	dh_install -pgrub-emu-dbg --sourcedir=debian/tmp-grub-emu
endif
ifneq (,$(filter grub2-common,$(BUILD_PACKAGES)))
	sed -i \
		-e "s/@DEFAULT_CMDLINE@/$(DEFAULT_CMDLINE)/g" \
		-e "s/@DEFAULT_TIMEOUT@/$(DEFAULT_TIMEOUT)/g" \
		debian/grub2-common/usr/share/grub/default/grub
ifneq (,$(DEFAULT_HIDDEN_TIMEOUT))
	perl -pi -e 's/^GRUB_TIMEOUT=.*/GRUB_HIDDEN_TIMEOUT=0\nGRUB_HIDDEN_TIMEOUT_QUIET=true\n$$&/' \
		debian/grub2-common/usr/share/grub/default/grub
endif
endif
ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
ifneq (,$(filter grub-pc,$(BUILD_PACKAGES)))
	patch debian/grub-pc/usr/lib/grub-legacy/update-grub \
		< debian/legacy/update-grub.ubuntu.patch
endif
endif

override_dh_installdocs:
	dh_installdocs $(patsubst %,-p%,$(filter grub-common grub-rescue-pc grub-firmware-qemu grub-xen-host,$(BUILD_PACKAGES))) -A AUTHORS NEWS README THANKS TODO
	dh_installdocs $(patsubst %,-p%,$(filter grub-efi-%-signed-template,$(BUILD_PACKAGES)))
	dh_installdocs --remaining-packages --link-doc=grub-common

ifeq ($(FLICKER_FREE_BOOT),yes)
override_dh_installinit:
	dh_installinit -- start 99 2 3 4 5 .
else
override_dh_installinit:
	:
endif

override_dh_bugfiles:
	dh_bugfiles $(patsubst %,-N%,$(filter grub-efi-%-signed-template,$(BUILD_PACKAGES))) -A

override_dh_strip:
	dh_strip -X/usr/bin/grub-emu -X/usr/lib/grub-xen/grub-x86_64-xen.bin -X/usr/lib/grub-xen/grub-i386-xen_pvh.bin -X/usr/lib/grub-xen/grub-i386-xen.bin

override_dh_shlibdeps:
	dh_shlibdeps -X.module

ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
LEGACY_DOC_BR := grub-doc (<< 0.97-29ubuntu60), grub-legacy-doc (<< 0.97-29ubuntu60)
else
LEGACY_DOC_BR := grub-doc (<< 0.97-32), grub-legacy-doc (<< 0.97-59)
endif

override_dh_gencontrol:
	dh_gencontrol -- -Vlegacy-doc-br="$(LEGACY_DOC_BR)" -V"efi:Vendor=$(SB_EFI_VENDOR)" $(substvars)

TARNAME := grub2_$(deb_version)_$(DEB_HOST_ARCH).tar.gz

override_dh_builddeb:
	dh_builddeb
ifneq (,$(SB_PACKAGE))
	echo $(deb_version) > obj/monolithic/$(SB_PACKAGE)/version
	tar -c -f ../$(TARNAME) -a -C obj/monolithic/$(SB_PACKAGE) -v .
	dpkg-distaddfile $(TARNAME) raw-uefi -
endif

override_dh_auto_clean:
	-rm -rf debian/grub-extras-enabled debian/stamps obj
	-rm -f contrib grub-core/contrib

override_dh_clean:
	dh_clean

	chmod +x debian/{bug-script,grub.d/*,legacy/*,kernel/*}

	for i in $(AUTOGEN_DEB_FILES) ; do \
		rm -f debian/grub-{pc,efi-*,ieee1275,coreboot,uboot,xen,yeeloong,emu}{,-bin,-dbg}.$$i ; \
	done
	rm -f debian/grub-common.bash-completion debian/grub-common.maintscript
	rm -rf debian/tmp-*/

	# make sure PO files are always up-to-date
	debconf-updatepo

.PHONY: $(patsubst %,configure/%,$(BUILD_PACKAGES)) $(patsubst %,build/%,$(BUILD_PACKAGES)) $(patsubst %,install/%,$(BUILD_PACKAGES))