bc383e6ba81c783518fe4bb24308e8942b90e64d
[openwrt/staging/mkresin.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) $(if $(SOURCE_DATE_EPOCH),-fixed-time $(SOURCE_DATE_EPOCH))
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 $(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
191 )
192 endef
193
194 # $(1) board name
195 # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
196 # $(3) rootfstype (e.g. squashfs or ubifs)
197 # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
198 define Image/Build/UbinizeImage
199 sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
200 "$(KDIR)/root.$(3)" \
201 "$(KDIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
202 $(4)
203 endef
204
205 endif
206
207 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
208 define Image/mkfs/ubifs/generate
209 $(CP) ./ubinize$(1).cfg $(KDIR)
210 ( cd $(KDIR); \
211 $(STAGING_DIR_HOST)/bin/ubinize \
212 $(if $($(PROFILE)_UBI_OPTS), \
213 $(shell echo $($(PROFILE)_UBI_OPTS)), \
214 $(shell echo $(UBI_OPTS)) \
215 ) \
216 -o $(KDIR)/root$(1).ubi \
217 ubinize$(1).cfg \
218 )
219 endef
220
221 define Image/mkfs/ubifs
222
223 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
224 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
225 $(if $($(PROFILE)_UBIFS_OPTS), \
226 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
227 $(shell echo $(UBIFS_OPTS)) \
228 ) \
229 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
230 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
231 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
232 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
233 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
234 --squash-uids \
235 -o $(KDIR)/root.ubifs \
236 -d $(TARGET_DIR)
237 endif
238 $(call Image/Build,ubifs)
239
240 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
241 $(if $(wildcard ./ubinize.cfg),$(call Image/mkfs/ubifs/generate,))
242 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
243 endif
244 $(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi))
245 endef
246 endif
247
248 define Image/mkfs/cpiogz
249 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9n >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
250 endef
251
252 define Image/mkfs/targz
253 $(TAR) -cp --numeric-owner --owner=0 --group=0 --sort=name \
254 $(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
255 -C $(TARGET_DIR)/ . | gzip -9n > $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE_SANITIZED),-$(PROFILE_SANITIZED))-rootfs.tar.gz
256 endef
257
258 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024)))
259
260 define Image/mkfs/ext4
261 $(STAGING_DIR_HOST)/bin/make_ext4fs \
262 -l $(E2SIZE) -b $(CONFIG_TARGET_EXT4_BLOCKSIZE) \
263 -i $(CONFIG_TARGET_EXT4_MAXINODE) \
264 -m $(CONFIG_TARGET_EXT4_RESERVED_PCT) \
265 $(if $(CONFIG_TARGET_EXT4_JOURNAL),,-J) \
266 $(if $(SOURCE_DATE_EPOCH),-T $(SOURCE_DATE_EPOCH)) \
267 $(KDIR)/root.ext4 $(TARGET_DIR)/
268 endef
269
270 define Image/mkfs/prepare/default
271 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
272 - $(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
273 - $(FIND) $(TARGET_DIR) -type f -perm /0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
274 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
275 $(INSTALL_DIR) $(TARGET_DIR)/tmp $(TARGET_DIR)/overlay
276 chmod 1777 $(TARGET_DIR)/tmp
277 endef
278
279 define Image/mkfs/prepare
280 $(call Image/mkfs/prepare/default)
281 endef
282
283
284 define Image/Checksum
285 ( cd ${BIN_DIR} ; \
286 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs $1 > $2 \
287 )
288 endef
289
290 define BuildImage/mkfs
291 install: mkfs-$(1)
292 .PHONY: mkfs-$(1)
293 mkfs-$(1): kernel_prepare
294 $(Image/mkfs/$(1))
295 $(call Build/mkfs/default,$(1))
296 $(call Build/mkfs/$(1),$(1))
297 $(KDIR)/root.$(1): mkfs-$(1)
298
299 endef
300
301 # Build commands that can be called from Device/* templates
302 define Build/uImage
303 mkimage -A $(LINUX_KARCH) \
304 -O linux -T kernel \
305 -C $(1) -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
306 -n '$(call toupper,$(LINUX_KARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $@ $@.new
307 @mv $@.new $@
308 endef
309
310 define Build/netgear-chk
311 $(STAGING_DIR_HOST)/bin/mkchkimg \
312 -o $@.new \
313 -k $@ \
314 -b $(NETGEAR_BOARD_ID) \
315 -r $(NETGEAR_REGION)
316 mv $@.new $@
317 endef
318
319 define Build/netgear-dni
320 $(STAGING_DIR_HOST)/bin/mkdniimg \
321 -B $(NETGEAR_BOARD_ID) -v OpenWrt.$(REVISION) \
322 $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
323 -r "$(1)" \
324 -i $@ -o $@.new
325 mv $@.new $@
326 endef
327
328 define Build/fit
329 $(TOPDIR)/scripts/mkits.sh \
330 -D $(DEVICE_NAME) -o $@.its -k $@ \
331 $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
332 -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
333 -A $(ARCH) -v $(LINUX_VERSION)
334 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
335 @mv $@.new $@
336 endef
337
338 define Build/lzma
339 $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
340 endef
341
342 define Build/lzma-no-dict
343 $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
344 @mv $@.new $@
345 endef
346
347 define Build/gzip
348 gzip -9n -c $@ $(1) > $@.new
349 @mv $@.new $@
350 endef
351
352 define Build/jffs2
353 rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
354 mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
355 cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
356 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
357 $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
358 --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
359 -o $@.new \
360 -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
361 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
362 $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
363 -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
364 @mv $@.new $@
365 endef
366
367 define Build/kernel-bin
368 rm -f $@
369 cp $< $@
370 endef
371
372 define Build/patch-cmdline
373 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
374 endef
375
376 define Build/append-kernel
377 dd if=$(word 1,$^) $(if $(1),bs=$(1) conv=sync) >> $@
378 endef
379
380 define Build/append-rootfs
381 dd if=$(word 2,$^) $(if $(1),bs=$(1) conv=sync) >> $@
382 endef
383
384 define Build/append-ubi
385 sh $(TOPDIR)/scripts/ubinize-image.sh \
386 $(if $(UBOOTENV_IN_UBI),--uboot-env) \
387 $(if $(KERNEL_IN_UBI),--kernel $(word 1,$^)) \
388 $(word 2,$^) \
389 $@.tmp \
390 -p $(BLOCKSIZE) -m $(PAGESIZE) -E 5 \
391 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE))
392 cat $@.tmp >> $@
393 rm $@.tmp
394 endef
395
396 define Build/pad-to
397 dd if=$@ of=$@.new bs=$(1) conv=sync
398 mv $@.new $@
399 endef
400
401 define Build/pad-rootfs
402 $(call prepare_generic_squashfs,$@ $(1))
403 endef
404
405 define Build/pad-offset
406 let \
407 size="$$(stat -c%s $@)" \
408 pad="$(word 1, $(1))" \
409 offset="$(word 2, $(1))" \
410 pad="(pad - ((size + offset) % pad)) % pad" \
411 newsize='size + pad'; \
412 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
413 mv $@.new $@
414 endef
415
416 define Build/check-size
417 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
418 echo "WARNING: Image file $@ is too big" >&2; \
419 rm -f $@; \
420 }
421 endef
422
423 define Build/combined-image
424 -sh $(TOPDIR)/scripts/combined-image.sh \
425 "$(word 1,$^)" \
426 "$@" \
427 "$@.new"
428 @mv $@.new $@
429 endef
430
431 define Build/sysupgrade-nand
432 sh $(TOPDIR)/scripts/sysupgrade-nand.sh \
433 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
434 --kernel $(word 1,$^) \
435 --rootfs $(word 2,$^) \
436 $@
437 endef
438
439 define Device/Init
440 PROFILES := $(PROFILE)
441 DEVICE_NAME := $(1)
442 KERNEL:=
443 KERNEL_INITRAMFS = $$(KERNEL)
444 KERNEL_SIZE:=
445 CMDLINE:=
446
447 IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
448 IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
449 KERNEL_PREFIX = $(1)
450 KERNEL_SUFFIX := -kernel.bin
451 KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
452 KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
453 KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_SUFFIX)
454 KERNEL_INITRAMFS_NAME = $$(KERNEL_NAME)-initramfs
455 KERNEL_INSTALL :=
456 KERNEL_NAME := vmlinux
457 KERNEL_DEPENDS :=
458 KERNEL_SIZE :=
459
460 FILESYSTEMS := $(TARGET_FILESYSTEMS)
461 endef
462
463 define Device/ExportVar
464 $(1) : $(2):=$$($(2))
465
466 endef
467 define Device/Export
468 $(foreach var,$(DEVICE_VARS) DEVICE_NAME KERNEL KERNEL_INITRAMFS KERNEL_INITRAMFS_IMAGE,$(call Device/ExportVar,$(1),$(var)))
469 $(1) : FILESYSTEM:=$(2)
470 endef
471
472 define Device/Check
473 _TARGET = $$(if $$(filter $(PROFILE),$$(PROFILES)),install,install-disabled)
474 _COMPILE_TARGET = $$(if $(if $(IB),,$(CONFIG_IB)$$(filter $(PROFILE),$$(PROFILES))),compile,compile-disabled)
475 endef
476
477 ifndef IB
478 define Device/Build/initramfs
479 $(call Device/Export,$(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE),$(1))
480 $$(_TARGET): $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE)
481
482 $(KDIR)/$$(KERNEL_INITRAMFS_NAME):: image_prepare
483 $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE)
484 cp $$^ $$@
485
486 $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
487 @rm -f $$@
488 $$(call concat_cmd,$$(KERNEL_INITRAMFS))
489 endef
490 endif
491
492 define Device/Build/check_size
493 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
494 echo "WARNING: Image file $@ is too big" >&2; \
495 rm -f $@; \
496 }
497 endef
498
499 define Device/Build/compile
500 $$(_COMPILE_TARGET): $(KDIR)/$(1)
501 $(eval $(call Device/Export,$(KDIR)/$(1)))
502 $(KDIR)/$(1):
503 $$(call concat_cmd,$(COMPILE/$(1)))
504
505 endef
506
507 define Device/Build/kernel
508 _KERNEL_IMAGES += $(KDIR)/$$(KERNEL_NAME)
509 $(KDIR)/$$(KERNEL_NAME):: image_prepare
510 $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
511 $(call Device/Export,$(KDIR)/$$(KERNEL_IMAGE),$(1))
512 $(BIN_DIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_IMAGE)
513 cp $$^ $$@
514 ifndef IB
515 ifdef CONFIG_IB
516 install: $(KDIR)/$$(KERNEL_IMAGE)
517 endif
518 $(KDIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
519 @rm -f $$@
520 $$(call concat_cmd,$$(KERNEL))
521 $$(if $$(KERNEL_SIZE),$$(call Device/Build/check_size,$$(KERNEL_SIZE)))
522 endif
523 endef
524
525 define Device/Build/image
526 $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
527 $(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
528 $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$$(KERNEL_IMAGE) $(KDIR)/root.$(1)
529 @rm -f $$@
530 [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
531 $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
532
533 .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
534 $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
535 cp $$^ $$@
536
537 endef
538
539 define Device/Build
540 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
541 $(call Device/Build/kernel,$(1))
542
543 $$(eval $$(foreach compile,$$(COMPILE), \
544 $$(call Device/Build/compile,$$(compile),$(1))))
545
546 $$(eval $$(foreach image,$$(IMAGES), \
547 $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
548 $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
549 endef
550
551 define Device
552 $(call Device/Init,$(1))
553 $(call Device/Default,$(1))
554 $(call Device/Check,$(1))
555 $(call Device/$(1),$(1))
556 $(call Device/Build,$(1))
557
558 endef
559
560 define BuildImage
561
562 download:
563 prepare:
564 compile:
565 clean:
566 image_prepare:
567
568 ifeq ($(IB),)
569 .PHONY: download prepare compile clean image_prepare mkfs_prepare kernel_prepare install
570 compile:
571 $(call Build/Compile)
572
573 clean:
574 $(call Build/Clean)
575
576 image_prepare: compile
577 mkdir -p $(KDIR)/tmp
578 $(call Image/Prepare)
579 else
580 image_prepare:
581 mkdir -p $(KDIR)/tmp
582 endif
583
584 mkfs_prepare: image_prepare
585 $(call Image/mkfs/prepare)
586
587 kernel_prepare: mkfs_prepare
588 $(call Image/BuildKernel)
589 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
590 $(call Image/InstallKernel)
591
592 $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
593 $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
594
595 install: kernel_prepare
596 $(foreach fs,$(TARGET_FILESYSTEMS),
597 $(call Image/Build,$(fs))
598 )
599 $(call Image/mkfs/ubifs)
600 $(call Image/Checksum,md5sum --binary,md5sums)
601 $(call Image/Checksum,openssl dgst -sha256,sha256sums)
602
603 endef