File: Makefile

package info (click to toggle)
debian-installer 20150422
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 5,008 kB
  • sloc: xml: 10,107; sh: 1,960; makefile: 948; perl: 613; awk: 100
file content (1077 lines) | stat: -rw-r--r-- 35,372 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/make -f
#
# Debian Installer system makefile.
# Copyright 2001-2003 by Joey Hess <joeyh@debian.org> and the d-i team.
# Licensed under the terms of the GPL.
#
# This makefile builds a debian-installer system and bootable images from 
# a collection of udebs which it downloads from a Debian archive. See
# README for details.
#
#
# General layout of our build directory hierarchy
#
# build/config/[<subarch>/][<medium>/][<flavour>/]<leaf-config>
# build/tmp/[<subarch>/][<medium>/][<flavour>/]<build-area>
# build/dest/[<subarch>/][<medium>/][<flavour>-]<images>
#
# Items in brackets can be left out if they are superfluous.
#
# These following <image> names are conventional.
#
# For small changeable media (floppies and the like):
# - boot.img, root.img, driver.img
#
# For single bootable images (e.g. tftp boot images):
# - boot.img
#
# For compressed single bootable images (harddisk or hd emulation):
# - boot.img.gz
#
# If those are not bootable:
# - root.img.gz
#
# Raw kernel images:
# - vmlinux or vmlinuz
#
# Example:
#
# dest/
# |-- cdrom-boot.img
# |-- floppy
# |   |-- access
# |   |   |-- boot.img
# |   |   |-- drivers.img
# |   |   `-- root.img
# |   |-- boot.img
# |   |-- cd-drivers.img
# |   |-- net-drivers.img
# |   `-- root.img
# |-- hd-media-boot.img.gz
# `-- netboot
#     |-- initrd.gz
#     `-- vmlinuz
#

# Add to PATH so dpkg will always work, and so local programs will be found.
PATH := util:$(PATH):/usr/sbin:/sbin
EATMYDATA = $(shell which eatmydata 2>/dev/null)
GZIP = $(shell which pigz gzip | head -1)

# We don't want this to be run each time we re-enter.
ifndef DEB_HOST_ARCH
DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS = $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_CPU = $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
DEB_HOST_GNU_SYSTEM = $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_MULTIARCH = $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_ARCH DEB_HOST_ARCH_OS DEB_HOST_GNU_CPU DEB_HOST_GNU_SYSTEM DEB_HOST_MULTIARCH
endif

define drop_lang
	@echo "Dropping languages: $(1)"
	@set -e; \
	for FILE in $$(find $(TREE)/var/lib/dpkg -name "*.templates"); do \
		perl -e 'my $$status=0; my $$drop=shift; while (<>) { if (/^[A-Z]/ || /^$$/) { if (/^(Choices|Description|Indices|Default)-($$drop)/ && ! /^Choices-C:/) { $$status = 0 } else { $$status = 1 } } print if ($$status); }' $(1) < $$FILE > temp; \
		mv temp $$FILE; \
	done
endef

# We loop over all needed combinations of ARCH, SUBARCH, MEDIUM, FLAVOUR
# via recursive make calls. ARCH is constant, we don't support
# crosscompiling.
ARCH = $(DEB_HOST_ARCH)

# By default, we just advertise what we can do.
.PHONY: all
all: list

# Globally useful variables.
targetstring = $(patsubst _%,%,$(if $(SUBARCH),_$(SUBARCH),)$(if $(MEDIUM),_$(MEDIUM),)$(if $(FLAVOUR),_$(FLAVOUR),))
targetdirs = $(subst _,/,$(targetstring))

# Overridden in config/hurd.cfg
define genext2fs
  genext2fs -d $(TREE) -b `expr $$(du -s $(TREE) | cut -f 1) + $$(expr $$(find $(TREE) | wc -l) \* 2)` -m 0
endef

# Configurations for the varying ARCH, SUBARCH, MEDIUM, FLAVOUR.
# For simplicity, we use a similiar tree layout for config/, tmp/
# and dest/.
#
# Cheap trick: if one of the variables isn't defined, we run in
# a non-existing file and ignore it.
include config/common
include config/dir
-include config/$(ARCH).cfg
-include config/$(ARCH)/$(SUBARCH).cfg
-include config/$(ARCH)/$(SUBARCH)/$(MEDIUM).cfg
-include config/$(ARCH)/$(SUBARCH)/$(MEDIUM)/$(FLAVOUR).cfg
-include config/local

export KEYRING
export KERNELVERSION
export LOCALUDEBDIR

ifneq ($(shell id -u),0)
  ROOTCMD ?= fakeroot
endif

# Useful command sequences
define submake
  $(ROOTCMD) $(MAKE) --no-print-directory -j1
endef

define recurse_once
  @set -e; $(submake) $(1)_$(2)
endef

define recurse_many
  @set -e; $(foreach var,$($(1)_SUPPORTED) $(if $(RECURSE_EXTRA),$($(1)_SUPPORTED_EXTRA)),$(submake) $(2)_$(3) $(1)=$(var);)
endef

define recurse
  $(if $($(1)_SUPPORTED)$(if $(RECURSE_EXTRA),$($(1)_SUPPORTED_EXTRA)),$(call recurse_many,$(1),$(2),$(3)),$(call recurse_once,$(2),$(3)))
endef

define mkinitramfs
  (cd $(TREE) && find . | sort | cpio --quiet -o -H newc) > 
endef

# Limit on mfsroot size. It is determined by NKPT (in <machine/pmap.h).
# On amd64, it can be increased by increasing this variable.  On i386,
# this is a hard limit due to constraints of 4 GiB VM space.
ifeq ($(DEB_HOST_ARCH),kfreebsd-i386)
MFSROOT_LIMIT := 42m
else ifeq ($(DEB_HOST_ARCH),kfreebsd-amd64)
MFSROOT_LIMIT := 72m
endif

define mkfs.ufs1
  sh -c 'makefs -t ffs -s $(MFSROOT_LIMIT) -f 3000 -o minfree=0,version=1 $$0 ${TREE}'
endef

define e2fsck
  e2fsck -fy
endef

# Define MKFS_JFFS2 in the config file for your target.
# For example: MKFS_JFFS2 = mkfs.jffs2 -f -p -b -e 0x20000
define mkjffs2
  $(MKFS_JFFS2) -d $(TREE) -o
endef

# A generic recursion rule
.PHONY: all_%
all_%:
	@install -d $(STAMPS)
	$(call recurse,SUBARCH,subarch,$*)

