一. 前言
接下来分析make -C image compile install TARGET_BUILD=。
二. Makefile分析
-
命令首先运行target/linux/mediatek/image/Makefile,该文件内容如下:
target/linux/mediatek/image/Makefile:
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/image.mk......
build signed fit
define Build/fit-sign
$(TOPDIR)/scripts/mkits.sh
-D $(DEVICE_NAME)
-o $@.its
-k $@
$(if (word 2,(1)),-d (word 2,(1))) -C (word 1,(1))
-a $(KERNEL_LOADADDR)
-e $(if (KERNEL_ENTRY),(KERNEL_ENTRY),$(KERNEL_LOADADDR))
-c $(if (DEVICE_DTS_CONFIG),(DEVICE_DTS_CONFIG),"config-1")
-A $(LINUX_KARCH)
-v $(LINUX_VERSION)
-s (KDIR)/(DEVICE_NAME)-u-boot-script
$(if $(FIT_KEY_NAME),-S $(FIT_KEY_NAME))
$(if $(FW_AR_VER),-r (FW_AR_VER)) PATH=(LINUX_DIR)/scripts/dtc:$(PATH) mkimage
-f $@.its
$(if $(FIT_KEY_DIR),-k $(FIT_KEY_DIR))
-r
$@.new
@mv $@.new $@
endefdefault all platform image(fit) build
define Device/Default
PROFILES = Default $$(DEVICE_NAME)
KERNEL_NAME := Image
KERNEL = kernel-bin | lzma |
fit lzma $$(KDIR)/image-$$(firstword $$(DEVICE_DTS)).dtb
KERNEL_INITRAMFS = kernel-bin | lzma |
fit lzma $$(KDIR)/image-$$(firstword $$(DEVICE_DTS)).dtb
FILESYSTEMS := squashfs
DEVICE_DTS_DIR := $(DTS_DIR)
IMAGES := sysupgrade.bin
IMAGE/sysupgrade.bin := append-kernel | pad-to 128k | append-rootfs |
pad-rootfs | append-metadata
FIT_KEY_DIR :=
FIT_KEY_NAME :=
endefinclude $(SUBTARGET).mk
define Image/Build
(call Image/Build/(1),$(1))
endef$(eval $(call BuildImage))
BuildImage在include/image.mk中定义,install目标在里面,代码如下:
include/image.mk:
define BuildImage
......
download:
prepare:
compile:
clean:
image_prepare:
ifeq ($(IB),)
.PHONY: download prepare compile clean image_prepare kernel_prepare install install-images
compile:
$(call Build/Compile)
clean:
$(call Build/Clean)
image_prepare: compile
mkdir -p $(BIN_DIR) $(KDIR)/tmp
rm -rf $(BUILD_DIR)/json_info_files
$(call Image/Prepare)
else
image_prepare:
mkdir -p $(BIN_DIR) $(KDIR)/tmp
endif
kernel_prepare: image_prepare
$(call Image/Build/targz)
$(call Image/Build/cpiogz)
$(call Image/BuildKernel)
$(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
$(call Image/InstallKernel)
$(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
install-images: kernel_prepare $(foreach fs,$(filter-out $(if $(UBIFS_OPTS),,ubifs),$(TARGET_FILESYSTEMS) $(fs-subtypes-y)),$(KDIR)/root.$(fs))
$(foreach fs,$(TARGET_FILESYSTEMS),
$(call Image/Build,$(fs))
)
install: install-images
$(call Image/Manifest)
endef
由上可知,install依赖于install-images,install-images依赖于kernel_prepare,kernel_prepare依赖于image_prepare,由于IB未定义,所以image_prepare定义如下:
compile:
$(call Build/Compile)
clean:
$(call Build/Clean)
image_prepare: compile
mkdir -p $(BIN_DIR) $(KDIR)/tmp
rm -rf $(BUILD_DIR)/json_info_files
$(call Image/Prepare)
image_prepare依赖于compile,这里,Build/Compile并未定义,所以什么都不干
注意:image_prepare在另一处也有定义,如下:
ifndef IB
define Device/Build/dtb
ifndef BUILD_DTS_$(1)
BUILD_DTS_$(1) := 1
$(KDIR)/image-$(1).dtb: FORCE
$(call Image/BuildDTB,$(strip $(2))/$(strip $(3)).dts,$$@)
image_prepare: $(KDIR)/image-$(1).dtb
endif
endef
endif
这里在执行完上面compile依赖,接下来会完成(KDIR)/image-(1).dtb这个依赖,也就是生成设备树文件。
这种有两个相同目标的执行过程,下面用一个简单案例解释,如下:
act1:
@echo "act1"
act2:
@echo "act2"
target:act1
target:act2
@echo "target finish"
all: target
@echo "all finish"
这里有两个target目标,分别依赖act1和act2,注意,一个target是有执行动作的,一个没有,这里先执行act2依赖,再act1依赖,执行结果如下:
act2
act1
target finish
all finish
回到分析,
ifndef IB
define Device/Build/dtb
ifndef BUILD_DTS_$(1)
BUILD_DTS_$(1) := 1
$(KDIR)/image-$(1).dtb: FORCE
$(call Image/BuildDTB,$(strip $(2))/$(strip $(3)).dts,$$@)
image_prepare: $(KDIR)/image-$(1).dtb
endif
endef
endif
(KDIR)/image-(1).dtb也是一个目标,动作为(call Image/BuildDTB,(strip (2))/(strip (3)).dts,$@),Image/BuildDTB定义如下,
define Image/BuildDTB
$(TARGET_CROSS)cpp -nostdinc -x assembler-with-cpp \
-I$(DTS_DIR) \
-I$(DTS_DIR)/include \
-I$(LINUX_DIR)/include/ \
-undef -D__DTS__ $(3) \
-o $(2).tmp $(1)
$(LINUX_DIR)/scripts/dtc/dtc -O dtb \
-i$(dir $(1)) $(DTC_FLAGS) $(4) \
-o $(2) $(2).tmp
$(RM) $(2).tmp
endef
完整命令展开如下:
aarch64-openwrt-linux-musl-cpp -nostdinc -x assembler-with-cpp -I/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/arch/arm64/boot/dts -I/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/arch/arm64/boot/dts/include -I/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/include/ -undef -D__DTS__ -o /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/image-mt7981-spim-nor-rfb.dtb.tmp /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/arch/arm64/boot/dts/mediatek/mt7981-spim-nor-rfb.dts
aarch64-openwrt-linux-musl-cpp -nostdinc -x assembler-with-cpp -I/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/arch/arm64/boot/dts -I/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/arch/arm64/boot/dts/include -I/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/include/ -undef -D__DTS__ -o /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/image-mt7981-spim-nor-rfb.dtb.tmp /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/arch/arm64/boot/dts/mediatek/mt7981-spim-nor-rfb.dts
/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/scripts/dtc/dtc -O dtb -i/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/arch/arm64/boot/dts/mediatek/ -Wno-unit_address_vs_reg -Wno-simple_bus_reg -Wno-unit_address_format -Wno-pci_bridge -Wno-pci_device_bus_num -Wno-pci_device_reg -Wno-avoid_unnecessary_addr_size -Wno-alias_paths -Wno-graph_child_address -Wno-graph_port -Wno-unique_unit_address -o /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/image-mt7981-spim-nor-rfb.dtb /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/image-mt7981-spim-nor-rfb.dtb.tmp
注意:
Device/Build/dtb是由下面代码展开得到的:
$(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
TARGET_DEVICES定义在target/linux/mediatek/image/mt7981.mk 中,可以如下:
openwrt# grep -rn "TARGET_DEVICES" target/linux/mediatek/image/mt7981.mk
10:TARGET_DEVICES += mt7981-spim-nor-rfb
27:TARGET_DEVICES += mt7981-spim-nand-2500wan-gmac2
44:TARGET_DEVICES += mt7981-spim-nand-rfb
61:TARGET_DEVICES += mt7981-spim-nand-gsw
74:TARGET_DEVICES += mt7981-emmc-rfb
87:TARGET_DEVICES += mt7981-sd-rfb
104:TARGET_DEVICES += mt7981-snfi-nand-2500wan-p5
113:TARGET_DEVICES += mt7981-fpga-spim-nor
130:TARGET_DEVICES += mt7981-fpga-snfi-nand
147:TARGET_DEVICES += mt7981-fpga-spim-nand
160:TARGET_DEVICES += mt7981-fpga-emmc
173:TARGET_DEVICES += mt7981-fpga-sd
Device的定义如下:
define Device
$(call Device/InitProfile,$(1))
$(call Device/Init,$(1))
$(call Device/Default,$(1))
$(call Device/$(1),$(1))
$(call Device/Check,$(1))
$(call Device/$(if $(DUMP),Dump,Build),$(1))
endef
Device/Default定义在target/linux/mediatek/image/Makefile,内容如下:
define Device/Default
PROFILES = Default $$(DEVICE_NAME)
KERNEL_NAME := Image
KERNEL = kernel-bin | lzma | \
fit lzma $$(KDIR)/image-$$(firstword $$(DEVICE_DTS)).dtb
KERNEL_INITRAMFS = kernel-bin | lzma | \
fit lzma $$(KDIR)/image-$$(firstword $$(DEVICE_DTS)).dtb
FILESYSTEMS := squashfs
DEVICE_DTS_DIR := $(DTS_DIR)
IMAGES := sysupgrade.bin
IMAGE/sysupgrade.bin := append-kernel | pad-to 128k | append-rootfs | \
pad-rootfs | append-metadata
FIT_KEY_DIR :=
FIT_KEY_NAME :=
endef
Device/(1):由于(1)=mt7981-spim-nor-rfb,表示Device/mt7981-spim-nor-rfb,定义在target/linux/mediatek/image/mt7981.mk,内容如下:
define Device/mt7981-spim-nor-rfb
DEVICE_VENDOR := MediaTek
DEVICE_MODEL := mt7981-spim-nor-rfb
DEVICE_DTS := mt7981-spim-nor-rfb
DEVICE_DTS_DIR := $(DTS_DIR)/mediatek
SUPPORTED_DEVICES := mediatek,mt7981-spim-nor-rfb
endef
TARGET_DEVICES += mt7981-spim-nor-rfb
由于DUMP未定义,(call Device/(if (DUMP),Dump,Build),(1))为Device/Build。
-
Device/Build展开如下
define Device/Build
(eval $$(foreach image,$$(IMAGES), \ $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \ $$(call Device/Build/per-device-fs,$$(fs),$$(image),$(1))))) $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1))) $(call Device/Build/kernel,$(1),$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS))) $$(eval $$(foreach compile,$$(COMPILE), \ $$(call Device/Build/compile,$$(compile),$(1)))) $$(eval $$(foreach image,$$(IMAGES), \ $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \ $$(call Device/Build/image,$$(fs),$$(image),$(1))))) $$(eval $$(foreach artifact,$$(ARTIFACTS), \ $$(call Device/Build/artifact,$$(artifact)))) endef
define split_args
$(foreach data,
(subst |,(space),
$(subst (space),^,(1))),
$(call (2),(strip (subst ^,(space),$(data)))))
endefdefine build_cmd
$(if (Build/(word 1,(1))),,(error Missing Build/(word 1,(1))))
(call Build/(word 1,(1)),(wordlist 2,$(words (1)),(1)))endef
define concat_cmd
(call split_args,(1),build_cmd)
endefifndef IB
define Device/Build/initramfs
(call Device/Export,(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE),$(1))(_TARGET): $$(if $$(KERNEL_INITRAMFS),$(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE) \ $$(if $$(CONFIG_JSON_OVERVIEW_IMAGE_INFO), $(BUILD_DIR)/json_info_files/$$(KERNEL_INITRAMFS_IMAGE).json,)) $(KDIR)/$$(KERNEL_INITRAMFS_NAME):: image_prepare $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE) cp $$^ $$@ $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS) image_prepare @rm -f $$@ $$(call concat_cmd,$$(KERNEL_INITRAMFS)) ...... endef endif
rm -f /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin cp /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/Image-initramfs /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin /home/work/openwrt/staging_dir/host/bin/lzma e /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin -lc1 -lp2 -pb2 /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin.new @mv /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin.new /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin /home/work/openwrt/scripts/mkits.sh -D mt7981-spim-nor-rfb -o /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin.its -k /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin -d /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/image-mt7981-spim-nor-rfb.dtb -C lzma -a 0x48080000 -e 0x48080000 -c "config-1" -A arm64 -v 5.4.203 PATH=/home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/linux-5.4.203/scripts/dtc:/home/work/openwrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-8.4.0_musl/bin:/home/work/openwrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-8.4.0_musl/bin:/home/work/openwrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-8.4.0_musl/bin:/home/work/openwrt/staging_dir/host/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/work/openwrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-8.4.0_musl/bin/ mkimage -f /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin.its /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin.new @mv /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin.new /home/work/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-mediatek_mt7981/tmp/openwrt-mediatek-mt7981-mt7981-spim-nor-rfb-initramfs-kernel.bin
(eval $$(foreach compile,$$(COMPILE), $$(call Device/Build/compile,$$(compile),$(1))))展开如下: Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-spim-nor-rfb))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-spim-nand-2500wan-gmac2))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-spim-nand-rfb))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-spim-nand-gsw))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-emmc-rfb))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-sd-rfb))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-snfi-nand-2500wan-p5))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-fpga-spim-nor))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-fpga-snfi-nand))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-fpga-spim-nand))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-fpga-emmc))) Makefile:91: $(eval $(foreach compile,$(COMPILE), $(call Device/Build/compile,$(compile),mt7981-fpga-sd))) 由于$(COMPILE)为空,所以这块执行没有效果。 接下来, $$(eval $$(foreach image,$$(IMAGES), \ $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \ $$(call Device/Build/image,$$(fs),$$(image),$(1))))) 由于TARGET_FILESYSTEMS是要编译的镜像格式类型,如下: fs-types-$(CONFIG_TARGET_ROOTFS_SQUASHFS) += squashfs fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addprefix jffs2-,$(JFFS2_BLOCKSIZE)) fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2_NAND) += $(addprefix jffs2-nand-,$(NAND_BLOCKSIZE)) fs-types-$(CONFIG_TARGET_ROOTFS_EXT4FS) += ext4 fs-types-$(CONFIG_TARGET_ROOTFS_UBIFS) += ubifs fs-subtypes-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addsuffix -raw,$(addprefix jffs2-,$(JFFS2_BLOCKSIZE))) TARGET_FILESYSTEMS := $(fs-types-y) 由于CONFIG_TARGET_ROOTFS_SQUASHFS等我都没选,所以,TARGET_FILESYSTEMS为空,这里也是不执行的。可以看出Device/Build/image的作用是生成不同文件系统的镜像。 接下来, $$(eval $$(foreach artifact,$$(ARTIFACTS), \ $$(call Device/Build/artifact,$$(artifact)))) 由于ARTIFACTS变量为空,所以,这里Device/Build/artifact什么也不干。 ### 三. 总结 这里需要注意的以下几点: 1. image_prepare有两个目标,有动作的这个目标做的事情很少,没有目标的依赖是用于编译设备树的,要非常注意。 2. 由于编译选项没选,Image/Build/targz,Image/Build/cpiogz这里什么也不做,Image/BuildKernel和Image/InstallKernel没有定义(可能是用于自定义的,这里没用到而已),这里什么也不做。 3. $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs))),这里结果为$(call Image/BuildKernel/Initramfs),继续展开为$(call Image/Build/Initramfs),但是Image/Build/Initramfs未定义,所以这里什么也不干,也是用于自定义的。 4. $(foreach device,$(TARGET_DEVICES),$(call Device,$(device))),这里TARGET_DEVICES定义在mt7981.mk中,表示不同的镜像(spi-nor,spi-nand,emmc等等),Device宏里面要关注Device/Default和Device/Build,Device/Default定义在target/linux/mediatek/image/Makefile中,定义了一些镜像相关的变量,Device/Build中要关注$(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1))),结果是$(call Device/Build/initramfs,$(1)),重点要关注$$(call concat_cmd,$$(KERNEL_INITRAMFS))这里,这里面打包了最终的镜像(lzma压缩,its文件生成,mkimage都在这里执行)。