include/kernel: sort module lists for reproducibility
[lede.git] / include / image.mk
1 #
2 # Copyright (C) 2006-2010 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 override TARGET_BUILD=
9 include $(INCLUDE_DIR)/prereq.mk
10 include $(INCLUDE_DIR)/kernel.mk
11 include $(INCLUDE_DIR)/host.mk
12 include $(INCLUDE_DIR)/version.mk
13
14 override MAKE:=$(_SINGLE)$(SUBMAKE)
15 override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
16
17 KDIR=$(KERNEL_BUILD_DIR)
18 KDIR_TMP=$(KDIR)/tmp
19 DTS_DIR:=$(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/dts
20
21 sanitize = $(call tolower,$(subst _,-,$(1)))
22
23 DIST_SANITIZED:=$(call sanitize,$(VERSION_DIST))
24 EXTRA_NAME_SANITIZED=$(call sanitize,$(EXTRA_IMAGE_NAME))
25
26 IMG_PREFIX:=$(DIST_SANITIZED)-$(if $(CONFIG_VERSION_FILENAMES),$(VERSION_NUMBER)-)$(if $(EXTRA_NAME_SANITIZED),$(EXTRA_NAME_SANITIZED)-)$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
27
28 MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
29
30 ifneq ($(CONFIG_BIG_ENDIAN),)
31   JFFS2OPTS     :=  --big-endian --squash-uids -v
32 else
33   JFFS2OPTS     :=  --little-endian --squash-uids -v
34 endif
35
36 ifeq ($(CONFIG_JFFS2_RTIME),y)
37   JFFS2OPTS += -X rtime
38 endif
39 ifeq ($(CONFIG_JFFS2_ZLIB),y)
40   JFFS2OPTS += -X zlib
41 endif
42 ifeq ($(CONFIG_JFFS2_LZMA),y)
43   JFFS2OPTS += -X lzma --compression-mode=size
44 endif
45 ifneq ($(CONFIG_JFFS2_RTIME),y)
46   JFFS2OPTS += -x rtime
47 endif
48 ifneq ($(CONFIG_JFFS2_ZLIB),y)
49   JFFS2OPTS += -x zlib
50 endif
51 ifneq ($(CONFIG_JFFS2_LZMA),y)
52   JFFS2OPTS += -x lzma
53 endif
54
55 JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
56
57 SQUASHFS_BLOCKSIZE := $(CONFIG_TARGET_SQUASHFS_BLOCK_SIZE)k
58 SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
59 SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
60 SQUASHFSCOMP := gzip
61 LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
62 ifeq ($(CONFIG_SQUASHFS_LZMA),y)
63   SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
64 endif
65 ifeq ($(CONFIG_SQUASHFS_XZ),y)
66   ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
67     BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
68   endif
69   SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
70 endif
71
72 JFFS2_BLOCKSIZE ?= 64k 128k
73
74 fs-types-$(CONFIG_TARGET_ROOTFS_SQUASHFS) += squashfs
75 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addprefix jffs2-,$(JFFS2_BLOCKSIZE))
76 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2_NAND) += $(addprefix jffs2-nand-,$(NAND_BLOCKSIZE))
77 fs-types-$(CONFIG_TARGET_ROOTFS_EXT4FS) += ext4
78 fs-types-$(CONFIG_TARGET_ROOTFS_ISO) += iso
79 fs-subtypes-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addsuffix -raw,$(addprefix jffs2-,$(JFFS2_BLOCKSIZE)))
80 fs-subtypes-$(CONFIG_TARGET_ROOTFS_CPIOGZ) += cpiogz
81 fs-subtypes-$(CONFIG_TARGET_ROOTFS_TARGZ) += targz
82
83 TARGET_FILESYSTEMS := $(fs-types-y)
84
85 FS_64K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-64k
86 FS_128K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-128k
87 FS_256K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-256k
88
89 define add_jffs2_mark
90         echo -ne '\xde\xad\xc0\xde' >> $(1)
91 endef
92
93 PROFILE_SANITIZED := $(call sanitize,$(PROFILE))
94
95 define split_args
96 $(foreach data, \
97         $(subst |,$(space),\
98                 $(subst $(space),^,$(1))), \
99         $(call $(2),$(strip $(subst ^,$(space),$(data)))))
100 endef
101
102 define build_cmd
103 $(if $(Build/$(word 1,$(1))),,$(error Missing Build/$(word 1,$(1))))
104 $(call Build/$(word 1,$(1)),$(wordlist 2,$(words $(1)),$(1)))
105
106 endef
107
108 define concat_cmd
109 $(call split_args,$(1),build_cmd)
110 endef
111
112 # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
113 define prepare_generic_squashfs
114         $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
115 endef
116
117 define Image/BuildKernel/Initramfs
118         $(call Image/Build/Initramfs)
119 endef
120
121 define Image/BuildKernel/MkuImage
122         mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
123                 -n '$(call toupper,$(ARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $(4) $(5)
124 endef
125
126 define Image/BuildKernel/MkFIT
127         $(TOPDIR)/scripts/mkits.sh \
128                 -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) $(if $(3),-d $(3)) -C $(4) -a $(5) -e $(6) \
129                 -A $(ARCH) -v $(LINUX_VERSION)
130         PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
131 endef
132
133 # $(1) source dts file
134 # $(2) target dtb file
135 # $(3) extra CPP flags
136 # $(4) extra DTC flags
137 define Image/BuildDTB
138         $(TARGET_CROSS)cpp -nostdinc -x assembler-with-cpp \
139                 -I$(DTS_DIR) \
140                 -I$(DTS_DIR)/include \
141                 -undef -D__DTS__ $(3) \
142                 -o $(2).tmp $(1)
143         $(LINUX_DIR)/scripts/dtc/dtc -O dtb \
144                 -i$(dir $(1)) $(4) \
145                 -o $(2) $(2).tmp
146         $(RM) $(2).tmp
147 endef
148
149 define Image/mkfs/jffs2/sub
150                 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) --pad -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2) -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
151                 $(call add_jffs2_mark,$(KDIR)/root.jffs2-$(2))
152 endef
153
154 define Image/mkfs/jffs2/sub-raw
155                 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2)-raw -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
156 endef
157
158 define Image/mkfs/jffs2/template
159   Image/mkfs/jffs2-$(1) = $$(call Image/mkfs/jffs2/sub,$(1),$(1),$(JFFS2OPTS))
160   Image/mkfs/jffs2-$(1)-raw = $$(call Image/mkfs/jffs2/sub-raw,$(1),$(1),$(JFFS2OPTS))
161
162 endef
163
164 define Image/mkfs/jffs2-nand/template
165   Image/mkfs/jffs2-nand-$(1) = \
166         $$(call Image/mkfs/jffs2/sub, \
167                 $(word 2,$(subst -, ,$(1))),nand-$(1), \
168                         $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst -, ,$(1))))
169
170 endef
171
172 $(eval $(foreach S,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/template,$(S))))
173 $(eval $(foreach S,$(NAND_BLOCKSIZE),$(call Image/mkfs/jffs2-nand/template,$(S))))
174
175 define Image/mkfs/squashfs
176         $(STAGING_DIR_HOST)/bin/mksquashfs4 $(TARGET_DIR) $(KDIR)/root.squashfs -nopad -noappend -root-owned -comp $(SQUASHFSCOMP) $(SQUASHFSOPT) -processors $(if $(CONFIG_PKG_BUILD_JOBS),$(CONFIG_PKG_BUILD_JOBS),1)
177 endef
178
179 # $(1): board name
180 # $(2): rootfs type
181 # $(3): kernel image
182 ifneq ($(CONFIG_NAND_SUPPORT),)
183    define Image/Build/SysupgradeNAND
184         mkdir -p "$(KDIR_TMP)/sysupgrade-$(1)/"
185         echo "BOARD=$(1)" > "$(KDIR_TMP)/sysupgrade-$(1)/CONTROL"
186         [ -z "$(2)" ] || $(CP) "$(KDIR)/root.$(2)" "$(KDIR_TMP)/sysupgrade-$(1)/root"
187         [ -z "$(3)" ] || $(CP) "$(3)" "$(KDIR_TMP)/sysupgrade-$(1)/kernel"
188         (cd "$(KDIR_TMP)"; $(TAR) cvf \
189                 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(2)-sysupgrade.tar" sysupgrade-$(1))
190    endef
191
192 # $(1) board name
193 # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
194 # $(3) rootfstype (e.g. squashfs or ubifs)
195 # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
196    define Image/Build/UbinizeImage
197         sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
198                 "$(KDIR)/root.$(3)" \
199                 "$(KDIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
200                 $(4)
201    endef
202
203 endif
204
205 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
206     define Image/mkfs/ubifs/generate
207         $(CP) ./ubinize$(1).cfg $(KDIR)
208         ( cd $(KDIR); \
209                 $(STAGING_DIR_HOST)/bin/ubinize \
210                 $(if $($(PROFILE)_UBI_OPTS), \
211                         $(shell echo $($(PROFILE)_UBI_OPTS)), \
212                         $(shell echo $(UBI_OPTS)) \
213                 ) \
214                 -o $(KDIR)/root$(1).ubi \
215                 ubinize$(1).cfg \
216         )
217     endef
218
219     define Image/mkfs/ubifs
220
221         ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
222                 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
223                         $(if $($(PROFILE)_UBIFS_OPTS), \
224                                 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
225                                 $(shell echo $(UBIFS_OPTS)) \
226                         ) \
227                         $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
228                         $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
229                         $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
230                         $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
231                         $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
232                         --squash-uids \
233                         -o $(KDIR)/root.ubifs \
234                         -d $(TARGET_DIR)
235         endif
236         $(call Image/Build,ubifs)
237
238         ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
239                 $(if $(wildcard ./ubinize.cfg),$(call Image/mkfs/ubifs/generate,))
240                 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
241         endif
242         $(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi))
243     endef
244 endif
245
246 define Image/mkfs/cpiogz
247         ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9n >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
248 endef
249
250 define Image/mkfs/targz
251         $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE_SANITIZED),-$(PROFILE_SANITIZED))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 --sort=name -C $(TARGET_DIR)/ .
252 endef
253
254 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024)))
255
256 define Image/mkfs/ext4
257         $(STAGING_DIR_HOST)/bin/make_ext4fs \
258                 -l $(E2SIZE) -b $(CONFIG_TARGET_EXT4_BLOCKSIZE) \
259                 -i $(CONFIG_TARGET_EXT4_MAXINODE) \
260                 -m $(CONFIG_TARGET_EXT4_RESERVED_PCT) \
261                 $(if $(CONFIG_TARGET_EXT4_JOURNAL),,-J) \
262                 $(KDIR)/root.ext4 $(TARGET_DIR)/
263 endef
264
265 define Image/mkfs/prepare/default
266         # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
267         - $(FIND) $(TARGET_DIR) -type f -not -perm /0100 -not -name 'ssh_host*' -not -name 'shadow' -print0 | $(XARGS) -0 chmod u+rw,g+r,o+r
268         - $(FIND) $(TARGET_DIR) -type f -perm /0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
269         - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
270         $(INSTALL_DIR) $(TARGET_DIR)/tmp $(TARGET_DIR)/overlay
271         chmod 1777 $(TARGET_DIR)/tmp
272 endef
273
274 define Image/mkfs/prepare
275         $(call Image/mkfs/prepare/default)
276 endef
277
278
279 define Image/Checksum
280         ( cd ${BIN_DIR} ; \
281                 $(FIND) -maxdepth 1 -type f \! -name 'md5sums'  -printf "%P\n" | sort | xargs $1 > $2 \
282         )
283 endef
284
285 define BuildImage/mkfs
286   install: mkfs-$(1)
287   .PHONY: mkfs-$(1)
288   mkfs-$(1): kernel_prepare
289         $(Image/mkfs/$(1))
290         $(call Build/mkfs/default,$(1))
291         $(call Build/mkfs/$(1),$(1))
292   $(KDIR)/root.$(1): mkfs-$(1)
293
294 endef
295
296 # Build commands that can be called from Device/* templates
297 define Build/uImage
298         mkimage -A $(LINUX_KARCH) \
299                 -O linux -T kernel \
300                 -C $(1) -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
301                 -n '$(call toupper,$(LINUX_KARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $@ $@.new
302         @mv $@.new $@
303 endef
304
305 define Build/netgear-chk
306         $(STAGING_DIR_HOST)/bin/mkchkimg \
307                 -o $@.new \
308                 -k $@ \
309                 -b $(NETGEAR_BOARD_ID) \
310                 -r $(NETGEAR_REGION)
311         mv $@.new $@
312 endef
313
314 define Build/netgear-dni
315         $(STAGING_DIR_HOST)/bin/mkdniimg \
316                 -B $(NETGEAR_BOARD_ID) -v OpenWrt.$(REVISION) \
317                 $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
318                 -r "$(1)" \
319                 -i $@ -o $@.new
320         mv $@.new $@
321 endef
322
323 define Build/fit
324         $(TOPDIR)/scripts/mkits.sh \
325                 -D $(DEVICE_NAME) -o $@.its -k $@ \
326                 $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
327                 -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
328                 -A $(ARCH) -v $(LINUX_VERSION)
329         PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
330         @mv $@.new $@
331 endef
332
333 define Build/lzma
334         $(STAGING_DIR_HOST)/bin/lzma e $@ -lc1 -lp2 -pb2 $(1) $@.new
335         @mv $@.new $@
336 endef
337
338 define Build/gzip
339         gzip -9n -c $@ $(1) > $@.new
340         @mv $@.new $@
341 endef
342
343 define Build/jffs2
344         rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
345                 mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
346                 cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
347                 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
348                         $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
349                         --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
350                         -o $@.new \
351                         -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
352                         2>&1 1>/dev/null | awk '/^.+$$$$/' && \
353                 $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
354         -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
355         @mv $@.new $@
356 endef
357
358 define Build/kernel-bin
359         rm -f $@
360         cp $< $@
361 endef
362
363 define Build/patch-cmdline
364         $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
365 endef
366
367 define Build/append-kernel
368         dd if=$(word 1,$^) $(if $(1),bs=$(1) conv=sync) >> $@
369 endef
370
371 define Build/append-rootfs
372         dd if=$(word 2,$^) $(if $(1),bs=$(1) conv=sync) >> $@
373 endef
374
375 define Build/append-ubi
376         sh $(TOPDIR)/scripts/ubinize-image.sh \
377                 $(if $(KERNEL_IN_UBI),--kernel $(word 1,$^)) \
378                 $(word 2,$^) \
379                 $@.tmp \
380                 -p $(BLOCKSIZE) -m $(PAGESIZE) -E 5 \
381                 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE))
382         cat $@.tmp >> $@
383         rm $@.tmp
384 endef
385
386 define Build/pad-to
387         dd if=$@ of=$@.new bs=$(1) conv=sync
388         mv $@.new $@
389 endef
390
391 define Build/pad-rootfs
392         $(call prepare_generic_squashfs,$@ $(1))
393 endef
394
395 define Build/pad-offset
396         let \
397                 size="$$(stat -c%s $@)" \
398                 pad="$(word 1, $(1))" \
399                 offset="$(word 2, $(1))" \
400                 pad="(pad - ((size + offset) % pad)) % pad" \
401                 newsize='size + pad'; \
402                 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
403         mv $@.new $@
404 endef
405
406 define Build/check-size
407         @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
408                 echo "WARNING: Image file $@ is too big" >&2; \
409                 rm -f $@; \
410         }
411 endef
412
413 define Build/combined-image
414         -sh $(TOPDIR)/scripts/combined-image.sh \
415                 "$(word 1,$^)" \
416                 "$@" \
417                 "$@.new"
418         @mv $@.new $@
419 endef
420
421 define Build/sysupgrade-nand
422         sh $(TOPDIR)/scripts/sysupgrade-nand.sh \
423                 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
424                 --kernel $(word 1,$^) \
425                 --rootfs $(word 2,$^) \
426                 $@
427 endef
428
429 define Device/Init
430   PROFILES := $(PROFILE)
431   DEVICE_NAME := $(1)
432   KERNEL:=
433   KERNEL_INITRAMFS = $$(KERNEL)
434   KERNEL_SIZE:=
435   CMDLINE:=
436
437   IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
438   IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
439   KERNEL_PREFIX = $(1)
440   KERNEL_SUFFIX := -kernel.bin
441   KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
442   KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
443   KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_SUFFIX)
444   KERNEL_INITRAMFS_NAME = $$(KERNEL_NAME)-initramfs
445   KERNEL_INSTALL :=
446   KERNEL_NAME := vmlinux
447   KERNEL_DEPENDS :=
448   KERNEL_SIZE :=
449
450   FILESYSTEMS := $(TARGET_FILESYSTEMS)
451 endef
452
453 define Device/ExportVar
454   $(1) : $(2):=$$($(2))
455
456 endef
457 define Device/Export
458   $(foreach var,$(DEVICE_VARS) DEVICE_NAME KERNEL KERNEL_INITRAMFS KERNEL_INITRAMFS_IMAGE,$(call Device/ExportVar,$(1),$(var)))
459   $(1) : FILESYSTEM:=$(2)
460 endef
461
462 define Device/Check
463   _TARGET = $$(if $$(filter $(PROFILE),$$(PROFILES)),install,install-disabled)
464   _COMPILE_TARGET = $$(if $(if $(IB),,$(CONFIG_IB)$$(filter $(PROFILE),$$(PROFILES))),compile,compile-disabled)
465 endef
466
467 ifndef IB
468 define Device/Build/initramfs
469   $(call Device/Export,$(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE),$(1))
470   $$(_TARGET): $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE)
471
472   $(KDIR)/$$(KERNEL_INITRAMFS_NAME):: image_prepare
473   $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE)
474         cp $$^ $$@
475
476   $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
477         @rm -f $$@
478         $$(call concat_cmd,$$(KERNEL_INITRAMFS))
479 endef
480 endif
481
482 define Device/Build/check_size
483         @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
484                 echo "WARNING: Image file $@ is too big" >&2; \
485                 rm -f $@; \
486         }
487 endef
488
489 define Device/Build/compile
490   $$(_COMPILE_TARGET): $(KDIR)/$(1)
491   $(eval $(call Device/Export,$(KDIR)/$(1)))
492   $(KDIR)/$(1):
493         $$(call concat_cmd,$(COMPILE/$(1)))
494
495 endef
496
497 define Device/Build/kernel
498   _KERNEL_IMAGES += $(KDIR)/$$(KERNEL_NAME)
499   $(KDIR)/$$(KERNEL_NAME):: image_prepare
500   $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
501   $(call Device/Export,$(KDIR)/$$(KERNEL_IMAGE),$(1))
502   $(BIN_DIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_IMAGE)
503         cp $$^ $$@
504   ifndef IB
505     ifdef CONFIG_IB
506       install: $(KDIR)/$$(KERNEL_IMAGE)
507     endif
508     $(KDIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
509         @rm -f $$@
510         $$(call concat_cmd,$$(KERNEL))
511         $$(if $$(KERNEL_SIZE),$$(call Device/Build/check_size,$$(KERNEL_SIZE)))
512   endif
513 endef
514
515 define Device/Build/image
516   $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
517   $(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
518   $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$$(KERNEL_IMAGE) $(KDIR)/root.$(1)
519         @rm -f $$@
520         [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
521         $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
522
523   .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
524   $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
525         cp $$^ $$@
526
527 endef
528
529 define Device/Build
530   $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
531   $(call Device/Build/kernel,$(1))
532
533   $$(eval $$(foreach compile,$$(COMPILE), \
534     $$(call Device/Build/compile,$$(compile),$(1))))
535
536   $$(eval $$(foreach image,$$(IMAGES), \
537     $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
538       $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
539 endef
540
541 define Device
542   $(call Device/Init,$(1))
543   $(call Device/Default,$(1))
544   $(call Device/Check,$(1))
545   $(call Device/$(1),$(1))
546   $(call Device/Build,$(1))
547
548 endef
549
550 define BuildImage
551
552   download:
553   prepare:
554   compile:
555   clean:
556   image_prepare:
557
558   ifeq ($(IB),)
559     .PHONY: download prepare compile clean image_prepare mkfs_prepare kernel_prepare install
560     compile:
561                 $(call Build/Compile)
562
563     clean:
564                 $(call Build/Clean)
565
566     image_prepare: compile
567                 mkdir -p $(KDIR)/tmp
568                 $(call Image/Prepare)
569   else
570     image_prepare:
571                 mkdir -p $(KDIR)/tmp
572   endif
573
574   mkfs_prepare: image_prepare
575         $(call Image/mkfs/prepare)
576
577   kernel_prepare: mkfs_prepare
578         $(call Image/BuildKernel)
579         $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
580         $(call Image/InstallKernel)
581
582   $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
583   $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
584
585   install: kernel_prepare
586         $(foreach fs,$(TARGET_FILESYSTEMS),
587                 $(call Image/Build,$(fs))
588         )
589         $(call Image/mkfs/ubifs)
590         $(call Image/Checksum,md5sum --binary,md5sums)
591         $(call Image/Checksum,openssl dgst -sha256,sha256sums)
592
593 endef