.PHONY: subarch_%
subarch_%:
	$(call recurse,MEDIUM,medium,$*)

.PHONY: medium_%
medium_%:
	$(call recurse,FLAVOUR,flavour,$*)

.PHONY: flavour_%
flavour_%:
	$(if $(targetstring),@$(submake) _$*)

# Validate a targetstring, echo env variables for valid ones
.PHONY: validate_%
validate_%:
	@set -e; \
	SUBARCH= var='$(subst _, ,$(subst validate_,,$@))'; \
	tmp=$$(echo $$var |sed 's/[ ].*$$//'); \
	[ -z '$(SUBARCH_SUPPORTED)$(SUBARCH_SUPPORTED_EXTRA)' ] || [ -z "$$tmp" ] || [ -z "$$(echo $(SUBARCH_SUPPORTED) $(SUBARCH_SUPPORTED_EXTRA) |grep -w $$tmp)" ] || SUBARCH=$$tmp; \
	$(submake) medium_validate SUBARCH=$$SUBARCH var="$$var"

.PHONY: medium_validate
medium_validate:
	@set -e; \
	MEDIUM= var="$(strip $(patsubst $(SUBARCH)%,%,$(var)))"; \
	tmp=$$(echo $$var |sed 's/[ ].*$$//'); \
	[ -z '$(MEDIUM_SUPPORTED)$(MEDIUM_SUPPORTED_EXTRA)' ] || [ -z "$$tmp" ] || [ -z "$$(echo $(MEDIUM_SUPPORTED) $(MEDIUM_SUPPORTED_EXTRA) |grep -w $$tmp)" ] || MEDIUM=$$tmp; \
	$(submake) flavour_validate MEDIUM=$$MEDIUM var="$$var"

.PHONY: flavour_validate
flavour_validate:
	@set -e; \
	FLAVOUR= var="$(strip $(patsubst $(MEDIUM)%,%,$(var)))"; \
	tmp=$$(echo $$var |sed 's/[ ].*$$//'); \
	[ -z '$(FLAVOUR_SUPPORTED)$(FLAVOUR_SUPPORTED_EXTRA)' ] || [ -z "$$tmp" ] || [ -z "$$(echo $(FLAVOUR_SUPPORTED) $(FLAVOUR_SUPPORTED_EXTRA) | grep -w $$tmp)" ] || FLAVOUR=$$tmp; \
	$(submake) finish_validate FLAVOUR=$$FLAVOUR var="$$var"

.PHONY: finish_validate
finish_validate:
	@set -e; \
	var="$(strip $(patsubst $(FLAVOUR)%,%,$(var)))"; \
	if [ -z "$$var" ]; then \
		echo SUBARCH=$$SUBARCH MEDIUM=$$MEDIUM FLAVOUR=$$FLAVOUR; \
	else \
		echo SUBARCH= MEDIUM= FLAVOUR=; \
	fi;


# List all targets useful for direct invocation.
.PHONY: list
list:
	@echo "Useful targets:"
	@echo
	@echo "list"
	@echo "all_build"
	@echo "all_rebuild"
	@echo "stats"
	@echo "all_clean"
	@echo "reallyclean"
	@echo
	@$(submake) all_list RECURSE_EXTRA=1

.PHONY: _list
_list:
	@set -e; \
	echo build_$(targetstring); \
	echo stats_$(targetstring); \
	$(if $(findstring $(MEDIUM),$(WRITE_MEDIA)),echo write_$(targetstring);) \
	echo clean_$(targetstring); \
	echo rebuild_$(targetstring); \
	echo demo_$(targetstring); \
	echo shell_$(targetstring); \
	echo uml_$(targetstring); \
	echo qemu_$(targetstring); \
	echo


# Clean all targets.
.PHONY: reallyclean
reallyclean: all_clean
	for png in $(shell awk -F= '/^SPLASH_PNG=/ {print $$2}' config/*.cfg); do if [ -f $${png%%.png}.svg ]; then rm -f $$png; fi; done
	rm -rf $(APTDIR) $(APTDIR).udeb $(APTDIR).deb $(BASE_DEST) $(BASE_TMP) $(SOURCEDIR) $(DEBUGUDEBDIR)
	rm -f sources.list sources.list.udeb $(LOCALUDEBDIR)/Packages.gz $(LOCALUDEBDIR)/Packages
	rm -rf $(UDEBDIR) $(STAMPS)

# For manual invocation, we provide a generic clean rule.
.PHONY: clean_%
clean_%:
	@$(submake) _clean $(shell $(submake) $(subst clean_,validate_,$@))
	update-manifest

# The general clean rule.
.PHONY: _clean
_clean: tree_umount
	@[ -n "$(SUBARCH) $(MEDIUM) $(FLAVOUR)" ] || { echo "invalid target"; false; }
	-rm -f $(STAMPS)tree-unpack-$(targetstring)-stamp $(STAMPS)tree-$(targetstring)-stamp $(STAMPS)extra-$(targetstring)-stamp $(STAMPS)get_udebs-$(targetstring)-stamp
	rm -f $(TEMP)/diskusage.txt
	rm -f $(TEMP)/all.utf
	rm -f $(TEMP)/unifont.bdf $(TREE)/lib/unifont.bgf
	rm -f pkg-lists/standard-udebs pkg-lists/kernel-module-udebs
	rm -rf $(TARGET)
	rm -rf $(TEMP)

# all_build is provided automagically, but for manual invocation
# we provide a generic build rule.
.PHONY: build_%
build_%:
	@install -d $(STAMPS)
	@$(submake) _build $(shell $(submake) $(subst build_,validate_,$@))

# The general build rule.
.PHONY: _build
_build:
	@[ -n "$(SUBARCH) $(MEDIUM) $(FLAVOUR)" ] || { echo "invalid target"; false; }
	@$(submake) tree_umount $(EXTRATARGETS) $(TARGET)

# Convenience rules to clean and build.
.PHONY: rebuild_%
rebuild_%:
	@$(submake) clean_$(subst rebuild_,,$@) build_$(subst rebuild_,,$@)

.PHONY: _rebuild
_rebuild:
	@$(submake) _clean _build

# Run before releasing built files.
release:
	egrep '^[[:space:]]' $(BASE_DEST)/MANIFEST.udebs | \
		sed 's/^[[:space:]]*//' | sort | uniq > $(BASE_DEST)/udeb.list
	rm -f $(BASE_DEST)/MD5SUMS $(BASE_DEST)/SHA256SUMS
	cd $(BASE_DEST) && md5sum `find -L . -type f | sort` > MD5SUMS # XXX remove this
	cd $(BASE_DEST) && sha256sum `find -L . -type f | sort` > SHA256SUMS

# The general tree target.
$(STAMPS)tree-unpack-$(targetstring)-stamp: $(STAMPS)get_udebs-$(targetstring)-stamp
	dh_testroot
	@[ -d ../debian ] || { echo "directory '../debian' not found; complete source of 'debian-installer' package is required"; false; }
	cd .. && dpkg-checkbuilddeps
	@rm -f $@

	# This build cannot be restarted, because dpkg gets confused.
	rm -rf $(TREE)
	# Set up the basic files [u]dpkg needs.
	mkdir -p $(DPKGDIR)/info
	touch $(DPKGDIR)/status
	# Create a tmp tree
	mkdir -p $(TREE)/tmp
	# Only dpkg needs this stuff, so it can be removed later.
	mkdir -p $(DPKGDIR)/updates/
	touch $(DPKGDIR)/available

ifdef TRANSSTATUS
	# Include translation status file; warn if older than 2 weeks.
	# Do this first to make possible warnings more visible.
	@echo; \
	if [ -f "$(TRANSSTATUS)" ]; then \
		if [ "$(USE_PROPOSED_UPDATES)" != 1 ]; then \
			olddate=$$(grep "^# Generated on:" $(TRANSSTATUS) | sed "s/.*: //"); \
			if [ "$$olddate" ]; then \
				if [ $$(( $$(date -u "+%s") - \
				          $$(date -d "$$olddate" "+%s") \
				       )) -gt 1209600 ]; then \
					echo "WARNING: The data in '$(TRANSSTATUS)' is older than 2 weeks."; \
					echo "         Should it maybe be updated?"; \
					sleep 15; \
				fi; \
			else \
				echo "WARNING: no timestamp found in $(TRANSSTATUS)."; \
				sleep 15; \
			fi; \
		fi; \
		mkdir -p $(TREE)/usr/share/localechooser; \
		cp $(TRANSSTATUS) $(TREE)/usr/share/localechooser/translation-status; \
	else \
		echo "WARNING: the file $(TRANSSTATUS) does not exist."; \
		sleep 5; \
	fi
endif

	# Unpack the udebs with dpkg. This command must run as root
	# or fakeroot.
	echo -n > $(TEMP)/diskusage.txt
	set -e; \
	oldsize=0; oldblocks=0; oldcount=0; for udeb in $(UDEBDIR)/*.udeb ; do \
		if [ -f "$$udeb" ]; then \
			pkg=`basename $$udeb` ; \
			$(EATMYDATA) dpkg $(DPKG_UNPACK_OPTIONS) --path-include='*' --log=/dev/null --root=$(TREE) --unpack $$udeb ; \
			newsize=`du -bs $(TREE) | awk '{print $$1}'` ; \
			newblocks=`du -s $(TREE) | awk '{print $$1}'` ; \
			newcount=`find $(TREE) -type f | wc -l | awk '{print $$1}'` ; \
			usedsize=`echo $$newsize - $$oldsize | bc`; \
			usedblocks=`echo $$newblocks - $$oldblocks | bc`; \
			usedcount=`echo $$newcount - $$oldcount | bc`; \
			version=`dpkg-deb --info $$udeb | awk '/ Version: / { print $$2 }' | sed 's/+b[0-9]*$$//g'` ; \
			source=`dpkg-deb --info $$udeb | awk '/ Source: / { print $$2 }'` ; \
			if [ -z "$$source" ]; then \
				source=`dpkg-deb --info $$udeb | awk '/ Package: / { print $$2 }'` ; \
			fi ; \
			echo " $$usedsize B - $$usedblocks blocks - $$usedcount files from $$pkg (version $$version)" >>$(TEMP)/diskusage.txt;\
			echo "$$source (= $$version)" >>$(TEMP)/built-using.txt;\
			oldsize=$$newsize ; \
			oldblocks=$$newblocks ; \
			oldcount=$$newcount ; \
		fi; \
	done
	sort -u < $(TEMP)/built-using.txt > $(TEMP)/built-using.txt.new && \
		mv $(TEMP)/built-using.txt.new $(TEMP)/built-using.txt
	sort -n < $(TEMP)/diskusage.txt > $(TEMP)/diskusage.txt.new && \
		mv $(TEMP)/diskusage.txt.new $(TEMP)/diskusage.txt
	grep-dctrl -nsPackage,Version,Architecture '' $(TREE)/var/lib/dpkg/status | \
		perl -nle '$$p = $$_; $$v = <>; chomp $$v; $$a = <>; chomp $$a; <>; print "$$p $$v $$a"' | \
		sort > $(TEMP_UDEB_LIST)

	# Clean up after dpkg.
	rm -rf $(DPKGDIR)/updates
	rm -f $(DPKGDIR)/available $(DPKGDIR)/*-old $(DPKGDIR)/lock

ifdef EXTRAFILES
	# Copy in any extra files.
	set -e; \
	for file in $(EXTRAFILES); do \
		mkdir -p $(TREE)/`dirname $$file`; \
		cp -a $$file $(TREE)/$$file; \
	done
endif

ifdef SSL_CERTS
	# Copy in any SSL certificates.
	mkdir -p $(TREE)/etc/ssl/certs
	cp -a $(SSL_CERTS) $(TREE)/etc/ssl/certs/
	c_rehash $(TREE)/etc/ssl/certs
	mkdir -p $(TREE)/usr/lib/ssl
	ln -nsf ../../../etc/ssl/certs $(TREE)/usr/lib/ssl/certs
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
ifdef KERNELVERSION
	# Set up modules.dep, ensure there is at least one standard dir (kernel
	# in this case), so depmod will use its prune list for archs with no
	# modules.
	set -e; \
	$(foreach VERSION,$(KERNELVERSION), \
		sysmap_name=; sysmap_opt=; \
		if [ -n "$(VERSIONED_SYSTEM_MAP)" ]; then \
			[ ! -e $(TREE)/boot/System.map-$(VERSION) ] || sysmap_name="$(TREE)/boot/System.map-$(VERSION)"; \
		else \
			[ ! -e $(TREE)/boot/System.map ] || sysmap_name="$(TREE)/boot/System.map"; \
		fi; \
		[ -z "$$sysmap_name" ] || sysmap_opt="-F $$sysmap_name"; \
		if [ -d $(TREE)/lib/modules/$(VERSION) ] && [ -z "$(OMIT_DEPMOD)" ]; then \
			mkdir -p $(TREE)/lib/modules/$(VERSION)/kernel; \
			$(shell choose-subarch-env $(VERSION)) depmod $$sysmap_opt -q -a -b $(TREE)/ $(VERSION); \
		fi; \
		[ -z "$$sysmap_name" ] || mv $$sysmap_name $(TEMP);)

	# These files depmod makes are used by udev.
	find $(TREE)/lib/modules/ -maxdepth 2 -name 'modules*.bin' \
		-not -name 'modules.builtin.bin' \
		-not -type d | while read f; do rm -f $${f%.bin}; done

	# These files are used to build special kernel images for some
	# subarchitectures. Move them out of the way.
	$(foreach VERSION,$(KERNELVERSION), \
		if [ -d $(TREE)/usr/lib/kernel-image-$(VERSION) ]; then \
			mv $(TREE)/usr/lib/kernel-image-$(VERSION) \
				$(TEMP)/lib; \
		fi; \
		if [ -d $(TREE)/usr/lib/linux-image-$(VERSION) ]; then \
			mv $(TREE)/usr/lib/linux-image-$(VERSION) \
				$(TEMP)/lib; \
		fi;)
endif
endif

ifdef KERNELNAME
ifeq ($(DEB_HOST_ARCH_OS),hurd)
	cp /lib/ld.so.1   $(TREE)/boot/ld.so.1
endif
	# Move the kernel image out of the way.
	$(foreach name,$(KERNELNAME), \
		mv -f $(TREE)/boot/$(name) $(TEMP)/$(name);)
	test -e $(TREE)/boot/zfs || rmdir $(TREE)/boot/
endif

ifdef PRESEED
	# Copy in preseed file.
	cp -a $(PRESEED) $(TREE)/preseed.cfg
endif

ifndef OMIT_LSB
	# Create an lsb release file.
	if [ ! -e $(TREE)/etc/lsb-release ]; then \
		set -e; \
		mkdir -p $(TREE)/etc; \
		echo 'DISTRIB_ID=$(LSB_DISTRIB_ID)' > $(TREE)/etc/lsb-release; \
		echo 'DISTRIB_DESCRIPTION=$(LSB_DISTRIB_DESCRIPTION)' >> $(TREE)/etc/lsb-release; \
		echo 'DISTRIB_RELEASE=$(LSB_DISTRIB_RELEASE)' >> $(TREE)/etc/lsb-release; \
		echo 'X_INSTALLATION_MEDIUM=$(MEDIUM)' >> $(TREE)/etc/lsb-release; \
	fi
endif

	@touch $@


$(STAMPS)tree-$(targetstring)-stamp: $(STAMPS)tree-unpack-$(targetstring)-stamp
ifndef OMIT_RELEASE_INFO
	# Add release info
	mkdir -p $(TREE)/etc/
	echo $(DEBIAN_RELEASE) >$(TREE)/etc/default-release
	echo $(USE_UDEBS_FROM) >$(TREE)/etc/udebs-source
endif

	# Create a dev tree.
	mkdir -p $(TREE)/dev
	# Always needed, in case udev is not mounted on boot.
	mknod $(TREE)/dev/console c 5 1
	mknod $(TREE)/dev/null c 1 3

	mkdir -p $(EXTRAUDEBSDIR)
	mkdir -p $(EXTRAUDEBSDPKGDIR)/info $(EXTRAUDEBSDPKGDIR)/updates
	touch $(EXTRAUDEBSDPKGDIR)/status $(EXTRAUDEBSDPKGDIR)/available

ifdef EXTRADRIVERS
	# Unpack the udebs of additional driver disks, so mklibs runs on them too.
	dpkg $(DPKG_UNPACK_OPTIONS) --log=/dev/null --root=$(EXTRAUDEBSDIR) --unpack \
		$(wildcard $(foreach dir,$(EXTRADRIVERS),$(dir)/*.udeb))
endif
ifdef EXTRAUDEBS
	# Get and unpack extra udebs too.
	get-packages udeb $(EXTRAUDEBS)
	dpkg $(DPKG_UNPACK_OPTIONS) --log=/dev/null --root=$(EXTRAUDEBSDIR) --unpack \
		$(foreach udeb,$(EXTRAUDEBS),$(UDEBDIR)/$(udeb).udeb)
endif

	# Library reduction. Existing libs from udebs are put in the udeblibs
	# directory and mklibs is made to use those in preference to the
	# system libs.
	rm -rf $(TEMP)/udeblibs
ifndef ONLY_KLIBC
	mkdir -p $(TEMP)/udeblibs
	find $(EXTRAUDEBSDIR)/lib -name '*.so.*' -print0 | \
		xargs -0r cp -a -t $(TEMP)/udeblibs
	find $(TREE)/lib -name '*.so.*' -print0 | \
		xargs -0r cp -a -t $(TEMP)/udeblibs
	mkdir -p $(TREE)/lib
	# HACK ALERT: X.Org modules are excluded from the scan as mklibs
	# is unable to find symbols provided by the /usr/bin/Xorg binary
	$(MKLIBS) -L $(TREE)/usr/lib -L $(TREE)/usr/lib/$(DEB_HOST_MULTIARCH) \
		-L $(TEMP)/udeblibs -v -d $(TREE)/lib --root=$(TREE) \
		-L $(TREE)/usr/lib/cdebconf/frontend \
		$(addprefix -l,$(notdir $(wildcard $(TREE)/usr/lib/cdebconf/frontend/*.so))) \
		`find $(TEMP) -type f -a \( -perm /0111 -o -name '*.so' -o -name '*.so.*' \) | \
		 grep -v udeblibs | grep -v 'usr/lib/xorg/modules/.*\.so'`

ifeq ($(DEB_HOST_ARCH),armhf)
	# armhf has two linkers for now, while the world gets sorted
	mkdir -p $(TREE)/lib/arm-linux-gnueabihf
	ln -s /lib/ld-linux-armhf.so.3 $(TREE)/lib/arm-linux-gnueabihf/ld-linux.so.3
endif

ifeq ($(DEB_HOST_ARCH_OS),hurd)
	# On Hurd the SONAME for the dynamic linker is ld.so.1, but binaries
	# have the ld.so symlink as the interpreter. Ideally mklibs should
	# be copying it along with the libraries.
	ln -sf ld.so.1 $(TREE)/lib/ld.so

	# We need /servers/exec and startup at boot time
	touch $(TREE)/servers/exec
	touch $(TREE)/servers/startup
endif
endif

	# Add missing symlinks for libraries
	ldconfig -n $(TREE)/lib $(TREE)/usr/lib

	# Remove any libraries that are present in both usr/lib and lib,
	# from lib. These were unnecessarily copied in by mklibs, and
	# we want to use the ones in usr/lib instead since they came 
	# from udebs. Only libdebconfclient0 has this problem so far.
	set -e; \
	for lib in `find $(TREE)/usr/lib/ -name "lib*" -type f -printf "%f\n" | cut -d . -f 1 | sort | uniq`; do \
		rm -f $(TREE)/lib/$$lib.*; \
	done

	# Reduce status file to contain only the elements we care about.
	egrep -i '^((Status|Provides|Depends|Package|Version|Description|installer-menu-item|Description-..):|$$)' \
		$(DPKGDIR)/status > $(DPKGDIR)/status.new
	mv $(DPKGDIR)/status.new $(DPKGDIR)/status

	# Add a dummy entry for the debian installer itself to the status
	# file, giving the overall version of this build.
	echo "Package: debian-installer" >> $(DPKGDIR)/status
	echo "Status: install ok installed" >> $(DPKGDIR)/status
	echo "Version: $(subst _,-,$(targetstring))-$(BUILD_DATE)" >> $(DPKGDIR)/status
	echo "Description: debian installation image" >> $(DPKGDIR)/status
	echo >> $(DPKGDIR)/status

ifeq ($(DEB_HOST_ARCH_OS),hurd)
        # There is a dependency loop which prevents d-i from being able to
        # configure these.  Since there is actually nothing to do to configure
        # them, they can as well be just marked installed already.
	sed -i -e '/Package: hurd-libs/,+1s/install ok unpacked/install ok installed/' $(DPKGDIR)/status
	sed -i -e '/Package: libparted/,+1s/install ok unpacked/install ok installed/' $(DPKGDIR)/status
endif

ifdef OMIT_DPKG
	rm -rf $(DPKGDIR)
endif

ifndef KEEP_GI_LANGS
	# It makes no sense to include languages only supported by the
	# graphical installer in regular images. This will also ensure
	# their glyphs don't get included a bit lower down.
	$(call drop_lang,$(GI_LANGS))
endif

ifdef DROP_LANG
	# Remove selected languages from the templates.
	# Not ideal, but useful if you're very tight on space.
	$(call drop_lang,$(DROP_LANG))
endif

	# Collect the used UTF-8 strings, to know which glyphs to include in
	# the font.
	cat needed-characters/*.utf > $(TEMP)/all.utf
	if [ -n "`find $(EXTRAUDEBSDPKGDIR)/info/ -name \\*.templates`" ]; then \
		cat $(EXTRAUDEBSDPKGDIR)/info/*.templates >> $(TEMP)/all.utf; \
	fi
	if [ -n "`find $(DPKGDIR)/info/ -name \\*.templates`" ]; then \
		cat $(DPKGDIR)/info/*.templates >> $(TEMP)/all.utf; \
	fi
	if [ -f $(TREE)/usr/share/localechooser/languagelist.data.gz ]; then \
		zcat $(TREE)/usr/share/localechooser/languagelist.data.gz >> $(TEMP)/all.utf; \
	fi


	# Strip all kernel modules, just in case they haven't already been
	set -e; \
	for module in `find $(TREE)/lib/modules/ -name '*.o' -or -name '*.ko'`; do \
	    strip -R .comment -R .note -g $$module || true; \
	done

	# Remove some unnecessary dpkg files.
	set -e; \
	for file in `find $(TREE)/var/lib/dpkg/info -name '*.md5sums' -o \
	    -name '*.postrm' -o -name '*.prerm' -o -name '*.preinst' -o \
	    -name '*.list' | sort`; do \
		if echo $$file | grep -qv '\.list'; then \
			echo "** Removing unnecessary control file $$file"; \
		fi; \
		rm $$file; \
	done
	rm -rf $(TREE)/var/lib/dpkg/triggers

	# Remove extra udebs now that we're done with them.
	rm -rf $(EXTRAUDEBSDIR)

	# Tree target ends here. Whew!
	@touch $@

# Get the list of udebs to install.
# HACK Alert: pkg-lists/ is still sorted by TYPE instead of a dir hierarchy.
UDEBS = $(shell set -e; get-packages udeb update >&2; pkg-list $(TYPE) "$(DRIVER_FOR)" $(KERNEL_FLAVOUR) $(KERNELMAJOR) "$(SUBARCH)" $(KERNELVERSION)) $(EXTRAS)

# Get all required udebs and put them in UDEBDIR.
$(STAMPS)get_udebs-$(targetstring)-stamp: sources.list.udeb
	dh_testroot
	@rm -f $@

	get-packages udeb $(UDEBS)
	@touch $@

# Auto-generate sources.list.udeb
sources.list.udeb:
	@(set -e; \
	echo "# This file is automatically generated, edit $@.local instead."; \
	echo "deb [trusted=yes] copy:$(shell pwd)/ $(LOCALUDEBDIR)/"; \
	if [ "$(MIRROR)x" != "x" ]; then \
		echo "deb $(MIRROR) $(USE_UDEBS_FROM) $(UDEB_COMPONENTS)"; \
	else \
		gen-sources.list.udeb "$(SYSTEM_SOURCES_LIST)" $(USE_UDEBS_FROM) $(UDEB_COMPONENTS) $(USE_PROPOSED_UPDATES); \
	fi) > $@
	@if [ -e $@.local ]; then \
		echo "Using $@.local:"; \
		sed -n "/^[^#]/ s/^/   /p" $@.local; \
	else \
		echo "Using generated $@:"; \
		sed -n "/^[^#]/ s/^/   /p" $@; \
	fi
	@if [ "$(USE_PROPOSED_UPDATES)" = 1 ] && ! grep -q proposed-updates $@; then \
		echo "ERROR: no valid source for $(USE_UDEBS_FROM)-proposed-updates"; \
		exit 1; \
	fi

# Font generation.
#
# Use the UTF-8 locale in installation-locale. This target shouldn't
# be called when it is not present anyway.
# The locale must be generated after installing the package locales
$(TEMP)/unifont.bdf: $(TEMP)/all.utf
	@set -e; \
	CHARMAP=`LOCPATH=$(LOCALE_PATH) LC_ALL=C.UTF-8 locale charmap`; \
            if [ UTF-8 != "$$CHARMAP" ]; then \
	        echo "error: Trying to build unifont.bgf without installation-locale!"; \
		echo "(Alternatively, installation-locale may have been built against"; \
		echo "a version of glibc with a different locale data format.)"; \
	        exit 1; \
	    fi
	LOCPATH=$(LOCALE_PATH) LC_ALL=C.UTF-8 reduce-font /usr/src/unifont.bdf < $(TEMP)/all.utf > $@.tmp
	mv $@.tmp $@

$(TREE)/lib/unifont.bgf: $(TEMP)/unifont.bdf
	bdftobogl -b $< > $@.tmp
	mv $@.tmp $@

# The Hurd console uses BDF fonts instead
$(TREE)/lib/unifont.bdf: $(TEMP)/unifont.bdf
	mkdir -p $(dir $@)
	cp $< $@.tmp
	mv $@.tmp $@

# Create a compressed image of the root filesystem.
$(TEMP_INITRD): $(STAMPS)tree-$(targetstring)-stamp arch_tree
	# Check for lost file attributes
	@if [ -e $(TREE)/dev/console ] && \
	    ! ls -l $(TREE)/dev/console | grep -q "^c"; then \
		echo "Error: incorrect file attributes detected in initrd tree."; \
		echo "Did you restart a (failed) fakeroot build?"; \
		echo "See fakeroot(1) ('-s' and '-i' options) for additional information."; \
		exit 1; \
	fi

	# Only build the font if we have installation-locale
ifeq ($(DEB_HOST_ARCH_OS),linux)
	if [ -d "$(LOCALE_PATH)/C.UTF-8" ] && [ -e /usr/src/unifont.bdf ]; then \
		$(submake) $(TREE)/lib/unifont.bgf; \
	fi
endif
ifeq ($(DEB_HOST_ARCH_OS),hurd)
	# The hurd console uses a BDF font
	if [ -d "$(LOCALE_PATH)/C.UTF-8" ] && [ -e /usr/src/unifont.bdf ]; then \
		$(submake) $(TREE)/lib/unifont.bdf; \
	fi
endif

	install -d $(TEMP)

	case $(INITRD_FS) in \
	ext2) \
		$(genext2fs) $(TEMP)/initrd; \
		$(e2fsck) $(TEMP)/initrd || true; \
		$(GZIP) -v9f $(TEMP)/initrd; \
	;; \
	initramfs) \
		$(mkinitramfs) $(TEMP)/initrd; \
		$(GZIP) -v9f $(TEMP)/initrd; \
	;; \
	jffs2) \
		$(mkjffs2) $(TEMP_INITRD); \
	;; \
	ufs1) \
		$(mkfs.ufs1) $(TEMP)/initrd; \
		$(GZIP) -v9f $(TEMP)/initrd; \
	;; \
	*) \
		echo "Unsupported filesystem type"; \
		exit 1 ;; \
	esac

# Create the images for dest/. Those are the targets called from config.

ifeq ($(SYMLINK_INITRD),)
$(INITRD): $(TEMP_INITRD)
	install -m 644 -D $< $@
	update-manifest $@ $(MANIFEST-INITRD) $(UDEB_LISTS)
else
$(INITRD): $(realpath $(dir $(INITRD))/$(SYMLINK_INITRD))
	mkdir -p $(dir $(INITRD))
	ln -s $(SYMLINK_INITRD) $@
	update-manifest $@ $(MANIFEST-INITRD) $(UDEB_LISTS)
endif

$(RAMDISK): $(TEMP_INITRD)
	install -m 644 -D $< $@
	update-manifest $@ $(MANIFEST-RAMDISK) $(UDEB_LISTS)

# raw kernel images
ifeq ($(SYMLINK_KERNEL),)
$(KERNEL): TEMP_REAL_KERNEL = $(TEMP)/$(shell echo ./$@ |sed 's,$(SOME_DEST)/$(EXTRANAME),,')
$(KERNEL):
	@$(MAKE) $(STAMPS)tree-unpack-$(targetstring)-stamp $(TEMP_REAL_KERNEL)
	install -m 644 -D $(TEMP_REAL_KERNEL) $@
	update-manifest $@ $(MANIFEST-KERNEL)
else
$(KERNEL): $(realpath $(dir $(KERNEL))/$(SYMLINK_KERNEL))
	mkdir -p $(dir $(KERNEL))
	ln -s $(SYMLINK_KERNEL) $@
	update-manifest $@ $(MANIFEST-KERNEL)
endif

$(DTBS): $(TEMP_KERNEL)
	mkdir -p $(SOME_DEST)/$(EXTRANAME)
	set -ex ; for dtb in $(TEMP_DTBS)/*.dtb ; do \
		tgt=$(SOME_DEST)/$(EXTRANAME)$$(basename $$dtb); \
		cp $$dtb $$tgt ; \
		update-manifest $$tgt "Device Tree Blob: $$(basename $$dtb)";\
	done
	cp boot/README.device-tree $@
	update-manifest $@ "Device Tree Blobs README"

# bootable images
$(BOOT): $(TEMP_BOOT)
	install -m 644 -D $(TEMP_BOOT)$(GZIPPED) $@
	update-manifest $@ $(MANIFEST-BOOT) $(UDEB_LISTS)

$(TEMP_KERNEL): $(STAMPS)tree-unpack-$(targetstring)-stamp

$(TEMP_BOOT): $(TEMP_INITRD) $(TEMP_KERNEL) $(TEMP_BOOT_SCREENS) arch_boot

# non-bootable root images
$(ROOT): $(TEMP_ROOT)
	install -m 644 -D $(TEMP_ROOT)$(GZIPPED) $@
	update-manifest $@ $(MANIFEST-ROOT) $(UDEB_LISTS)

$(TEMP_ROOT): $(TEMP_INITRD) arch_root

# miniature ISOs with only a boot image
$(MINIISO): $(TEMP_MINIISO)
	install -m 644 -D $(TEMP_MINIISO) $@
	update-manifest $@ $(MANIFEST-MINIISO) $(UDEB_LISTS)

$(TEMP_MINIISO): $(TEMP_BOOT_SCREENS) arch_miniiso

# various kinds of information, for use on debian-cd isos
$(DEBIAN_CD_INFO): $(TEMP_BOOT_SCREENS) $(TEMP_CD_INFO_DIR)
	(cd $(TEMP_CD_INFO_DIR); tar czf - .) > $@
	update-manifest $@ $(MANIFEST-DEBIAN_CD_INFO)

# a directory full of files for netbooting
$(NETBOOT_DIR): $(NETBOOT_DIR_TARGETS) $(TEMP_BOOT_SCREENS) $(TEMP_NETBOOT_DIR)
	rm -rf $(SOME_DEST)/$(EXTRANAME)/$(NETBOOT_DI_DIR)
	mkdir -p $(SOME_DEST)/$(EXTRANAME)/$(NETBOOT_DI_DIR)
	cp -a $(TEMP_NETBOOT_DIR)/$(NETBOOT_DI_DIR)/* $@
	$(foreach link,$(NETBOOT_DIR_LINKS), \
		rm -f $@/../$(link); \
		cp -a $(TEMP_NETBOOT_DIR)/$(link) $@/..; \
	)
	update-manifest $@ $(MANIFEST-NETBOOT_DIR) $(UDEB_LISTS)

# a tarball for netbooting
$(NETBOOT_TAR): $(TEMP_NETBOOT_DIR)
	# Create an version info file.
	echo 'Debian version:  $(DEBIAN_VERSION)' > $(TEMP_NETBOOT_DIR)/version.info
	echo 'Installer build: $(BUILD_DATE)' >> $(TEMP_NETBOOT_DIR)/version.info
	(cd $(TEMP_NETBOOT_DIR); tar czf - .) > $@
	update-manifest $@ $(MANIFEST-NETBOOT_TAR) $(UDEB_LISTS)

$(TEMP_BOOT_SCREENS): arch_boot_screens
$(TEMP_CD_INFO_DIR): arch_cd_info_dir
$(TEMP_NETBOOT_DIR): arch_netboot_dir

$(MISC): TEMP_REAL_MISC = $(shell echo ./$@ |sed 's,$(SOME_DEST)/$(EXTRANAME),,')
$(MISC): 
	install -m 644 $(TEMP_REAL_MISC) $(SOME_DEST)/$(EXTRANAME)
	update-manifest \
		$(SOME_DEST)/$(EXTRANAME)$(shell basename $(TEMP_REAL_MISC)) \
		$(MANIFEST-MISC)

# Other images, e.g. driver floppies. Those are simply handled as flavours
$(EXTRA): $(TEMP_EXTRA)
	install -m 644 -D $(TEMP_EXTRA)$(GZIPPED) $@
	update-manifest $@ $(MANIFEST-EXTRA) $(UDEB_LISTS)

$(TEMP_EXTRA): $(STAMPS)extra-$(targetstring)-stamp
	install -d $(shell dirname $@)
	install -d $(TREE)
	set -e; case $(INITRD_FS) in \
	ext2) \
		genext2fs -d $(TREE) -b $(IMAGE_SIZE) -m 0 $@ ; \
		$(e2fsck) $@ || true ;; \
	*) \
		echo "Unsupported filesystem type"; \
		exit 1 ;; \
	esac
	$(if $(GZIPPED),$(GZIP) -v9f $(TEMP_EXTRA))

$(STAMPS)extra-$(targetstring)-stamp: $(STAMPS)get_udebs-$(targetstring)-stamp
	@rm -f $@
	mkdir -p $(TREE)
	echo -n > $(TEMP)/diskusage.txt
	echo -n > $(TEMP_UDEB_LIST)
	
	set -e; \
	for file in $(UDEBS); do \
		cp $(UDEBDIR)/$$file* $(TREE) ; \
	done
	for udeb in $(TREE)/*.udeb ; do \
		if [ -f "$$udeb" ]; then \
			pkg=`basename $$udeb .udeb` ; \
			usedsize=`du -bs $$udeb | awk '{print $$1}'` ; \
			usedblocks=`du -s $$udeb | awk '{print $$1}'` ; \
			usedcount=1 ; \
			version=`dpkg-deb --info $$udeb | grep '^ *Version:' | awk '{print $$2}'` ; \
			arch=`dpkg-deb --info $$udeb | grep '^ *Architecture:' | awk '{print $$2}'` ; \
			echo " $$usedsize B - $$usedblocks blocks - $$usedcount files used by pkg $$pkg (version $$version)" >>$(TEMP)/diskusage.txt; \
			echo "$$pkg $$version $$arch" >> $(TEMP_UDEB_LIST); \
		fi; \
	done
	sort -n < $(TEMP)/diskusage.txt > $(TEMP)/diskusage.txt.new && \
		mv $(TEMP)/diskusage.txt.new $(TEMP)/diskusage.txt
	echo $(UDEBS) > $(TREE)/udeb_include
	makelabel $(DISK_LABEL) $(BUILD_DATE) > $(TREE)/disk.lbl
	@touch $@

# Get a list of all the standard priority udebs, excluding kernel stuff.
pkg-lists/standard-udebs:
	get-packages udeb update
	grep-dctrl -sPackage -FPriority -e 'required|standard|important' \
		$(if $(SUBARCH),-a \( -! -FSubarchitecture -r . -o -FSubarchitecture -e '(^| )$(SUBARCH)( |$$)' \)) \
		apt.udeb/state/lists/*_Packages* | \
		grep -v kernel-image | grep -v -- -modules | \
		cut -d " " -f 2 > $@

# Get a list of all kernel modules matching the kernel version.
pkg-lists/kernel-module-udebs:
	get-packages udeb update
	> $@
	$(foreach VERSION,$(KERNELVERSION), \
		grep-dctrl -P -e '.*-modules-$(VERSION)-$(KERNEL_FLAVOUR)' \
			-sPackage apt.udeb/state/lists/*_Packages* | \
			cut -d " " -f 2 >> $@;)

# The kernel version changes from build to build, so always regen the file.
.PHONY: pkg-lists/kernel-module-udebs

# Write (floppy) images
.PHONY: write_%
write_%:
	@install -d $(STAMPS)
	@$(submake) _write $(shell $(submake) $(subst write_,validate_,$@))

.PHONY: _write
_write: _build
	sudo dd if=$(TARGET) of=$(FLOPPYDEV) bs=$(FLOPPY_SIZE)k

# If you're paranoid (or things are mysteriously breaking..),
# you can check the floppy to make sure it wrote properly.
.PHONY: checkedwrite_%
checkedwrite_%:
	@install -d $(STAMPS)
	@$(submake) _checkedwrite $(shell $(submake) $(subst checkedwrite_,validate_,$@))

.PHONY: _checkedwrite
_checkedwrite: _write
	sudo cmp $(FLOPPYDEV) $(TARGET)


# Generate statistics for all previously built images.
.PHONY: stats
stats:
	@echo "Image size stats"
	@echo
	$(submake) all_stats

# For manual invocation we provide a generic stats rule.
.PHONY: stats_%
stats_%:
	@$(submake) _stats $(shell $(submake) $(subst stats_,validate_,$@))

.PHONY: _stats
_stats: 
	@[ ! -f $(TEMP)/diskusage.txt ] || $(submake) general-stats

TOTAL_SZ = $(shell expr $(shell du -bs $(TREE) | cut -f 1) / 1024)
LIBS_SZ = $(shell [ -d $(TREE)/lib ] && du -hs --exclude=modules $(TREE)/lib |cut -f 1)
MODULES_SZ = $(shell [ -d $(TREE)/lib/modules ] && du -hs $(TREE)/lib/modules |cut -f 1)
ifeq ($(TEMP_INITRD),)
INITRD_SZ = 0
else
INITRD_SZ = $(shell expr \( $(shell [ -f $(TEMP_INITRD) ] && du -bs $(TEMP_INITRD) |cut -f 1) + 0 \) / 1024)
endif
ifeq ($(TEMP_KERNEL),)
KERNEL_SZ = 0
else
KERNEL_SZ = $(shell expr \( $(foreach kern,$(TEMP_KERNEL),$(shell [ -f $(kern) ] && du -bs $(kern) |cut -f 1)) + 0 \) / 1024)
endif
ifeq ($(TARGET),$(ROOT))
# image without a kernel
KERNEL_SZ = 0
endif
.PHONY: general-stats
general-stats:
	@echo "System stats for $(targetstring)"
	@echo "-------------------------"
	@printf "Total system size: $(TOTAL_SZ)k"
ifneq ($(LIBS_SZ)$(MODULES_SZ),)
	@echo " ($(LIBS_SZ) libs, $(MODULES_SZ) kernel modules)"
else
	@echo
endif
	@echo "Initrd size: $(INITRD_SZ)k"
	@echo "Kernel size: $(KERNEL_SZ)k"
ifdef FLOPPY_SIZE
ifneq ($(INITRD_SZ),0)
	@echo "Free space: $(shell expr $(FLOPPY_SIZE) - $(KERNEL_SZ) - $(INITRD_SZ))k"
else
	@echo "Free space: $(shell expr $(FLOPPY_SIZE) - $(KERNEL_SZ) - $(TOTAL_SZ))k"
endif
endif
	@echo "Disk usage per package:"
	@sed 's/^/  /' < $(TEMP)/diskusage.txt

.PHONY: tree_mount
tree_mount: $(STAMPS)tree-$(targetstring)-stamp
	-@sudo /bin/mount -t proc proc $(TREE)/proc
	-@if [ -e $(TREE)/usr/bin/update-dev ]; then \
		sudo chroot $(TREE) /usr/bin/update-dev; \
	elif [ ! -d $(TREE)/etc/udev ]; then \
		sudo /bin/mount -t devfs dev $(TREE)/dev; \
	fi

.PHONY: tree_umount
tree_umount:
	-@[ ! -L $(TREE)/proc/self ] || sudo /bin/umount $(TREE)/proc

# For manual invocation, we provide a demo rule. This starts the
# d-i demo from the tree in tmp.
.PHONY: demo_%
demo_%:
	@set -e; \
	export SUBARCH=; \
	export FLAVOUR=; \
	export MEDIUM=$(subst demo_,,$@); \
	$(submake) _demo

.PHONY: _demo
_demo: $(TARGET)
	@$(submake) tree_mount; \
	[ -f questions.dat ] && cp -f questions.dat $(TREE)/var/lib/cdebconf/ ; \
	: >$(TREE)/var/lib/UTF-8 ; \
	sudo chroot $(TREE) bin/sh -c "export DEBCONF_DEBUG=5; export LANG=C.UTF-8; export TERM_UTF8=yes; /usr/bin/debconf-loadtemplate debian /var/lib/dpkg/info/*.templates; exec /usr/share/debconf/frontend /usr/bin/main-menu" ; \
	rm -f $(TREE)/var/lib/UTF-8 ; \
	$(submake) tree_umount

# For a shell into a d-i chroot.
.PHONY: shell_%
shell_%:
	@export SUBARCH=; \
	export FLAVOUR=; \
	export MEDIUM=$(subst shell_,,$@); \
	$(submake) _shell

.PHONY: _shell
_shell: $(TREE)
	@$(submake) tree_mount; \
	sudo chroot $(TREE) bin/sh; \
	$(submake) tree_umount

# For running a d-i image in UML.
.PHONY: uml_%
uml_%:
	@set -e; \
	export SUBARCH=; \
	export FLAVOUR=; \
	export MEDIUM=$(subst uml_,,$@); \
	$(submake) _uml

.PHONY: _uml
_uml: $(TARGET)
	-linux initrd=$(INITRD) root=/dev/rd/0 ramdisk_size=8192 con=fd:0,fd:1 devfs=mount

# For running a d-i image in qemu.
.PHONY: qemu_%
qemu_%:
	@set -e; \
	export SUBARCH=; \
	export FLAVOUR=; \
	export MEDIUM=$(subst qemu_,,$@); \
	$(submake) _qemu $(shell $(submake) $(subst qemu_,validate_,$@))

.PHONY: _qemu
_qemu: $(TARGET)
	@case "$(MEDIA_TYPE)" in \
	floppy) \
		$(QEMU) -fda $(BOOT); \
	;; \
	CD-ROM) \
		$(QEMU) -cdrom $(BOOT); \
	;; \
	*) \
		echo "Sorry, don't know how to handle media type '$(MEDIA_TYPE)'" >&2; \
		exit 1; \
	esac

# For running a d-i image in a subhurd
.PHONY: subhurd_%
subhurd_%:
	@set -e; \
	export SUBARCH=; \
	export FLAVOUR=; \
	export MEDIUM=$(subst subhurd_,,$@); \
	$(submake) _subhurd

.PHONY: _subhurd
_subhurd: $(TEMP_INITRD)
	settrans -cfga $(BASE_TMP)/rd0 /hurd/storeio -r -T file initrd.gz
	-boot -I -f rd0=$(BASE_TMP)/rd0 -c "TERM=vt100 root=gunzip:device:rd0" /boot/servers.boot $(BASE_TMP)/rd0